On 05/03/2013 15:39, Cédric Moonen wrote: > Hello, > > First, I am sorry to post such a newbie question but I'm very new to Eclipse-clp and I've been > reading a bit in the tutorial but couldn't find an answer to my question. > > So, I'm using a Java program which is actually executing a query via the OutOfProcessEclipse::rcp > method. This is a very simple query which calls a predicate to find a solution and then output this > solution to a file: > > findSolution(X), generateFile("Solution.txt", X) > > I need to modify this query so that not only the first solution gets generated to a file, but all > solutions (to different files: Solution1.txt, Solution2.txt, ... ). So, I've found that I can use > findall in this way: > > findall(X, findSolution(X), List), ( foreach(Inst, List) do ( sprintf(FileName, > "Solution%d.txt",[Index]), generateFile(FileName, Inst) ) ) > > So, I need to have the Index variable to reflect the iteration count. I tried several things (use > foreachelem, count, ...) but I didn't succeed in finding a way to do that. You add a 'count'-iterator to the loop: ... ( foreach(Inst, List), count(Index,1,_) do sprintf(FileName, "Solution%d.txt",[Index]), generateFile(FileName, Inst) ) ) Having said that, using findall plus iterating through the results is not the most efficient way to do this. You can do it more directly, but it needs a "failure-driven loop" and a non-logical counter ("shelf"): solutions_to_files :- shelf_create(count(0), Handle), ( findSolution(X), % possibly multiple solutions shelf_inc(Handle, 1), shelf_get(Handle, 1, Index), sprintf(FileName, "Solution%d.txt",[Index]), generateFile(FileName, X), fail % to find the next solution ; true % no more solution ). And finally, you should probably consider not to use files at all... -- JoachimReceived on Tue Mar 05 2013 - 17:05:30 CET
This archive was generated by hypermail 2.2.0 : Sat Mar 23 2013 - 06:13:53 CET