如何从Jena Ontology模型中获取唯一的推断数据

时间:2014-07-31 09:23:16

标签: rdf jena owl inference pellet

尝试通过Jena界面使用PelletReasoner仅列出知识(新三元组)。我只想要从本体( OWL )应用托盘推理器 InfModel 后生成的推断数据。目前,我列出了所有个人 - 包含断言和推断的知识。

那么我怎样才能从猫头鹰那里获得唯一的推断知识。 Thanx in Advance ..

//Inference Logic//

    com.hp.hpl.jena.reasoner.Reasoner reasoner = org.mindswap.pellet.jena.PelletReasonerFactory.theInstance().create();

    Model infModel = ModelFactory.createInfModel(reasoner, ModelFactory.createDefaultModel());

    OntModel model = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM, infModel);

    try
    {
        inputStream = new FileInputStream(OWL_File);
    }
    catch (FileNotFoundException p)
    {
        p.printStackTrace();
    }
    model.read(inputStream, null, "RDF/XML");

1 个答案:

答案 0 :(得分:3)

这是创建一个简单的OntModel的代码,其中包含B和A类,其中B⊑A,i是B类的个体。我们能够推断出,例如,我是A的一个实例,我们'我会在结果中看到。

import org.mindswap.pellet.jena.PelletReasonerFactory;

import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.rdf.model.InfModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.impl.StmtIteratorImpl;
import com.hp.hpl.jena.util.iterator.ExtendedIterator;
import com.hp.hpl.jena.util.iterator.Filter;

public class JustTheDeductionsPlease {
    public static void main(String[] args) {
        // Create the base model.  B is a subclass of A, and i is an instance of B.
        String NS = "http://example.org/";
        final OntModel model = ModelFactory.createOntologyModel( OntModelSpec.OWL_DL_MEM );
        OntClass a = model.createClass( NS+"A" );
        OntClass b = model.createClass( NS+"B" );
        a.addSubClass( b );
        model.createIndividual( NS+"i", b );

        // Create the inference model.
        InfModel pModel = ModelFactory.createInfModel( PelletReasonerFactory.theInstance().create(), model);

        // An iterator over the statements of pModel that *aren't* in the base model.
        ExtendedIterator<Statement> stmts = pModel.listStatements().filterDrop( new Filter<Statement>() {
            @Override
            public boolean accept(Statement o) {
                return model.contains( o );
            }
        });

        // For convenient printing, we create a model for the deductions.
        // (If stmts were a StmtIterator, we could add it directly.  It's not,
        // so we end up creating a new StmtIteratorImpl, which is kludgey, but
        // it's more efficient than converting the thing to a list.)
        Model deductions = ModelFactory.createDefaultModel().add( new StmtIteratorImpl( stmts ));
        deductions.write( System.out, "TTL" );
    }
}

整个产量有点大,因为有很多纯粹基于OWL的结论。我们看到纯粹演绎结果最明显的地方是我们定义的类和个体。请注意,例如,B被列为其自身和owl的子类:Thing,但不是A(因为那是在原始模型中),并且我有类型owl:Thing和A,但不是B(因为也是在原始模型中):

<http://example.org/A>
      <http://www.w3.org/2000/01/rdf-schema#subClassOf>
              <http://www.w3.org/2002/07/owl#Thing> , <http://example.org/A> ;
      <http://www.w3.org/2002/07/owl#disjointWith>
              <http://www.w3.org/2002/07/owl#Nothing> ;
      <http://www.w3.org/2002/07/owl#equivalentClass>
              <http://example.org/A> .

<http://example.org/B>
      <http://www.w3.org/2000/01/rdf-schema#subClassOf>
              <http://www.w3.org/2002/07/owl#Thing> , <http://example.org/B> ;
      <http://www.w3.org/2002/07/owl#disjointWith>
              <http://www.w3.org/2002/07/owl#Nothing> ;
      <http://www.w3.org/2002/07/owl#equivalentClass>
              <http://example.org/B> .

<http://example.org/i>
      a       <http://www.w3.org/2002/07/owl#Thing> , <http://example.org/A> ;
      <http://www.w3.org/2002/07/owl#sameAs>
              <http://example.org/i> .