简体   繁体   中英

OWL Inferences With Jena (Turtle format)

I'm trying to make OWL inferences with Jena. As a start, my goal is merely to infer that if an educational institution is of type dbo:EducationalInstitution , then it is also dbo:institution

Here's the java code (adapted from the jena doc) :

package test;

import org.apache.jena.util.FileManager;
import org.apache.jena.util.PrintUtil;
import org.apache.jena.rdf.model.*;
import org.apache.jena.reasoner.Reasoner;
import org.apache.jena.reasoner.ReasonerRegistry;  

public class Test {
    public static void main(String[] args){
        Model schema = FileManager.get().loadModel("file:/home/mica/Downloads/sparql/ontology.ttl");
        Model data = FileManager.get().loadModel("file:/home/mica/Downloads/sparql/result.ttl");
        Reasoner reasoner = ReasonerRegistry.getOWLReasoner();
        reasoner = reasoner.bindSchema(schema);
        InfModel infmodel = ModelFactory.createInfModel(reasoner, data);

        Resource nForce = infmodel.getResource("<0762762P>");
        System.out.println(nForce);
        System.out.println("nForce *:");
        printStatements(infmodel, nForce, null, null);
}
    public static void printStatements(Model m, Resource s, Property p, Resource o) {
        for (StmtIterator i = m.listStatements(s,p,o); i.hasNext(); ) {
            Statement stmt = i.nextStatement();
            System.out.println(" - " + PrintUtil.print(stmt));
        }
    }

}

An example of my data :

@prefix dbo:  <http://dbpedia.org/ontology/> .
@prefix ex:  <http://ex.org/a#> .
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .
@prefix dbf:  <http://fr.dbpedia.org/resource/> .
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

<0762762P>  rdf:type  dbo:Event ;
        dbo:status  "En cours" ;
        dbo:Place   _:b0 .

_:b0    dbf:Ville                   "Rouen" ;
    rdf:type dbo:EducationalInstitution ;
        foaf:name                   "Université du Havre" .

And the following ontology:

@prefix dbo:  <http://dbpedia.org/ontology/> .
@prefix ex:  <http://ex.org/a#> .
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#'> .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .
@prefix dbf:  <http://fr.dbpedia.org/resource/> .
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .

dbo:Event rdf:type owl:Class .
dbo:Place rdf:type owl:ObjectProperty ;
          rdfs:domain dbo:Event ;
          rdfs:range dbo:Event .
dbo:institution rdf:type owl:Class .
dbo:EducationalInstitution rdf:type owl:Class ;
                           rdfs:subClassOf dbo:institution .
dbo:Place rdf:type dbo:EducationalInstitution .

I'm not getting the expected results: nForce *:

Your whole ontology is strange:

  1. You're reusing the DBpedia ontology in which dbo:Place is already defined as a class, but you're using it as an object property.
  2. Next, you declared the range of this object property to be an dbo:Event . Semantically, this also sound strange.
  3. You have a triple which assigns dbo:Place to be an instance of class dbo:EducationalInstitution . That means, in the end dbo:Place is a class, a property and an individual. This is really bad modelling.

Other conventions:

  • reusing ontologies is good, but you should not reuse the namespace but use your own namespace
  • naming convention:
    • for classes CamelCase style
    • for properties lowerCamelCase style

Why don't you use a more appropriate modelling like this:

@prefix dbo:  <http://dbpedia.org/ontology/> .
@prefix ex:  <http://ex.org/a#> .
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#'> .
@prefix owl: <http://www.w3.org/2002/07/owl#> .

dbo:Event rdf:type owl:Class .
ex:takesPlaceAt rdf:type owl:ObjectProperty ;
          rdfs:domain dbo:Event ;
          rdfs:range dbo:Place .
ex:Institution rdf:type owl:Class .
ex:EducationalInstitution rdf:type owl:Class ;
                           rdfs:subClassOf ex:Institution .
dbo:EducationalInstitution rdfs:subClassOf dbo:Place .

Regarding the data:

_:b0    dbf:Ville                   "Rouen" ;
    rdf:type ex:EducationalInstitution ;
        foaf:name                   "Université du Havre" .

DBpedia ontology has propertiesThat's for sure wrong.

  • dbf:Ville is not a property but an individual. Why do you use it as a property? The Dbpedia ontology has a property dbo:city . And the resource for Rouen exists already.

Maybe you could do it like this:

@prefix dbo:  <http://dbpedia.org/ontology/> .
@prefix ex:  <http://ex.org/a#> .
@prefix rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix xsd:  <http://www.w3.org/2001/XMLSchema#> .
@prefix dbr:  <http://dbpedia.org/resource/> .
@prefix foaf:  <http://xmlns.com/foaf/0.1/> .

<0762762P>  rdf:type  dbo:Event ;
        dbo:status  "En cours" ;
        dbo:Place   _:b0 .

_:b0    dbo:city                 http://dbpedia.org/resource/Rouen ;
    rdf:type ex:EducationalInstitution ;
        foaf:name                   "Université du Havre" .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM