Search in sources :

Example 31 with OntModel

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

the class SimpleReasonerSameAsTest method tBoxSubclassAssertion1.

/*
	 * adding and removing subclass assertion when there is an
	 * individual member who has a sameAs individual.
	 */
@Test
public void tBoxSubclassAssertion1() throws InterruptedException {
    // Create a Tbox with a simple class hierarchy. 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");
    // create aBox and SimpleReasoner to listen to them
    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);
    // set up ABox
    Resource a = aBox.createResource("http://test.vivo/a");
    Resource b = aBox.createResource("http://test.vivo/b");
    Resource c = aBox.createResource("http://test.vivo/c");
    aBox.add(a, RDF.type, classC);
    aBox.add(a, OWL.sameAs, b);
    aBox.add(c, OWL.sameAs, a);
    // update TBox
    addSubclass(classA, classB);
    // wait for SimpleReasonerTBoxListener thread to end
    while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
        Thread.sleep(delay);
    }
    addSubclass(classB, classC);
    // wait for SimpleReasonerTBoxListener thread to end
    while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
        Thread.sleep(delay);
    }
    // Verify inferences
    Assert.assertFalse(inf.contains(a, RDF.type, classC));
    Assert.assertTrue(inf.contains(a, RDF.type, classB));
    Assert.assertTrue(inf.contains(a, RDF.type, classA));
    Assert.assertTrue(inf.contains(b, RDF.type, classC));
    Assert.assertTrue(inf.contains(b, RDF.type, classB));
    Assert.assertTrue(inf.contains(b, RDF.type, classA));
    Assert.assertTrue(inf.contains(c, RDF.type, classC));
    Assert.assertTrue(inf.contains(c, RDF.type, classB));
    Assert.assertTrue(inf.contains(c, RDF.type, classA));
    // update TBox
    removeSubclass(classA, classB);
    // wait for SimpleReasonerTBoxListener thread to end
    while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
        Thread.sleep(delay);
    }
    // Verify inferences
    Assert.assertFalse(inf.contains(a, RDF.type, classC));
    Assert.assertTrue(inf.contains(a, RDF.type, classB));
    Assert.assertFalse(inf.contains(a, RDF.type, classA));
    Assert.assertTrue(inf.contains(b, RDF.type, classC));
    Assert.assertTrue(inf.contains(b, RDF.type, classB));
    Assert.assertFalse(inf.contains(b, RDF.type, classA));
    Assert.assertTrue(inf.contains(c, RDF.type, classC));
    Assert.assertTrue(inf.contains(c, RDF.type, classB));
    Assert.assertFalse(inf.contains(c, RDF.type, classA));
    // update TBox
    removeSubclass(classB, classC);
    // wait for SimpleReasonerTBoxListener thread to end
    while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
        Thread.sleep(delay);
    }
    // Verify inferences
    Assert.assertFalse(inf.contains(a, RDF.type, classC));
    Assert.assertFalse(inf.contains(a, RDF.type, classB));
    Assert.assertFalse(inf.contains(a, RDF.type, classA));
    Assert.assertTrue(inf.contains(b, RDF.type, classC));
    Assert.assertFalse(inf.contains(b, RDF.type, classB));
    Assert.assertFalse(inf.contains(b, RDF.type, classA));
    Assert.assertTrue(inf.contains(c, RDF.type, classC));
    Assert.assertFalse(inf.contains(c, RDF.type, classB));
    Assert.assertFalse(inf.contains(c, RDF.type, classA));
    simpleReasonerTBoxListener.setStopRequested();
}
Also used : 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) Test(org.junit.Test)

Example 32 with OntModel

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

the class SimpleReasonerSameAsTest method addABoxAssertion2.

/*
	* sameAs with datatype properties
	*/
@Test
public void addABoxAssertion2() {
    OntModel tBox = createTBoxModel();
    OntProperty desc = tBox.createDatatypeProperty("http://test.vivo/desc");
    desc.setLabel("property desc", "en-US");
    Literal desc1 = tBox.createLiteral("individual 1");
    Literal desc2 = tBox.createLiteral("individual 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 and b
    Resource a = aBox.createResource("http://test.vivo/a");
    Resource b = aBox.createResource("http://test.vivo/b");
    aBox.add(a, desc, desc1);
    aBox.add(b, desc, desc2);
    aBox.add(a, OWL.sameAs, b);
    Assert.assertTrue(inf.contains(a, desc, desc2));
    Assert.assertTrue(inf.contains(b, desc, desc1));
    Assert.assertTrue(inf.contains(b, OWL.sameAs, a));
}
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 33 with OntModel

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

the class SimpleReasonerSameAsTest method removeSameAsABoxAssertion1.

/*
	* basic scenario of removing an abox sameAs assertion
	*/
@Test
public void removeSameAsABoxAssertion1() {
    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));
    aBox.remove(a, OWL.sameAs, b);
    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));
}
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 34 with OntModel

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

the class SimpleReasonerSameAsTest method recomputeABox1.

/*
	 * Basic scenario around recomputing the ABox inferences
	 */
@Test
public void recomputeABox1() 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");
    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);
    SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf);
    aBox.register(simpleReasoner);
    // Individuals a, b, c and d
    Resource a = aBox.createIndividual("http://test.vivo/a", OWL.Thing);
    Resource b = aBox.createIndividual("http://test.vivo/b", OWL.Thing);
    Resource c = aBox.createIndividual("http://test.vivo/c", OWL.Thing);
    Resource d = aBox.createIndividual("http://test.vivo/d", OWL.Thing);
    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));
    inf.remove(b, OWL.sameAs, a);
    inf.remove(b, P, c);
    inf.remove(b, S, literal1);
    inf.remove(a, Q, d);
    inf.remove(a, T, literal2);
    simpleReasoner.recompute();
    while (simpleReasoner.isRecomputing()) {
        Thread.sleep(delay);
    }
    // Verify inferences
    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));
}
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 35 with OntModel

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

the class SimpleReasonerSameAsTest method addABoxAssertion1.

/*
	* adding abox assertion for individual in sameAs chain.
	*/
@Test
public void addABoxAssertion1() {
    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");
    Resource e = aBox.createResource("http://test.vivo/e");
    Resource f = aBox.createResource("http://test.vivo/f");
    aBox.add(a, OWL.sameAs, b);
    aBox.add(b, OWL.sameAs, e);
    aBox.add(e, OWL.sameAs, f);
    Assert.assertTrue(inf.contains(b, OWL.sameAs, a));
    Assert.assertTrue(inf.contains(e, OWL.sameAs, a));
    Assert.assertTrue(inf.contains(e, OWL.sameAs, b));
    Assert.assertTrue(inf.contains(a, OWL.sameAs, e));
    Assert.assertTrue(inf.contains(f, OWL.sameAs, e));
    Assert.assertTrue(inf.contains(f, OWL.sameAs, b));
    Assert.assertTrue(inf.contains(f, OWL.sameAs, a));
    Assert.assertTrue(inf.contains(b, OWL.sameAs, f));
    Assert.assertTrue(inf.contains(a, OWL.sameAs, f));
    aBox.add(a, P, c);
    aBox.add(a, S, literal1);
    aBox.add(b, Q, d);
    aBox.add(b, T, literal2);
    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.assertTrue(inf.contains(e, P, c));
    Assert.assertTrue(inf.contains(e, S, literal1));
    Assert.assertTrue(inf.contains(e, Q, d));
    Assert.assertTrue(inf.contains(e, T, literal2));
    Assert.assertTrue(inf.contains(f, P, c));
    Assert.assertTrue(inf.contains(f, S, literal1));
    Assert.assertTrue(inf.contains(f, Q, d));
    Assert.assertTrue(inf.contains(f, T, literal2));
    aBox.remove(b, OWL.sameAs, e);
    Assert.assertTrue(inf.contains(b, OWL.sameAs, a));
    Assert.assertFalse(inf.contains(e, OWL.sameAs, a));
    Assert.assertFalse(inf.contains(e, OWL.sameAs, b));
    Assert.assertFalse(inf.contains(a, OWL.sameAs, e));
    Assert.assertTrue(inf.contains(f, OWL.sameAs, e));
    Assert.assertFalse(inf.contains(f, OWL.sameAs, b));
    Assert.assertFalse(inf.contains(f, OWL.sameAs, a));
    Assert.assertFalse(inf.contains(b, OWL.sameAs, f));
    Assert.assertFalse(inf.contains(a, OWL.sameAs, f));
    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(inf.contains(e, P, c));
    Assert.assertFalse(inf.contains(e, S, literal1));
    Assert.assertFalse(inf.contains(e, Q, d));
    Assert.assertFalse(inf.contains(e, T, literal2));
    Assert.assertFalse(inf.contains(f, P, c));
    Assert.assertFalse(inf.contains(f, S, literal1));
    Assert.assertFalse(inf.contains(f, Q, d));
    Assert.assertFalse(inf.contains(f, 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)

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