Index: read-tools.pl =================================================================== RCS file: read-tools.pl diff -N read-tools.pl --- /dev/null 1 Jan 1970 00:00:00 -0000 +++ read-tools.pl 3 Jun 2009 20:14:22 -0000 @@ -0,0 +1,96 @@ +% Copyright (C) Paweł Płazieński 2009 +% +% This program is free software; you can redistribute it and/or modify it +% under the terms of the GNU General Public License as published by the +% Free Software Foundation; either version 2, or (at your option) any +% later version. +% +% This program is distributed in the hope that it will be useful, +% but WITHOUT ANY WARRANTY; without even the implied warranty of +% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +% GNU General Public License for more details. +% +% You should have received a copy of the GNU General Public License +% along with this program; if not, write to the Free Software +% Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA + +get_line(Text) :- + get_line(Text,[]). + +get_line(Text,Completions) :- + get_line(Text,Completions,'-.> '). + +get_line(Text,Completions,Prompt) :- + write(Prompt), + get_line([],ReverseString,Completions,Prompt), + reverse(ReverseString,String), + string_to_list(Text,String). + +get_line(Got,ReverseString,Completions,Prompt) :- + get_single_char(B), +% write(B), + ( (B = 13 ; B = 10), + nl, ttyflush, + Got = ReverseString, ! + ; B = 9, + reverse(Got,ReverseGot), + string_to_atom(ReverseGot,Begin), + find_begin(Completions,Begin,Begins), + ( Begins = [], + nl, write('. + + +:- write('VARDA: $Id: varda_controller.pl,v 0.9 2009-05-25 22:48:20 niver Exp $'),nl. + +:- ensure_loaded('string-list'). +:- ensure_loaded('read-tools'). + +shl :- + get_line(Line, + [sar,shi,sha,hic,tic,gax,sxt,pxt,xop,kid,axg,xxg,arp,pur,pux,hlp,hlt,ada,adp,dep,fin,spl]), + shell_interpret_line(Line). + +shl. + +shell_interpret_line(Line) :- + Line = end_of_file. + +shell_interpret_line(Line) :- + empty_line(Line), + !, shl. + +shell_interpret_line(Line) :- + string_to_atom(Line,Name), + current_functor(Name,0), + (call(Name) -> + write('<+- : Success'), nl; + write('. +% + +gen :- + write('please specify file prefix with gen/1'), nl. + +gen(F) :- + ard_property(X), + \+ ard_hist(_,X), + setof(Y, ard_generate_collaptions([X],[],Y), L), + length(L,K), + write('generated '), write(K), write(' scenarios, '), + write('first 10 will be saved to files'), nl, + ard_save_scenarios(L,F). + +ard_save_scenarios(L,F) :- + length(L,K), K > 10, + append(C,_,L), + length(C, 10), + ard_save_scenarios(C,F). + +ard_save_scenarios([],_). + +ard_save_scenarios(L,F) :- + length(L,K), K =< 10, K > 0, + string_to_atom(Ks,K), + concat(F,'-',S1), + concat(S1,Ks,S2), + concat(S2,'.pl',S), + write('saving '), write(S), nl, + L=[H|T], + tell(S), + reverse(H,RH), + ard_save_scenario(RH,F), + told, + ard_save_scenarios(T,F). + +ard_save_scenario(_,F) :- + write(F), write(' :-'), nl, fail. + +ard_save_scenario(_,_) :- + ard_att(X), + write(' ard_att_add(\''), write(X), write('\'),'), nl, + fail. + +ard_save_scenario(_,_) :- + ard_property(X), + \+ ard_hist(_,X), + write(' ard_property_add('), write_escaped(X), write('),'), nl, + fail. + +ard_save_scenario(H,_) :- + member(X,H), + X = [Action,From,To], + (Action = 'finalize' -> + write(' ard_finalize('), write_escaped(From), write(','), write_escaped(To), write('),'), nl + ; + write(' ard_split('), write_escaped(From), write(','), write_escaped(To), write(', []),'), nl + ), + fail. + +ard_save_scenario(_,F) :- + write(' true.'), nl, + write(':- '), write(F), write('.'), nl. + +write_escaped(X) :- + write('['), + member(E,X), + (not(is_list(E)) -> + write('\''), write(E), write('\'') + ; + write_escaped(E) + ), + (last(X,E) -> true ; write(', ')), + fail. + +write_escaped(_) :- + write(']'). + +ard_generate_collaptions(EndPropertyList,PrevTrans,FinalTrans) :- + select(Candidate,EndPropertyList,RestProperties), + bagof(X, ard_hist(Candidate,X), Products), + length(Products, ProductCount), + ProductCount > 1, + CurrTrans = [['split', Candidate, Products]|PrevTrans], + ard_non_final(Products,NonFinalProducts), + append(RestProperties, NonFinalProducts, NextTry), + ard_generate_collaptions(NextTry,CurrTrans,FinalTrans). + +ard_generate_collaptions(EndPropertyList,PrevTrans,FinalTrans) :- + select(Candidate,EndPropertyList,RestProperties), + bagof(X, ard_hist(Candidate,X), Products), + [Product] = Products, + CurrTrans = [['finalize', Candidate, Product]|PrevTrans], + ard_non_final(Products,NonFinalProducts), + append(RestProperties, NonFinalProducts, NextTry), + ard_generate_collaptions(NextTry,CurrTrans,FinalTrans). + +% ard_generate_collaptions(EndPropertyList,PrevTrans,FinalTrans) :- +% select(Candidate,EndPropertyList,RestProperties), +% \+ ard_hist(Candidate,_), +% CurrTrans = PrevTrans, +% ard_generate_collaptions(RestProperties,CurrTrans,FinalTrans). + +ard_generate_collaptions([],FinalTrans,FinalTrans). + +ard_non_final(Properties,NonFinals) :- + Properties = [X|PropRest], + ard_hist(X,_), + NonFinals = [X|NonFinalRest], + ard_non_final(PropRest,NonFinalRest). + +ard_non_final(Properties,NonFinals) :- + Properties = [X|PropRest], + \+ ard_hist(X,_), + ard_non_final(PropRest,NonFinals). + +ard_non_final([],[]).