
% borrowed from SWI's rdf_db.pl, and then ... messed with.  sorry.


		 /*******************************
		 *	     NAMESPACES		*
		 *******************************/

:- dynamic(ns/2).
:- multifile(ns/2).

%	rdf_register_ns(+Alias, +URI)
%
%	Register a namespace.  What to do if the Alias already
%	exists?  Throw a permission error?  Use both URI's as synonyms?

rdf_register_ns(Alias, URI) :-
	ns(Alias, URI), !.
rdf_register_ns(Alias, _) :-
	ns(Alias, _),
	throw(error(permission_error(register, namespace, Alias),
		    context(_, 'Already defined'))).
rdf_register_ns(Alias, URI) :-
	assert(ns(Alias, URI)).

:- rdf_register_ns(rdf,  'http://www.w3.org/1999/02/22-rdf-syntax-ns#').
:- rdf_register_ns(rdfs, 'http://www.w3.org/2000/01/rdf-schema#').
:- rdf_register_ns(owl,  'http://www.w3.org/2002/07/owl#').
:- rdf_register_ns(xsd,  'http://www.w3.org/2001/XMLSchema#').
:- rdf_register_ns(dc,   'http://purl.org/dc/elements/1.1/').
:- rdf_register_ns(eor,  'http://dublincore.org/2000/03/13/eor#').

rdf_unregister_ns(Alias) :-
	retractall(ns(Alias, _)).

%	rdf_global_id(?Id, ?GlobalId)
%
%	Convert between NS:Local and global atomic identifier.
%	To be completed.

rdf_global_id(Global, Global) :-
	var(Global), !.
rdf_global_id(NS:Local, Global) :- !,
	(   ns(NS, Full)
	*-> atom_concat(Full, Local, Global)
	;   atom_concat(NS, Local, Global) % ,
	    % print_message(warning, format('Did concat of ns-prefix "~a" with local part "~a"', [NS, Local]))
	).
rdf_global_id(Global, Global) :- atom(Global), !.
rdf_global_id(literal(Global), literal(Global)) :- !.
rdf_global_id(Global, Global) :- number(Global), !.   % not really rdf
rdf_global_id(Other,_) :-
	throw(no_global_id(Other)).


%	rdf_global_term(+TermIn, -GlobalTerm)
%	
%	Does rdf_global_id/2 on all terms NS:Local by recursively analysing
%	the term.

rdf_global_term(Var, Var) :-
	var(Var), !.
rdf_global_term(NS:Local, Global) :-
	rdf_global_id(NS:Local, Global).
rdf_global_term([H0|T0], [H|T]) :- !,
	rdf_global_term(H0, H),
	rdf_global_term(T0, T).
rdf_global_term(Term0, Term) :-
	compound(Term0), !,
	Term0 =.. [H|L0],
	rdf_global_term(L0, L),         % don't do the head?  Hrm.
	Term =.. [H|L].
rdf_global_term(Term, Term).


%	user:goal_expansion(+NSGoal, -Goal)
%	
%	This predicate allows for writing down rdf queries in a friendly
%	name-space fashion.  

:- multifile
	user:goal_expansion/2.

user:goal_expansion(says(Subj0, Pred0, Obj0, Source),
		    says(Subj, Pred, Obj, Source)) :-
	rdf_global_id(Subj0, Subj),
	rdf_global_id(Pred0, Pred),
	rdf_global_id(Obj0, Obj).

%    user:term_expansion(claim(S, P, O),


term_qname_uri(Qname, URI) :-
	rdf_global_term(Qname, URI).

