4
votes

Trying to list only the knowledge(new triples) by using PelletReasoner through Jena interface. I want only inferred data which is generated after applying the Pallet Reasoner and InfModel from ontology(OWL). Currently, I am listing all the individuals - which contain both asserted and inferred knowledge.

So how can i get only inferred knowledge from owl. 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
Short answer: for some inference models you can use InfModel#getDeductionsModel, but in general, you'd have to iterate through the statements of the InfModel and skip those that are present in the base model, because the deductions model could still repeat things from the base model. Jena's iterators can make that relatively clean, too.Joshua Taylor
@Joshua: Thanks for the quick reply. Looks like Pellet resasoner returns null when getDeductionsModel is used with InfModel. Am I wrong somewhere? I just came across ModelExtractor(Pellet supports) which takes some filter criteria on statementTypes, do you think it is a suitable approach? Please suggest.Vatsal Shah
You're correct; the InfModel for pellet returns null for the deductions model. I mentioned that because for some inference models, getDeductionsModel is a useful thing to have, but it's not universal. (I didn't remember until I checked, though.) In this case, you'll want to iterate through the InfModel statements and filter out those that are in the base model. I've added an example.Joshua Taylor

1 Answers

4
votes

Here's code that creates a simple OntModel with classes B and A, where B ⊑ A, and i is an individual of type B. We're able to infer, e.g., that i is an instance of A, and we'll see that in the results.

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" );
    }
}

The whole output is sort of big, since there are lots purely OWL-based conclusions. The places where we see the purely deduction results most clearly are in the classes and individual that we defined. Notice, for instance, that B is listed as a subclass of itself and of owl:Thing, but not of A (since that was in the original model), and that i has type owl:Thing and A, but not B (since that was in the original model, too):

<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> .