:- ensure_loaded(common).
:- rdf_register_ns(rs, 'http://www.w3.org/2005/6/rule_systems#').

%  This isn't really a part of Ontaria/Semwalker at all, but ... ummm
%  putting it here is the easiest thing for me!
%
%            -- sandro
%
%    [ la la la! ]
%
% :- discontiguous title/2, org/3.

:- ensure_loaded(library(odbc)).
:- ensure_loaded(html).

web_server_page(rwspcX, x).

std_head2(Q, Head, Title) :-
	memberchk(request_id(ReqId), Q),
	ensure_content_type_sent(ReqId, 'text/html'),
	Head = [title(Title),
		link(rel='stylesheet', href='http://www.w3.org/StyleSheets/base.css'),
		link(rel='stylesheet', href='http://www.w3.org/2004/12/rules-ws/style.css')
	       ].



x(Q) :-
	findall(feature(Supers, Depth, Feature),
		(   has(_, Feature),
		    fstack(Feature, RevSupers),
		    reverse(RevSupers, Supers),
		    length(Supers, Depth)
		),
		FeaturesUnsorted),
	sort(FeaturesUnsorted, Features),
	max_depth(Features, MaxDepth),

	std_head2(Q, Head, 'Foo'),
	Body =[ h2('Rule System Feature Matrix'),
		table(+matrix_head(Q, Systems, MaxDepth),
		      +matrix_rows(Q, Systems, Features, MaxDepth))
	      ],
	write_html(html(head(Head), body(Body))).

platform_name(n3).
platform_name(swrl).
application_name(brain_structures).
application_name(data_integration).

max_depth([], 0).
max_depth([feature(_, Depth, _) | Fs], Max) :-
	max_depth(Fs, SubMax),
	(  Depth > SubMax
	-> Max = Depth
	;  Max = SubMax
	).
	
matrix_head(Q, Systems, MaxDepth, H) :-
	findall(P, platform_name(P), Plats),
	length(Plats, PlatCount),
	findall(P, application_name(P), Apps),
	length(Apps, AppCount),
	append(Plats, Apps, Systems),
	MaxDepthPlus1 is MaxDepth + 1,
	H = [ tr(td(colspan=MaxDepthPlus1, []),
		 th(colspan=PlatCount, 'Platforms'),
		 th(colspan=AppCount, 'Applications')
		),
	      tr(td(colspan=MaxDepthPlus1, []),
		 +forall(member(P, Plats), th(P)),
		 +forall(member(P, Apps), th(P))
		)
	    ].

has(n3, programmer_syntax).
has(swrl, foo).

ff(consyntax, programmer_syntax).

ff(syn, consyntax).

has(S, F) :-
	ff(F, X),
	has(S, X).

fstack(Feature, []) :-
	\+ ff(_, Feature).
fstack(Feature, [Super | Supers]) :-
	ff(Super, Feature),
	once(fstack(Super, Supers)).
	

matrix_rows(Q, Systems, Features, MaxDepth, H) :-
	H = +forall(member(Feature, Features), tr(+feature(Q, MaxDepth, Feature), +data(Q, Feature, Systems))).

feature(Q, MaxDepth, feature(Supers, Depth, Feature), H) :-
	Remainder is (MaxDepth + 1) - Depth,
	H = [ % +if(Depth > 0, td(colspan=Depth, 'x')),
	      +forall(between(1, Depth, _), td(' ')),
	      th(align=left, colspan=Remainder, Feature)
	    ].
	
data(Q, Feature, Systems, H) :-
	H = +forall(member(System, Systems),
		   +ifelse(has(System, Feature), td('Yes'), td('-'))).


			  
 