/* Complement to LPS Interpreter Copyright (c) 2016, Imperial College, London All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ %% This module acts as a data placeholder: LPS programs will be loaded over it %% All LPS engine state persists here %% The local predicate definitions MUST NOT be dynamic, see note in copy_from_module below :- module(db, [ d/2, lps_goal_id_counter/1, action/1, current_goal/1, current_time/1, real_time_beginning/1, lps_updating_current_state/0, (events)/1, (prolog_events)/1, (fluents)/1, (actions)/1, (unserializable)/1, d_pre/1, depth/1, event/1, expanded_consequent/2, failed/3, fluent/1, happens/3, initial_state/1, (initiated)/3, l_events/2, l_int/2, l_timeless/2, (observe)/2, option/1, reactive_rule/2, expanded_reactive_rule/2, reactive_rule/3, expanded_reactive_rule/3, state/1, next_state/1, steps/1, succ_t/2, (terminated)/3, tried/3, updated/4, used/1, maxTime/1, maxRealTime/1, minCycleTime/1, simulatedRealTimePerCycle/1, simulatedRealTimeBeginning/1, lps_test_options/1, lps_test_result_item/3, lps_test_action_ancestor/3, lps_test_result/3, lps_failed_test/2, lps_saved_state/8, my_load_dyn/2, % The following hosted here for psyntax: '_currenty__defining'/1, head_hint/3 ]). :- if(\+ current_prolog_flag(dialect, swi)). :- writeln("LPS requires SWI-Prolog"), throw(swi_prolog_required). :- endif. :- thread_local lps_goal_id_counter/1. :- thread_local d/2. :- thread_local action/1. :- thread_local current_goal/1. :- thread_local current_time/1. :- thread_local real_time_beginning/1. :- thread_local lps_updating_current_state/0. % convenience to encode a bunch of declarations in Wei or other syntaxes: :- thread_local (events)/1, (prolog_events)/1, (fluents)/1, (actions)/1, (unserializable)/1. :- thread_local d_pre/1. :- thread_local depth/1. :- thread_local event/1. :- thread_local expanded_consequent/2. :- thread_local failed/3. :- thread_local fluent/1. :- thread_local happens/3. :- thread_local initial_state/1. :- thread_local (initiated)/3. :- thread_local l_events/2. :- thread_local l_int/2. :- thread_local l_timeless/2. :- thread_local (observe)/2. :- thread_local option/1. :- thread_local reactive_rule/2. :- thread_local expanded_reactive_rule/2. :- thread_local reactive_rule/3. % adds priority :- thread_local expanded_reactive_rule/3. :- thread_local state/1. :- thread_local next_state/1. :- thread_local steps/1. :- thread_local succ_t/2. :- thread_local (terminated)/3. :- thread_local tried/3. :- thread_local updated/4. :- thread_local used/1. :- thread_local maxTime/1. % LPS cycles :- thread_local maxRealTime/1. % seconds :- thread_local minCycleTime/1. % seconds :- thread_local simulatedRealTimePerCycle/1, simulatedRealTimeBeginning/1. :- thread_local lps_test_result_item/3. :- thread_local lps_test_action_ancestor/3. :- thread_local lps_test_result/3. :- thread_local lps_failed_test/2. :- thread_local lps_test_options/1. :- thread_local lps_saved_state/8. :- thread_local '_currenty__defining'/1. :- thread_local head_hint/3. % TODO: somehow runing tests a second time still originates "Clauses of interpreter:terminated/3 , etc. are not together..." % It may be necessary to inject these declarations in the Wei syntax file... :- discontiguous( ((initiated) /3, (terminated)/3, updated/4, l_events/2, reactive_rule/3, reactive_rule/2, l_int/2, l_timeless/2, action/1, fluent/1, event/1, d_pre/1, happens/3, d/2 )). :- use_module(library(lists), [append/3,member/2]). my_load_dyn(F,WithWarnings) :- (style_check('?'(singleton))->Old=true;Old=false), (WithWarnings==true -> style_check('+'(singleton)) ; style_check('-'(singleton))), interpreter:get_lps_program_module(M), % load_files([F],[module(M)]), M:load_files([F],[]), (Old==true -> style_check('+'(singleton)) ; style_check('-'(singleton))). % Generic Prolog code (used to run on other Prologs too) % Stubs for system actions lps_ask(A,B,C) :- interpreter:lps_ask(A,B,C). lps_ask(A,B) :- interpreter:lps_ask(A,B). lps_outcome(A,B) :- interpreter:lps_outcome(A,B). lps_save_finish_execution(File) :- interpreter:lps_save_finish_execution(File). uassert(X) :- interpreter:uassert(X). uasserta(X) :- interpreter:uasserta(X). uassertz(X) :- interpreter:uassertz(X). uretract(X) :- interpreter:uretract(X). uretractall(X) :- interpreter:uretractall(X). % Assert into this module all clauses in module M % Hacky code; this module MUST have only static predicates, so they're protected against malicious redifinitions in the LPS program copy_from_module(M) :- current_predicate(M:F/N), functor(H,F,N), (\+ predicate_property(H,defined) -> thread_local(H) ; true), catch(retractall(H),_,fail), clause(M:H,B), catch(assert((H:-B)),_,fail), fail. copy_from_module(_).