:- ensure_loaded(common).
cvs_id('$Id: looks_like_ontology.pl,v 1.11 2004/10/27 16:50:31 sandro Exp $').
/*

   foo....    
  
*/

:- ensure_loaded(library(debug)).
% :- debug(llo).

% kinda stumbling in the dark here.... not sure the right
% way to do this right now.

/*
mark_all :-
	% reload_cache_metadata,
	forall(rdfpage(Ret, retr:graph, Graph),
	       (   maybe_mark_as_ontology(Ret, Graph),
		   rdfpage_sub_uri(Page, _, Ret),
		   rdfpage_qsave(Page)
	       )
	      ).
*/

maybe_mark_as_ontology(Ret) :-
	forall(   ret_get(Ret, retr:triples, _),
		  (   once((ret_get(Ret, retr:chunk, Graph),
			    looks_like_ontology(Graph, S, P, O)))
		  ->  debug(llo, 'marking as ontology: ~q ~q ~q', [S, P, O]),
		      mark_as_ontology(Ret, Graph, S, P, O)
		  ;   debug(llo, 'not marking as ontology', [])
		  )
	      ).

mark_as_ontology(Ret, Graph, S, P, O) :-
	/* reading these tiny proof-graphs back in is rather slow....
        Perhaps we should log them to a prolog file (which can be compiled)
        like ret_set does?  vrdf_assert/4, ...?  */
	ret_get(Ret, retr:finalResource, Address),
	rdfpage_gen_uri(Proof),
	rdf_global_id(S, SL),
	rdf_global_id(P, PL),
	rdf_global_id(O, OL),
	rdfpage_assert(Proof, rdf:subject, SL),
	rdfpage_assert(Proof, rdf:predicate, PL),
	rdfpage_assert(Proof, rdf:object, OL),
	rdf_transaction((
			 rdf_assert(Graph, rdf:type, owl:'Ontology', Proof),
			 rdf_assert(Address, rdf:type, owl:'Ontology', Proof)
			)),
	rdfpage_qsave(Proof),
	ret_set(Ret, retr:inference, Proof).

/*   we used to put the proof in another graph.   Nahhhh.   
mark_as_ontology(Ret, Graph, S, P, O) :-	
	rdfpage_gen_uri(Proof),
	rdfpage_assert(Ret, retr:inference, Proof),
	rdf_assert(Graph, rdf:type, owl:'Ontology', Proof),
	rdf_global_id(S, SL),
	rdf_global_id(P, PL),
	rdf_global_id(O, OL),
	rdfpage_assert(Proof, rdf:subject, SL),
	rdfpage_assert(Proof, rdf:predicate, PL),
	rdfpage_assert(Proof, rdf:object, OL),
	rdfpage_qsave(Proof).
*/

looks_like_ontology(G, Source, rdf:type, owl:'Ontology') :-
	rdfpage(Ret, retr:chunk, G),
	(   rdfpage(Ret, retr:finalResource, Source)
	;   rdfpage(Ret, retr:initialResource, Source)
	),
	rdf(Source, rdf:type, owl:'Ontology', G).
looks_like_ontology(G, Subj, rdf:type, X) :-
	rdf(Subj, rdf:type, X, G),
	owl_class(Y),
	rdf_global_id(Y, X).
looks_like_ontology(G, Subj, X, Obj) :-
	rdf(Subj, X, Obj, G),
	owl_property(Y),
	rdf_global_id(Y, X).
looks_like_ontology(G, Subj, rdf:type, X) :-
	rdf(Subj, rdf:type, X, G),
	rdfs_class(Y),
	rdf_global_id(Y, X).
looks_like_ontology(G, Subj, X, Obj) :-
	rdf(Subj, X, Obj, G),
	rdfs_property(Y),
	rdf_global_id(Y, X).



% from http://www.w3.org/TR/owl-ref/#appC

% owl:AllDifferent (might well be used outside of an ontology...?)
owl_class(owl:'AnnotationProperty').
owl_class(owl:'Class').
owl_class(owl:'DataRange').
owl_class(owl:'DatatypeProperty').
owl_class(owl:'DeprecatedClass').
owl_class(owl:'DeprecatedProperty').
owl_class(owl:'FunctionalProperty').
owl_class(owl:'InverseFunctionalProperty').
% owl:Nothing
owl_class(owl:'ObjectProperty').
% owl:Ontology   (saying something else is an ontology doesn't make you one)
owl_class(owl:'OntologyProperty').
owl_class(owl:'Restriction').
owl_class(owl:'SymmetricProperty').
owl_class(owl:'Thing').
owl_class(owl:'TransitiveProperty').

%owl_property(owl:allValuesFrom).
%owl_property(owl:backwardCompatibleWith).
owl_property(owl:cardinality).
owl_property(owl:complementOf).
%owl_property(owl:differentFrom).
owl_property(owl:disjointWith).
owl_property(owl:distinctMembers).
owl_property(owl:equivalentClass).
owl_property(owl:equivalentProperty).
owl_property(owl:hasValue).
%owl_property(owl:imports).
%owl_property(owl:incompatibleWith).
owl_property(owl:intersectionOf).
owl_property(owl:inverseOf).
owl_property(owl:maxCardinality).
owl_property(owl:minCardinality).
owl_property(owl:oneOf).
owl_property(owl:onProperty).
%owl_property(owl:priorVersion).
%owl_property(owl:sameAs).
owl_property(owl:someValuesFrom).
owl_property(owl:unionOf).
%owl_property(owl:versionInfo).

% from http://www.w3.org/TR/2004/REC-rdf-schema-20040210/

%rdfs_class(rdfs:'Resource').
rdfs_class(rdfs:'Class').
%rdfs_class(rdfs:'Literal').
%rdfs_class(rdfs:'Datatype').
%rdfs_class(rdf:'XMLLiteral').
rdfs_class(rdf:'Property').

rdfs_property(rdfs:range).
rdfs_property(rdfs:domain).
%rdfs_property(rdf:type).
rdfs_property(rdfs:subClassOf).
rdfs_property(rdfs:subPropertyOf).
%rdfs_property(rdfs:label).
%rdfs_property(rdfs:comment).

