:- ensure_loaded(common).
cvs_id('$Id: property_views.pl,v 1.12 2005/04/14 00:01:29 sandro Exp $').
/*

    Views which just naively show properties of things....
  
*/


%tab(8001, Q, Focus, 'Resource :: Side-by-Side Properties', +side_by_side_table_of_properties(Q, Focus)).
side_by_side_table_of_properties(Q, Focus, HTree) :-
	inboundt(Q, Focus, TriplesIn),
	outboundt(Q, Focus, TriplesOut),
	This = span(class=semWalkerThis, +word(Q, Focus)),
	HTree = [
		 % h3(+word(Q, Focus)),
		 +joint_table(TriplesIn, This, TriplesOut)
		].


joint_table([], _This, TriplesOut, HTree) :-
	HTree = table(class=semWalkerOPTable, width='100%',
		      tr(td(width='35%', valign=top, '*'),
			 td(valign=top, '*'),
			 td(valign=top,
			    table(class=semWalkerPTable,
				  %tr(th('>-- property -->')),
				  TriplesOut))
			)).

joint_table(TriplesIn, _This, TriplesOut, HTree) :-
	HTree = table(class=semWalkerOPTable, width='100%',
		      tr(th(width='35%',' - incoming arcs ->'),th('*'),th(' - outgoing arcs ->')),
		      tr(td(valign=top,
			    table(class=semWalkerPTable,
				  %tr(th([]), th('>-- property -->')),
				  TriplesIn)),
			 % td(valign=top, This),
			 td([]),
			 td(valign=top,
			    table(class=semWalkerPTable,
				  %tr(th('>-- property -->')),
				  TriplesOut))
			)).


tab(9600, Q, Focus, 'Resource', +table_of_properties(Q, Focus)).
table_of_properties(Q, Focus, div(class=foo, HTree)) :-
	inboundt3(Q, Focus, TriplesIn),
	outboundt(Q, Focus, TriplesOut),
	%This = span(class=semWalkerThis, +word(Q, Focus)),
	debug(property_views, 'TriplesIn: ~q', [TriplesIn]),
	(   TriplesIn = [[], []]
	->  HPlus = [ p('No related ("inbound") statements found.') ]
	;   HPlus = [ h3('Related ("Inbound") Statements'),
		      table(tr(th(align=left, 'Subject'),
			       th(align=left, 'Property'),
			       th(align=left, 'Value')),
			    TriplesIn)
		    ],
	    debug(property_views, 'TriplesIn: ~q', TriplesIn)
	),
	!, 
	HTree = [table(tr(th(align=left, 'Property'), th(align=left, 'Value')),
		       TriplesOut),
		 HPlus
		].


outboundt(Q, Focus, [Text, Extra]) :-
	setofsome(200,Done,x(P,O),
		rdfq(Q, Focus, P, O),
		Triples
	       ),
	(   Done
	->  Extra = []
	;   Extra = tr(th('(Listing stopped at 200 properties)'))
	),
	findall(tr(td(class=semWalkerPTableCellp, +word(Q, P)),
		   td(class=semWalkerPTableCell, +word(Q, O))),
		member(x(P,O), Triples),
		Text).

inboundt(Q, Focus, [Text, Extra]) :-
	setofsome(1000,_,x(S,P),
		rdfq(Q, S,P,Focus),
		UnsortedTriples
	       ),
	length(UnsortedTriples, Len),
	(   Len == 1000
	->  Extra = tr(th('(Listing stopped at 1000 properties)'))
	;   Extra = []
	),
	sort(UnsortedTriples, Triples),
	findall(tr(td(class=semWalkerPTableCell, +word(Q, S)),
		   td(class=semWalkerPTableCellp, +word(Q, P))),
		member(x(S,P), Triples),
		Text).

inboundt2(Q, Focus, [Text, Extra]) :-
	setofsome(200,Done,x(S,P),
		  rdfq(Q,S,P,Focus),
		  Triples
	       ),
	(   Done
	->  Extra = []
	;   Extra = tr(th('(Listing stopped at 200 properties)'))
	),
	findall(tr(td(class=semWalkerPTableCellp, +word(Q, P)),
		   td(class=semWalkerPTableCell, +word(Q, S))),
		  member(x(S,P), Triples),
		Text).


inboundt3(Q, Focus, [Text, Extra]) :-
	setofsome(200,Done,x(S,P),
		rdfq(Q, S,P,Focus),
		Triples
	       ),
	(   Done
	->  Extra = []
	;   Extra = tr(th('(Listing stopped at 200 properties)'))
	),
	findall(tr(td(class=semWalkerPTableCell, +word(Q, S)),
		   td(class=semWalkerPTableCellp, +word(Q, P)),
		   td(class=semWalkerPTableCell, +word(Q, Focus))
		  ),
		  member(x(S,P), Triples),
		Text).
/*
html_view('List of Properties', page, Focus, Q, HTree) :-
	outbound(Q, Focus, Triples),
	inbound(Q, Focus, Triples2),
	HTree = [
		    h3(+word(Q, Focus)),
		    Triples,
		    Triples2
		].

outbound(Q, Focus, Text) :-
	findsome(1000,0,x(P,O),
		rdfq(Q, Focus, P, O),
		UnsortedTriples),
	(   UnsortedTriples = []
	->  Text = p('No information about this subject.   (Perhaps some term is  misspelled?)')
	;   sort(UnsortedTriples, Triples),
	    findall(p('has ', +word(Q, P),
		      ' ', +word(Q, O)),
		    member(x(P,O), Triples),
		    Text)
	).
	
inbound(Q, Focus, Text) :-
	findsome(1000,0,x(P,S),
		rdfq(Q, S, P, Focus),
		UnsortedTriples),
	(   UnsortedTriples = []
	->  Text = ''
	;   sort(UnsortedTriples, Triples),
	    findall(p('is ',
		      +word(Q, P),
		      ' of ',
		      +word(Q, S)),
		    member(x(P,S), Triples),
		    Text)
	).

xxhtml_view('Detailed List of Properties', page, Focus, Q, HTree) :-
	findall(
		p('has ', +word(Q, P), ' ', +para(Q, O)),
		(rdfq(Q, Focus, P, O)),
		Out),
	sort(Out, Out2),
	findall(
		p('is ', +word(Q, P), ' of ', +para(Q, S)),
		(rdfq(Q, S, P, Focus)),
		In),
	sort(In, In2),
	HTree = [ h3(+word(Q, Focus)),  Out2, In2 ].

*/

