<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">/* **************************************************
	module for tr bulk updates
*************************************************** */

:- op(850,xfy,[from]).
:- op(900,xfy,[assign_to]).


% bulk insertion (always true, i.e. weak)
%
bulkins(FromFun,ToFun) :- 
	ToFun =.. [ToName|ToArgs],
	dbname(ToName,DBName),
	insertedname(ToName,IName),
	deletedname(ToName,DName), 
	DBPred =.. [DBName|ToArgs],
	(
	predicate_property(DBPred,(dynamic)) -&gt;
		do_bulkinsert(FromFun,IName,DName,ToFun)
	;
	not(predicate_property(DBPred,(dynamic))),
	functor(DBPred,DBName,NArgs),
	insertnewdbrule(ToName,DBName,IName,DName,NArgs), !,
	deleting(tr),
	do_bulkinsert(FromFun,IName,DName,ToFun),
	inserting(tr)).

do_bulkinsert(FromFun,IName,DName,ToFun) :-
	getlist(ToFun,FromFun,List),!,
	do_insertlist(IName,DName,List).

% weak insert all predicates in the list
do_insertlist(_,_,[]).
do_insertlist(IName,DName,[ToFun|Rest]) :-
	do_weakinsert(IName,DName,ToFun),
	do_insertlist(IName,DName,Rest).

% weak insert, i.e. it succeeds if Pred is already in the database
do_weakinsert(_,_,ToFun) :-
	call(ToFun).
do_weakinsert(IName,DName,ToFun) :-
	ToFun =.. [_|Args],
	IPred =.. [IName|Args],
	do_insert(IPred,DName).

% bulk deletion (always true)
%
bulkdel(X) :-
	X =.. [Name|Args],
	deletedname(Name,DName),
	DTerm =.. [DName|Args],
	(predicate_property(DTerm,(dynamic)) -&gt; bulk_erase(X,DTerm)
	;
	not(predicate_property(DTerm,(dynamic)))).

% Unlike bulkins, there is only one term for bulk_erase, so database(Term)
% works the same as removing tr flag first.
bulk_erase(Term,DTerm) :-
	inserting(tr),
	getlist(DTerm,Term,DList),
	deleting(tr),
	insertlist(DList).

% assignment
% For example,  assign q(X,Y) from p(X,Y)
% syntax:       assign_to(q, from([X,Y],p(X,Y))
%           or  assign_to(q, p(X,Y))
%
assign_to(OutPred,TestPred) :-
	OutPred =.. [OutName|ArgList],
	dbname(OutName,DBName),
	insertedname(OutName,IName),
	deletedname(OutName,DName), 
	DBPred =.. [DBName|ArgList],
	(
	predicate_property(DBPred,(dynamic)) -&gt; true
	;
	functor(DBPred,DBName,NArgs),
	insertnewdbrule(OutName,DBName,IName,DName,NArgs)), !,
	simple_assign(OutName,ArgList,TestPred).

% bulk delete OutPred first and then bulk insert from TestPred to OutPred
simple_assign(OutName,ArgList,TestPred) :-
	insertedname(OutName,IName),	
	deletedname(OutName,DName),
	OutPred =.. [OutName|ArgList],
	deleting(tr),
	getlist(OutPred,TestPred,BufferList), % copy output to buffer
	inserting(tr),
	DPred =.. [DName|ArgList],
	bulk_erase(OutPred,DPred),
	do_insertlist(IName,DName,BufferList).

</pre></body></html>