I have a prolog file test1.Pmention 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?