Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Part of LogicMOO Base Logicmoo Debug Tools
- % ===================================================================
- % File '$FILENAME.pl'
- % Purpose: An Implementation in SWI-Prolog of certain debugging tools
- % Maintainer: Douglas Miles
- % Contact: $Author: dmiles $@users.sourceforge.net ;
- % Version: '$FILENAME.pl' 1.0.0
- % Revision: $Revision: 1.1 $
- % Revised At: $Date: 2002/07/11 21:57:28 $
- % Licience: LGPL
- % ===================================================================
- */
- :- module(dmiles_version_of_pfc, [
- (::::)/2, (<-)/2, (<==>)/2, (==>)/2,action_is_undoable/1,assert_i/1,assert_s/1,assert_u/1,
- pfc_assumption/1,pfc_assumptions/2,pfc_axiom/1,bagof_or_nil/3,bases_union/2,brake/1,bt/2,build_rhs/2,
- build_neg_test/3,build_rule/3,build_test/2,build_trigger/3,call_i/1,call_s/1,call_u/1,clause_i/2,
- clause_i/3,clause_s/2,clause_s/3,clause_u/2,clause_u/3,defaultpfc_select/1,fc_eval_action/2,foob/1,
- foreachl_do/2,get_next_fact/1,if_missing/2,pfc_justification/2,pfc_justification_S/2,pfc_BC/1,pfc_BC_CACHE/1,pfc_CALL/1,
- pfc_CALL/2,pfc_CALL/3,pfc_CALL_MI/3,pfc_halt/0,pfc_halt/1,pfc_halt/2,hs/1,pfc_action/2,
- pfc_ain/1,pfc_ain/2,pfc_ain_DbToHead/2,pfc_ain_actiontrace/2,pfc_ain_special_support/2,pfc_add_support/2,pfc_ain_trigger_reprop/2,pfc_ain_by_type/2,
- pfc_ask/2,pfc_assert/2,pfc_asserta/2,pfc_assertz/2,pfc_basis_list/2,pfc_bt_pt_combine/3,pfc_child/2,pfc_children/2,
- pfc_classifyFacts/4,pfc_clause_i/1,pfc_collect_supports/1,pfc_unhandled_command/3,pfc_compile_rhs_term/2,pfc_conjoin/3,pfc_connective/1,pfc_current_db/1,
- pfc_database/1,pfc_database_item/1,pfc_database_term/1,pfc_db_type/2,pfc_debugging/0,pfc_default/2,pfc_define_bc_rule/3,pfc_descendant/2,
- pfc_descendants/2,pfc_do_and_undo_method/2,pfc_enqueue/2,pfc_error/1,pfc_error/2,pfc_eval_lhs/2,pfc_eval_rhs/2,pfc_fact/1,
- pfc_fact/2,pfc_facts/1,pfc_facts/2,pfc_facts/3,pfc_fwc/1,pfc_get_support/2,pfc_get_trigger_quick/1,pfc_is_tracing/1,
- pfc_is_tracing_exec/0,pfc_literal/1,pfc_load/1,pfc_make_supports/1,pfc_ain_object/1,pfc_aina/2,pfc_ainz/2,
- pfc_negated_literal/1,pfc_negation/2,pfc_nf/2,pfc_nf1_negation/2,pfc_nf_negation/2,pfc_nf_negations/2,pfc_noTrace/0,pfc_noWatch/0,
- pfc_nospy/0,pfc_nospy/1,pfc_nospy/3,pfc_positive_literal/1,pfc_post/2,qu/0,qu/1,pfc_rem_actionTrace/1,
- pfc_rem_support/2,pfc_remove_old_version/1,pfc_remove_supports/1,pfc_remove_supports_quietly/1,pfc_reset/0,pfc_retract/1,pfc_retract_i_or_warn/1,pfc_retract_supported_relations/1,
- pfc_retract_type_1/2,pfc_run/0,pfc_search/1,pfc_select_hook/1,pfc_select_justification_node/3,pfc_set_warnings/1,pfc_pp_justifications/2,pfc_spied/2,
- pfc_spy/1,pfc_spy/2,pfc_spy/3,pfc_step/0,pfc_support_relation/1,pfc_supported/1,pfc_supported/2,pfc_test/1,
- pfc_tms_mode/1,pfc_trace/0,pfc_trace/1,pfc_trace/2,pfc_trace_add_print/2,pfc_trace_break/2,pfc_trace_exec/0,pfc_trace_pfc_ain/1,
- pfc_trace_pfc_ain/2,pfc_trace_msg/1,pfc_trace_msg/2,pfc_trace_rem/1,pfc_trigger_key/2,pfc_trigger_key/2,pfc_undo/1,pfc_unfwc/1,
- pfc_unfwc_check_triggers/1,pfc_union/3,pfc_unique_i/1,pfc_unique_u/1,pfc_untrace/0,pfc_untrace/1,pfc_warn/0,pfc_warn/1,
- pfc_warn/2,pfc_warnings/1,pfc_watch/0,well_founded_0/2,pfc_why/0,pfc_why/1,pfc_whyBrouse/2,pfc_handle_why_command/3,
- nopfc_warn/0,nt/3,pfcl_do/1,pp_DB/0,pp_facts/0,pp_facts/1,pp_facts/2,pp_items/1,
- pp_rules/0,pp_supports/0,pp_triggers/0,pfc_load/1,process_rule/3,pt/2,
- remove_if_unsupported/1,remove_selection/1,retract_i/1,retract_s/1,retract_u/1,retractall_i/1,retractall_s/1,retractall_u/1,
- select_next_fact/1,spft/3,stop_trace/1,supporters_list/2,triggerSupports/2,trigger_trigger/3,well_founded/1,
- well_founded_list/2,why_buffer/2,
- do_assumpts/2,pfc_do_negitive_triggers/2,pfc_do_postive_triggers/2,pfc_fwc1/1,pfc_ain_rule0/1,pfc_descendant1/3,pfc_eval_rhs1/2,pfc_nf1/2,
- pfc_post1/2,pfc_withdraw/1,pfc_withdraw/2,pfc_remove/1,pfc_remove/2,pfc_pp_justification1/2,pfc_pp_justifications2/3,pfc_spy1/3,
- pfc_unfwc1/1,pfc_why1/1,pfc_blast/1,trigger_trigger1/2 ]).
- :- meta_predicate
- bagof_or_nil(?, ^, -),
- brake(0),
- call_i(0),
- call_s(0),
- call_u(0),
- fc_eval_action(0, ?),
- foreachl_do(0, ?),
- pfc_CALL(1, +),
- pfc_fact(?, 0),
- with_each(1,+),
- with_each(2,+,+),
- pfcl_do(0).
- :- (multifile user:term_expansion/2).
- :- module_transparent (( bagof_or_nil/3,brake/1,call_i/1,call_s/1,call_u/1,fc_eval_action/2,foreachl_do/2,pfc_CALL/2,
- pfc_fact/2,pfcl_do/1 )).
- :- export(( do_assumpts/2,pfc_do_negitive_triggers/2,pfc_do_postive_triggers/2,pfc_fwc1/1,pfc_ain_rule0/1,pfc_descendant1/3,pfc_eval_rhs1/2,pfc_nf1/2,
- pfc_post1/2,pfc_withdraw/1,pfc_withdraw/2,pfc_remove/1,pfc_remove/2,pfc_pp_justification1/2,pfc_pp_justifications2/3,pfc_spy1/3,
- pfc_unfwc1/1,pfc_why1/1,pfc_blast/1,trigger_trigger1/2 )).
- :- dynamic (( (::::)/2, (<-)/2, (<==>)/2, (==>)/2,bt/2,foob/1,if_missing/2,hs/0,
- pfc_action/2,pfc_database/1,pfc_debugging/0,pfc_do_and_undo_method/2,pfc_is_tracing/1,pfc_is_tracing_exec/0,qu/1,pfc_search/1,
- pfc_select_hook/1,pfc_spied/2,pfc_tms_mode/1,pfc_warnings/1,nt/3,pt/2,spft/3,
- why_buffer/2 )).
- :- multifile(( (::::)/2, (<-)/2, (<==>)/2, (==>)/2,bt/2,foob/1,if_missing/2,hs/0,
- pfc_action/2,pfc_database/1,pfc_debugging/0,pfc_do_and_undo_method/2,pfc_is_tracing/1,pfc_is_tracing_exec/0,qu/1,pfc_search/1,
- pfc_select_hook/1,pfc_spied/2,pfc_tms_mode/1,pfc_warnings/1,nt/3,pt/2,spft/3,user:term_expansion/2,
- why_buffer/2 )).
- % =================================================
- % ============== UTILS BEGIN ==============
- % =================================================
- isSlot(V):- is_ftVar(V).
- %% with_each(+P2,+HT,+S) semidet.
- %
- % Call P(E,S). each Element in the list.
- %
- with_each(P,HV,S):- var(HV),!,call(P,HV,S).
- with_each(P,M:HT,S) :- !,must_be(atom,M),M:with_each(P,HT,S).
- with_each(P,[H|T],S) :- !, call(P,H,S), with_each(P,T,S).
- with_each(P,(H,T),S) :- !,with_each(P,H,S), with_each(P,T,S).
- with_each(P,H,S) :- call(P,H,S).
- %% with_each(+P2,+HT) semidet.
- %
- % Call P(E). each Element in the list.
- %
- with_each(P,HV):- var(HV),!,call(P,HV).
- with_each(P,M:HT) :- !,must_be(atom,M),M:with_each(P,HT).
- with_each(P,[H|T]) :- !, call(P,H), with_each(P,T).
- with_each(P,(H,T)) :- !,with_each(P,H), with_each(P,T).
- with_each(P,H) :- call(P,H).
- % =================================================
- % ============== UTILS END ==============
- % =================================================
- % File : pfc_syntax.pl
- % Author : Tim Finin, finin@prc.unisys.com
- % Purpose: syntactic sugar for Pfc - operator definitions and term expansions.
- :- op(500,fx,'-').
- :- op(300,fx,'~').
- :- op(1050,xfx,('==>')).
- :- op(1050,xfx,'<==>').
- :- op(1050,xfx,('<-')).
- :- op(1100,fx,('==>')).
- :- op(1150,xfx,('::::')).
- :- use_module(library(lists)).
- :- dynamic ('==>')/2.
- :- dynamic ('::::')/2.
- :- dynamic '<==>'/2.
- :- dynamic '<-'/2.
- :- dynamic 'pt'/2.
- :- dynamic 'nt'/3.
- :- dynamic 'bt'/2.
- :- dynamic pfc_do_and_undo_method/2.
- :- dynamic pfc_action/2.
- :- dynamic pfc_tms_mode/1.
- :- dynamic qu/1.
- :- dynamic pfc_database/1.
- :- dynamic hs/1.
- :- dynamic pfc_debugging/0.
- :- dynamic pfc_select_hook/1.
- :- dynamic pfc_search/1.
- pfc_current_db(fooo).
- :- meta_predicate brake(0).
- :- meta_predicate fc_eval_action(0,*).
- :- meta_predicate foreachl_do(0,*).
- :- meta_predicate pfcl_do(0).
- :- meta_predicate pfc_fact(*,0).
- :- meta_predicate call_s(0).
- :- meta_predicate call_u(0).
- :- meta_predicate bagof_or_nil(?,^,-).
- :- meta_predicate call_i(0).
- :- meta_predicate pfc_CALL(1,+).
- :- dynamic('user:term_expansion'/2).
- :- multifile('user:term_expansion'/2).
- :- dynamic((spft/3,why_buffer/2)).
- user:term_expansion((P==>Q),(:- pfc_ain((P==>Q)))).
- %user:term_expansion((P==>Q),(:- pfc_ain(('<-'(Q,P))))). % speed-up attempt
- user:term_expansion(('<-'(P,Q)),(:- pfc_ain(('<-'(P,Q))))).
- user:term_expansion((P<==>Q),(:- pfc_ain((P<==>Q)))).
- user:term_expansion((_ruleName :::: Rule),(:- pfc_ain((_ruleName :::: Rule)))).
- user:term_expansion((==>P),(:- pfc_ain(P))).
- % predicates to examine the state of pfc_
- qu:- listing(qu/1).
- call_i(G):-G.
- assert_i(A):-assert(A).
- clause_i(H,B):-clause(H,B).
- clause_i(H,B,R):-clause(H,B,R).
- retract_i(A):-retract(A).
- retractall_i(A):-retractall(A).
- call_u(G):-G.
- assert_u(A):-assert(A).
- clause_u(H,B):-clause(H,B).
- clause_u(H,B,R):-clause(H,B,R).
- retract_u(A):-retract(A).
- retractall_u(A):-retractall(A).
- call_s(G):-G.
- assert_s(A):-assert(A).
- clause_s(H,B):-clause(H,B).
- clause_s(H,B,R):-clause(H,B,R).
- retract_s(A):-retract(A).
- retractall_s(A):-retractall(A).
- % File : pfc_core.pl
- % Author : Tim Finin, finin@prc.unisys.com
- % Updated: 10/11/87, ...
- % 4/2/91 by R. McEntire: added calls to valid_dbref as a
- % workaround for the Quintus 3.1
- % bug in the recorded database.
- % Purpose: core Pfc predicates.
- % % initialization of global assertons
- % pfc_default/1 initialized a global assertion.
- % pfc_default(P,Q) - if there is any fact unifying with P, then do
- % nothing, else assert_i Q.
- pfc_default(GeneralTerm,Default):-
- clause_i(GeneralTerm,true) -> true ; assert_i(Default).
- % pfc_tms_mode is one of {none,local,cycles} and controles the tms alg.
- :- pfc_default(pfc_tms_mode(_), pfc_tms_mode(cycles)).
- % Pfc Search strategy. pfc_search(X) where X is one of {direct,depth,breadth}
- :- pfc_default(pfc_search(_), pfc_search(direct)).
- %% pfc_ainz( ?G, ?S) is semidet.
- %
- % PFC Ainz.
- %
- pfc_ainz(G,S):-pfc_ain(G,S).
- %% pfc_aina( ?G, ?S) is semidet.
- %
- % PFC Aina.
- %
- pfc_aina(G,S):-pfc_ain(G,S).
- %% pfc_ain(P,S)
- %
- % asserts P into the dataBase with support from S.
- %
- % pfc_ain/2 and pfc_post/2 are the proper ways to add new clauses into the
- % database and have forward reasoning done.
- %
- pfc_ain(P):- pfc_ain(P,(u,u)).
- pfc_ain(( \+ P ), S):- !, pfc_withdraw(P, S).
- pfc_ain((==>P),S):- !, pfc_ain(P,S).
- pfc_ain(P,S):-
- pfc_post(P,S),
- pfc_run.
- %pfc_ain(_,_).
- pfc_ain(P,S):- pfc_warn("pfc_ain(~p,~p) failed",[P,S]).
- %% pfc_post(+Ps,+S)
- %
- % tries to assert a fact or set of fact to the database. For
- % each fact (or the singelton) pfc_post1 is called. It always succeeds.
- %
- pfc_post(Ps,S):- with_each(pfc_post1,Ps,S).
- /*
- pfc_post([H|T],S):-
- !,
- pfc_post1(H,S),
- pfc_post(T,S).
- pfc_post([],_):- !.
- pfc_post(P,S):- pfc_post1(P,S).
- */
- %% pfc_post1(+P,+S) is det.
- %
- % tries to add a fact to the database, and, if it succeeded,
- % adds an entry to the Pfc queue for subsequent forward chaining.
- % It always succeeds.
- %
- pfc_post1(P,S):-
- % db pfc_ain_DbToHead(P,P2),
- % pfc_remove_old_version(P),
- pfc_add_support(P,S),
- pfc_unique_u(P),
- assert_u(P),
- pfc_ain_special_support(P,S),
- !,
- pfc_enqueue(P,S),
- !.
- pfc_post1(_,_).
- % pfc_post1(P,S):- pfc_warn("pfc_ain(~p,~p) failed",[P,S]).
- %% pfc_ain_DbToHead(+P,~NewP) is semidet.
- % takes a fact P or a conditioned fact
- % (P:-C) and adds the Db context.
- %
- pfc_ain_DbToHead(P,NewP):-
- pfc_current_db(Db),
- (Db=true -> NewP = P;
- P=(Head:-Body) -> NewP = (Head:- (Db,Body));
- otherwise -> NewP = (P:- Db)).
- %% pfc_unique_i( ?P) is semidet.
- %
- % PFC Unique For Internal Interface.
- %
- % pfc_unique_i(X) is true if there is no assertion X in the prolog db.
- pfc_unique_i((Head:-Tail)):- !, \+ clause_i(Head,Tail).
- pfc_unique_i(P):- !, \+ clause_i(P,true).
- pfc_unique_u((Head:-Tail)):- !, \+ clause_u(Head,Tail).
- pfc_unique_u(P):- !, \+ clause_u(P,true).
- pfc_enqueue(P,S):-
- pfc_search(Mode)
- -> (Mode=direct -> pfc_fwc(P) ;
- Mode=depth -> pfc_asserta(qu(P),S) ;
- Mode=breadth -> pfc_assert(qu(P),S) ;
- true -> pfc_warn("Unrecognized pfc_search mode: ~p", Mode))
- ; pfc_warn("No pfc_search mode").
- %% pfc_remove_old_version( :TermIdentifier) is semidet.
- %
- % if there is a rule of the form Identifier ::: Rule then delete it.
- %
- pfc_remove_old_version((Identifier::::Body)):-
- % this should never happen.
- var(identifier),
- !,
- pfc_warn("variable used as an rule name in ~p :::: ~p",
- [Identifier,Body]).
- pfc_remove_old_version((Identifier::::Body)):-
- nonvar(Identifier),
- clause_i((Identifier::::OldBody),_),
- \+(Body=OldBody),
- pfc_withdraw((Identifier::::OldBody)),
- !.
- pfc_remove_old_version(_).
- % pfc_run compute the deductive closure of the current database.
- % How this is done depends on the searching mode:
- % direct - pfc_fwc has already done the job.
- % depth or breadth - use the qu mechanism.
- % pfc_run :- pfc_search(direct),!.
- % pfc_run :- \+ pfc_search(direct), !, repeat, \+ pfc_step, !.
- pfc_run:-
- (\+ pfc_search(direct)),
- pfc_step,
- pfc_run.
- pfc_run.
- % pfc_step removes one entry from the qu and reasons from it.
- pfc_step:-
- % if hs/1 is true, reset it and fail, thereby stopping inferencing.
- pfc_retract(hs(Was)),
- pfc_trace_msg('Stopping on: ~p',[hs(Was)]),
- !,
- fail.
- pfc_step:-
- % draw immediate conclusions from the next fact to be considered.
- % fails iff the queue is empty.
- get_next_fact(P),
- pfcl_do(pfc_fwc(P)),
- !.
- get_next_fact(P):-
- %identifies the nect fact to pfc_fwc from and removes it from the queue.
- select_next_fact(P),
- remove_selection(P).
- remove_selection(P):-
- pfc_retract(qu(P)),
- pfc_remove_supports_quietly(qu(P)),
- !.
- remove_selection(P):-
- brake(format("~Npfc_:get_next_fact - selected fact not on Queue: ~p",
- [P])).
- % select_next_fact(P) identifies the next fact to reason from.
- % It tries the user defined predicate first and, failing that,
- % the default mechanism.
- select_next_fact(P):-
- pfc_select_hook(P),
- !.
- select_next_fact(P):-
- defaultpfc_select(P),
- !.
- % the default selection predicate takes the item at the froint of the queue.
- defaultpfc_select(P):- qu(P),!.
- % pfc_halt stops the forward chaining.
- pfc_halt:- pfc_halt(anonymous(pfc_halt)).
- pfc_halt(Format,Args):- format(string(Now),Format,Args), pfc_halt(Now).
- pfc_halt(Now):-
- pfc_trace_msg("New halt signal ",[Now]),
- (hs(Was) ->
- pfc_warn("pfc_halt finds halt signal already set to: ~p ",[Was])
- ; assert_i(hs(Now))).
- stop_trace(Msg):- notrace((tracing,leash(+all),dtrace(dmsg(Msg)))),!,rtrace.
- stop_trace(Msg):- dtrace(dmsg(Msg)).
- %
- % predicates for manipulating triggers
- %
- pfc_ain_trigger_reprop(pt(Trigger,Body),Support):-
- !,
- pfc_trace_msg('~N~n\tAdding positive~n\t\ttrigger: ~p~n\t\tbody: ~p~n\t Support: ~p~n',
- [Trigger,Body,Support]),
- pfc_assert(pt(Trigger,Body),Support),
- copy_term(pt(Trigger,Body),Tcopy),
- pfc_BC(Trigger),
- pfc_eval_lhs(Body,(Trigger,Tcopy)),
- fail.
- pfc_ain_trigger_reprop(nt(Trigger,Test,Body),Support):-
- !,
- pfc_trace_msg('~N~n\tAdding negative~n\t\ttrigger: ~p~n\t\ttest: ~p~n\t\tbody: ~p~n\t Support: ~p~n',
- [Trigger,Test,Body,Support]),
- copy_term(Trigger,TriggerCopy),
- pfc_assert(nt(TriggerCopy,Test,Body),Support),
- % stop_trace(pfc_assert(nt(TriggerCopy,Test,Body),Support)),
- \+Test,
- pfc_eval_lhs(Body,((\+Trigger),nt(TriggerCopy,Test,Body))).
- pfc_ain_trigger_reprop(bt(Trigger,Body),Support):-
- !,
- pfc_trace_msg('~N~n\tAdding backwards~n\t\ttrigger: ~p~n\t\tbody: ~p~n\t Support: ~p~n',
- [Trigger,Body,Support]),
- pfc_assert(bt(Trigger,Body),Support),
- pfc_bt_pt_combine(Trigger,Body,Support).
- pfc_ain_trigger_reprop(X,Support):-
- pfc_warn("Unrecognized trigger to pfc_ain_trigger_reprop: ~p\n~~p~n",[X,Support]).
- pfc_bt_pt_combine(Head,Body,Support):-
- % a backward trigger (bt) was just added with head and Body and support Support
- % find any pt''s with unifying heads and add the instantied bt body.
- pfc_get_trigger_quick(pt(Head,Body)),
- pfc_eval_lhs(Body,Support),
- fail.
- pfc_bt_pt_combine(_,_,_):- !.
- pfc_get_trigger_quick(Trigger):- clause_i(Trigger,true).
- %
- % predicates for manipulating action traces.
- %
- pfc_ain_actiontrace(Action,Support):-
- % adds an action trace and it''s support.
- pfc_add_support(pfc_action(Action),Support).
- pfc_rem_actionTrace(pfc_action(A)):-
- pfc_do_and_undo_method(A,M),
- M,
- !.
- %% pfc_retract(X) is det.
- %
- % predicates to remove Pfc facts, triggers, action traces, and queue items
- % from the database.
- %
- pfc_retract(X):-
- % retract an arbitrary thing.
- pfc_db_type(X,Type),!,
- pfc_retract_type_1(Type,X),
- !.
- pfc_retract_type_1(fact,X):-
- % db pfc_ain_DbToHead(X,X2), retract_u(X2).
- % stop_trace(pfc_retract_type_1(fact,X)),
- (retract_u(X)
- *-> pfc_unfwc(X) ; pfc_unfwc(X)).
- pfc_retract_type_1(rule,X):-
- % db pfc_ain_DbToHead(X,X2), retract_u(X2).
- retract_u(X).
- pfc_retract_type_1(trigger,X):-
- retract_u(X)
- -> pfc_unfwc(X)
- ; pfc_warn("Trigger not found to retract_u: ~p",[X]).
- pfc_retract_type_1(action,X):- pfc_rem_actionTrace(X).
- %% pfc_ain_object(X)
- %
- % adds item X to some database
- %
- pfc_ain_object(X):-
- % what type of X do we have?
- pfc_db_type(X,Type),
- % call the appropriate predicate.
- pfc_ain_by_type(Type,X).
- pfc_ain_by_type(fact,X):-
- pfc_unique_u(X),
- assert_u(X),!.
- pfc_ain_by_type(rule,X):-
- pfc_unique_i(X),
- assert_u(X),!.
- pfc_ain_by_type(trigger,X):-
- assert_u(X).
- pfc_ain_by_type(action,_ZAction):- !.
- %% pfc_withdraw(P).
- % removes support S from P and checks to see if P is still supported.
- % If it is not, then the fact is retreactred from the database and any support
- % relationships it participated in removed.
- pfc_withdraw(Ps):- pfc_withdraw(Ps,(u,u)).
- /*
- pfc_withdraw(List):-
- % iterate down the list of facts to be pfc_withdraw'ed.
- nonvar(List),
- List=[_|_],
- remlist(List).
- pfc_withdraw(P):-
- % pfc_withdraw/1 is the user''s interface - it withdraws user support for P.
- pfc_withdraw(P,(u,u)).
- remlist([H|T]):-
- % pfc_withdraw each element in the list.
- pfc_withdraw(H,(u,u)),
- remlist(T).
- */
- %% pfc_withdraw(P,S) is det.
- % removes support S from P and checks to see if P is still supported.
- % If it is not, then the fact is retreactred from the database and any support
- % relationships it participated in removed.
- pfc_withdraw(Ps,S):- with_each(pfc_withdraw1,Ps,S).
- pfc_withdraw1(P,S):-
- pfc_trace_msg('~N~n\tRemoving~n\t\tsupport: ~p~n\t\tfrom: ~p~n',[S,P]),
- pfc_rem_support(P,S)
- -> remove_if_unsupported(P)
- ; pfc_warn("pfc_withdraw/2 Could not find support ~p to remove from fact ~p",
- [S,P]).
- %% pfc_remove(+P) is det.
- %
- % pfc_remove is like pfc_withdraw, but if P is still in the DB after removing the
- % user''s support, it is retracted by more forceful means (e.g. remove).
- %
- pfc_remove(P):- pfc_remove(P,(u,u)).
- pfc_remove(P,S):- with_each(pfc_remove1,P,S).
- pfc_remove1(P,S):-
- pfc_withdraw(P,S),
- pfc_BC(P)
- -> pfc_blast(P)
- ; true.
- %
- % pfc_blast(+F) retracts fact F from the DB and removes any dependent facts
- %
- pfc_blast(F):-
- pfc_remove_supports(F),
- pfc_undo(F).
- % removes any remaining supports for fact F, complaining as it goes.
- pfc_remove_supports(F):-
- pfc_rem_support(F,S),
- pfc_warn("~p was still supported by ~p",[F,S]),
- fail.
- pfc_remove_supports(_).
- pfc_remove_supports_quietly(F):-
- pfc_rem_support(F,_),
- fail.
- pfc_remove_supports_quietly(_).
- %% pfc_undo(X) undoes X.
- %
- % - a positive or negative trigger.
- % - an action by finding a method and successfully executing it.
- % - or a random fact, printing out the trace, if relevant.
- %
- pfc_undo(pfc_action(A)):-
- % undo an action by finding a method and successfully executing it.
- !,
- pfc_rem_actionTrace(pfc_action(A)).
- pfc_undo(pt(Key,Head,Body)):-
- % undo a positive trigger.
- %
- !,
- (show_success(retract_u(pt(Key,Head,Body)))
- -> pfc_unfwc(pt(Head,Body))
- ; pfc_warn("Trigger not found to undo: ~p",[pt(Head,Body)])).
- pfc_undo(pt(Head,Body)):- fail,
- % undo a positive trigger.
- %
- !,
- (show_success(retract_u(pt(Head,Body)))
- -> pfc_unfwc(pt(Head,Body))
- ; pfc_warn("Trigger not found to undo: ~p",[pt(Head,Body)])).
- pfc_undo(nt(Head,Condition,Body)):-
- % undo a negative trigger.
- !,
- (show_success(retract_u(nt(Head,Condition,Body)))
- -> pfc_unfwc(nt(Head,Condition,Body))
- ; pfc_warn("Trigger not found to undo: ~p",[nt(Head,Condition,Body)])).
- pfc_undo(Fact):-
- % undo a random fact, printing out the trace, if relevant.
- retract_u(Fact),
- pfc_trace_rem(Fact),
- pfc_unfwc(Fact).
- %% pfc_unfwc(+P)
- %
- % "un-forward-chains" from fact P. That is, fact P has just
- % been removed from the database, so remove all support relations it
- % participates in and check the things that they support to see if they
- % should stayuser in the database or should also be removed.
- %
- pfc_unfwc(F):-
- pfc_retract_supported_relations(F),
- pfc_unfwc1(F).
- pfc_unfwc1(F):-
- pfc_unfwc_check_triggers(F),
- % is this really the right place for pfc_run<?
- pfc_run.
- pfc_unfwc_check_triggers(F):-
- pfc_db_type(F,fact),
- copy_term(F,Fcopy),
- nt(Fcopy,Condition,Action),
- (\+ Condition),
- pfc_eval_lhs(Action,((\+F),nt(F,Condition,Action))),
- fail.
- pfc_unfwc_check_triggers(_).
- pfc_retract_supported_relations(Fact):-
- pfc_db_type(Fact,Type),
- (Type=trigger -> pfc_rem_support(P,(_,Fact))
- ; pfc_rem_support(P,(Fact,_))),
- remove_if_unsupported(P),
- fail.
- pfc_retract_supported_relations(_).
- % remove_if_unsupported(+Ps) checks to see if all Ps are supported and removes
- % it from the DB if they are not.
- remove_if_unsupported(P):-
- pfc_supported(P) -> true ; pfc_undo(P).
- %% pfc_fwc(+X)
- %
- % forward chains from a fact or a list of facts X.
- %
- pfc_fwc([H|T]):- !, pfc_fwc1(H), pfc_fwc(T).
- pfc_fwc([]):- !.
- pfc_fwc(P):- pfc_fwc1(P).
- % pfc_fwc1(+P) forward chains for a single fact.
- pfc_fwc1(Fact):-
- pfc_ain_rule0(Fact),
- copy_term(Fact,F),
- % check positive triggers
- pfc_do_postive_triggers(Fact,F),
- % check negative triggers
- pfc_do_negitive_triggers(Fact,F).
- %
- % pfc_ain_rule_if_rule(P) does some special, built in forward chaining if P is
- % a rule.
- %
- pfc_ain_rule0((P==>Q)):-
- !,
- process_rule(P,Q,(P==>Q)).
- pfc_ain_rule0((Name::::P==>Q)):-
- !,
- process_rule(P,Q,(Name::::P==>Q)).
- pfc_ain_rule0((P<==>Q)):-
- !,
- process_rule(P,Q,(P<==>Q)),
- process_rule(Q,P,(P<==>Q)).
- pfc_ain_rule0((Name::::P<==>Q)):-
- !,
- process_rule(P,Q,((Name::::P<==>Q))),
- process_rule(Q,P,((Name::::P<==>Q))).
- pfc_ain_rule0(('<-'(P,Q))):-
- !,
- pfc_define_bc_rule(P,Q,('<-'(P,Q))).
- pfc_ain_rule0(_).
- pfc_do_postive_triggers(Fact,F):-
- pfc_get_trigger_quick(pt(F,Body)),
- pfc_trace_msg('~N~n\tFound positive trigger: ~p~n\t\tbody: ~p~n',
- [F,Body]),
- pfc_eval_lhs(Body,(Fact,pt(F,Body))),
- fail.
- %pfc_do_postive_triggers(Fact,F):-
- % pfc_get_trigger_quick(pt(presently(F),Body)),
- % pfc_eval_lhs(Body,(presently(Fact),pt(presently(F),Body))),
- % fail.
- pfc_do_postive_triggers(_,_).
- pfc_do_negitive_triggers(_ZFact,F):-
- spft(X,_,nt(F,Condition,Body)),
- Condition,
- pfc_withdraw(X,(_,nt(F,Condition,Body))),
- fail.
- pfc_do_negitive_triggers(_,_).
- %
- % pfc_define_bc_rule(+Head,+Body,+Parent_rule) - defines a backeard
- % chaining rule and adds the corresponding bt triggers to the database.
- %
- pfc_define_bc_rule(Head,_ZBody,Parent_rule):-
- (\+ pfc_literal(Head)),
- pfc_warn("Malformed backward chaining rule. ~p not atomic.",[Head]),
- pfc_warn("rule: ~p",[Parent_rule]),
- !,
- fail.
- pfc_define_bc_rule(Head,Body,Parent_rule):-
- copy_term(Parent_rule,Parent_ruleCopy),
- build_rhs(Head,Rhs),
- foreachl_do(pfc_nf(Body,Lhs),
- (build_trigger(Lhs,rhs(Rhs),Trigger),
- pfc_ain(bt(Head,Trigger),(Parent_ruleCopy,u)))).
- %
- % eval something on the LHS of a rule.
- %
- pfc_eval_lhs((Test->Body),Support):-
- !,
- (call(Test) -> pfc_eval_lhs(Body,Support)),
- !.
- pfc_eval_lhs(rhs(X),Support):-
- !,
- pfc_eval_rhs(X,Support),
- !.
- pfc_eval_lhs(X,Support):-
- pfc_db_type(X,trigger),
- !,
- pfc_ain_trigger_reprop(X,Support),
- !.
- %pfc_eval_lhs(snip(X),Support):-
- % snip(Support),
- % pfc_eval_lhs(X,Support).
- pfc_eval_lhs(X,_):-
- pfc_warn("Unrecognized item found in trigger body, namely ~p.",[X]).
- %
- % eval something on the RHS of a rule.
- %
- pfc_eval_rhs([],_):- !.
- pfc_eval_rhs([Head|Tail],Support):-
- pfc_eval_rhs1(Head,Support),
- pfc_eval_rhs(Tail,Support).
- pfc_eval_rhs1({Action},Support):-
- % evaluable Prolog code.
- !,
- fc_eval_action(Action,Support).
- pfc_eval_rhs1(P,_ZSupport):-
- % predicate to remove.
- pfc_negated_literal(P),
- !,
- pfc_withdraw(P).
- pfc_eval_rhs1([X|Xrest],Support):-
- % embedded sublist.
- !,
- pfc_eval_rhs([X|Xrest],Support).
- pfc_eval_rhs1(Assertion,Support):-
- % an assertion to be added.
- pfc_post1(Assertion,Support).
- pfc_eval_rhs1(X,_):-
- pfc_warn("Malformed rhs of a rule: ~p",[X]).
- %% fc_eval_action(+Action,+Support)
- %
- % evaluate an action found on the rhs of a rule.
- %
- fc_eval_action(Action,Support):-
- call(Action),
- (action_is_undoable(Action)
- -> pfc_ain_actiontrace(Action,Support)
- ; true).
- %
- %
- %
- trigger_trigger(Trigger,Body,_ZSupport):-
- trigger_trigger1(Trigger,Body).
- trigger_trigger(_,_,_).
- %trigger_trigger1(presently(Trigger),Body):-
- % !,
- % copy_term(Trigger,TriggerCopy),
- % pfc_BC(Trigger),
- % pfc_eval_lhs(Body,(presently(Trigger),pt(presently(TriggerCopy),Body))),
- % fail.
- trigger_trigger1(Trigger,Body):-
- copy_term(Trigger,TriggerCopy),
- pfc_BC(Trigger),
- pfc_eval_lhs(Body,(Trigger,pt(TriggerCopy,Body))),
- fail.
- %% pfc_BC(F) is det.
- %
- % is true iff F is a fact available for forward chaining
- % (or from the backchaining store)
- % Note that this has the side effect of catching unsupported facts and
- % assigning them support from God.
- %
- pfc_BC(P):-pfc_BC_CACHE(P),pfc_CALL(pfc_BC, P).
- pfc_BC_CACHE(P):-
- ignore((
- % trigger any bc rules.
- bt(P,Trigger),
- pfc_get_support(bt(P,Trigger),S),
- pfc_eval_lhs(Trigger,S),
- fail)).
- pfc_CALL(F):- pfc_CALL(pfc_CALL, Cut, F), (var(Cut)->true;(Cut=cut(Cut)->(!,Cut);Cut)).
- pfc_CALL(How,F):- pfc_CALL(How, Cut, F), (var(Cut)->true;(Cut=cut(Cut)->(!,Cut);Cut)).
- pfc_CALL(How,SCut, F):-
- % this is probably not advisable due to extreme inefficiency.
- var(F) -> pfc_fact(F) ;
- predicate_property(F,number_of_clauses(_)) ->
- (clause_u(F,Condition),pfc_CALL(How,Cut,Condition),(var(Cut)->true;(Cut=cut(Cut)->(!,Cut);Cut)));
- pfc_CALL_MI(How,SCut,F).
- pfc_CALL_MI(_How, cut(true), !):- !.
- pfc_CALL_MI(How, Cut, (P1,P2)):- !, pfc_CALL(How, Cut, P1), pfc_CALL(How, Cut, P2).
- pfc_CALL_MI(How, Cut, (P1;P2)):- !, pfc_CALL(How, Cut, P1); pfc_CALL(How, Cut, P2).
- pfc_CALL_MI(How, Cut, (P1->P2)):- !, pfc_CALL(How, Cut, P1)-> pfc_CALL(How, Cut, P2).
- pfc_CALL_MI(How, Cut, (P1*->P2)):- !, pfc_CALL(How, Cut, P1)*-> pfc_CALL(How, Cut, P2).
- pfc_CALL_MI(_How,_, F):-
- % we really need to check for system predicates as well.
- current_predicate(_,F),!, call(F).
- %% action_is_undoable(?A)
- %
- % an action is action_is_undoable if there exists a method for undoing it.
- %
- action_is_undoable(A):- pfc_do_and_undo_method(A,_).
- %% pfc_nf(+In,-Out)
- %
- % maps the LHR of a Pfc rule In to one normal form
- % Out. It also does certpfc_ain optimizations. Backtracking into this
- % predicate will produce additional clauses.
- %
- pfc_nf(LHS,List):-
- pfc_nf1(LHS,List2),
- pfc_nf_negations(List2,List).
- %% pfc_nf1(+In,-Out)
- %
- % maps the LHR of a Pfc rule In to one normal form
- % Out. Backtracking into this predicate will produce additional clauses.
- % handle a variable.
- pfc_nf1(P,[P]):- var(P), !.
- % these next two rules are here for upward compatibility and will go
- % away eventually when the P/Condition form is no longer used anywhere.
- pfc_nf1(P/Cond,[(\+P)/Cond]):- pfc_negated_literal(P), !.
- pfc_nf1(P/Cond,[P/Cond]):- pfc_literal(P), !.
- % handle a negated form
- pfc_nf1(NegTerm,NF):-
- pfc_negation(NegTerm,Term),
- !,
- pfc_nf1_negation(Term,NF).
- % disjunction.
- pfc_nf1((P;Q),NF):-
- !,
- (pfc_nf1(P,NF) ; pfc_nf1(Q,NF)).
- % conjunction.
- pfc_nf1((P,Q),NF):-
- !,
- pfc_nf1(P,NF1),
- pfc_nf1(Q,NF2),
- append(NF1,NF2,NF).
- % handle a random literal.
- pfc_nf1(P,[P]):-
- pfc_literal(P),
- !.
- %=% shouln't we have something to catch the rest as errors?
- pfc_nf1(Term,[Term]):-
- pfc_warn("pfc_nf doesn't know how to normalize ~p",[Term]),!,fail.
- %% pfc_nf1_negation( ?P, ?P) is semidet.
- %
- % pfc_nf1_negation(P,NF) is true if NF is the normal form of \+P.
- %
- pfc_nf1_negation((P/Cond),[(\+(P))/Cond]):- !.
- pfc_nf1_negation((P;Q),NF):-
- !,
- pfc_nf1_negation(P,NFp),
- pfc_nf1_negation(Q,NFq),
- append(NFp,NFq,NF).
- pfc_nf1_negation((P,Q),NF):-
- % this code is not correct! twf.
- !,
- pfc_nf1_negation(P,NF)
- ;
- (pfc_nf1(P,Pnf),
- pfc_nf1_negation(Q,Qnf),
- append(Pnf,Qnf,NF)).
- pfc_nf1_negation(P,[\+P]).
- %% pfc_nf_negations(List2,List) is det.
- %
- % sweeps through List2 to produce List,
- % changing -{...} to {\+...}
- % % ? is this still needed? twf 3/16/90
- %% pfc_nf_negations( :TermX, :TermX) is semidet.
- %
- % PFC Normal Form Negations.
- %
- pfc_nf_negations(X,X) :- !. % I think not! twell_founded_0 3/27/90
- pfc_nf_negations([],[]).
- pfc_nf_negations([H1|T1],[H2|T2]):-
- pfc_nf_negation(H1,H2),
- pfc_nf_negations(T1,T2).
- %% pfc_nf_negation( ?X, ?X) is semidet.
- %
- % PFC Normal Form Negation.
- %
- pfc_nf_negation(Form,{\+ X}):-
- nonvar(Form),
- Form=(-({X})),
- !.
- pfc_nf_negation(X,X).
- %% build_rhs(+Conjunction,-Rhs)
- %
- build_rhs(X,[X]):-
- var(X),
- !.
- build_rhs((A,B),[A2|Rest]):-
- !,
- pfc_compile_rhs_term(A,A2),
- build_rhs(B,Rest).
- build_rhs(X,[X2]):-
- pfc_compile_rhs_term(X,X2).
- pfc_compile_rhs_term((P/C),((P:-C))):- !.
- pfc_compile_rhs_term(P,P).
- %% pfc_negation( ?N, ?P) is semidet.
- %
- % is true if N is a negated term and P is the term
- % with the negation operator stripped.
- %
- pfc_negation((-P),P).
- pfc_negation((\+(P)),P).
- %% pfc_negated_literal( ?P) is semidet.
- %
- % PFC Negated Literal.
- %
- pfc_negated_literal(P):-
- pfc_negation(P,Q),
- pfc_positive_literal(Q).
- pfc_literal(X):- pfc_negated_literal(X).
- pfc_literal(X):- pfc_positive_literal(X).
- pfc_positive_literal(X):- is_ftNonvar(X),
- functor(X,F,_),
- \+ pfc_connective(F).
- %% pfc_connective( ?VALUE1) is semidet.
- %
- % PFC Connective.
- %
- pfc_connective(';').
- pfc_connective(',').
- pfc_connective('/').
- pfc_connective('|').
- pfc_connective(('==>')).
- pfc_connective(('<-')).
- pfc_connective('<==>').
- pfc_connective('-').
- % pfc_connective('-').
- pfc_connective('\\+').
- %% process_rule( ?Lhs, ?Rhs, ?Parent_rule) is semidet.
- %
- % Process Rule.
- %
- process_rule(Lhs,Rhs,Parent_rule):-
- copy_term(Parent_rule,Parent_ruleCopy),
- build_rhs(Rhs,Rhs2),
- foreachl_do(pfc_nf(Lhs,Lhs2),
- build_rule(Lhs2,rhs(Rhs2),(Parent_ruleCopy,u))).
- %% build_rule( ?Lhs, ?Rhs, ?Support) is semidet.
- %
- % Build Rule.
- %
- build_rule(Lhs,Rhs,Support):-
- build_trigger(Lhs,Rhs,Trigger),
- pfc_eval_lhs(Trigger,Support).
- build_trigger([],Consequent,Consequent).
- build_trigger([V|Triggers],Consequent,pt(V,X)):-
- var(V),
- !,
- build_trigger(Triggers,Consequent,X).
- build_trigger([(T1/Test)|Triggers],Consequent,nt(T2,Test2,X)):-
- pfc_negation(T1,T2),
- !,
- build_neg_test(T2,Test,Test2),
- build_trigger(Triggers,Consequent,X).
- build_trigger([(T1)|Triggers],Consequent,nt(T2,Test,X)):-
- pfc_negation(T1,T2),
- !,
- build_neg_test(T2,true,Test),
- build_trigger(Triggers,Consequent,X).
- build_trigger([{Test}|Triggers],Consequent,(Test->X)):-
- !,
- build_trigger(Triggers,Consequent,X).
- build_trigger([T/Test|Triggers],Consequent,pt(T,X)):-
- !,
- build_test(Test,Test2),
- build_trigger([{Test2}|Triggers],Consequent,X).
- %build_trigger([snip|Triggers],Consequent,snip(X)):-
- % !,
- % build_trigger(Triggers,Consequent,X).
- build_trigger([T|Triggers],Consequent,pt(T,X)):-
- !,
- build_trigger(Triggers,Consequent,X).
- %
- % build_neg_test(+,+,-).
- %
- % builds the test used in a negative trigger (nt/3). This test is a
- % conjunction of the check than no matching facts are in the db and any
- % additional test specified in the rule attached to this - term.
- %
- build_neg_test(T,Testin,Testout):-
- build_test(Testin,Testmid),
- pfc_conjoin((pfc_BC(T)),Testmid,Testout).
- % this just strips away any currly brackets.
- build_test({Test},Test):- !.
- build_test(Test,Test).
- % simple typeing for Pfc objects
- pfc_db_type(Var,Type):- var(Var),!, Type=fact.
- pfc_db_type(~_,Type):- !, Type=fact.
- pfc_db_type(('==>'(_,_)),Type):- !, Type=rule.
- pfc_db_type(('<==>'(_,_)),Type):- !, Type=rule.
- pfc_db_type(('<-'(_,_)),Type):- !, Type=rule.
- pfc_db_type(pt(_,_,_),Type):- !, Type=trigger.
- pfc_db_type(pt(_,_),Type):- !, Type=trigger.
- pfc_db_type(nt(_,_,_),Type):- !, Type=trigger.
- pfc_db_type(bt(_,_),Type):- !, Type=trigger.
- pfc_db_type(pfc_action(_),Type):- !, Type=action.
- pfc_db_type((('::::'(_,X))),Type):- !, pfc_db_type(X,Type).
- pfc_db_type(((':'(_,X))),Type):- !, pfc_db_type(X,Type).
- pfc_db_type(_,fact):-
- % if it''s not one of the above, it must be a fact!
- !.
- pfc_assert(P,Support):-
- (pfc_clause_i(P) ; assert_i(P)),
- !,
- pfc_add_support(P,Support).
- pfc_asserta(P,Support):-
- (pfc_clause_i(P) ; asserta_i(P)),
- !,
- pfc_add_support(P,Support).
- pfc_assertz(P,Support):-
- (pfc_clause_i(P) ; assertz_i(P)),
- !,
- pfc_add_support(P,Support).
- %% pfc_clause_i( ?Head) is semidet.
- %
- % PFC Clause For Internal Interface.
- %
- pfc_clause_i((Head:- Body)):-
- !,
- copy_term((Head:-Body),(Head_copy:-Body_copy)),
- clause_i(Head,Body),
- variant(Head,Head_copy),
- variant(Body,Body_copy).
- pfc_clause_i(Head):-
- % find a unit clause identical to Head by finding one which unifies,
- % and then checking to see if it is identical
- copy_term(Head,Head_copy),
- clause_i(Head_copy,true),
- variant(Head,Head_copy).
- %% foreachl_do( ?Binder, ?Body) is semidet.
- %
- % Foreachl Do.
- %
- foreachl_do(Binder,Body):- Binder,pfcl_do(Body),fail.
- foreachl_do(_,_).
- %% pfcl_do( ?X) is semidet.
- %
- % executes X once and always succeeds.
- %
- pfcl_do(X):- X,!.
- pfcl_do(_).
- %% pfc_union(L1,L2,L3) is semidet.
- %
- % true if set L3 is the result of appending sets
- % L1 and L2 where sets are represented as simple lists.
- %
- pfc_union([],L,L).
- pfc_union([Head|Tail],L,Tail2):-
- memberchk(Head,L),
- !,
- pfc_union(Tail,L,Tail2).
- pfc_union([Head|Tail],L,[Head|Tail2]):-
- pfc_union(Tail,L,Tail2).
- % pfc_conjoin(+Conjunct1,+Conjunct2,?Conjunction).
- % arg3 is a simplified expression representing the conjunction of
- % args 1 and 2.
- pfc_conjoin(true,X,X):- !.
- pfc_conjoin(X,true,X):- !.
- pfc_conjoin(C1,C2,(C1,C2)).
- % File : pfcdb.pl
- % Author : Tim Finin, finin@prc.unisys.com
- % Author : Dave Matuszek, dave@prc.unisys.com
- % Author : Dan Corpron
- % Updated: 10/11/87, ...
- % Purpose: predicates to manipulate a Pfc database (e.g. save,
- % restore, reset, etc.0
- % pfc_database_term(P/A) is true iff P/A is something that Pfc adds to
- % the database and should not be present in an empty Pfc database
- pfc_database_term(spft/3).
- pfc_database_term(pt/2).
- pfc_database_term(bt/2).
- pfc_database_term(nt/3).
- pfc_database_term('==>'/2).
- pfc_database_term('<==>'/2).
- pfc_database_term('<-'/2).
- pfc_database_term(qu/1).
- pfc_database_term('==>'/1).
- pfc_database_term('~'/1).
- %% pfc_reset() is det.
- %
- % removes all forward chaining rules and pfc_justification_S from db.
- %
- pfc_reset:-
- clause_i(spft(P,F,Trigger),true),
- pfc_retract_i_or_warn(P),
- pfc_retract_i_or_warn(spft(P,F,Trigger)),
- fail.
- pfc_reset:-
- pfc_database_item(T),
- pfc_error("Pfc database not empty after pfc_reset, e.g., ~p.~n",[T]).
- pfc_reset.
- % true if there is some Pfc crud still in the database.
- pfc_database_item(Term):-
- pfc_database_term(P/A),
- functor(Term,P,A),
- clause_i(Term,_).
- pfc_retract_i_or_warn(X):- retract_u(X), !.
- pfc_retract_i_or_warn(X):- X=hs(_),!.
- pfc_retract_i_or_warn(X):- X=spft(_,hs(_),_),!.
- pfc_retract_i_or_warn(X):-
- pfc_warn("Couldn't retract_user ~p.~n",[X]).
- % File : pfcdebug.pl
- % Author : Tim Finin, finin@prc.unisys.com
- % Author : Dave Matuszek, dave@prc.unisys.com
- % Updated:
- % Purpose: provides predicates for examining the database and debugginh
- % for Pfc.
- :- dynamic pfc_is_tracing/1.
- :- dynamic pfc_spied/2.
- :- dynamic pfc_is_tracing_exec/0.
- :- dynamic pfc_warnings/1.
- :- pfc_default(pfc_warnings(_), pfc_warnings(true)).
- % pfc_fact(P) is true if fact P was asserted into the database via add.
- pfc_fact(P):- pfc_fact(P,true).
- % pfc_fact(P,C) is true if fact P was asserted into the database via
- % add and contdition C is satisfied. For example, we might do:
- %
- % pfc_fact(X,pfc_userFact(X))
- %
- pfc_fact(P,C):-
- pfc_get_support(P,_),
- pfc_db_type(P,fact),
- call(C).
- % pfc_facts(-ListofPpfc_facts) returns a list of facts added.
- pfc_facts(L):- pfc_facts(_,true,L).
- pfc_facts(P,L):- pfc_facts(P,true,L).
- % pfc_facts(Pattern,Condition,-ListofPpfc_facts) returns a list of facts added.
- %% pfc_facts( ?P, ?C, ?L) is semidet.
- %
- % PFC Facts.
- %
- pfc_facts(P,C,L):- setof(P,pfc_fact(P,C),L).
- %% brake( ?X) is semidet.
- %
- % Brake.
- %
- brake(X):- X, break.
- %
- % predicates providing a simple tracing facility
- %
- pfc_trace_pfc_ain(P):-
- % this is here for upward compat. - should go away eventually.
- pfc_trace_pfc_ain(P,(o,o)).
- pfc_ain_special_support(Fact,Support):- fail,
- Support = (How,pt(How2,rhs([Fact]))),
- ignore(((pfc_ain_trigger_reprop(nt(How,(pfc_CALL(How2)),rhs([Fact])),(How==>Fact,How)),fail))),
- dmsg('~n \\\\^ Extra ^// ~n',[]),
- fail.
- pfc_ain_special_support(P,S):-pfc_trace_pfc_ain(P,S),!.
- pfc_trace_pfc_ain(P,S):-
- notrace((
- pfc_trace_add_print(P,S),
- pfc_trace_break(P,S))).
- pfc_trace_add_print(P,S):-
- pfc_is_tracing(P), !,
- (
- \+ \+
- (S=(U,U)
- -> wdmsg("~NAdding (~p) ~p",[U,P])
- ; wdmsg("~NAdding (:) ~p~NSupported By: ~p",[P,S]))),!.
- pfc_trace_add_print(_,_).
- pfc_trace_break(P,_ZS):-
- pfc_spied(P,add) ->
- (wdmsg("~NBreaking on pfc_ain(~p)",[P]),
- break)
- ; true.
- pfc_trace_rem(pt(_,_)):-
- % hack for now - never trace triggers.
- !.
- pfc_trace_rem(nt(_,_)):-
- % hack for now - never trace triggers.
- !.
- pfc_trace_rem(P):-
- (pfc_is_tracing(P)
- -> wdmsg('~NRemoving ~p.',[P])
- ; true),
- (pfc_spied(P,pfc_withdraw)
- -> (wdmsg("~NBreaking on pfc_withdraw(~p)",[P]),
- break)
- ; true).
- pfc_trace:- pfc_trace(_).
- pfc_trace(Form):-
- assert_i(pfc_is_tracing(Form)).
- %% pfc_trace( ?Form, ?Condition) is semidet.
- %
- % PFC Trace.
- %
- pfc_trace(Form,Condition):-
- assert_i((pfc_is_tracing(Form):- Condition)).
- pfc_spy(Form):- pfc_spy(Form,[add,pfc_withdraw],true).
- pfc_spy(Form,Modes):- pfc_spy(Form,Modes,true).
- pfc_spy(Form,[add,pfc_withdraw],Condition):-
- !,
- pfc_spy1(Form,add,Condition),
- pfc_spy1(Form,pfc_withdraw,Condition).
- pfc_spy(Form,Mode,Condition):-
- pfc_spy1(Form,Mode,Condition).
- pfc_spy1(Form,Mode,Condition):-
- assert_i((pfc_spied(Form,Mode):- Condition)).
- pfc_nospy:- pfc_nospy(_,_,_).
- pfc_nospy(Form):- pfc_nospy(Form,_,_).
- pfc_nospy(Form,Mode,Condition):-
- clause_i(pfc_spied(Form,Mode), Condition, Ref),
- erase(Ref),
- fail.
- pfc_nospy(_,_,_).
- pfc_noTrace:- pfc_untrace.
- pfc_untrace:- pfc_untrace(_).
- pfc_untrace(Form):- retractall_i(pfc_is_tracing(Form)).
- % needed: pfc_trace_rule(Name) ...
- pfc_trace_msg(MsgArgs):-pfc_trace_msg('~p',[MsgArgs]).
- % if the correct flag is set, trace exection of Pfc
- pfc_trace_msg(Msg,Args):- notrace((tracing,in_cmt(wdmsg(Msg, Args)))),!.
- pfc_trace_msg(Msg,Args):-
- pfc_is_tracing_exec,
- !,
- in_cmt(wdmsg(Msg, Args)).
- pfc_trace_msg(_ZMsg,_ZArgs).
- pfc_watch:- assert_i(pfc_is_tracing_exec).
- pfc_trace_exec:- assert_i(pfc_is_tracing_exec).
- pfc_noWatch:- retractall_i(pfc_is_tracing_exec).
- pfc_error(Msg):- pfc_error(Msg,[]).
- pfc_error(Msg,Args):-
- format("~NERROR/Pfc: ",[]),
- format(Msg,Args).
- pfc_test(G):- pfc_why(G).
- pfc_load_term(:- module(_,L)):-!, maplist(export,L).
- pfc_load_term(:- TermO):-call(TermO).
- pfc_load_term(TermO):-pfc_ain_object(TermO).
- pfc_load(PLNAME):- % unload_file(PLNAME),
- open(PLNAME, read, In, []),
- repeat,
- line_count(In,_Lineno),
- % double_quotes(_DQBool)
- Options = [variables(_Vars),variable_names(VarNames),singletons(_Singletons),comment(_Comment)],
- catchv((read_term(In,Term,[syntax_errors(error)|Options])),E,(dmsg(E),fail)),
- b_setval('$variable_names',VarNames),expand_term(Term,TermO),pfc_load_term(TermO),
- Term==end_of_file,
- close(In).
- %
- % These control whether or not warnings are printed at all.
- % pfc_warn.
- % nopfc_warn.
- %
- % These print a warning message if the flag pfc_warnings is set.
- % pfc_warn(+Message)
- % pfc_warn(+Message,+ListOfArguments)
- %
- pfc_warn:-
- retractall_i(pfc_warnings(_)),
- assert_i(pfc_warnings(true)).
- nopfc_warn:-
- retractall_i(pfc_warnings(_)),
- assert_i(pfc_warnings(false)).
- pfc_warn(Msg):- pfc_warn(Msg,[]).
- pfc_warn(Msg,Args):-
- format(string(S),Msg,Args),
- (pfc_warnings(true) -> wdmsg(warn(pfc_,S)) ; pfc_trace_msg('WARNING/PFC: ~s',[S])),!.
- %% pfc_set_warnings(+TF) is det.
- % true = sets flag to cause Pfc warning messages to print.
- % false = sets flag to cause Pfc warning messages not to print.
- %
- pfc_set_warnings(True):-
- retractall_i(pfc_warnings(_)),
- assert_i(pfc_warnings(True)).
- pfc_set_warnings(false):-
- retractall_i(pfc_warnings(_)).
- % File : pfcjust.pl
- % Author : Tim Finin, finin@prc.unisys.com
- % Author : Dave Matuszek, dave@prc.unisys.com
- % Updated:
- % Purpose: predicates for accessing Pfc pfc_justification_S.
- % Status: more or less working.
- % Bugs:
- % *** predicates for exploring supports of a fact *****
- :- use_module(library(lists)).
- pfc_justification(F,J):- supporters_list(F,J).
- pfc_justification_S(F,Js):- bagof(J,pfc_justification(F,J),Js).
- %% pfc_basis_list(P,-list:L)
- %
- % is true iff L is a list of "pfc_basis_list" facts which, taken
- % together, allows us to deduce P. A mpred "based on" list fact is an axiom (a fact
- % added by the user or a raw Prolog fact (i.e. one w/o any support))
- % or an assumption.
- %
- pfc_basis_list(F,[F]):- (pfc_axiom(F) ; pfc_assumption(F)),!.
- pfc_basis_list(F,L):-
- % i.e. (reduce 'append (map 'pfc_basis_list (pfc_justification f)))
- pfc_justification(F,Js),
- bases_union(Js,L).
- %% bases_union(+list:L1,+list:L2).
- %
- % is true if list L2 represents the union of all of the
- % facts on which some conclusion in list L1 is based.
- %
- bases_union([],[]).
- bases_union([X|Rest],L):-
- pfc_basis_list(X,Bx),
- bases_union(Rest,Br),
- pfc_union(Bx,Br,L).
- pfc_axiom(F):-
- pfc_get_support(F,(U,U)).
- %% pfc_assumption(P)
- %
- % an pfc_assumption is a failed goal, i.e. were assuming that our failure to
- % prove P is a proof of not(P)
- %
- pfc_assumption(P):- pfc_negation(P,_).
- %% pfc_assumptions( +X, +AsSet) is semidet.
- %
- % true if AsSet is a set of assumptions which underly X.
- %
- pfc_assumptions(X,[X]):- pfc_assumption(X).
- pfc_assumptions(X,[]):- pfc_axiom(X).
- pfc_assumptions(X,L):-
- pfc_justification(X,Js),
- do_assumpts(Js,L).
- %% do_assumpts(+Set1,?Set2) is semidet.
- %
- % Assumptions Secondary Helper.
- %
- do_assumpts([],[]).
- do_assumpts([X|Rest],L):-
- pfc_assumptions(X,Bx),
- do_assumpts(Rest,Br),
- pfc_union(Bx,Br,L).
- % pfc_proofTree(P,T) the proof tree for P is T where a proof tree is
- % of the form
- %
- % [P , J1, J2, ;;; Jn] each Ji is an independent P justifier.
- % ^ and has the form of
- % [J11, J12,... J1n] a list of proof trees.
- %% pfc_child(+P,?Q) is semidet.
- %
- % is true iff P is an immediate justifier for Q.
- %
- pfc_child(P,Q):-
- pfc_get_support(Q,(P,_)).
- pfc_child(P,Q):-
- pfc_get_support(Q,(_,Trig)),
- pfc_db_type(Trig,trigger),
- pfc_child(P,Trig).
- %% pfc_children( ?P, ?L) is semidet.
- %
- % PFC Children.
- %
- pfc_children(P,L):- bagof(C,pfc_child(P,C),L).
- %% pfc_descendant( ?P, ?Q) is semidet.
- %
- % pfc_descendant(P,Q) is true iff P is a justifier for Q.
- %
- pfc_descendant(P,Q):-
- pfc_descendant1(P,Q,[]).
- %% pfc_descendant1( ?P, ?Q, ?Seen) is semidet.
- %
- % PFC Descendant Secondary Helper.
- %
- pfc_descendant1(P,Q,Seen):-
- pfc_child(X,Q),
- (\+ member(X,Seen)),
- (P=X ; pfc_descendant1(P,X,[X|Seen])).
- %% pfc_descendants( ?P, ?L) is semidet.
- %
- % PFC Descendants.
- %
- pfc_descendants(P,L):-
- bagof(Q,pfc_descendant1(P,Q,[]),L).
- bagof_or_nil(T,G,B):- (bagof(T,G,B) *-> true; B=[]).
- %
- % predicates for manipulating support relationships
- %
- % pfc_add_support(+Fact,+Support)
- pfc_add_support(P,(Fact,Trigger)):-
- (Trigger= nt(F,Condition,Action) ->
- (pfc_trace_msg('~N~n\tAdding pfc_do_negitive_triggers via support~n\t\ttrigger: ~p~n\t\tcond: ~p~n\t\taction: ~p~n\t from: ~p~N',
- [F,Condition,Action,pfc_add_support(P,(Fact,Trigger))]));true),
- assert_i(spft(P,Fact,Trigger)).
- pfc_get_support(P,(Fact,Trigger)):-
- spft(P,Fact,Trigger).
- % There are three of these to try to efficiently handle the cases
- % where some of the arguments are not bound but at least one is.
- pfc_rem_support(P,(Fact,Trigger)):-
- nonvar(P),
- !,
- pfc_retract_i_or_warn(spft(P,Fact,Trigger)).
- pfc_rem_support(P,(Fact,Trigger)):-
- nonvar(Fact),
- !,
- pfc_retract_i_or_warn(spft(P,Fact,Trigger)).
- pfc_rem_support(P,(Fact,Trigger)):-
- pfc_retract_i_or_warn(spft(P,Fact,Trigger)).
- pfc_collect_supports(Tripples):-
- bagof(Tripple, pfc_support_relation(Tripple), Tripples),
- !.
- pfc_collect_supports([]).
- pfc_support_relation((P,F,T)):-
- spft(P,F,T).
- pfc_make_supports((P,S1,S2)):-
- pfc_add_support(P,(S1,S2)),
- (pfc_ain_object(P); true),
- !.
- %% pfc_trigger_key(+Trigger,-Key)
- %
- % Arg1 is a trigger. Key is the best term to index it on.
- %
- % Get a key from the trigger that will be used as the first argument of
- % the trigger pfc_basis_list clause_i that stores the trigger.
- pfc_trigger_key(X,X):- var(X), !.
- pfc_trigger_key(pt(Key,_),Key).
- pfc_trigger_key(pt(Key,_,_),Key).
- pfc_trigger_key(nt(Key,_,_),Key).
- pfc_trigger_key(Key,Key).
- % For chart parser
- pfc_trigger_key(chart(word(W),_ZL),W):- !.
- pfc_trigger_key(chart(stem([Char1|_ZRest]),_ZL),Char1):- !.
- pfc_trigger_key(chart(Concept,_ZL),Concept):- !.
- pfc_trigger_key(X,X).
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999%%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- %%%%%%%%%%%%%%%%%% 99999999999999999999999999999999999999999999
- pp_DB:-
- pp_facts,
- pp_rules,
- pp_triggers,
- pp_supports.
- % pp_facts ...
- pp_facts:- ignore(pp_facts(_,true)).
- pp_facts(Pattern):- pp_facts(Pattern,true).
- pp_facts(P,C):-
- pfc_facts(P,C,L),
- pfc_classifyFacts(L,User,Pfc,_ZRule),
- format("~N~nUser added facts:",[]),
- pp_items(User),
- format("~N~nPPfc added facts:",[]),
- pp_items(Pfc).
- % printitems clobbers it''s arguments - beware!
- pp_items([]).
- pp_items([H|T]):-
- numbervars(H,0,_),
- format("~N ~p",[H]),
- pp_items(T).
- pfc_classifyFacts([],[],[],[]).
- pfc_classifyFacts([H|T],User,Pfc,[H|Rule]):-
- pfc_db_type(H,rule),
- !,
- pfc_classifyFacts(T,User,Pfc,Rule).
- pfc_classifyFacts([H|T],[H|User],Pfc,Rule):-
- pfc_get_support(H,(u,u)),
- !,
- pfc_classifyFacts(T,User,Pfc,Rule).
- pfc_classifyFacts([H|T],User,[H|Pfc],Rule):-
- pfc_classifyFacts(T,User,Pfc,Rule).
- pp_rules:-
- format("~NRules...~n",[]),
- bagof_or_nil((P==>Q),clause_i((P==>Q),true),R1),
- pp_items(R1),
- bagof_or_nil((P<==>Q),clause_i((P<==>Q),true),R2),
- pp_items(R2),
- bagof_or_nil((P<-Q),clause_i((P<-Q),true),R3),
- pp_items(R3).
- pp_triggers:-
- format("~NPositive triggers...~n",[]),
- bagof_or_nil(pt(T,B),pfc_get_trigger_quick(pt(T,B)),Pts),
- pp_items(Pts),
- format("~NNegative triggers...~n",[]),
- bagof_or_nil(nt(A,B,C),pfc_get_trigger_quick(nt(A,B,C)),Nts),
- pp_items(Nts),
- format("~NGoal triggers...~n",[]),
- bagof_or_nil(bt(A,B),pfc_get_trigger_quick(bt(A,B)),Bts),
- pp_items(Bts).
- pp_supports:-
- % temporary hack.
- format("~NSupports...~n",[]),
- setof((P >= S), pfc_get_support(P,S),L),
- pp_items(L).
- % File : pfc_why.pl
- % Author : Tim Finin, finin@prc.unisys.com
- % Updated:
- % Purpose: predicates for interactively exploring Pfc pfc_justification_S.
- % ***** predicates for brousing pfc_justification_S *****
- :- use_module(library(lists)).
- pfc_why:-
- why_buffer(P,_),
- pfc_why(P).
- pfc_why(N):-
- number(N),
- !,
- why_buffer(P,Js),
- pfc_handle_why_command(N,P,Js).
- pfc_why(P):-
- pfc_justification_S(P,Js),
- retractall_i(why_buffer(_,_)),
- assert_i(why_buffer(P,Js)),
- in_cmt((pfc_whyBrouse(P,Js))).
- pfc_why1(P):-
- pfc_justification_S(P,Js),
- in_cmt((pfc_whyBrouse(P,Js))).
- % non-interactive
- pfc_whyBrouse(P,Js):-
- pfc_pp_justifications(P,Js), !.
- % Interactive
- pfc_whyBrouse(P,Js):-
- pfc_pp_justifications(P,Js),
- pfc_ask(' >> ',Answer),
- pfc_handle_why_command(Answer,P,Js).
- pfc_handle_why_command(q,_,_):- !.
- pfc_handle_why_command(h,_,_):-
- !,
- format("~N
- Justification Brouser Commands:
- q quit.
- N focus on Nth pfc_justification.
- N.M brouse step M of the Nth pfc_justification
- user up a level ~n",
- []).
- pfc_handle_why_command(N,_ZP,Js):-
- float(N),
- !,
- pfc_select_justification_node(Js,N,Node),
- pfc_why1(Node).
- pfc_handle_why_command(u,_,_):-
- % u=up
- !.
- pfc_unhandled_command(N,_,_):-
- integer(N),
- !,
- format("~N~p is a yet unimplemented command.",[N]),
- fail.
- pfc_unhandled_command(X,_,_):-
- format("~N~p is an unrecognized command, enter h. for help.",[X]),
- fail.
- pfc_pp_justifications(P,Js):-
- format("~NJustifications for ~p:",[P]),
- pfc_pp_justification1(Js,1).
- pfc_pp_justification1([],_).
- pfc_pp_justification1([J|Js],N):-
- % show one pfc_justification and recurse.
- nl,
- pfc_pp_justifications2(J,N,1),
- N2 is N+1,
- pfc_pp_justification1(Js,N2).
- pfc_pp_justifications2([],_,_).
- pfc_pp_justifications2([C|Rest],JustNo,StepNo):-
- (StepNo==1->fmt('~N~n',[]);true),
- copy_term(C,CCopy),
- numbervars(CCopy,0,_),
- format("~N ~p.~p ~p",[JustNo,StepNo,CCopy]),
- StepNext is 1+StepNo,
- pfc_pp_justifications2(Rest,JustNo,StepNext).
- pfc_ask(Msg,Ans):-
- format("~N~p",[Msg]),
- read(Ans).
- pfc_select_justification_node(Js,Index,Step):-
- JustNo is integer(Index),
- nth1(JustNo,Js,Justification),
- StepNo is 1+ integer(Index*10 - JustNo*10),
- nth1(StepNo,Justification,Step).
- %% pfc_supported(+P) is semidet.
- %
- % succeeds if P is "supported". What this means
- % depends on the TMS mode selected.
- %
- pfc_supported(P):-
- pfc_tms_mode(Mode),
- pfc_supported(Mode,P).
- %% pfc_supported(+TMS,+P) is semidet.
- %
- % succeeds if P is "supported". What this means
- % depends on the TMS mode supplied.
- %
- pfc_supported(local,P):- !, pfc_get_support(P,_).
- pfc_supported(cycles,P):- !, well_founded(P).
- pfc_supported(_,_):- true.
- %% well_founded(+Fact) is semidet.
- %
- % a fact is well founded if it is supported by the user
- % or by a set of facts and a rules, all of which are well founded.
- %
- well_founded(Fact):- with_each(well_founded_0,Fact,[]).
- well_founded_0(F,_):-
- % supported by user (pfc_axiom) or an "absent" fact (pfc_assumption).
- (pfc_axiom(F) ; pfc_assumption(F)),
- !.
- well_founded_0(F,Descendants):-
- % first make sure we aren't in a loop.
- (\+ memberchk(F,Descendants)),
- % find a pfc_justification.
- supporters_list(F,Supporters),
- % all of whose members are well founded.
- well_founded_list(Supporters,[F|Descendants]),
- !.
- %% well_founded_list(+List,-Decendants) is det.
- %
- % simply maps well_founded over the list.
- %
- well_founded_list([],_).
- well_founded_list([X|Rest],L):-
- well_founded_0(X,L),
- well_founded_list(Rest,L).
- %% supporters_list(+F,-ListofSupporters) is det.
- %
- % where ListOfSupports is a list of the
- % supports for one pfc_justification for fact F -- i.e. a list of facts which,
- % together allow one to deduce F. One of the facts will typically be a rule.
- % The supports for a user-defined fact are: [u].
- %
- supporters_list(F,[Fact|MoreFacts]):-
- pfc_get_support(F,(Fact,Trigger)),
- triggerSupports(Trigger,MoreFacts).
- triggerSupports(u,[]):- !.
- triggerSupports(Trigger,[Fact|MoreFacts]):-
- pfc_get_support(Trigger,(Fact,AnotherTrigger)),
- triggerSupports(AnotherTrigger,MoreFacts).
- :- use_module(library(logicmoo_utils)).
- :- pfc_reset.
- :- dynamic((foob/1,if_missing/2)).
- :- pfc_trace.
- :- pfc_watch.
- % this should have been ok
- % (if_missing(Missing,Create) ==> ((\+ Missing/(Missing\==Create), \+ Create , \+ ~(Create)) ==> Create)).
- :- pfc_ain((if_missing(Missing,Create) ==>
- ( ( \+ Missing/(Missing\=@=Create)) ==> Create))).
- :- pfc_ain((good(X) ==> if_missing(foob(_),foob(X)))).
- :- pfc_ain(good(az)).
- :- pfc_why(foob(az)).
- :- pp_DB.
- :- rtrace(pfc_ain(foob(b))).
- :- call(\+foob(az)).
- ==> (\+ foob(b)).
- :- pfc_why(foob(az)).
- :- rtrace(pfc_withdraw( good(az) )).
- :- listing([foob,good]).
- % :- trace.
- :- call( \+foob(az)).
- :- pfc_ain(~ foob(b)).
- end_of_file.
- :- pp_DB.
- :- pfc_why(~foob(b)).
- :- pfc_ain(good(az)).
- :- pfc_why(foob(az)).
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement