如何从文本文件中读取、修改和调用查询

How to read a query from a text file, modify it, and call it

我有一个 Weka arff file,其中的属性是 Prolog 查询。

我想从文件中读取查询,修改它,然后运行查询。

weka arff 文件的开头如下:

@relation propositionalization
@attribute 'link(_, A, _), link(A, _, _)' {'+'}
@attribute 'reaction(A, 0), link(A, B, _), link(B, C, _), reaction(C, 1), link(B, D, activation), reaction(D, 0), reaction(B, 0), link(A, E, _), link(E, F, follows), reaction(F, 1)' {'+','-'}
@attribute 'reaction(A, 0), link(A, B, _), link(B, C, _), reaction(C, 1), reaction(B, 1), link(A, D, _), link(D, E, follows), reaction(E, 0)' {'+','-'}

等等

我有一个 dcg 由:

list([]) -->[].
list([L|Ls]) --> [L],list(Ls).
get_feature(Feature) --> "@attribute '",list(Feature), "'",list(_).

我阅读了文件:

read_my_file(File,Lines):-
open(File,read,Stream,[]),
    read_lines(Lines,Stream),!.

read_lines([H|T],Stream):-
  read_line_to_codes(Stream,H), H\=end_of_file, read_lines(T,Stream).

read_lines([],Stream):-close(Stream).

并使用以下调用 phrase/2 将特征提取为代码列表。

lineCodes_feature(Line,Feature):-
 phrase(get_feature(Feature),Line),!.

然后我打电话给:

read_my_file('my_file.arff',Lines), 
member(Line,Lines), 
lineCodes_feature(Line,Feature), 
atom_codes(Atom,Feature).

然后返回以将每个特征视为一个原子。

我被困在修改的下一步,运行将其作为查询。 我想将 reaction/2 术语更改为 reaction/3,其中每个术语的附加参数都相同。

例如原子项

'reaction(A, 0), link(A, B, _), link(B, C, _), reaction(C, 1), reaction(B, 1), link(A, D, _), link(D, E, follows), reaction(E, 0)' 

我会想象成为一个列表:

[reaction(newarg,A, 0), link(A, B, _), link(B, C, _), reaction(newarg,C, 1), reaction(newarg,B, 1), link(A, D, _), link(D, E, follows), reaction(newarg,E, 0)]

哪个可以让我以某种方式使用 call/1 查询内部数据库?

感谢任何对我的方法的帮助或评论。

我想你可以使用 read_term_from_codes,然后调用结果项。

...
read_term_from_codes(Feature, Callable, []),
transform(Callable, Transformed),
Transformed,
...

一个示例,使用 library(dcg/basics) and phrase_from_file/2 来简化解析

:- use_module(library(dcg/basics)).

sw :- phrase_from_file(arff_file, '/home/carlo/test/x.weka').

arff_file --> [] ; arff_line, arff_file.

arff_line -->
    "@relation propositionalization", eol.
arff_line -->
    "@attribute '", string(Codes), "'",
    {read_term_from_codes(Codes, Prolog, []),
     transform_conjunction(Prolog,Transf),
     format('~w ->~n ~w~n', [Prolog, Transf])},
    string(_), eol.
arff_line -->
    eol.

eol --> "\n".

transform_conjunction((A,B),(C,D)) :- transform_term(A,C), !, transform_conjunction(B,D).
transform_conjunction(A,C) :- transform_term(A,C).

transform_term(reaction(B,C), reaction(newArg,B,C)).
transform_term(T, T).