这是一个后续问题:Adapting csv reading for multiple tables
如果我定义了以下模块:
:- module(csv_load_mod,[prepare_db/3]).
:- use_module(library(csv)).
:- set_prolog_stack(global, limit(4*10**9)).
prepare_db(File, Column_Key,Relation) :-
Column_Key_Term =.. [Column_Key,_],
Relation_Term =.. [Relation,_,_,_],
retractall(Column_Key_Term),
retractall(Relation_Term),
forall(read_row(File, Row), store_row(Row,Column_Key,Relation)).
store_row(Row,Column_Key,Relation) :-
Column_Key_Test =.. [Column_Key,ColKeys],
Row =.. [row|Cols],
( call(Column_Key_Test)
-> Cols = [RowKey|Values],
maplist(store_relation(Relation,RowKey), ColKeys, Values)
; ( Cols = [_H|T],
Column_Key_Term =.. [Column_Key,T],
assertz(Column_Key_Term)
)
).
store_relation(Relation,RowKey, ColKey, Values) :-
Relation_Term =.. [Relation,RowKey,ColKey,Values],
assertz(Relation_Term).
read_row(File, Row) :-
csv_read_file_row(File, Row, []).
然后我可以从 csv 文件中读取表格。
例如:
:? prepare_db('my_table.csv',mt_col_key, mt_relation).
然后我会有一个事实mt_col_key([col1,col2,...,coln])
和一组事实mt_relation/3
。但这些将是模块本地的,不会被导出。我需要使用csv_load_mod:mt_relation/3
等。有没有办法让模块导出动态谓词或进行调整prepare_db/3
,以便它断言的事实不是本地的,或者它们被断言到调用它的模块?