knowsys / rulewerk

Java library based on the VLog rule engine
Apache License 2.0
32 stars 13 forks source link

[examples] Add OWL API usage example #19

Open mkroetzsch opened 6 years ago

mkroetzsch commented 6 years ago

We are still lacking an example for how to use the OWL API for reasoning with OWL. This could be modelled after the evaluations.

mkroetzsch commented 6 years ago

Actually it would be good to have several examples here, e.g, one for CQ answering, and one for computing all atomic class subsumptions.

irina-dragoste commented 6 years ago

added an example of reasoning with an OWL ontology in OwlOntologyToRulesAndFacts (vlog4j-examples module).. This example includes transforming an OWL ontology to rules and facts, reasoning with these rules and facts, and answering atomic queries.

irina-dragoste commented 6 years ago

Detailed description of solving CQ answering using VLog: VLog's Reasoner method QueryResultIterator answerQuery(@NonNull Atom queryAtom, boolean includeBlanks) is not equivalent to a answering a CQ over

To solve CQ answering on an OWL Ontology using VLog:

  1. transform given OWL ontology into a program with rules and facts, using vlog4j-owlapi
  2. Given a conjunctive query

    q=\exists \vec{v} . B[\vec{x}, \vec{v}]

    where B is a conjunction of atoms containing only variables from \vec{x} and \vec{v}, add to the program rule

    Answer_q(\vec{x}) :- B[\vec{x}, \vec{v}]

where Answer_q is a fresh predicate that will contain all the answers over answer variables of q.

  1. run reason() on the reasoner object
  2. ask an atomic query for predicate Answer_q on the reasoner object, with the option of not including blanks:

    answerQuery(Answer_q(\vec{x}), false)

The setting includeBlanks=false is needed because conjunctive queries do not allow mapping answer variables to unnamed individuals (blanks, in our case). The results given by returned QueryResultIterator are the answers to the given conjunctive query q.

irina-dragoste commented 6 years ago

Detailed description of computing all atomic class subsumptions:

  1. transform given OWL ontology into a program with rules and facts, using vlog4j-owlapi
  2. obtain all class names of given ontology using OWL API library (should be equivalent to all unary predicates of the program)
  3. for each pair of class names A and B, we know that the ontology (program) entails that A is a subclass of B if, in all models of the ontology (program), for all individuals (constants or blanks) c such that A(c) belongs to the model, the fact B(c) also belongs to the model. Thus, to determine whether A is a subclass of B, we compute the most general model (i.e. the model with the least restrictions), such that i) there is at least one individual in the subclass A, ii) the model can be homomorphycally embeded into all other models for which there is at least one individual in the subclass A:
    • add a fresh individual to the subclass A (create a fresh constant constant constA, and add fresh fact A(constA) to the program)
    • load all rules and facts to the reasoner object (initial program facts + fresh fact)
    • run reason()
    • if, for each answer c of answerQuery(A(x), true), the boolean query answerQuery(B(c), true) (or, in this case, answerQuery(B(c), false) should give the same answers) has results, then A is a subclass of B.
    • make sure to remove the fresh fact from the reasoner, before determining subsumption for the first pair of classes (in the current version of VLog, just create a new reasoner instance).
      1. display all <subclass, superclass> pairs.