
% dynamic predicates get wiped when the file which introduced them
% gets re-compiled.   This is not normally what I want, so I use this
% instead.
multi_dynamic(Predicate) :-
	dynamic(Predicate),
	multifile(Predicate).

% for cvs.pl
:- multifile cvs_id/1.

% for unittest.pl
:- multi_dynamic(unit_test/1).

% for basicpage.pl
:- multifile(tab/5).
%    tab(?SortOrder, ?Q, +Focus, ?Label, ?Content)
%  

% used by reply.pl; clauses are defined near the other predicates
% for related data (ie we don't clump all web page generation code
% in one place).
:- multifile(web_server_path_view/2).
%     web_server_path_view(+FirstPathSegment, -ViewPredicateForPageBody)
	

:- multifile
	user:goal_expansion/2.

% for retrieve.pl
user:goal_expansion(ret_set(Subj0, Pred0, Obj0),
		    ret_set(Subj, Pred, Obj)) :-
	rdf_global_id(Subj0, Subj),
	rdf_global_id(Pred0, Pred),
	rdf_global_id(Obj0, Obj).
user:goal_expansion(ret_get(Subj0, Pred0, Obj0),
		    ret_get(Subj, Pred, Obj)) :-
	rdf_global_id(Subj0, Subj),
	rdf_global_id(Pred0, Pred),
	rdf_global_id(Obj0, Obj).
user:goal_expansion(ret_assert_triple(Ret, Subj0, Pred0, Obj0),
		    ret_assert_triple(Ret, Subj, Pred, Obj)) :-
	rdf_global_id(Subj0, Subj),
	rdf_global_id(Pred0, Pred),
	rdf_global_id(Obj0, Obj).
user:goal_expansion(ret_triple(Ret, Subj0, Pred0, Obj0),
		    ret_triple(Ret, Subj, Pred, Obj)) :-
	rdf_global_id(Subj0, Subj),
	rdf_global_id(Pred0, Pred),
	rdf_global_id(Obj0, Obj).



% for views.pl
%    could call a different version if terms were variables...?
user:goal_expansion(rdfq(Q, Subj0, Pred0, Obj0),
		    rdfq(Q, Subj, Pred, Obj)) :-
	rdf_global_id(Subj0, Subj),
	rdf_global_id(Pred0, Pred),
	rdf_global_id(Obj0, Obj).


user:goal_expansion(vrdf(Subj0, Pred0, Obj0, Proof),
		    vrdf(Subj, Pred, Obj, Proof)) :-
	rdf_global_id(Subj0, Subj),
	rdf_global_id(Pred0, Pred),
	rdf_global_id(Obj0, Obj).
user:goal_expansion(vrdf_provide_by_predicate(Pred0, Subj0, Obj0, Proof),
		    vrdf_provide_by_predicate(Pred, Subj, Obj, Proof)) :-
	rdf_global_id(Subj0, Subj),
	rdf_global_id(Pred0, Pred),
	rdf_global_id(Obj0, Obj).
user:goal_expansion(vrdf_provide_by_subject(Subj0, Pred0, Obj0, Proof),
		    vrdf_provide_by_subject(Subj, Pred, Obj, Proof)) :-
	rdf_global_id(Subj0, Subj),
	rdf_global_id(Pred0, Pred),
	rdf_global_id(Obj0, Obj).
user:goal_expansion(vrdf_provide(Subj0, Pred0, Obj0, Proof),
		    vrdf_provide(Subj, Pred, Obj, Proof)) :-
	rdf_global_id(Subj0, Subj),
	rdf_global_id(Pred0, Pred),
	rdf_global_id(Obj0, Obj).
user:goal_expansion(vrdf_nested(Subj0, Pred0, Obj0, Proof),
		    vrdf_nested(Subj, Pred, Obj, Proof)) :-
	rdf_global_id(Subj0, Subj),
	rdf_global_id(Pred0, Pred),
	rdf_global_id(Obj0, Obj).


% for findsome.pl so these act like proper metalogical predicates
user:goal_expansion(setofsome(Max, Done, T, G, Result),
		    setofsome(Max, Done, T, G2, Result)) :-
		   expand_goal(G, G2).


user:goal_expansion(findsome(Max, Skip, T, G, Result),
		    findsome(Max, Skip, T, G2, Result)) :-
		   expand_goal(G, G2).

:- multifile
	user:term_expansion/2.

user:term_expansion((rdfq_provide(P0,S0,O0,Q) :- Body),
		    (rdfq_provide(P,S,O,Q) :- Body)) :-
	rdf_global_id(S0, S),
	rdf_global_id(P0, P),
	rdf_global_id(O0, O).


% and also these
user:goal_expansion(with_mutex(M, G), with_mutex(M, G2)) :-
	expand_goal(G, G2).
user:goal_expansion(with_read_lock(M, G), with_read_lock(M, G2)) :-
	expand_goal(G, G2).
user:goal_expansion(with_read_lock(M, G, T), with_read_lock(M, G2, T)) :-
	expand_goal(G, G2).
user:goal_expansion(with_write_lock(M, G), with_write_lock(M, G2)) :-
	expand_goal(G, G2).
user:goal_expansion(with_write_lock(M, G, T), with_write_lock(M, G2, T)) :-
	expand_goal(G, G2).

/*
user:context(some_sys_id, File, Line, Time) :-
	(
	  prolog_load_context(term_position, '$stream_position'(_,Line,_,_,_)),
	  prolog_load_context(file, File)
	  % time_file(File, Time)
 	-> true
 	;  throw(context_called_in_bad_context)
 	).
*/

here :-
	prolog_load_context(file, File),
	prolog_load_context(term_position, '$stream_position'(_,Line,_,_,_)),
	format(user_error, 'at ~q:~w~n', [File,Line]).
%  COULD use this to prevent it in mt.pl ?!

/*
user:goal_expansion(rdf(S0,P0,O0,G), mt_rdf(S,P,O,G)) :-
	rdf_global_id(S0, S),
	rdf_global_id(P0, P),
	rdf_global_object(O0, O).
user:goal_expansion(rdf_assert(S0,P0,O0,G), mt_rdf_assert(S,P,O,G)) :-
	rdf_global_id(S0, S),
	rdf_global_id(P0, P),
	rdf_global_object(O0, O).
user:goal_expansion(rdf_retractall(A,B,C,D), mt_rdf_retractall(A,B,C,D)).
user:goal_expansion(rdf_update(A,B,C,D), mt_rdf_update(A,B,C,D)).
*/

user:goal_expansion(user:db_get(A,B,C), mt_db_get(A,B,C)).
user:goal_expansion(user:db_put(A,B,C), mt_db_put(A,B,C)).
user:goal_expansion(user:db_open(A,B,C), mt_db_open(A,B,C)).
user:goal_expansion(user:db_delall(A,B,C), mt_db_delall(A,B,C)).
user:goal_expansion(user:db_enum(A,B,C), mt_db_enum(A,B,C)).
user:goal_expansion(user:db_close(A), mt_db_close(A)).


:- ensure_loaded(qtest_header).


user:goal_expansion(rdf_transaction(G1), rdf_transaction(G)) :-
	expand_goal(G1, G).

