简体   繁体   中英

Run Prolog queries within prolog file and store the result in a txt file

I have a prolog file test1.P mention below, I want to test multiple queries on this file, for this reason i have added 3-loops (query1,query2,quer3): I want to store the result of the query and time that query has took for evaluation to text file "result.txt". the problem is this code works fine but it only store the result of the last query in the file and does not store the time along side with query.

sol(Goal, Pair) :-
   term_variables(Goal, Vars),
   findall(Vars, Goal, Substs),
   write_list_to_file('result.txt',Substs),
   Pair = Vars-Substs.
   
ans(Goal, Pair):- statistics(runtime,[Start|_]),
sol(Goal, Pair),
statistics(runtime,[Stop|_]),
Runtime is Stop - Start,
write(Runtime).

loop_through_list(File, List) :-
    member(Element, List),
    write(File, Element),
    write(File, ' '),
    fail.

write_list_to_file(Filename,List) :-
    open(Filename, write, File),
    \+ loop_through_list(File, List),
    close(File).
    
:- forall(ans((pred1(X,Y),pred2(X,'BaldEagle')),L), writeln(L)). //query1
:- forall(ans((pred1(X,Y), pred3(X,'Eagle')),L), writeln(L1)). //quer2
:- forall(ans((pred1(X,Y), pred4(Y,Z)),L), writeln(L2)). //query3

So instead of using 3 queries in test1.P I have created another file named as testQueries.P and write my queries like this:

query(ans((pred1(X,Y),pred2(X,'BaldEagle')),L), writeln(L)).
query(ans((pred1(X,Y), pred3(X,'Eagle')),L), writeln(L1)).
query(ans((pred1(X,Y), pred4(Y,Z)),L), writeln(L2)).

myforall(X,Y) :- \+ (X, \+ Y).
mywrite(T) :- write(T), nl.
mycount(Query, Count) :-
                setof(Query, Query, Set),
                length(Set, Count),
                Count = 0.
test :-
    mycount(query(Q), Count).    
    myforall(query(Q), (Q -> mywrite(yes:Q); mywrite(no:Q))),
    halt.


:- initialization(['test1.P']).
:- initialization(test).

But still I am unable to store the results of all the queries and time in the text file. Also how can i get the number of results generated against query?

The problem is twofold:

  • You perform write(Runtime) of the "runtime in milliseconds" without the File as argument. Consequently, the output goes to "standard output" (or maybe "current output"), ie the terminal, not the file.
  • For each query, you open the file as open(Filename, write, File) , write whatever you want to write, then close the file. The file is opened in write mode, which is really truncation mode: The file emptied, and writing begins anew at file position 0. This explains why only the information regarding the last query is seen. You have to either open the file in append mode: open(Filename, append, File) or write all that you want to write before closing the file.

An improved write_list_to_file/3 , where we use forall/2 instead of a "failure-driven loop":

write_list_to_file(Filename,List,Mode) :-
    must_be(oneof([write,append]),Mode),
    setup_call_cleanup(    
       open(Filename, Mode, File),  % but what's the encoding? It depends!
       forall(
          member(Element,List),
          (
             write(File,Element),
             write(File,' ')
          )
       ),
       close(File)).

So, one has to call it once with Mode = write and twice with Mode = append . But this still doesn't help with getting the runtime milliseconds into the file.

The code is quite convoluted. Maybe it can be greatly simplified.

For example, this is still not ideal (in particular because it repeatedly opens and closes the file), but using the above write_list_to_file/3 :


question1(X,Y) :-
   pred1(X,Y),
   pred2(X,'BaldEagle')).
   
question2(X,Y) :-
   pred1(X,Y), 
   pred3(X,'Eagle')),

question3(X,Y,Z) :-
   pred1(X,Y),
   pred4(Y,Z).
   
time_goal(Goal,VarsBindings,Delta_ms) :-
   statistics(runtime,[Start|_]),
   term_variables(Goal, Vars),
   findall(Vars,Goal,VarsBindings),
   statistics(runtime,[Stop|_]),  
   Delta_ms is Stop - Start.
   
time_goal(Goal,Filename,Mode) :-
   time_goal(Goal,VarsBindings,Delta_ms),
   write_list_to_file(Filename,[Goal,Delta_ms],Mode),
   write_list_to_file(Filename,VarsBindings,append).

time_all(Filename) :-
   time_goal(question1(X,Y),Filename,write),
   time_goal(question2(X,Y),Filename,append),
   time_goal(question3(X,Y,Z),Filename,append).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM