1:- module(logicmoo_ocl_and_pddl,[test_blocks/0,test_domain/1,test_all/0,test_rest/0,test_sas/0,test_dir_files_sas/1,test_dir_files_sas/3]).    2
    3
    4
    5solve_files_w_ocl(DomainFile, ProblemFile):-
    6   must_det_l(( 
    7      format('~q.~n',[solve_files(DomainFile, ProblemFile)]))),
    8   parseDomain(DomainFile, DD),
    9    parseProblem(ProblemFile, PP),
   10    solve_files_w_ocl_pt2(DD, PP).
   11
   12solve_files_w_ocl_pt2(DD, PP):-
   13   must_det_l(( 
   14    term_to_ord_term(DD, D),prop_get(domain_name,D,DName),save_type_named(domain,DName,D),
   15    term_to_ord_term(PP, P),prop_get(problem_name,P,PName),save_type_named(problem,PName,P),    
   16    reset_statistic)),
   17    !,
   18   locally(t_l:other_planner(hyhtn_solve), record_time(try_solve(PName, D,P,S),SolverTime)),
   19    flag(time_used_other,X,X + SolverTime),
   20    show_statistic(P, S),
   21    !.
   22
   23
   24
   25compile_problem(Pu,P):-
   26 must_det_l((
   27 replc_structure_vars(Pu,Pu0),
   28  term_to_ord_term(Pu0,P),
   29  prop_get(init,P, UCI),
   30  prop_get(goal,P, UCG),
   31  term_to_ord_term((UCI,UCG),OT),
   32  copy_term_for_assert(OT,(I,G)),
   33  prop_set(init,P, I),
   34  prop_set(goal,P, G),
   35  prop_get(problem_name,P,PName),
   36  save_type_named(problem,PName,P))).
   37
   38compile_domain(D,Dc):-
   39 must_det_l((
   40    prop_get(domain_name,D, DName),
   41    prop_get(actions,D,A),
   42    must_maplist(copy_term_spec,A,AC),
   43    prop_set(actions,D,AC),
   44    must_maplist(to_action5,AC,A5),
   45    prop_set(actions5,D,A5),
   46    must_maplist(save_action(DName), A5),    
   47    D = Dc,  % replc_structure_vars2(D,Dc),
   48    save_type_named(domain,DName,Dc))).
   49
   50save_varnames_in_action(A,CA):-varnames_for_assert(A,CA,Vars),prop_set(varnames,CA,Vars).
   51
   52% DMILES
   53to_action5(action(_S, _L, P, PE, NE, Assign, ActionDef,_,_),action5(ActionDef, P, PE, NE,Assign)):-!.
   54to_action5(Struct,Term):-
   55  prop_get_nvlist(Struct,[parameters=UT,preconditions=Precon,positiv_effect=Pos,negativ_effect=Neg,assign_effect=Af]),
   56  Term=action5(UT, Precon, Pos, Neg, Af).
   57     
   58save_action(Mt,A):- ain(actn(Mt,A)).
   59
   60
   61
   62record_time(G,TimeUsed):- record_time(G,runtime,TimeUsed).
   63record_time(G,Runtime,TimeUsed):- statistics(Runtime, [B,_]),G,statistics(Runtime, [E,_]),TimeUsed is E - B.
   64
   65
   66try_solve(PN,D,P,S):- t_l:loading_files,!,pmsg((loading_files(PN):-try_solve(D,P,S))),!.
   67% try_solve(PN,D,P,S):- once(time_out(solve(PN,D, P, S), 3000, Result)), Result == time_out, portray_clause(hard_working:-try_solve(PN,D,P,S)),fail.
   68try_solve(PN,D,P,S):- gripe_time(14,time_out((solve(PN,D, P, S)), 30000, Result)),!, % time limit for a planner (was 500000)  30000
   69   ((\+ is_list(S)
   70     -> portray_clause('failed'(Result):-try_solve(PN,D,P,S)) ;
   71       ((Result=time_out)->portray_clause('failed'(Result):-try_solve(PN,D,P,S));true))),fail.
   72
   73try_solve(PN,D,P,S):- solve(PN,D, P, S),fail.
   74
   75try_solve(PN,D,P,S):-dmsg('utter_failed'(warn):-try_solve(PN,D,P,S)),!.
   76
   77sdmsg(B,B):- \+ compound(B),!.
   78sdmsg((D:-B),SS):-B==true,!,sdmsg(D,SS).
   79sdmsg(B,SS):-B=..[F|A],must_maplist(sdmsg,A,AA),SS=..[F|AA].
   80
   81pmsg(D):- sdmsg(D,SS),D \=@= SS, !,pmsg(SS).
   82pmsg(D):- compound(D),functor(D,(:-),_),!,subst(D,=,'k_===_v',SS),wdmsg(SS).
   83pmsg(D):- subst(D,=,'k_===_v',SS),wdmsg(SS),wdmsg((:-SS)).
   84
   85
   86
   87
   88hyhtn_solve(_,D, P, Solution):-
   89  must_det_l((
   90    env_clear_doms_and_tasks,
   91    clean_problem,
   92    bb_put(currentProblem, P),
   93    bb_put(currentDomain, D),
   94    prop_get(init,P, UCI),
   95    prop_get(goal,P, UCG),
   96
   97
   98    copy_term_for_assert((UCI,UCG),(I,G)),
   99    prop_get(domain_name,D,Mt),    
  100    must(prop_get(domain_name,P,Mt)),
  101    must(prop_get(types,D,Types)),
  102    must(prop_get(predicates,D,Preds)),
  103    must(prop_get(objects,P,Objects)),
  104    must_maplist(save_ocl_objects,Objects),
  105    must_maplist(save_ocl_predicates,Preds),
  106    must_maplist(save_ocl_types,Types),
  107    wdmsg(dtpo(Mt,Types,Preds,Objects)),
  108    prop_get(actions,D, A),
  109    must_maplist(save_ocl_operators,A),
  110    bb_put(goalState, G),        
  111    bb_put(fictiveGoal, G))),
  112    ignore(init_locol_planner_interface0(G,I,Solution)).
  113
  114save_ocl_operators(A):-dmsg(save_ocl_operators(A)), % varnames_for_assert(A,CA,Vars),
  115   must(( 
  116      prop_get_nvlist(A,
  117         [(preconditions)=Precon,positiv_effect=Pos,negativ_effect=Neg, assign_effect=Af, (parameters)= UT, 
  118                 parameter_types=SPT,parameters_decl=PDs]),
  119     UT=..[_|ARGS],     
  120     SPT=..[_|PTs], 
  121     nop(must_maplist(record_var_names,Vars)),
  122     must_maplist(create_hint,PTs,ARGS,ARGHints),
  123     %conjuncts_to_list(Call,MORE),
  124     append(ARGHints,Precon,M0),
  125     append(M0,Af,PrecondHints),
  126     append(Pos,Neg,POSNEG),
  127     append(PrecondHints,POSNEG,ALLHINTS),
  128     append(POSNEG,PrecondHints,REVALLHINTS),
  129     to_ssify(ALLHINTS,se,PrecondHints,SE),
  130     get_one_isa(S,X,REVALLHINTS),
  131     SC = sc(S,X,Neg=>Pos),
  132     OP = operator(UT,SE,SC,[]),
  133     varnames_for_assert(OP,COP,_Vars),
  134     env_aif(COP))).
  135
  136env_aif(G):-functor(G,F,_),wdmsg(F:-G), assertz_new(ocl:G).
  137
  138save_ocl_predicates(Decl):-dmsg(save_ocl_predicates(Decl)),prop_get(parameter_types,Decl,PTDecl),   
  139   env_aif(predicates([PTDecl])),PTDecl=..[F|PTypes],must_maplist(save_ocl_types,PTypes).
  140
  141save_ocl_types(Atom):- atom( Atom ),!, save_ocl_types([Atom]-type).
  142save_ocl_types(Obj):- Obj=..[Type,List],!,save_ocl_types(List-Type).
  143save_ocl_types(Type-Type):-!.
  144save_ocl_types([Type]-Type):-!.
  145save_ocl_types(List-Type):- atom(List),!,save_ocl_types([List]-Type).
  146save_ocl_types(List-Type):- Type==type-> save_ocl_objects(type(List))->save_ocl_types(List-top)->save_ocl_types(List-primitive_sorts).
  147save_ocl_types(List-Type):-  env_aif(sorts(Type,List)),  
  148    (Type==non_primitive_sorts;true;save_ocl_types(Type-non_primitive_sorts)).
  149
  150save_ocl_objects(Atom):- atom( Atom ),!, save_ocl_objects([Atom]-top).
  151save_ocl_objects(Type-Type):-!.
  152save_ocl_objects(_-top):-!.
  153save_ocl_objects(_-type):-!.
  154save_ocl_objects([Type]-Type):-!.
  155save_ocl_objects(List-Type):- atom(List),!,save_ocl_objects([List]-Type).
  156% save_ocl_objects(List-Type):- Type==top-> !.
  157save_ocl_objects(Obj):- Obj=..[Type,List],!,save_ocl_objects(List-Type).
  158save_ocl_objects(List-Type):- env_aif(objects(Type,List)).
  159% FILENAME:  domain.pl 
  160% :- module(pdd_domain, [pdd_domain/2, pdd_domain/3, pddl_not_equal/2, pddl_bind/1]).
  161
  162% :- expects_dialect(sicstus).
  163
  164:- use_module(library(when)).  165% :- use_module(library(atts)).
  166:- use_module(library(ordsets), [
  167        ord_intersection/3,
  168        ord_intersect/2,
  169        ord_union/3,
  170        ord_union/2,
  171        %ord_member/2,
  172        %ord_nonmember/2,
  173        ord_add_element/3,
  174        ord_del_element/3,
  175        list_to_ord_set/2
  176   ]).  177
  178% :- use_module(library(sets), [ del_element/3 ]).
  179
  180sicstus_get_atts(V,As):- get_attrs(V,As).
  181sicstus_put_atts(V,As):- put_attrs(V,As).
  182%:- attribute(pddl_dom/2).
  183%:- attribute forbidden/1.
  184
  185domain3:verify_attributes(Var, Other, Goals) :-
  186    sicstus_get_atts(Var, pddl_dom(Da, Fa)),             % are we involved?
  187    !,
  188    (   var(Other) ->                       % must be attributed then
  189        (   sicstus_get_atts(Other, pddl_dom(Db, Fb)) -> %   has a pdd_domain?
  190            (my_del_element2(Var, Fb, _) -> 
  191                 !, 
  192                 fail
  193                 ;
  194                 true
  195            ),
  196            ord_intersection(Da, Db, Dc),
  197            ord_union(Fa, Fb, Fc),
  198            Dc = [El|Els],              % at least one element
  199            (   Els = [] ->             % exactly one element
  200                Goals = [Other=El]      % implied binding
  201                ;   
  202                Goals = [],
  203                sicstus_put_atts(Other, pddl_dom(Dc, Fc))% rescue intersection
  204            )
  205            ;
  206            Goals = [],
  207            sicstus_put_atts(Other, pddl_dom(Da, Fa))    % rescue the pdd_domain
  208        )
  209        ;
  210        Goals = [],
  211        ord_intersect([Other], Da),      % value in pdd_domain?
  212        delete_from(Fa, Var, Other),
  213        sicstus_put_atts(Var, pddl_dom([Other], Fa)),
  214%        my_del_element(Var, Fa, NewFa),
  215        bind_all(Fa)
  216    ).
  217domain3:verify_attributes(_, _, []).                % unification triggered
  218                                            % because of attributes
  219                                            % in other modules
  220
  221attribute_goal(Var, pdd_domain(Var,Dom, F)) :-     % interpretation as goal
  222    sicstus_get_atts(Var, pddl_dom(Dom, F)).
  223
  224pdd_domain(X, Dom) :-
  225    pdd_domain(X, Dom, _).
  226pdd_domain(X, List) :-
  227    list_to_ord_set(List, Set),
  228    Set = [El|Els],                     % at least one element
  229    (   Els = [] ->                     % exactly one element
  230        X = El                          % implied binding
  231        ;
  232        sicstus_put_atts(Fresh, pddl_dom(Set, [])),
  233        X = Fresh                       % may call
  234                                        % domain3:verify_attributes/3
  235    ).
  236
  237pdd_domain(X, Dom, F) :-
  238    var(Dom),
  239    !,
  240    sicstus_get_atts(X, pddl_dom(Dom, F)).
  241
  242delete_from([], _, _).
  243delete_from([A|T], V, Value):-
  244    (A==V ->
  245        true
  246        ;
  247        sicstus_get_atts(A, pddl_dom(Ad, Af)),
  248        my_del_element(Value, Ad, NewAd),
  249        my_del_element(V, Af, NewAf),
  250        sicstus_put_atts(A, pddl_dom(NewAd, NewAf))
  251    ),
  252    delete_from(T, V, Value).
  253
  254my_del_element(_, [], []).
  255my_del_element(E, [H|T], R):-
  256    E==H,
  257    !,
  258    my_del_element(E, T, R).
  259my_del_element(E, [H|T], [H|R]):-
  260    my_del_element(E, T, R).
  261
  262my_del_element2(E, [H|T], R):-
  263    E==H,
  264    !,
  265    my_del_element(E, T, R).
  266my_del_element2(E, [H|T], [H|R]):-
  267    my_del_element2(E, T, R).
  268
  269
  270pddl_not_equal([], []).
  271pddl_not_equal(A, B):-
  272    ground(A), ground(B),
  273    !,
  274    A\=B.
  275
  276pddl_not_equal(A, B):-
  277    ground(A),
  278    !,
  279    pddl_not_equal(B, A).
  280pddl_not_equal(A, B):-
  281    var(A), ground(B),
  282    !,
  283    sicstus_get_atts(A, pddl_dom(Ad, Fa)),
  284    ord_del_element(Ad, B, NewAd),
  285    sicstus_put_atts(A, pddl_dom(NewAd, Fa)),
  286    pddl_bind(Fa).
  287pddl_not_equal(A, B):-
  288    A==B,
  289    !,
  290    fail.
  291pddl_not_equal(A, B):-
  292    var(A), var(B),
  293    sicstus_get_atts(A, pddl_dom(Da, Fa)),
  294    sicstus_get_atts(B, pddl_dom(Db, Fb)),
  295%    ord_union([Fa,Fb,[A,B]], F),
  296    ord_union([[B],Fa], Faa),
  297    ord_union([[A],Fb], Fbb),
  298    sicstus_put_atts(A, pddl_dom(Da, Faa)),
  299    sicstus_put_atts(B, pddl_dom(Db, Fbb)),
  300    ord_union([[A],Fa], Faaaa),
  301    ord_union([[B],Fb], Fbbbb),
  302    pddl_bind(Faaaa),
  303    pddl_bind(Fbbbb).
  304pddl_not_equal([Ha|Ta], [Hb|Tb]):-
  305    !,
  306    pddl_not_equal(Ha, Hb),
  307    pddl_not_equal(Ta, Tb).
  308pddl_not_equal(A, B):-
  309    compound(A), compound(B),
  310    !,
  311    A =.. [Fa|Pa], B=..[Fb|Pb],
  312    (Fa=Fb ->
  313        pddl_not_equal(Pa, Pb)
  314        ;
  315        true
  316    ).
  317
  318
  319set_forbidden([], _).
  320set_forbidden([H|T], F):-
  321    sicstus_get_atts(H, pddl_dom(D, _)),
  322    sicstus_put_atts(H, pddl_dom(D, F)),
  323%    write(H-D-F),nl,
  324    set_forbidden(T, F).
  325
  326bind_all([]).
  327bind_all([H|T]):-
  328  (var(H) ->
  329       pddl_bind([H])
  330       ;
  331       true
  332  ),
  333  bind_all(T).
  334
  335pddl_bind(F):-
  336    setof(B, solvable(F, [], B), Bs),
  337    rotate(Bs, RB),
  338    bind_value(F, RB).
  339
  340bind_value([], _).
  341bind_value([H|T], [B|Bs]):-
  342    (var(H) ->
  343        list_to_ord_set(B, OB),
  344        (OB=[VB] ->
  345            H=VB
  346            ;
  347            sicstus_get_atts(H, pddl_dom(_, Hf)),
  348            sicstus_put_atts(H, pddl_dom(OB, Hf))
  349        )
  350        ;
  351        true
  352   ),
  353   bind_value(T, Bs).
  354
  355rotate([[]|_], []).
  356rotate(S, [F|Fs]):-
  357    first(S, F, R),
  358    rotate(R, Fs).
  359
  360first([], [], []).
  361first([[F|T]|T2], [F|Fs], [T|R]):-
  362    first(T2, Fs, R).
  363
  364solvable([], _, []).
  365solvable([H|T], FV, [M|S]):-
  366    (var(H) ->
  367        sicstus_get_atts(H, pddl_dom(Hd, _)),
  368        member(M, Hd),
  369        ordsets:ord_nonmember(M, FV),
  370        ord_add_element(FV, M, NewFV)
  371        ;
  372        NewFV=FV
  373    ),
  374    solvable(T, NewFV, S).
  375 
  376 
  377 
  378 
  379 
  380 
  381 
  382 
  383 
  384 
  385 
  386 
  387 
  388 
  389 
  390 
  391
  392
  393prop_merge_svo(Struct,Name,Value):-prop_merge(Name,Struct,Value).
  394
  395
  396domain_name_for_ocl(Name):- use_local_pddl, no_repeats(domain_name0(Name)).
  397domain_name0(Name):- bb_get(currentDomain, D),prop_get(domain_name,D,Name).
  398domain_name0(Name):- bb_get(currentProblem, P),prop_get(domain_name,P,Name).
  399domain_name0(Name):- user:is_saved_type(domain,Name,_).
  400domain_name0(Name):- user:is_saved_type(problem,_,P),prop_get(domain_name,P,Name).
  401
  402problem_name(Name):- use_local_pddl, no_repeats(problem_name0(Name)).
  403problem_name0(Name):- bb_get(currentProblem, P),prop_get(problem_name,P,Name).
  404problem_name0(Name):- user:is_saved_type(problem,Name,_).
  405
  406
  407/* EXAMPLE OCLh
  408
  409% Sorts
  410sorts(chameleonWorld,primitive_sorts,[door,flexarium,chameleon,box,substrate]).
  411*/ 
  412
  413sorts_for_ocl(DName,primitive_sorts,TypesList):-  use_local_pddl, pddl_sorts(DName,primitive_sorts,TypesList).
  414pddl_sorts(DName,primitive_sorts,TypesList):- bb_get(currentDomain, D),prop_get(domain_name,D,DName),!,prop_get(types,D,TypesList).
  415pddl_sorts(DName,primitive_sorts,List):-nonvar(DName),findall(S,is_a_type(DName,S),List).
  416
  417
  418is_a_type(D,S):- use_local_pddl, loop_check(is_a_type0(D,S)).
  419is_a_type0(D,S):-sorts(D,_,L),member(S,L).
  420is_a_type0(Name,S):-bb_get(currentProblem,P),prop_get(domain_name,P,Name),objects_3(P,S,_).
  421
  422
  423pname_to_dname(t7,chameleonWorld).
  424pname_to_dname(PName,DName):-name_to_problem_struct(PName,P),!,prop_get(domain_name,P,DName).
  425pname_to_dname(_,DName):-domain_name(DName).
  426
  427% Objects
  428objects_for_ocl(Name,Type,List):- use_local_pddl, name_to_problem_struct(Name,P),
  429   prop_get(objects,P,ObjsDef),
  430   member(Objs,ObjsDef),Objs=..[Type,List].
  431
  432/* EXAMPLE OCLh
  433objects(t7,door,[door1]).
  434objects(t7,flexarium,[flexarium1]).
  435objects(t7,chameleon,[veiledChameleon]).
  436objects(t7,box,[box1,box2]).
  437objects(t7,substrate,[newsPaper1,newsPaper2]).
  438*/
  439
  440% Predicates
  441predicates_for_ocl(Name,List):- use_local_pddl, name_to_domain_struct(Name,D),   
  442   prop_get(predicates,D,List).
  443
  444
  445/* EXAMPLE OCLh
  446predicates(chameleonWorld,[
  447    doorOpen(door),
  448    doorClosed(door),
  449    dirty(flexarium),
  450    clean(flexarium),
  451    inBox(chameleon,box),
  452    inHands(chameleon),
  453    inFlexarium(chameleon),
  454    boxOpen(box),
  455    boxClosed(box),
  456    insideFlexarium(substrate),
  457    outsideFlexarium(substrate)]).
  458
  459% Object Class Definitions
  460substate_classes(chameleonWorld,door,Door,[
  461    [doorOpen(Door)],
  462    [doorClosed(Door)]]).
  463substate_classes(chameleonWorld,flexarium,Flexarium,[
  464    [dirty(Flexarium)],
  465    [clean(Flexarium)]]).
  466substate_classes(chameleonWorld,chameleon,Chameleon,[
  467    [inBox(Chameleon,_Box)],
  468    [inHands(Chameleon)],
  469    [inFlexarium(Chameleon)]]).
  470substate_classes(chameleonWorld,box,Box,[
  471    [boxOpen(Box)],
  472    [boxClosed(Box)]]).
  473substate_classes(chameleonWorld,substrate,Substrate,[
  474    [insideFlexarium(Substrate)],
  475    [outsideFlexarium(Substrate)]]).
  476
  477% Atomic Invariants
  478
  479% Implied Invariants
  480
  481% Inconsistent Constraints
  482
  483% Operators
  484operator(chameleonWorld,takeOutFlex(Door,Chameleon),
  485    % prevail
  486    [     se(door,Door,[doorOpen(Door)])],
  487    % necessary
  488    [     sc(chameleon,Chameleon,[inFlexarium(Chameleon)]=>[inHands(Chameleon)])],
  489    % conditional
  490    []).
  491operator(chameleonWorld,putInBox(Box,Chameleon),
  492    % prevail
  493    [     se(box,Box,[boxOpen(Box)])],
  494    % necessary
  495    [     sc(chameleon,Chameleon,[inHands(Chameleon)]=>[inBox(Chameleon,Box)])],
  496    % conditional
  497    []).
  498operator(chameleonWorld,takeOutBox(Box,Chameleon),
  499    % prevail
  500    [     se(box,Box,[boxOpen(Box)])],
  501    % necessary
  502    [     sc(chameleon,Chameleon,[inBox(Chameleon,Box)]=>[inHands(Chameleon)])],
  503    % conditional
  504    []).
  505operator(chameleonWorld,putInFlex(Door,Substrate,Flexarium,Chameleon),
  506    % prevail
  507    [     se(door,Door,[doorOpen(Door)]),
  508     se(substrate,Substrate,[insideFlexarium(Substrate)]),
  509     se(flexarium,Flexarium,[clean(Flexarium)])],
  510    % necessary
  511    [     sc(chameleon,Chameleon,[inHands(Chameleon)]=>[inFlexarium(Chameleon)])],
  512    % conditional
  513    []).
  514operator(chameleonWorld,openDoor(Door),
  515    % prevail
  516    [],
  517    % necessary
  518    [     sc(door,Door,[doorClosed(Door)]=>[doorOpen(Door)])],
  519    % conditional
  520    []).
  521operator(chameleonWorld,closeDoor(Door),
  522    % prevail
  523    [],
  524    % necessary
  525    [     sc(door,Door,[doorOpen(Door)]=>[doorClosed(Door)])],
  526    % conditional
  527    []).
  528operator(chameleonWorld,time(Flexarium),
  529    % prevail
  530    [],
  531    % necessary
  532    [     sc(flexarium,Flexarium,[clean(Flexarium)]=>[dirty(Flexarium)])],
  533    % conditional
  534    []).
  535operator(chameleonWorld,wash(Chameleon,Box,Door,Substrate,Flexarium),
  536    % prevail
  537    [     se(chameleon,Chameleon,[inBox(Chameleon,Box)]),
  538     se(door,Door,[doorOpen(Door)]),
  539     se(substrate,Substrate,[outsideFlexarium(Substrate)])],
  540    % necessary
  541    [     sc(flexarium,Flexarium,[dirty(Flexarium)]=>[clean(Flexarium)])],
  542    % conditional
  543    []).
  544operator(chameleonWorld,addCleanNewspaper(Flexarium,Door,Chameleon,Box,Substrate),
  545    % prevail
  546    [     se(flexarium,Flexarium,[clean(Flexarium)]),
  547     se(door,Door,[doorOpen(Door)]),
  548     se(chameleon,Chameleon,[inBox(Chameleon,Box)])],
  549    % necessary
  550    [     sc(substrate,Substrate,[outsideFlexarium(Substrate)]=>[insideFlexarium(Substrate)])],
  551    % conditional
  552    []).
  553operator(chameleonWorld,removeDirtyNewspaper(Flexarium,Door,Chameleon,Box,Substrate),
  554    % prevail
  555    [     se(flexarium,Flexarium,[dirty(Flexarium)]),
  556     se(door,Door,[doorOpen(Door)]),
  557     se(chameleon,Chameleon,[inBox(Chameleon,Box)])],
  558    % necessary
  559    [     sc(substrate,Substrate,[insideFlexarium(Substrate)]=>[outsideFlexarium(Substrate)])],
  560    % conditional
  561    []).
  562operator(chameleonWorld,openBox(Box),
  563    % prevail
  564    [],
  565    % necessary
  566    [     sc(box,Box,[boxClosed(Box)]=>[boxOpen(Box)])],
  567    % conditional
  568    []).
  569operator(chameleonWorld,closeBox(Box),
  570    % prevail
  571    [],
  572    % necessary
  573    [     sc(box,Box,[boxOpen(Box)]=>[boxClosed(Box)])],
  574    % conditional
  575    []).
  576
  577
  578operator(chameleonWorld,removeDirtyNewspaper(Flexarium,Door,Chameleon,Box,Substrate),
  579    % prevail
  580    [     se(flexarium,Flexarium,[dirty(Flexarium)]),
  581     se(door,Door,[doorOpen(Door)]),
  582     se(chameleon,Chameleon,[inBox(Chameleon,Box)])],
  583    % necessary
  584    [     sc(substrate,Substrate,[insideFlexarium(Substrate)]=>[outsideFlexarium(Substrate)])],
  585    % conditional
  586    []).
  587*/
  588
  589:- style_check(+singleton).  590
  591op_action(Mt, S, PTs, NPrecon, Pos, Neg, Af, UT , True):- use_local_pddl,
  592   loop_check(env_call(operator(Mt,UT,SE,SC,SS))),
  593   must_det_l((
  594      True = true,
  595      UT=..[S|ARGS],
  596      must_maplist(lock_var,ARGS),
  597      HintsIn=[],
  598      unss_ify(=,HintsIn,ss,SS,Af,Hints1),
  599      unss_ify(=,Hints1,se,SE,Precon,Hints2),
  600      unss_ify(=,Hints2,sc,SC,NEGPOS,Hints),
  601      divide_neg_pos(NEGPOS,[],[],Neg,Pos),   
  602      append(Precon,Neg,NPrecon),
  603      must_maplist(get_type_of(Mt,top,Hints),ARGS,PTs))).
  604
  605lock_var(X):-when((?=(X,Y);nonvar(X)),X==Y).
  606unlock_var(V):-del_attrs(V).
  607
  608
  609add_wrapper(W,In , Out):-Out=..[W,In].
  610
  611actn_operator(Mt,UT,SE,SC,SS):- use_local_pddl, actn(Mt,A),
  612 must_det_l(( 
  613    prop_get_nvlist(A,
  614       [(preconditions)=Precon,positiv_effect=Pos,negativ_effect=Neg, assign_effect=Af, (parameters)= UT, 
  615               parameter_types=PTs,
  616               (constraints)=Call,
  617               (varnames)=Vars]),
  618   UT=..[_|ARGS],
  619   show_call(Call),   
  620   must_maplist(record_var_names,Vars),
  621   must_maplist(create_hint,PTs,ARGS,ARGHints),
  622   conjuncts_to_list(Call,MORE),
  623   append(ARGHints,MORE,PrecondHints),
  624   unss_ify(=,[],PrecondHints,Precon,se,SEPs,HintsSE),
  625   unss_ify(=,[],HintsSE,Af,ss,ASEPs,HintsSS),
  626   unss_ify(add_wrapper(del),[],HintsSS,Neg,ss,NOTS,HintsNEG),
  627   unss_ify(add_wrapper(add),NOTS,HintsNEG,Pos,ss,NOTSPOSC,HintsPOS),
  628   mylist_to_set(HintsPOS,Hints),
  629   must_maplist(ress_ify(Mt,Def,Hints,se),SEPs,SE),
  630   must_maplist(ress_ify(Mt,Def,Hints,ss),ASEPs,SS),   
  631   must_maplist(make_rem_adds(Mt,Hints),NOTSPOSC,SC))).
  632  
  633
  634make_rem_adds(Mt,Hints,A1-LIST,sc(Type,A1,(NEG=>POS))):-findall(N,member(del(N),LIST),NEG),findall(N,member(add(N),LIST),POS),
  635  must(get_type_of(Mt,top,Hints,A1,Type)).
  636
  637create_hint(S,X,is_of_sort(X,S)).
  638
  639mylist_to_set(HintsIn,HintsM):-must(list_to_set(HintsIn,HintsM)).
  640
  641ghints(HintsIn,[],HintsIn).
  642ghints(HintsIn,[G|More],HintsOut):-
  643   ghints(HintsIn,G,HintsM),
  644   ghints(HintsM,More,HintsOut).
  645ghints(HintsIn,G,[kt(F,A),p(F)|HintsIn]):-G=..[F,A],!.
  646ghints(HintsIn,G,[kta(F,1,A),p(F)|Hints]):-G=..[F,A|ARGS],garg_hints(HintsIn,F,2,ARGS,Hints).
  647
  648ss_other(ss).
  649ss_other(sc).
  650ss_other(se).
  651
  652garg_hints(HintsIn,_,_,[],HintsIn).
  653garg_hints(HintsIn,F,N,[A|ARGS],[kta(F,N,A)|Hints]):- 
  654  N2 is N + 1, garg_hints(HintsIn,F,N2,ARGS,Hints).
  655
  656unss_ify(_ ,WAS,Hints,G,_,WAS,Hints):- (nonvar(G);G==['Uninitialized']),!.
  657unss_ify(GT,WAS,HintsIn,G,SS,OUTS,Hints):-
  658  must((dess_ify(GT,WAS,HintsIn,G,SS,OUT,HintsM),!,mygroup_pairs_by_key(OUT,OUTS),!,mylist_to_set(HintsM,Hints))).
  659
  660dess_ify(GT,WAS,HintsIn,G,SS,OUT,Hints):-mylist_to_set(HintsIn,HintsM),HintsIn\=@=HintsM,!,
  661  dess_ify(GT,WAS,HintsM,G,SS,OUT,Hints).
  662
  663dess_ify( _,WAS,HintsIn,[],_SS,WAS,HintsIn).
  664
  665dess_ify(GT,WAS,HintsIn,NC,_SS,OUT,HintsIn):- ( \+ compound(NC)),!,must((call(GT,NC,NCC),append(WAS,[NCC],OUT))).
  666
  667dess_ify(GT,WAS,HintsIn,[G|GG],SS,OUT,HintsOut):- !,
  668  dess_ify(GT,WAS,HintsIn,G,SS,MID,Hints1),dess_ify(GT,MID,Hints1,GG,SS,OUT,HintsOut).
  669
  670dess_ify(GT,WAS,HintsIn,A1-[GLs|GG],SS,OUT,HintsOut):- 
  671  dess_ify(GT,WAS,HintsIn,A1-GLs,SS,SEs,Hints1),dess_ify(GT,SEs,Hints1,A1-GG,SS,OUT,HintsOut).
  672
  673dess_ify(GT,WAS,HintsIn,SSG,SS,OUT,[kt(Type,A1)|HintsOut]):- SSG=..[SS,Type,A1,GLs],
  674  dess_ify(GT,WAS,HintsIn,A1-GLs,SS,OUT,HintsOut).
  675
  676dess_ify(GT,WAS,HintsIn,SSG,SS,OUT,[kt(Type,A1)|HintsOut]):- SSG=..[SOTHER,Type,A1,GLs],ss_other(SOTHER),
  677  dess_ify(GT,WAS,HintsIn,A1-GLs,SS,OUT,HintsOut).
  678
  679dess_ify(GT,WAS,HintsIn,A1-G,_ ,[A1-GO|WAS],HintsOut):- ghints(HintsIn,G,HintsOut),call(GT,G,GO).
  680
  681dess_ify(GT,WAS,HintsIn,G,SS,OUT,Hints):- arg(1,G,A1), dess_ify(GT,WAS,HintsIn,A1-G,SS,OUT,Hints).
  682
  683ress_ify(Mt, Def, Hints,SS,A1-Gs,GO):-GO=..[SS,Type,A1,Gs],must(get_type_of(Mt,Def,Hints,A1,Type)),!.
  684ress_ify(_Mt,Def,_Hints,SS,A1-Gs,GO):-GO=..[SS,Def,A1,Gs],!.
  685
  686
  687
  688
  689get_type_of(_ , Def, Hints,A1,Type):-member(kt(K,V),Hints),A1==V,K\==Def,!,Type=K,record_var_type(A1,Type).
  690get_type_of(Mt, Def, Hints,A1,Type):-atom(A1),get_type_of_atom(Mt,Def,Hints,A1,Type),!.
  691get_type_of(Mt,_Def,_Hints,A1,Type):-nonvar(A1),loop_check(objects_3(Mt,Type,List)),member(A1,List).
  692
  693:- style_check(-singleton).  694get_type_of_atom(Mt,Def,Hints,veiledChameleon,chameleon).
  695get_type_of_atom(Mt,Def,Hints,veiledchameleon,chameleon).
  696get_type_of_atom(Mt,Def,Hints,A1,Type):-pname_to_dname(P,D),is_a_type(D,Type),atom_concat(Type,Num,A1),Num=_.
  697get_type_of_atom(Mt,Def,Hints,A1,Type):-pname_to_dname(P,D),is_a_type(D,Type),atom_concat(_,Type,A1).
  698:- style_check(+singleton).  699
  700
  701
  702divide_neg_pos([],Neg,Pos,Neg,Pos).
  703divide_neg_pos([A|MORE],NL2,PL2,Neg,Pos):-divide_neg_pos(A,NL2,PL2,NegM,PosM),divide_neg_pos(MORE,NegM,PosM,Neg,Pos).
  704divide_neg_pos(NL1=>PL1,NL2,PL2,Neg,Pos):-append(NL1,NL2,Neg),append(PL1,PL2,Pos).
  705
  706/* EXAMPLE OCLh
  707
  708% Methods
  709
  710% Domain Tasks
  711planner_task(chameleonWorld,t7,
  712    % Goals
  713    [
  714     se(door,door1,[doorClosed(door1)]),
  715     se(flexarium,flexarium1,[clean(flexarium1)]),
  716     se(chameleon,veiledChameleon,[inHands(veiledChameleon)]),
  717     se(box,box1,[boxClosed(box1)]),
  718     se(box,box2,[boxClosed(box2)]),
  719     se(substrate,newsPaper1,[outsideFlexarium(newsPaper1)]),
  720     se(substrate,newsPaper2,[insideFlexarium(newsPaper2)])],
  721    % INIT States
  722    [
  723     ss(door,door1,[doorClosed(door1)]),
  724     ss(flexarium,flexarium1,[dirty(flexarium1)]),
  725     ss(chameleon,veiledChameleon,[inFlexarium(veiledChameleon)]),
  726     ss(box,box1,[boxOpen(box1)]),
  727     ss(box,box2,[boxOpen(box2)]),
  728     ss(substrate,newsPaper1,[insideFlexarium(newsPaper1)]),
  729     ss(substrate,newsPaper2,[outsideFlexarium(newsPaper2)])]).
  730*/
  731
  732planner_task_for_ocl(Domain,PName,
  733  % Goals
  734    SEs,SSs):-  use_local_pddl,
  735     name_to_problem_struct(PName,P),prop_get(domain_name,P,Domain),
  736     must_det_l((
  737        prop_get(init,P, UCI),
  738        prop_get(goal,P, UCG),    
  739        copy_term_for_assert((UCI,UCG),(I,G)),       
  740        unss_ify(=,[],[],I,ss,SSK,HintsSS),
  741        unss_ify(=,[],HintsSS,G,se,SEK,Hints),
  742        must_maplist(ress_ify(Mt,Def,Hints,ss),SSK,SSs),
  743        must_maplist(ress_ify(Mt,Def,Hints,se),SEK,SEs))).
  744
  745name_to_problem_struct(Name,P):- use_local_pddl, problem_name(Name),name_to_problem_struct0(Name,P).
  746name_to_problem_struct0(Name,P):-Name==current,!,bb_get(currentProblem,P).
  747name_to_problem_struct0(Name,P):-is_saved_type(problem,Name,P).
  748name_to_problem_struct0(Name,P):-bb_get(currentProblem,P),prop_get(problem_name,P,NameO),Name=NameO.
  749name_to_problem_struct0(Name,P):-loop_check(ocl_problem_struct(Name,P)).
  750
  751kv_to_pddl([], []).
  752kv_to_pddl([_-N|T0], OUT) :- 
  753 	kv_to_pddl(T0,VALUES),
  754        append(N,VALUES,OUT).
  755
  756%TODO work on later        
  757ocl_problem_struct(Name,P):- use_local_pddl, no_repeats(Name,planner_task(DomainName,Name,SE,SS)),
  758   must_det_l((
  759      unss_ify(=,[],[],ss,SS,OUTI,HintsM),mygroup_pairs_by_key(OUTI,IOUT),kv_to_pddl(IOUT,I),
  760      unss_ify(=,[],HintsM,se,SE,OUTG,Hints),mygroup_pairs_by_key(OUTG,GOUT),kv_to_pddl(GOUT,G),
  761      P = problem(Name, DomainName, [ocl], [], I, G, Hints, /*MS*/ [], /*LS*/ []),
  762      ignore(loop_check(domain_name(DomainName))))).
  763
  764
  765name_to_domain_struct(Name,P):-Name==current,!,bb_get(currentDomain,P).
  766name_to_domain_struct(Name,P):-bb_get(currentDomain,P),prop_get(domain_name,P,NameO),Name=NameO.
  767name_to_domain_struct(Name,P):-is_saved_type(domain,Name,P).
  768name_to_domain_struct(Name,P):-loop_check(ocl_domain_struct(Name,P)).
  769
  770%TODO work on later
  771% sorts % consts % preds
  772ocl_domain_struct(Name,D):- use_local_pddl, no_repeats(Name,loop_check(predicates(Name,PredsList))),
  773   % once((not((bb_get(currentDomain,D),once(Name==current;prop_get(domain_name,D,Name)))))),   
  774   must_det_l((
  775      findall(S,is_a_type(Name,S),Types),
  776      D = domain(Name, [ocl], Types, /*Consts*/ [] , PredsList, /*Fuents*/[]  ,/*Constrs*/ [], /*Dconstraints*/[], Actions),
  777      Mt=Name,
  778      findall(A,actn(Mt,A),Actions))).
  779
  780mygroup_pairs_by_key([], []).
  781mygroup_pairs_by_key([M-N|T0], [M-ORDERED|T]) :-
  782 	mysame_key(M, T0, TN, T1),
  783 	mygroup_pairs_by_key(T1, T),
  784        mylist_to_set([N|TN],ORDERED).
  785
  786
  787mysame_key(M0, [M-N|T0], [N|TN], T) :-
  788 	M0 == M, !,
  789 	mysame_key(M, T0, TN, T).
  790mysame_key(_, L, [], L).
  791
  792
  793
  794
  795
  796%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  797%% Handling mutexes
  798
  799 make_mutex(M):-
  800		bagof(R1, forbiden_pair(R1), MA),
  801		bagof(R2, forbiden_pair(MA, R2), MB),
  802%		writel(MA),nl,
  803%		writel(MB),nl,
  804		union(MA, MB, M0),
  805%		list_to_set(M0_, M0),
  806%		write('Cistim:'),nl,	
  807		clear_mutex1(M0, M1),
  808		clear_mutex2(M1, M2),
  809		clear_duplicates(M2, M).
  810		%write('Ocistene:'),nl,writel(M),nl, length(M, L), write('Pocet: '), write(L),nl.
  811
  812clear_duplicates([], []).
  813clear_duplicates([H|T], R):-
  814    member(M, T),
  815    identical_but_for_variables(H, M),
  816    !,
  817    clear_duplicates(T, R).
  818clear_duplicates([H|T], [H|R]):-
  819    clear_duplicates(T, R).
  820
  821forbiden_pair(R):-
  822		get_action(A),
  823		get_positiv_effect(A, PE),
  824		get_negativ_effect(A, NE),
  825		member(P, PE),
  826		member(Q, NE),
  827		copy_term_spec(P-Q, R).
  828forbiden_pair(MA, NR):-
  829		member(P-Q, MA),
  830		get_action(A),
  831		get_precondition(A, Precond),
  832		get_positiv_effect(A, PE),
  833		member(R, Precond),
  834		member(P, PE),
  835		copy_term_spec(R-Q, NR).
  836
  837clear_mutex1([], []):-!.
  838clear_mutex1([PP-QQ|T], M):-
  839		(P-Q = PP-QQ ; P-Q = QQ-PP),
  840		get_init(I),
  841		select_20_faster(P, I, R),
  842		member(Q, R),
  843%		write('Rule1: '), write(PP-QQ),nl,
  844		clear_mutex1(T, M), !.
  845clear_mutex1([P-Q|R], [P-Q|M]):-
  846		clear_mutex1(R, M).
  847
  848clear_mutex2(M0, M):-
  849		(select_20_faster(P-Q, M0, R) ; select_20_faster(Q-P, M0, R)),
  850		get_action(A, _Def), get_precondition(A, Precond), get_positiv_effect(A, PE), get_negativ_effect(A, NE),
  851		select_20_faster(P, PE, RPE),
  852		\+ member(Q, NE),
  853		(
  854			member(Q, RPE)%, write('prva cast')
  855			;
  856			all_not_in(Precond, P, Q, M0)%, write('druha cast')
  857		),
  858%		write('Rule2: '), write(P-Q-_Def),nl,
  859
  860		clear_mutex2(R, M), !.
  861clear_mutex2(M0, M0).
  862
  863all_not_in([], _, _, _).
  864all_not_in([P|T], P, Q, M):-
  865	all_not_in(T, P, Q, M).
  866all_not_in([R|T], P, Q, M):-
  867		\+ (member(R-Q, M) ; member(Q-R, M)),
  868		%write(precon-R),nl,
  869		all_not_in(T, P, Q, M).
  870
  871
  872
  873%check_mutex(+State).
  874check_mutex(S):-
  875		bb_get(mutex, M),
  876		pairfrom(S, P, Q, _),
  877		(member(P-Q, M) ; member(Q-P, M)),
  878%		write('Mutex pair.'), write(P-Q), nl,
  879		!, fail.
  880check_mutex(_).
  881
  882
  883
  884
  885identical_but_for_variables(X, Y) :-
  886		\+ \+ (
  887			copy_term(X, Z),
  888			numbervars(Z, 0, N),
  889			numbervars(Y, 0, N),
  890			Z = Y
  891		).% Dostupne veci:
  892
  893
  894% FILENAME:  test_validate_input_2009.pl 
  895% :- expects_dialect(sicstus).
  896% :-[parseProblem, parseDomain].
  897
  898%nop(_).
  899
  900%parse_file(+File).
  901test_parse_file(F,O):- must(read_file(F, L, _Filename)),!,((domainBNF(O, L, _R1); must(((problem(O, L, _R2)))))),!.
  902
  903test_parse_file(F):- test_parse_file(F,L),arg(2,L,List),!,
  904 (not(member('(',List))->true;
  905    ((absolute_file_name(F,A),
  906	write('Parsing file failed. '), write('('), write(F:A), write(')'), nl))),!.
  907
  908
  909test_dir_sas(DirIn):-forall(must_filematch(DirIn,DirInM),test_dir_m(DirInM)).
  910test_dir_m(DIR):-
  911  working_directory(WAS,WAS),
  912     call_cleanup(( 
  913        cd(DIR),
  914	write('Testing ':DIR), nl,
  915	test_dir_files_sas(DIR)),
  916        cd(WAS)).
  917
  918test_sas:- 
  919      test_dir_sas('ipc2008-no-cybersec/seq-sat/elevators-strips/'),!, % NO FIRST ANSWER
  920      !.
  921
  922test_sas_sanity:- 
  923      test_dir_sas('ipc2008-no-cybersec/seq-opt/openstacks-strips/'), %PASSES BUT RUNS SLOW
  924       test_dir_sas('ipc2008-no-cybersec/seq-opt/transport-strips/'), %PASSES BUT RUNS
  925      test_dir_sas('ipc2008-no-cybersec/netben-opt/elevators-strips/'), % FAIL ALL
  926      !.
  927
  928pddl_dir(PDDLDir,Dir):- atom(PDDLDir),absolute_file_name(PDDLDir,Dir0),expand_file_name(Dir0,DirS),DirS\==[],!,member(Dir,DirS).
  929pddl_dir(PDDLDir,Dir):- atom(PDDLDir),absolute_file_name(pddl(PDDLDir),Dir0),expand_file_name(Dir0,DirS),DirS\==[],!,member(Dir,DirS).
  930pddl_dir(PDDLDir,Dir):- absolute_file_name(PDDLDir,Dir),expand_file_name(Dir0,DirS),member(Dir,DirS).
  931
  932test_rest:-	
  933	test_dir_sas('ipc2008-no-cybersec/seq-opt/parcprinter-strips/'),
  934	test_dir_sas('ipc2008-no-cybersec/seq-opt/pegsol-strips/'),
  935	test_dir_sas('ipc2008-no-cybersec/seq-opt/scanalyzer-strips/'),
  936	test_dir_sas('ipc2008-no-cybersec/seq-opt/sokoban-strips/'),  % NO FIRST ANSWER
  937       
  938	test_dir_sas('ipc2008-no-cybersec/seq-opt/woodworking-strips/'),
  939	
  940        
  941        expand_file_name('../pddl/ipc2008-no-cybersec/?*?/*/',O),
  942        forall(member(E,O),test_dir_sas(E)).
  943
  944test_dir_files_sas(PDDLDir,D,P):- pddl_dir(PDDLDir,Dir), directory_file_path(Dir,D,DF), directory_file_path(Dir,P,PF),
  945        test_parse_file(DF),test_parse_file(PF),
  946        solve_files(DF,PF),!.
  947
  948test_dir_files_sas(Dir):-   
  949	test_dir_files_sas(Dir,'p01-domain.pddl','p01.pddl'),
  950	test_dir_files_sas(Dir,'p02-domain.pddl','p02.pddl'),
  951	test_dir_files_sas(Dir,'p03-domain.pddl','p03.pddl'),
  952	test_dir_files_sas(Dir,'p04-domain.pddl','p04.pddl'),
  953	test_dir_files_sas(Dir,'p05-domain.pddl','p05.pddl'),
  954	test_dir_files_sas(Dir,'p06-domain.pddl','p06.pddl'),
  955	test_dir_files_sas(Dir,'p07-domain.pddl','p07.pddl'),
  956	test_dir_files_sas(Dir,'p08-domain.pddl','p08.pddl'),
  957	test_dir_files_sas(Dir,'p09-domain.pddl','p09.pddl'),
  958	test_dir_files_sas(Dir,'p10-domain.pddl','p10.pddl'),
  959	test_dir_files_sas(Dir,'p11-domain.pddl','p11.pddl'),
  960	test_dir_files_sas(Dir,'p12-domain.pddl','p12.pddl'),
  961	test_dir_files_sas(Dir,'p13-domain.pddl','p13.pddl'),
  962	test_dir_files_sas(Dir,'p14-domain.pddl','p14.pddl'),
  963	test_dir_files_sas(Dir,'p15-domain.pddl','p15.pddl'),
  964	test_dir_files_sas(Dir,'p16-domain.pddl','p16.pddl'),
  965	test_dir_files_sas(Dir,'p17-domain.pddl','p17.pddl'),
  966	test_dir_files_sas(Dir,'p18-domain.pddl','p18.pddl'),
  967	test_dir_files_sas(Dir,'p19-domain.pddl','p19.pddl'),
  968	test_dir_files_sas(Dir,'p20-domain.pddl','p20.pddl'),
  969	test_dir_files_sas(Dir,'p21-domain.pddl','p21.pddl'),
  970	test_dir_files_sas(Dir,'p22-domain.pddl','p22.pddl'),
  971	test_dir_files_sas(Dir,'p23-domain.pddl','p23.pddl'),
  972	test_dir_files_sas(Dir,'p24-domain.pddl','p24.pddl'),
  973	test_dir_files_sas(Dir,'p25-domain.pddl','p25.pddl'),
  974	test_dir_files_sas(Dir,'p26-domain.pddl','p26.pddl'),
  975	test_dir_files_sas(Dir,'p27-domain.pddl','p27.pddl'),
  976	test_dir_files_sas(Dir,'p28-domain.pddl','p28.pddl'),
  977	test_dir_files_sas(Dir,'p29-domain.pddl','p29.pddl'),
  978	test_dir_files_sas(Dir,'p30-domain.pddl','p30.pddl')