1/*  Part of Optic Planner interface for SWI-Prolog
    2
    3    Author:        Andrew Dougherty, Douglas Miles
    4    E-mail:        andrewdo@frdcsa.org, logicmoo@gmail.com
    5    WWW:           https://github.com/TeamSPoon/pddl_valoptic_api
    6    Copyright (C): 2017, Process Design Center, Breda, The Netherlands.
    7    All rights reserved.
    8
    9    Redistribution and use in source and binary forms, with or without
   10    modification, are permitted provided that the following conditions
   11    are met:
   12
   13    1. Redistributions of source code must retain the above copyright
   14       notice, this list of conditions and the following disclaimer.
   15
   16    2. Redistributions in binary form must reproduce the above copyright
   17       notice, this list of conditions and the following disclaimer in
   18       the documentation and/or other materials provided with the
   19       distribution.
   20
   21    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   22    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   23    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   24    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   25    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   26    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   27    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   28    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   29    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   30    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   31    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   32    POSSIBILITY OF SUCH DAMAGE.
   33*/

   34
   35/*
   36":typing" | ":strips" | ":equality" | ":fluents" | ":durative-actions" | ":duration-inequalities" | ":numeric-fluents" | ":action-costs" | ":adl" | ":negative-preconditions" | ":disjunctive-preconditions" | ":existential-preconditions" | "universal-preconditions" | "quantified-preconditions" | ":conditional-effects" | ":timed-initial-literals" | ":preferences" | ":constraints"
   37
   38":domain-axioms" | ":derived-predicates" ":action-expansions" | ":foreach-expansions" | ":dag-expansions" | ":subgoal-through-axioms" | ":safety-constraints" | ":expression-evaluation" | ":open-world" | ":true-negation" | ":ucpop"
   39
   40*/

   41
   42:- module(pddl_valoptic_interface, [
   43   optic_workspace/2,   % optic_workspace(Opr,W)
   44   optic_requirement/3,   % optic_requirement(Opr,W,Require)          
   45   optic_init/3, % optic_init(Opr,W,Fact)
   46   optic_predicate/3, % optic_predicate(Opr,W,Predicate)
   47   optic_function/3, % optic_function(Opr,W,Function)
   48   optic_type/3, % optic_type(Opr,W,Sort)
   49   optic_object/3, % optic_object(Opr,W,Object)
   50   optic_derived/4, % optic_derived(Opr,W,Fact,Condition) 
   51   optic_axiom/3, % optic_axiom(Opr,W,Axiom)
   52   optic_action/4, % optic_action(Opr,W,Action,Info)
   53   optic_copy_workspace/2, % optic_copy_workspace(+W,?NewWorkspace)
   54   optic_load_pddl_file/2, % optic_load_pddl_file(+W,+FileName)
   55
   56   optic_get_plan/3, % optic_get_plan(+W,+Goal,-Plan)
   57   optic_apply_step/3, % optic_apply_step(+W,+Step,-NewWorkspace)
   58   ensure_optic/0,
   59   optic_debug/1
   60
   61  ]).
   62
   63
   64optic_copy_workspace(W,NewWorkspace):- optic_missing(optic_copy_workspace(W,NewWorkspace)).
   65
   66optic_load_pddl_file(W,FileName):- optic_missing(optic_load_pddl_file(W,FileName)).
Enumerates Planner workspace names
Adds a workspace name
Deletes workspace freeing up resources
   82optic_workspace(Opr,W):- call_mockup(optic_workspace(Opr,W)).
   83
   84optic_requirement(Opr,W,Require):- call_mockup(optic_requirement(Opr,W,Require)).
   85
   86optic_init(Opr,W,Fact):- 
   87  glean_objs(Opr,W,Fact),
   88  call_mockup(optic_init(Opr,W,Fact)).
   89
   90optic_predicate(Opr,W,Predicate):- 
   91  glean_types(Opr,W,Predicate),
   92  call_mockup(optic_predicate(Opr,W,Predicate)).
   93
   94optic_function(Opr,W,Function):- 
   95  glean_types(Opr,W,Function),
   96  call_mockup(optic_function(Opr,W,Function)).
   97
   98optic_type(Opr,W,Type):-
   99  glean_types(Opr,W,Type),
  100  call_mockup(optic_type(Opr,W,Type)).
  101
  102optic_object(Opr,W,Object):- 
  103  glean_types(Opr,W,Object),
  104  call_mockup(optic_object(Opr,W,Object)).
  105
  106optic_derived(Opr,W,Fact,Cond) :- Cond==[], !, optic_init(Opr,W,Fact).
  107optic_derived(Opr,W,Fact,Condition) :- 
  108  call_mockup(optic_derived(Opr,W,Fact,Condition)).
  109
  110
  111optic_axiom(Opr,W,Axiom):- call_mockup(optic_axiom(Opr,W,Axiom)).
  112
  113optic_action(Opr,W,Action,Info):- check_opr(W,Opr), 
  114  glean_types(Opr,W,Action),
  115  call_mockup(optic_action(Opr,W,Action,Info)).
 optic_get_plan(+W, +Goal, -Plan) is nondet
  119optic_get_plan(W,Goal,Plan):-
  120  check_workspace(W),
  121  ignore(Plan=[s1,s2,s3]),
  122  optic_missing(optic_get_plan(W,Goal,Plan)).
 optic_apply_step(+W, +Step, -NewWorkspace) is det
  125optic_apply_step(W,Step,NewWorkspace):-
  126 check_workspace(W),
  127 (var(NewWorkspace)->gensym(W,NewWorkspace);true),
  128 check_workspace(NewWorkspace),
  129 optic_missing(optic_apply_step(W,Step,NewWorkspace)).
  130
  131
  132ensure_optic.
  133
  134glean_types(Opr,W,Any):- Opr=add,!,
  135  check_opr(W,Opr),
  136  forall((sub_term(Sub,Any),
  137  compound(Sub),member(Sub,[_-Type, Type:_])),
  138  optic_type(Opr,W,Type)).
  139glean_types(_,_,_).   
  140
  141
  142glean_objs(Opr,W,Any):- Opr=add,!,
  143  forall((sub_term(Sub,Any),
  144   compound(Sub),member(Sub,[Obj-_])),
  145   optic_object(Opr,W,Obj)),
  146  forall((sub_term(Obj,Any),
  147  atom(Obj)),optic_object(Opr,W,Obj)).
  148glean_objs(_,_,_).   
  149
  150check_opr(W,add):- check_workspace(W).
  151check_opr(W,del):- check_workspace(W).
  152check_opr(W,current):- check_workspace(W).
  153check_opr(_Workspace,Opr):- throw(opr_missing(Opr)).
  154
  155:- dynamic(current_workspace/1).
  156
  157check_workspace(W):- current_workspace(W),!.
  158check_workspace(W):- asserta(current_workspace(W)).
  159
  160call_mockup(Goal):- 
  161  arg(1,Goal,Opr),
  162  arg(2,Goal,W),
  163  check_opr(W,Opr),
  164  optic_missing(Goal).
  165
  166optic_debug(Info):- format('~N% ~q.~n',[Info]).
  167
  168optic_missing(Goal):- !,optic_debug(g(Goal)).
  169optic_missing(Goal):- throw(optic_missing(Goal)).
  170
  171
  172
  173%e_member([L|ST],E):- nonvar(L),!,member(E,[L|ST]).
  174%e_member(E,E).