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):- 
  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):- optic_missing(optic_get_plan(W,Goal,Plan)).
 optic_apply_step(+W, +Step, -NewWorkspace) is det
  122optic_apply_step(W,Step,NewWorkspace):- optic_missing(optic_apply_step(W,Step,NewWorkspace)).
  123
  124
  125ensure_optic.
  126
  127glean_types(Opr,W,Any):- Opr=add,!,
  128  check_opr(W,Opr),
  129  forall((sub_term(Sub,Any),
  130  compound(Sub),member(Sub,[_-Type, Type:_])),
  131  optic_type(Opr,W,Type)).
  132glean_types(_,_,_).   
  133
  134
  135glean_objs(Opr,W,Any):- Opr=add,!,
  136  forall((sub_term(Sub,Any),
  137   compound(Sub),member(Sub,[Obj-_])),
  138   optic_object(Opr,W,Obj)),
  139  forall((sub_term(Obj,Any),
  140  atom(Obj)),optic_object(Opr,W,Obj)).
  141glean_objs(_,_,_).   
  142
  143check_opr(W,add):- check_workspace(W).
  144check_opr(W,del):- check_workspace(W).
  145check_opr(W,current):- check_workspace(W).
  146check_opr(_Workspace,Opr):- throw(opr_missing(Opr)).
  147
  148:- dynamic(current_workspace/1).
  149
  150check_workspace(W):- current_workspace(W),!.
  151check_workspace(W):- asserta(current_workspace(W)).
  152
  153call_mockup(Goal):- 
  154  arg(1,Goal,Opr),
  155  arg(2,Goal,W),
  156  check_opr(W,Opr),
  157  optic_missing(Goal).
  158
  159%optic_missing(Goal):- throw(optic_missing(Goal)).
  160optic_debug(Goal):- format('~N~q.~n',[optic_goal(Goal)]).
  161optic_missing(Goal):- format('~N~q.~n',[optic_goal(Goal)]).
  162
  163
  164
  165%e_member([L|ST],E):- nonvar(L),!,member(E,[L|ST]).
  166%e_member(E,E).