Search in sources :

Example 36 with OntModel

use of org.apache.jena.ontology.OntModel in project Vitro by vivo-project.

the class SimpleReasonerSameAsTest method tBoxInverseAssertion1.

/*
	 * adding and removing an inverseOf assertion for individuals who
	 * are sameAs each other.
	 */
@Test
public void tBoxInverseAssertion1() throws InterruptedException {
    OntModel tBox = createTBoxModel();
    OntProperty P = createObjectProperty(tBox, "http://test.vivo/P", "property P");
    OntProperty Q = createObjectProperty(tBox, "http://test.vivo/Q", "property Q");
    // Create ABox and Inference models and register
    // the ABox reasoner listeners with the ABox and TBox
    OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    Model inf = ModelFactory.createDefaultModel();
    SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
    aBox.register(simpleReasoner);
    SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner);
    tBox.register(simpleReasonerTBoxListener);
    // Individuals a and b
    Resource a = aBox.createResource("http://test.vivo/a");
    Resource b = aBox.createResource("http://test.vivo/b");
    // abox statements
    aBox.add(a, P, b);
    aBox.add(a, OWL.sameAs, b);
    setInverse(Q, P);
    while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
        Thread.sleep(delay);
    }
    Assert.assertTrue(inf.contains(b, Q, a));
    Assert.assertTrue(inf.contains(b, OWL.sameAs, a));
    Assert.assertTrue(inf.contains(b, P, b));
    Assert.assertTrue(inf.contains(a, Q, a));
    removeInverse(Q, P);
    while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
        Thread.sleep(delay);
    }
    Assert.assertFalse(inf.contains(b, Q, a));
    Assert.assertTrue(inf.contains(b, OWL.sameAs, a));
    Assert.assertTrue(inf.contains(b, P, b));
    Assert.assertFalse(inf.contains(a, Q, a));
    simpleReasonerTBoxListener.setStopRequested();
}
Also used : OntProperty(org.apache.jena.ontology.OntProperty) OntModel(org.apache.jena.ontology.OntModel) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) OntModel(org.apache.jena.ontology.OntModel) Test(org.junit.Test)

Example 37 with OntModel

use of org.apache.jena.ontology.OntModel in project Vitro by vivo-project.

the class SimpleReasonerSameAsTest method addSameAsABoxAssertion1.

/*
	* basic scenario of adding an abox sameAs assertion
	//*/
@Test
public void addSameAsABoxAssertion1() {
    OntModel tBox = createTBoxModel();
    OntProperty P = createObjectProperty(tBox, "http://test.vivo/P", "property P");
    OntProperty Q = createObjectProperty(tBox, "http://test.vivo/Q", "property Q");
    OntProperty S = createObjectProperty(tBox, "http://test.vivo/S", "property S");
    OntProperty T = createObjectProperty(tBox, "http://test.vivo/T", "property T");
    Literal literal1 = tBox.createLiteral("Literal value 1");
    Literal literal2 = tBox.createLiteral("Literal value 2");
    // this is the model to receive inferences
    Model inf = ModelFactory.createDefaultModel();
    // create an ABox and register the SimpleReasoner listener with it
    OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    aBox.register(new SimpleReasoner(tBox, aBox, inf));
    // Individuals a, b, c and d
    Resource a = aBox.createResource("http://test.vivo/a");
    Resource b = aBox.createResource("http://test.vivo/b");
    Resource c = aBox.createResource("http://test.vivo/c");
    Resource d = aBox.createResource("http://test.vivo/d");
    aBox.add(a, P, c);
    aBox.add(a, S, literal1);
    aBox.add(b, Q, d);
    aBox.add(b, T, literal2);
    aBox.add(a, OWL.sameAs, b);
    Assert.assertTrue(inf.contains(b, OWL.sameAs, a));
    Assert.assertTrue(inf.contains(b, P, c));
    Assert.assertTrue(inf.contains(b, S, literal1));
    Assert.assertTrue(inf.contains(a, Q, d));
    Assert.assertTrue(inf.contains(a, T, literal2));
    Assert.assertFalse(aBox.contains(b, OWL.sameAs, a));
    Assert.assertFalse(aBox.contains(b, P, c));
    Assert.assertFalse(aBox.contains(b, S, literal1));
    Assert.assertFalse(aBox.contains(a, Q, d));
    Assert.assertFalse(aBox.contains(a, T, literal2));
    // run same test with sameAs = false
    inf = ModelFactory.createDefaultModel();
    aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    SimpleReasoner sres = new SimpleReasoner(tBox, aBox, inf);
    sres.setSameAsEnabled(false);
    aBox.register(sres);
    a = aBox.createResource("http://test.vivo/a");
    b = aBox.createResource("http://test.vivo/b");
    c = aBox.createResource("http://test.vivo/c");
    d = aBox.createResource("http://test.vivo/d");
    aBox.add(a, P, c);
    aBox.add(a, S, literal1);
    aBox.add(b, Q, d);
    aBox.add(b, T, literal2);
    aBox.add(a, OWL.sameAs, b);
    // these are now false since sameAs is off
    Assert.assertFalse(inf.contains(b, OWL.sameAs, a));
    Assert.assertFalse(inf.contains(b, P, c));
    Assert.assertFalse(inf.contains(b, S, literal1));
    Assert.assertFalse(inf.contains(a, Q, d));
    Assert.assertFalse(inf.contains(a, T, literal2));
    // these still shouldn't be in the abox
    Assert.assertFalse(aBox.contains(b, OWL.sameAs, a));
    Assert.assertFalse(aBox.contains(b, P, c));
    Assert.assertFalse(aBox.contains(b, S, literal1));
    Assert.assertFalse(aBox.contains(a, Q, d));
    Assert.assertFalse(aBox.contains(a, T, literal2));
}
Also used : OntProperty(org.apache.jena.ontology.OntProperty) Literal(org.apache.jena.rdf.model.Literal) OntModel(org.apache.jena.ontology.OntModel) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) OntModel(org.apache.jena.ontology.OntModel) Test(org.junit.Test)

Example 38 with OntModel

use of org.apache.jena.ontology.OntModel in project Vitro by vivo-project.

the class SimpleReasonerTest method addABoxTypeAssertion2.

/*
	* Test that when an individual is asserted have a type,
	* that inferences are materialized that it has the types
	* of its superclasses
	*/
public void addABoxTypeAssertion2(boolean enableSameAs) {
    // Create a Tbox with a simple class hierarchy. D and E are subclasses
    // of C. B and C are subclasses of A.
    OntModel tBox = createTBoxModel();
    OntClass classA = createClass(tBox, "http://test.vivo/A", "class A");
    OntClass classB = createClass(tBox, "http://test.vivo/B", "class B");
    OntClass classC = createClass(tBox, "http://test.vivo/C", "class C");
    OntClass classD = createClass(tBox, "http://test.vivo/D", "class D");
    OntClass classE = createClass(tBox, "http://test.vivo/E", "class E");
    addSubclass(classA, classB);
    addSubclass(classA, classC);
    addSubclass(classC, classD);
    addSubclass(classC, classE);
    // this is the model to receive inferences
    Model inf = ModelFactory.createDefaultModel();
    // create an Abox and register the SimpleReasoner listener with it
    OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    SimpleReasoner sr = new SimpleReasoner(tBox, aBox, inf);
    sr.setSameAsEnabled(enableSameAs);
    aBox.register(sr);
    // add a statement to the ABox that individual x is of type E.
    Resource ind_x = aBox.createResource("http://test.vivo/x");
    aBox.add(ind_x, RDF.type, classE);
    // Verify that "x is of type C" was inferred
    Statement xisc = ResourceFactory.createStatement(ind_x, RDF.type, classC);
    Assert.assertTrue(inf.contains(xisc));
    // Verify that "x is of type A" was inferred
    Statement xisa = ResourceFactory.createStatement(ind_x, RDF.type, classA);
    Assert.assertTrue(inf.contains(xisa));
}
Also used : Statement(org.apache.jena.rdf.model.Statement) OntModel(org.apache.jena.ontology.OntModel) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) OntModel(org.apache.jena.ontology.OntModel) OntClass(org.apache.jena.ontology.OntClass)

Example 39 with OntModel

use of org.apache.jena.ontology.OntModel in project Vitro by vivo-project.

the class SimpleReasonerTest method removeABoxTypeAssertion1.

/*
	* Test that when it is retracted that an individual is of a type,
	* that the inferences that it is of the type of all superclasses
	* of the retracted type are retracted from the inference graph.
	* However, any assertions that are otherwise entailed (by the
	* TBox, ABox and inference graph minus the retracted type statement)
	* should not be retracted.
	*/
public void removeABoxTypeAssertion1(boolean enableSameAs) {
    // Create a Tbox with a simple class hierarchy. C is a subclass of B
    // and B is a subclass of A.
    OntModel tBox = createTBoxModel();
    OntClass classA = createClass(tBox, "http://test.vivo/A", "class A");
    OntClass classB = createClass(tBox, "http://test.vivo/B", "class B");
    OntClass classC = createClass(tBox, "http://test.vivo/C", "class C");
    addSubclass(classB, classC);
    addSubclass(classA, classB);
    // this is the model to receive inferences
    Model inf = ModelFactory.createDefaultModel();
    // create an Abox and register the SimpleReasoner listener with it
    OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    SimpleReasoner sr = new SimpleReasoner(tBox, aBox, inf);
    sr.setSameAsEnabled(enableSameAs);
    aBox.register(sr);
    // add a statement to the ABox that individual x is of type C.
    Resource ind_x = aBox.createResource("http://test.vivo/x");
    aBox.add(ind_x, RDF.type, classC);
    // add a statement to the ABox that individual x is of type B.
    aBox.add(ind_x, RDF.type, classB);
    // remove the statement that individual x is of type C
    aBox.remove(ind_x, RDF.type, classC);
    // Verify that the inference graph contains the statement that x is of type A.
    Statement xisa = ResourceFactory.createStatement(ind_x, RDF.type, classA);
    Assert.assertTrue(inf.contains(xisa));
    // Hopefully the assertion that x is b got removed from
    // the inference graph
    Statement xisb = ResourceFactory.createStatement(ind_x, RDF.type, classB);
    Assert.assertFalse(inf.contains(xisb));
}
Also used : Statement(org.apache.jena.rdf.model.Statement) OntModel(org.apache.jena.ontology.OntModel) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) OntModel(org.apache.jena.ontology.OntModel) OntClass(org.apache.jena.ontology.OntClass)

Example 40 with OntModel

use of org.apache.jena.ontology.OntModel in project Vitro by vivo-project.

the class SimpleReasonerInversePropertyTest method removedABoxAssertion2.

/*
	 *  removing abox data with equivalent and inverse properties
	 *  don't remove inference if it's still inferred.
	 */
public void removedABoxAssertion2(boolean sameAs) {
    // set up the tbox
    OntModel tBox = createTBoxModel();
    OntProperty P = createObjectProperty(tBox, "http://test.vivo/P", "property P");
    OntProperty Q = createObjectProperty(tBox, "http://test.vivo/Q", "property Q");
    OntProperty T = createObjectProperty(tBox, "http://test.vivo/T", "property T");
    setInverse(P, Q);
    setInverse(T, Q);
    setEquivalent(P, T);
    // this is the model to receive abox inferences
    Model inf = ModelFactory.createDefaultModel();
    // create an abox and register the SimpleReasoner listener with it
    OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
    SimpleReasoner sr = new SimpleReasoner(tBox, aBox, inf);
    sr.setSameAsEnabled(sameAs);
    aBox.register(sr);
    // add abox data and verify inferences
    Resource a = aBox.createResource("http://test.vivo/a");
    Resource b = aBox.createResource("http://test.vivo/b");
    // b Q a is inferred from a P b and  also from a T b.
    aBox.add(a, P, b);
    aBox.add(a, T, b);
    Assert.assertTrue(inf.contains(b, Q, a));
    Assert.assertFalse(inf.contains(a, P, b));
    Assert.assertFalse(inf.contains(a, T, b));
    aBox.remove(a, P, b);
    Assert.assertTrue(inf.contains(b, Q, a));
}
Also used : OntProperty(org.apache.jena.ontology.OntProperty) OntModel(org.apache.jena.ontology.OntModel) Model(org.apache.jena.rdf.model.Model) Resource(org.apache.jena.rdf.model.Resource) OntModel(org.apache.jena.ontology.OntModel)

Aggregations

OntModel (org.apache.jena.ontology.OntModel)272 Model (org.apache.jena.rdf.model.Model)112 Resource (org.apache.jena.rdf.model.Resource)92 Test (org.junit.Test)51 Statement (org.apache.jena.rdf.model.Statement)41 OntClass (org.apache.jena.ontology.OntClass)33 StmtIterator (org.apache.jena.rdf.model.StmtIterator)31 ArrayList (java.util.ArrayList)29 OntProperty (org.apache.jena.ontology.OntProperty)29 OntResource (org.apache.jena.ontology.OntResource)26 Literal (org.apache.jena.rdf.model.Literal)26 RDFNode (org.apache.jena.rdf.model.RDFNode)26 IOException (java.io.IOException)24 Individual (org.apache.jena.ontology.Individual)22 Property (org.apache.jena.rdf.model.Property)21 QueryExecution (org.apache.jena.query.QueryExecution)16 StringReader (java.io.StringReader)15 DatatypeProperty (org.apache.jena.ontology.DatatypeProperty)14 InputStream (java.io.InputStream)12 DataProperty (edu.cornell.mannlib.vitro.webapp.beans.DataProperty)11