rhetoric_ns('http://www.cwi.nl/~media/ns/rhetoric#').

g :-
	load_rdf('chiaroscuro.rhetoric.rdf', Museum),
	process_triples(Museum, [[],[]], [Mlist,Rlist]),
	rlist2tree(Rlist,[],Result),
	write(Mlist), nl,
	write(Result), nl.

rlist2tree([], Result, Result).
rlist2tree([rhetoric(_Id, Attlist)|Rlist], [], Result) :-
	select(nucleus:Nuc, Attlist, Attlist2),
	select(satellite:Sat, Attlist2, Attlist3),
	select(type:Type, Attlist3, _Attrs),
        rlist2tree(Rlist, rhetoric(Nuc, [sat(Type,Sat)]), Result).

rlist2tree([rhetoric(_Id, Attlist)|Rlist], rhetoric(Nuc, Sats), Result) :-
	select(nucleus:Nuc, Attlist, Attlist2),
	select(satellite:Sat, Attlist2, Attlist3),
	select(type:Type, Attlist3, _Attrs),
	rlist2tree(Rlist, rhetoric(Nuc, [sat(Type,Sat)|Sats]), Result).

rlist2tree([rhetoric(_Id, Attlist)|Rlist], [], Result) :-
        select(type:Type, Attlist, Attrs),
	Newterm =.. [Type,Attrs],
        rlist2tree(Rlist, rhetoric(Newterm), Result).

rlist2tree([rhetoric(Id, Attlist)|Rlist], rhetoric(Nuc, Sats), Result) :-
	select(sat(SatType, Id), Sats, OtherSats),
        select(type:Type, Attlist, Attrs),
	Newterm =.. [Type,Attrs],
        rlist2tree(Rlist, rhetoric(Nuc, [sat(SatType,rhetoric(Newterm))|OtherSats]), Result).
	
% Ready!  Assign results in second parameter to last parameter
process_triples([], Result, Result).

% Media Item, e.g rdf('id', rdf:type, namespace#mediaItem).
process_triples([rdf(Id, rdf:type, MediaItem)|T], [Mlist, Rlist], Result) :-
	rhetoric_ns(Rhetoric_ns), 
	sub_string(MediaItem, 0,  L1, _A1, Rhetoric_ns),
	S2 is L1-1,
	sub_string(MediaItem, S2, _L2, _A2, '#mediaItem'),
	process_triples(T,[[media(Id,[])|Mlist],Rlist], Result).

% Media Attributes, e.g rdf('id', namespace:width, literal(100)).
process_triples([rdf(Id, Ns:MediaAtt, literal(Value))|T], [Mlist, Rlist], Result) :-
	rhetoric_ns(Ns),
	select(media(Id,Attlist), Mlist, OtherItems),
	process_triples(T,[[media(Id,[MediaAtt:Value|Attlist])|OtherItems],Rlist], Result).

% RST nucleus/satellite relation
process_triples([rdf(Id, rdf:type, RstRel)|T], [Mlist, Rlist], Result) :-
	rhetoric_ns(Rhetoric_ns), 
        sub_string(RstRel, 0,  L1, _A1, Rhetoric_ns),
        S2 is L1-1,
	sub_string(RstRel, S2, _L2, _A2, '#nsRelation'),
	process_triples(T,[Mlist, [rhetoric(Id,[])|Rlist]],Result).

% RST multi nucleus relation
process_triples([rdf(Id, rdf:type, RstRel)|T], [Mlist, Rlist], Result) :-
	rhetoric_ns(Rhetoric_ns), 
        sub_string(RstRel, 0,  L1, _A1, Rhetoric_ns),
        S2 is L1-1,
        sub_string(RstRel, S2, _L2, _A2, '#mnRelation'),
	process_triples(T,[Mlist, [rhetoric(Id,[])|Rlist]],Result).

% RST nucleus or satellite:
process_triples([rdf(Id, Namespace:Type, Value)|T], [Mlist,Rlist], Result) :-
        rhetoric_ns(Namespace),
	member(Type, [nucleus, satellite]),
	select(rhetoric(Id,Attlist), Rlist, OtherRels),
	process_triples(T, [Mlist, [rhetoric(Id,[Type:Value|Attlist])|OtherRels]], Result).

% RST Relation type names, e.g  rdf('id', namespace:type, literal(sequence)).
process_triples([rdf(Id, Ns:type, literal(Value))|T], [Mlist, Rlist], Result) :-
	rhetoric_ns(Ns),
	select(rhetoric(Id,Attlist), Rlist, OtherRels),
	process_triples(T, [Mlist, [rhetoric(Id,[type:Value|Attlist])|OtherRels]], Result).

% Error, no matching rules above :-(
process_triples([rdf(S,P,V)|_T], [Mlist, Rlist], _Result) :-
	write('no match on '),
	write([S,P,V]), nl,
	write([Mlist, Rlist]), nl.
