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@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2010-2018, University of Amsterdam,
    7                              VU University Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(rdfs_entailment,
   37          [
   38          ]).   39:- use_module(rdfql(rdfql_runtime)).    % runtime tests
   40:- use_module(library(nb_set)).   41:- use_module(library('semweb/rdf_db'),
   42              [ rdf_current_predicate/1,
   43                rdf_global_id/2,
   44                rdf_reachable/3,
   45                rdf_has/3,
   46                rdf_has/4,
   47                rdf_subject/1,
   48                rdf_equal/2,
   49                (rdf_meta)/1,
   50                op(_,_,_)
   51              ]).   52:- use_module(library('semweb/rdfs'),
   53              [ rdfs_subclass_of/2,
   54                rdfs_subproperty_of/2
   55              ]).

RDFS entailment

The function of an entailment module is to provide an implementation of rdf/3 that extends basic triple-lookup using the entailment rules of the semantic web sub language of RDF.

This entailment module does RDFS entailment.

To be done
- Check the completeness */
   68:- rdf_meta
   69        rdf(o,o,o),
   70        individual_of(r,r).   71
   72:- public
   73    rdf/3,
   74    rdf/4.   75
   76rdf(literal(L), _, _) :-                % should move to compiler
   77    nonvar(L), !, fail.
   78rdf(_, literal(L), _) :-                % idem
   79    nonvar(L), !, fail.
   80rdf(S, P, O) :-
   81    var(P),
   82    !,
   83    (   rdf_db:rdf(S,P,O)
   84    ;   rdf(P, rdf:type, rdf:'Property'),
   85        rdf(S, P, O),
   86        \+ rdf_db:rdf(S,P,O)
   87    ).
   88rdf(S, P, C) :-
   89    rdf_reachable(rdf:type, rdfs:subPropertyOf, P),
   90    !,
   91    individual_of(S, C).
   92rdf(S, P, O) :-                                 % transitive predicates
   93    rdf_reachable(rdfs:subClassOf, rdfs:subPropertyOf, P),
   94    !,
   95    (   (nonvar(S) ; nonvar(O))
   96    ->  rdfs_subclass_of(S, O)              % generate from given start
   97    ;   individual_of(S, rdfs:'Class'),     % generated unbounded (slow!)
   98        rdfs_subclass_of(S, O)
   99    ).
  100rdf(S, rdfs:subPropertyOf, O) :-
  101    !,
  102    (   nonvar(S)
  103    ->  individual_of(S, rdf:'Property'),
  104        rdfs_subproperty_of(S, O)
  105    ;   nonvar(O)
  106    ->  individual_of(O, rdf:'Property'),
  107        rdfs_subproperty_of(S, O)
  108    ;   individual_of(S, rdf:'Property'),
  109        rdfs_subproperty_of(S, O)
  110    ).
  111rdf(S, serql:directSubClassOf, O) :-
  112    !,
  113    rdf_has(S, rdfs:subClassOf, O).
  114rdf(S, serql:directType, O) :-
  115    !,
  116    rdf_has(S, rdf:type, O).
  117rdf(S, serql:directSubPropertyOf, O) :-
  118    !,
  119    rdf_has(S, rdfs:subPropertyOf, O).
  120rdf(S, P, O) :-
  121    rdf_has(S, P, O).
 rdf(?S, ?P, ?O, ?G)
  125rdf(S, P, O, G) :-
  126    var(P),
  127    !,
  128    rdf_db:rdf(S, P, O, G).
  129rdf(S, P, O, G) :-
  130    rdf_has(S, P, O, RP),
  131    rdf_db:rdf(S, RP, O, G).
 individual_of(?Resource, ?Class)
  136individual_of(Resource, Class) :-
  137    nonvar(Resource),
  138    !,
  139    (   Resource = literal(_)
  140    ->  rdfs_subclass_of(Class, rdfs:'Literal')
  141    ;   rdfs_has_type(Resource, MyClass),
  142        rdfs_subclass_of(MyClass, Class)
  143    ;   rdf_equal(Class, rdfs:'Resource')
  144    ).
  145individual_of(Resource, Class) :-
  146    nonvar(Class),
  147    !,
  148    (   rdf_equal(Class, rdfs:'Resource')
  149    ->  rdf_subject(Resource)
  150    ;   rdfs_subclass_of(SubClass, Class),
  151        rdfs_has_type(Resource, SubClass)
  152    ).
  153individual_of(Resource, Class) :-
  154    rdf_subject(Resource),
  155    individual_of(Resource, Class).
 rdfs_has_type(+Resource, -Class) is nondet
rdfs_has_type(-Resource, +Class) is nondet
Perform RDFS entailment rules to enumerate the types of Resource or generate all resources entailed by the given class.
  164rdfs_has_type(Resource, Class) :-
  165    empty_nb_set(Set),
  166    (   atom(Resource)
  167    ->  (   rdf_has(Resource, rdf:type, Class)
  168        ;   rdf_db:rdf(Resource, P, _),
  169            rdf_has(P, rdfs:domain, Class)
  170        ;   rdf_db:rdf(_, P, Resource),
  171            rdf_has(P, rdfs:range, Class)
  172        ;   rdf_db:rdf(Resource, rdfs:subPropertyOf, _),
  173            rdf_equal(Class, rdf:'Property')
  174        ;   rdf_db:rdf(_, rdfs:subPropertyOf, Resource),
  175            rdf_equal(Class, rdf:'Property')
  176        ;   (   rdf_db:rdf(_,Resource,_)
  177            ->  rdf_equal(Class, rdf:'Property'),
  178                \+ rdf_has(Resource, rdf:type, Class)
  179            )
  180        ),
  181        add_nb_set(Class, Set, New),
  182        New == true
  183    ;   atom(Class)
  184    ->  (   rdf_has(Resource, rdf:type, Class)
  185        ;   rdf_has(P, rdfs:domain, Class),
  186            rdf_has(Resource, P, _)
  187        ;   rdf_has(P, rdfs:range, Class),
  188            rdf_has(_, P, Resource)
  189        ;   (   rdf_reachable(Class, rdfs:subClassOf, rdf:'Property')
  190            ->  rdf_current_predicate(Resource),
  191                (   rdf(_,Resource,_)
  192                ->  \+ rdf_has(Resource, rdf:type, Class)
  193                )
  194            )
  195        ),
  196        add_nb_set(Resource, Set, New),
  197        New == true
  198    ;   throw(error(instantiation_error, _))
  199    ).
  200
  201
  202                 /*******************************
  203                 *             REGISTER         *
  204                 *******************************/
  205
  206:- multifile
  207    cliopatria:entailment/2.  208
  209cliopatria:entailment(rdfs, rdfs_entailment)