View source with raw comments or as raw
   1/*  Part of ClioPatria SeRQL and SPARQL server
   2
   3    Author:        Jan Wielemaker
   4    E-mail:        J.Wielemaker@cs.vu.nl
   5    WWW:           http://www.swi-prolog.org
   6    Copyright (C): 2004-2010, University of Amsterdam,
   7			      VU University Amsterdam
   8
   9    This program is free software; you can redistribute it and/or
  10    modify it under the terms of the GNU General Public License
  11    as published by the Free Software Foundation; either version 2
  12    of the License, or (at your option) any later version.
  13
  14    This program is distributed in the hope that it will be useful,
  15    but WITHOUT ANY WARRANTY; without even the implied warranty of
  16    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17    GNU General Public License for more details.
  18
  19    You should have received a copy of the GNU General Public
  20    License along with this library; if not, write to the Free Software
  21    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  22
  23    As a special exception, if you link this library with other files,
  24    compiled with a Free Software compiler, to produce an executable, this
  25    library does not by itself cause the resulting executable to be covered
  26    by the GNU General Public License. This exception does not however
  27    invalidate any other reasons why the executable file might be covered by
  28    the GNU General Public License.
  29*/
  30
  31
  32:- module(rdfs_entailment,
  33	  [
  34	  ]).
  35:- use_module(rdfql(rdfql_runtime)).	% runtime tests
  36:- use_module(library(nb_set)).
  37:- use_module(library('semweb/rdf_db'),
  38	      [ rdf_current_predicate/1,
  39		rdf_global_id/2,
  40		rdf_reachable/3,
  41		rdf_has/3,
  42		rdf_has/4,
  43		rdf_subject/1,
  44		rdf_equal/2,
  45		(rdf_meta)/1,
  46		op(_,_,_)
  47	      ]).
  48:- use_module(library('semweb/rdfs'),
  49	      [ rdfs_subclass_of/2,
  50		rdfs_subproperty_of/2
  51	      ]).
  52
  53/** <module> RDFS entailment
  54
  55The function of an entailment module is  to provide an implementation of
  56rdf/3 that extends basic triple-lookup using the entailment rules of the
  57semantic web sub language of RDF.
  58
  59This entailment module does RDFS entailment.
  60
  61@tbd	Check the completeness
  62*/
  63
  64:- rdf_meta
  65	rdf(o,o,o),
  66	individual_of(r,r).
  67
  68:- public
  69	rdf/3,
  70	rdf/4.
  71
  72rdf(literal(L), _, _) :-		% should move to compiler
  73	nonvar(L), !, fail.
  74rdf(_, literal(L), _) :-		% idem
  75	nonvar(L), !, fail.
  76rdf(S, P, O) :-
  77	var(P), !,
  78	(   rdf_db:rdf(S,P,O)
  79	;   rdf(P, rdf:type, rdf:'Property'),
  80	    rdf(S, P, O),
  81	    \+ rdf_db:rdf(S,P,O)
  82	).
  83rdf(S, P, C) :-
  84	rdf_reachable(rdf:type, rdfs:subPropertyOf, P), !,
  85	individual_of(S, C).
  86rdf(S, P, O) :-					% transitive predicates
  87	rdf_reachable(rdfs:subClassOf, rdfs:subPropertyOf, P), !,
  88	(   (nonvar(S) ; nonvar(O))
  89	->  rdfs_subclass_of(S, O)		% generate from given start
  90	;   individual_of(S, rdfs:'Class'),	% generated unbounded (slow!)
  91	    rdfs_subclass_of(S, O)
  92	).
  93rdf(S, rdfs:subPropertyOf, O) :- !,
  94	(   nonvar(S)
  95	->  individual_of(S, rdf:'Property'),
  96	    rdfs_subproperty_of(S, O)
  97	;   nonvar(O)
  98	->  individual_of(O, rdf:'Property'),
  99	    rdfs_subproperty_of(S, O)
 100	;   individual_of(S, rdf:'Property'),
 101	    rdfs_subproperty_of(S, O)
 102	).
 103rdf(S, serql:directSubClassOf, O) :- !,
 104	rdf_has(S, rdfs:subClassOf, O).
 105rdf(S, serql:directType, O) :- !,
 106	rdf_has(S, rdf:type, O).
 107rdf(S, serql:directSubPropertyOf, O) :- !,
 108	rdf_has(S, rdfs:subPropertyOf, O).
 109rdf(S, P, O) :-
 110	rdf_has(S, P, O).
 111
 112%!	rdf(?S, ?P, ?O, ?G)
 113
 114rdf(S, P, O, G) :-
 115	var(P),
 116	!,
 117	rdf_db:rdf(S, P, O, G).
 118rdf(S, P, O, G) :-
 119	rdf_has(S, P, O, RP),
 120	rdf_db:rdf(S, RP, O, G).
 121
 122
 123%%	individual_of(?Resource, ?Class)
 124
 125individual_of(Resource, Class) :-
 126	nonvar(Resource), !,
 127	(   Resource = literal(_)
 128	->  rdfs_subclass_of(Class, rdfs:'Literal')
 129	;   rdfs_has_type(Resource, MyClass),
 130	    rdfs_subclass_of(MyClass, Class)
 131	;   rdf_equal(Class, rdfs:'Resource')
 132	).
 133individual_of(Resource, Class) :-
 134	nonvar(Class), !,
 135	(   rdf_equal(Class, rdfs:'Resource')
 136	->  rdf_subject(Resource)
 137	;   rdfs_subclass_of(SubClass, Class),
 138	    rdfs_has_type(Resource, SubClass)
 139	).
 140individual_of(Resource, Class) :-
 141	rdf_subject(Resource),
 142	individual_of(Resource, Class).
 143
 144
 145%%	rdfs_has_type(+Resource, -Class) is nondet.
 146%%	rdfs_has_type(-Resource, +Class) is nondet.
 147%
 148%	Perform RDFS entailment rules to enumerate the types of Resource
 149%	or generate all resources entailed  by   the  given  class.
 150
 151rdfs_has_type(Resource, Class) :-
 152	empty_nb_set(Set),
 153	(   atom(Resource)
 154	->  (   rdf_has(Resource, rdf:type, Class)
 155	    ;	rdf_db:rdf(Resource, P, _),
 156		rdf_has(P, rdfs:domain, Class)
 157	    ;	rdf_db:rdf(_, P, Resource),
 158		rdf_has(P, rdfs:range, Class)
 159	    ;	rdf_db:rdf(Resource, rdfs:subPropertyOf, _),
 160		rdf_equal(Class, rdf:'Property')
 161	    ;	rdf_db:rdf(_, rdfs:subPropertyOf, Resource),
 162		rdf_equal(Class, rdf:'Property')
 163	    ;	(   rdf_db:rdf(_,Resource,_)
 164		->  rdf_equal(Class, rdf:'Property'),
 165		    \+ rdf_has(Resource, rdf:type, Class)
 166		)
 167	    ),
 168	    add_nb_set(Class, Set, New),
 169	    New == true
 170	;   atom(Class)
 171	->  (	rdf_has(Resource, rdf:type, Class)
 172	    ;	rdf_has(P, rdfs:domain, Class),
 173		rdf_has(Resource, P, _)
 174	    ;	rdf_has(P, rdfs:range, Class),
 175		rdf_has(_, P, Resource)
 176	    ;	(   rdf_reachable(Class, rdfs:subClassOf, rdf:'Property')
 177		->  rdf_current_predicate(Resource),
 178		    (   rdf(_,Resource,_)
 179		    ->  \+ rdf_has(Resource, rdf:type, Class)
 180		    )
 181		)
 182	    ),
 183	    add_nb_set(Resource, Set, New),
 184	    New == true
 185	;   throw(error(instantiation_error, _))
 186	).
 187
 188
 189		 /*******************************
 190		 *	       REGISTER		*
 191		 *******************************/
 192
 193:- multifile
 194	cliopatria:entailment/2.
 195
 196cliopatria:entailment(rdfs, rdfs_entailment).