
:- [cvs, unit_test].
cvs_id('$Id: assert_header_rdf.pl,v 1.5 2004/10/27 17:24:41 sandro Exp $').

:- ensure_loaded(library(debug)).

:- ensure_loaded(rdfpage).
:- ensure_loaded(time_conversions).

:- multifile rdf_header_map/3.

%%  assert_header_rdf(+Ret, +Header)
%%
%%  ... where Header is like date('Sun Mar 13 2004') or some such.
%%
%%  Uses rdf_header_map(FieldName, Predicate, Mode) as a guide
%%

assert_header_rdf(Ret, Header) :-
	Header =.. [Functor, Arg | _],   % "_" is for status(Code, Message)
	(   \+ rdf_header_map(Functor, _, _)
	->  once((ret_get(Ret, retr:finalResource, URI) ; URI=unknown)),
	    log_term(unknown_header(URI, Header))
	;   true
	),
	!,
	(   rdf_header_map(Functor, Predicate1, Mode),
	    debug(retmeta, '~q', rdf_header_map(Functor, Predicate1, Mode)),
	    rdf_global_id(Predicate1, Predicate),
	    assert_header_rdf_aux(Ret, Predicate, Mode, Arg),
	    fail   % do all possible such assertions
	; true
	).

assert_header_rdf_aux(_Ret, _Predicate, skip, _Arg).
assert_header_rdf_aux(Ret, Predicate, atom, Arg) :-
	ret_set(Ret, Predicate, Arg).
assert_header_rdf_aux(Ret, Predicate, http_version, Major-Minor) :-
	concat_atom([Major, Minor], '.', Atom),
	ret_set(Ret, Predicate, literal(Atom)), !.
assert_header_rdf_aux(Ret, Predicate, literal, Arg) :-
	ret_set(Ret, Predicate, literal(Arg)).
assert_header_rdf_aux(Ret, Predicate, number, Arg) :-
	atom_number(Arg, Number),
	ret_set(Ret, Predicate, literal(Number)).
assert_header_rdf_aux(Ret, Predicate, already_number, Arg) :-
	ret_set(Ret, Predicate, literal(Arg)).
assert_header_rdf_aux(Ret, Predicate, datetime, Arg) :-
	parse_http_datetime(Arg, T),
	ret_set(Ret, Predicate, literal(unixtime(T))).

