/* 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, [ display/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. tl_preds(X):- X = [ lps_goal_id_counter/1, display/2, action/1, current_goal/1, current_time/1, real_time_beginning/1, lps_updating_current_state/0, % convenience to encode a bunch of declarations in Wei or other syntaxes: (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, % adds priority expanded_reactive_rule/3, state/1, next_state/1, steps/1, succ_t/2, (terminated)/3, tried/3, updated/4, used/1, maxTime/1, % LPS cycles maxRealTime/1, % seconds minCycleTime/1, % seconds simulatedRealTimePerCycle/1, simulatedRealTimeBeginning/1, lps_test_result_item/3, lps_test_action_ancestor/3, lps_test_result/3, lps_failed_test/2, lps_test_options/1, lps_saved_state/8, '_currenty__defining'/1, head_hint/3]. :- export(define_lps_into_module/1). define_lps_into_module(Into):- db:tl_preds(List), maplist(define_lps_pred_into_module(Into),List). :- export(define_lps_pred_into_module/2). define_lps_pred_into_module(Into, F/A):- thread_local(Into:F/A), !. define_lps_pred_into_module(Into, F/A):- functor(H,F,A), (\+ predicate_property(Into:H,defined) ; true) -> (Into:discontiguous(F/A),Into:thread_local(F/A)) ; true. :- define_lps_into_module(db). % 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, display/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:unload_file(F), 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(_). copy_from_module(M):- prolog_load_context(module, Into), copy_from_module(M, Into). :- export(copy_from_module/2). copy_from_module(M, Into) :- current_predicate(M:F/N), functor(H,F,N), \+ predicate_property(M:H, imported_from(_)), transfer_into_module(M, Into , F/N), fail. copy_from_module(M, Into):- db:tl_preds(List), maplist(copy_from_module(M, Into),List). copy_from_module(_,_). :- export(transfer_into_module/3). transfer_into_module(M, Into, F/N):- functor(H,F,N), (\+ predicate_property(Into:H,defined) -> Into:thread_local(H) ; true), catch(Into:retractall(H),_,fail), clause(M:H,B), catch(Into:assert((H:-B)),_,fail).