diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerInversePropertyTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerInversePropertyTest.java index 12f157725..1494dc870 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerInversePropertyTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerInversePropertyTest.java @@ -6,7 +6,6 @@ import org.apache.log4j.Level; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.mindswap.pellet.jena.PelletReasonerFactory; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModelSpec; @@ -16,10 +15,9 @@ import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.Resource; import com.hp.hpl.jena.vocabulary.OWL; -import edu.cornell.mannlib.vitro.testing.AbstractTestClass; import edu.cornell.mannlib.vitro.webapp.utils.threads.VitroBackgroundThread; -public class SimpleReasonerInversePropertyTest extends AbstractTestClass { +public class SimpleReasonerInversePropertyTest extends SimpleReasonerTBoxHelper { long delay = 50; @@ -46,21 +44,16 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { * basic scenarios around adding abox data * * Create a Tbox with property P inverseOf property Q. - * Pellet will compute TBox inferences. Add a statement - * a P b, and verify that b Q a is inferred. - * Add a statement c Q d and verify that d Q c - * is inferred. + * Add a statement a P b, and verify that b Q a is inferred. + * Add a statement c Q d and verify that d Q c is inferred. */ public void addABoxAssertion1(boolean sameAs ) { // set up the tbox - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - OntProperty P = tBox.createOntProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - OntProperty Q = tBox.createOntProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); - - P.addInverseOf(Q); + OntModel tBox = createTBoxModel(); + OntProperty P = createObjectProperty(tBox, "http://test.vivo/P", "property P"); + OntProperty Q = createObjectProperty(tBox, "http://test.vivo/Q", "property Q"); + setInverse(P, Q); // this is the model to receive abox inferences Model inf = ModelFactory.createDefaultModel(); @@ -109,14 +102,11 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { public void addABoxAssertion2(boolean sameAs ) { // set up the tbox - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - - OntProperty P = tBox.createOntProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - OntProperty Q = tBox.createOntProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); - P.addInverseOf(Q); - + OntModel tBox = createTBoxModel(); + OntProperty P = createObjectProperty(tBox, "http://test.vivo/P", "property P"); + OntProperty Q = createObjectProperty(tBox, "http://test.vivo/Q", "property Q"); + setInverse(P, Q); + // this is the model to receive abox inferences Model inf = ModelFactory.createDefaultModel(); @@ -153,13 +143,11 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { public void addABoxAssertion3(boolean sameAs) { // set up the tbox - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - OntProperty P = tBox.createOntProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - OntProperty Q = tBox.createOntProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); - P.addInverseOf(Q); - + OntModel tBox = createTBoxModel(); + OntProperty P = createObjectProperty(tBox, "http://test.vivo/P", "property P"); + OntProperty Q = createObjectProperty(tBox, "http://test.vivo/Q", "property Q"); + setInverse(P, Q); + // this is the model to receive abox inferences Model inf = ModelFactory.createDefaultModel(); @@ -197,16 +185,13 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { public void addABoxAssertion4( boolean sameAs ) { // set up the tbox - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - OntProperty P = tBox.createOntProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - OntProperty R = tBox.createOntProperty("http://test.vivo/R"); - R.setLabel("property R", "en-US"); - OntProperty Q = tBox.createOntProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); - - R.addEquivalentProperty(P); - P.addInverseOf(Q); + OntModel tBox = createTBoxModel(); + OntProperty P = createObjectProperty(tBox, "http://test.vivo/P", "property P"); + OntProperty Q = createObjectProperty(tBox, "http://test.vivo/Q", "property Q"); + OntProperty R = createObjectProperty(tBox, "http://test.vivo/R", "property R"); + setInverse(P, Q); + setInverse(R, Q); + setEquivalent(R, P); // this is the model to receive abox inferences Model inf = ModelFactory.createDefaultModel(); @@ -250,15 +235,12 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { public void removedABoxAssertion1(boolean sameAs) { // set up the tbox - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - OntProperty P = tBox.createOntProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - OntProperty Q = tBox.createOntProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); - OntProperty T = tBox.createOntProperty("http://test.vivo/T"); - Q.setLabel("property T", "en-US"); - P.addInverseOf(Q); - P.addInverseOf(T); + 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(P, T); // this is the model to receive abox inferences Model inf = ModelFactory.createDefaultModel(); @@ -307,20 +289,14 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { public void removedABoxAssertion2(boolean sameAs) { // set up the tbox - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - OntProperty P = tBox.createOntProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - OntProperty Q = tBox.createOntProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); - OntProperty T = tBox.createOntProperty("http://test.vivo/T"); - Q.setLabel("property T", "en-US"); - P.addInverseOf(Q); - P.addEquivalentProperty(T); - - // not clear what these will do - tBox.rebind(); - tBox.prepare(); - + 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(); @@ -360,16 +336,12 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { */ public void removedABoxAssertion3(boolean sameAs) { - //set up the tbox - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - OntProperty P = tBox.createOntProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - OntProperty Q = tBox.createOntProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); - P.addInverseOf(Q); - - tBox.rebind(); // not sure what effect this has - + // 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"); + setInverse(P, Q); + // this is the model to receive abox inferences Model inf = ModelFactory.createDefaultModel(); @@ -410,12 +382,10 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { */ public void addTBoxInverseAssertion1(boolean sameAs) throws InterruptedException { - // Set up the TBox. - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - OntProperty P = tBox.createOntProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - OntProperty Q = tBox.createOntProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); + // 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"); // this is the model to receive abox inferences Model inf = ModelFactory.createDefaultModel(); @@ -444,7 +414,7 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { // Assert P and Q as inverses and wait for // SimpleReasonerTBoxListener thread to end - Q.addInverseOf(P); + setInverse(P, Q); while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); @@ -473,14 +443,11 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { */ public void removeTBoxInverseAssertion1(boolean sameAs) throws InterruptedException { - // set up the tbox. - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - - OntProperty P = tBox.createOntProperty("http://test.vivo/propP"); - P.setLabel("property P", "en-US"); - OntProperty Q = tBox.createOntProperty("http://test.vivo/propQ"); - Q.setLabel("property Q", "en-US"); - Q.addInverseOf(P); + // 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"); + setInverse(P, Q); // this is the model to receive abox inferences Model inf = ModelFactory.createDefaultModel(); @@ -505,7 +472,7 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { // Remove P and Q inverse relationship and wait for // SimpleReasoner TBox thread to end. - Q.removeInverseProperty(P); + removeInverse(P, Q); while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); @@ -532,20 +499,14 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { */ public void recomputeABox1(boolean sameAs) throws InterruptedException { - // set up tbox - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - - OntProperty P = tBox.createOntProperty("http://test.vivo/propP"); - P.setLabel("property P", "en-US"); - OntProperty Q = tBox.createOntProperty("http://test.vivo/propQ"); - Q.setLabel("property Q", "en-US"); - Q.addInverseOf(P); - - OntProperty X = tBox.createOntProperty("http://test.vivo/propX"); - P.setLabel("property X", "en-US"); - OntProperty Y = tBox.createOntProperty("http://test.vivo/propY"); - Q.setLabel("property Y", "en-US"); - X.addInverseOf(Y); + // 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"); + setInverse(P, Q); + OntProperty X = createObjectProperty(tBox, "http://test.vivo/X", "property X"); + OntProperty Y = createObjectProperty(tBox, "http://test.vivo/Y", "property Y"); + setInverse(X, Y); // create abox and abox inf model and register simplereasoner // with abox. @@ -565,8 +526,8 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { aBox.add(c,X,d); Assert.assertTrue(inf.contains(b,Q,a)); - Assert.assertTrue(inf.contains(d,Y,c)); - + Assert.assertTrue(inf.contains(d,Y,c)); + inf.remove(b,Q,a); inf.remove(d,Y,c); @@ -581,7 +542,7 @@ public class SimpleReasonerInversePropertyTest extends AbstractTestClass { Assert.assertTrue(inf.contains(b,Q,a)); Assert.assertTrue(inf.contains(d,Y,c)); } - + //==================================== Utility methods ==================== SimpleReasonerTBoxListener getTBoxListener(SimpleReasoner simpleReasoner) { return new SimpleReasonerTBoxListener(simpleReasoner, new Exception().getStackTrace()[1].getMethodName()); diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerPluginTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerPluginTest.java index 82c1fdf97..5d5509e94 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerPluginTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerPluginTest.java @@ -9,7 +9,6 @@ import org.apache.log4j.Level; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.mindswap.pellet.jena.PelletReasonerFactory; import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModelSpec; @@ -23,9 +22,7 @@ import com.hp.hpl.jena.rdf.model.ResourceFactory; import com.hp.hpl.jena.vocabulary.OWL; import com.hp.hpl.jena.vocabulary.RDFS; -import edu.cornell.mannlib.vitro.testing.AbstractTestClass; - -public class SimpleReasonerPluginTest extends AbstractTestClass { +public class SimpleReasonerPluginTest extends SimpleReasonerTBoxHelper { long delay = 50; private final static String DEFAULT_NS = "http://vivoweb.org/individual/"; @@ -53,7 +50,7 @@ public class SimpleReasonerPluginTest extends AbstractTestClass { */ @Test public void test1() { - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); + OntModel tBox = createTBoxModel(); OntProperty authorInAuthorship = tBox.createObjectProperty(authorInAuthorship_URI); OntProperty linkedAuthor = tBox.createObjectProperty(linkedAuthor_URI); diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerSameAsTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerSameAsTest.java index 81b7242f5..72089696b 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerSameAsTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerSameAsTest.java @@ -6,7 +6,6 @@ import org.apache.log4j.Level; import org.junit.Assert; import org.junit.Before; import org.junit.Test; -import org.mindswap.pellet.jena.PelletReasonerFactory; import com.hp.hpl.jena.ontology.AnnotationProperty; import com.hp.hpl.jena.ontology.OntClass; @@ -21,10 +20,9 @@ import com.hp.hpl.jena.rdf.model.ResourceFactory; import com.hp.hpl.jena.vocabulary.OWL; import com.hp.hpl.jena.vocabulary.RDF; -import edu.cornell.mannlib.vitro.testing.AbstractTestClass; import edu.cornell.mannlib.vitro.webapp.utils.threads.VitroBackgroundThread; -public class SimpleReasonerSameAsTest extends AbstractTestClass { +public class SimpleReasonerSameAsTest extends SimpleReasonerTBoxHelper { long delay = 50; private static final String mostSpecificTypePropertyURI = "http://vitro.mannlib.cornell.edu/ns/vitro/0.7#mostSpecificType"; @@ -43,21 +41,11 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { //*/ @Test public void addSameAsABoxAssertion1() { - - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - - OntProperty P = tBox.createObjectProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - - OntProperty Q = tBox.createObjectProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); - - OntProperty S = tBox.createDatatypeProperty("http://test.vivo/"); - S.setLabel("property S", "en-US"); - - OntProperty T = tBox.createDatatypeProperty("http://test.vivo/"); - T.setLabel("property T", "en-US"); - + 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"); @@ -129,21 +117,11 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { */ @Test public void removeSameAsABoxAssertion1() { - - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - - OntProperty P = tBox.createObjectProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - - OntProperty Q = tBox.createObjectProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); - - OntProperty S = tBox.createDatatypeProperty("http://test.vivo/data1"); - S.setLabel("property S", "en-US"); - - OntProperty T = tBox.createDatatypeProperty("http://test.vivo/data2"); - T.setLabel("property T", "en-US"); - + 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"); @@ -186,21 +164,11 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { */ @Test public void addABoxAssertion1() { - - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - - OntProperty P = tBox.createObjectProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - - OntProperty Q = tBox.createObjectProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); - - OntProperty S = tBox.createDatatypeProperty("http://test.vivo/"); - S.setLabel("property S", "en-US"); - - OntProperty T = tBox.createDatatypeProperty("http://test.vivo/"); - T.setLabel("property T", "en-US"); - + 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"); @@ -283,21 +251,11 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { */ @Test public void disabledSameAs() { - - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - - OntProperty P = tBox.createObjectProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - - OntProperty Q = tBox.createObjectProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); - - OntProperty S = tBox.createDatatypeProperty("http://test.vivo/"); - S.setLabel("property S", "en-US"); - - OntProperty T = tBox.createDatatypeProperty("http://test.vivo/"); - T.setLabel("property T", "en-US"); - + 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"); @@ -369,7 +327,7 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { */ @Test public void addABoxAssertion2() { - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); + OntModel tBox = createTBoxModel(); OntProperty desc = tBox.createDatatypeProperty("http://test.vivo/desc"); desc.setLabel("property desc", "en-US"); @@ -403,21 +361,11 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { */ @Test public void removeABoxAssertion1() { - - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - - OntProperty P = tBox.createObjectProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - - OntProperty Q = tBox.createObjectProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); - - OntProperty S = tBox.createDatatypeProperty("http://test.vivo/"); - S.setLabel("property S", "en-US"); - - OntProperty T = tBox.createDatatypeProperty("http://test.vivo/"); - T.setLabel("property T", "en-US"); - + 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"); @@ -453,18 +401,12 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { */ @Test public void tBoxInverseAssertion1() throws InterruptedException { - - // Create TBox, ABox and Inference models and register - // the ABox reasoner listeners with the ABox and TBox - // Pellet will compute TBox inferences - - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - - OntProperty P = tBox.createOntProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); + OntModel tBox = createTBoxModel(); + OntProperty P = createObjectProperty(tBox, "http://test.vivo/P", "property P"); + OntProperty Q = createObjectProperty(tBox, "http://test.vivo/Q", "property Q"); - OntProperty Q = tBox.createOntProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); + // 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(); @@ -482,7 +424,7 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { aBox.add(a,P,b); aBox.add(a, OWL.sameAs,b); - Q.addInverseOf(P); + setInverse(Q, P); while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); @@ -492,8 +434,8 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { Assert.assertTrue(inf.contains(b,OWL.sameAs,a)); Assert.assertTrue(inf.contains(b,P,b)); Assert.assertTrue(inf.contains(a,Q,a)); - - Q.removeInverseProperty(P); + + removeInverse(Q, P); while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); @@ -513,19 +455,12 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { * a sameAs individual. */ //@Test - public void tBoxTypeAssertion1() throws InterruptedException { - + public void tBoxTypeAssertion1() { // Create a Tbox with a simple class hierarchy. B is a subclass of A. - // Pellet will compute TBox inferences - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - - classA.addSubClass(classB); + OntModel tBox = createTBoxModel(); + OntClass classA = createClass(tBox, "http://test.vivo/A", "class A"); + OntClass classB = createClass(tBox, "http://test.vivo/B", "class B"); + addSubclass(classA, classB); // this is the model to receive inferences Model inf = ModelFactory.createDefaultModel(); @@ -560,17 +495,13 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { */ //@Test public void tBoxSubclassAssertion1() throws InterruptedException { - - //create aBox and tBox, and SimpleReasoner to listen to them - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - // set up TBox - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - OntClass classC = tBox.createClass("http://test.vivo/C"); - classC.setLabel("class C", "en-US"); - + // 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(); @@ -589,17 +520,14 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { aBox.add(c, OWL.sameAs, a); // update TBox - classA.addSubClass(classB); + addSubclass(classA, classB); // wait for SimpleReasonerTBoxListener thread to end while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); } - classB.addSubClass(classC); - classA.addSubClass(classC); // simulate what Pellet would infer, and - // thus what the SimpleReasonerTBoxListener - // would be notified of. + addSubclass(classB, classC); // wait for SimpleReasonerTBoxListener thread to end while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { @@ -620,10 +548,7 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { Assert.assertTrue(inf.contains(c, RDF.type, classA)); // update TBox - classA.removeSubClass(classB); - classA.removeSubClass(classC); // simulate what Pellet would infer, and - // thus what the SimpleReasonerTBoxListener - // would be notified of. + removeSubclass(classA, classB); // wait for SimpleReasonerTBoxListener thread to end while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { @@ -643,8 +568,8 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { Assert.assertTrue(inf.contains(c, RDF.type, classB)); Assert.assertFalse(inf.contains(c, RDF.type, classA)); - // update TBox - classB.removeSubClass(classC); + // update TBox + removeSubclass(classB, classC); // wait for SimpleReasonerTBoxListener thread to end while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { @@ -672,24 +597,19 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { * individuals */ //@Test - public void mostSpecificTypeTest1() throws InterruptedException { + public void mostSpecificTypeTest1() { + // 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 classC = createClass(tBox, "http://test.vivo/B", "class C"); + OntClass classD = createClass(tBox, "http://test.vivo/B", "class D"); + OntClass classE = createClass(tBox, "http://test.vivo/B", "class E"); + addSubclass(classA, classC); + addSubclass(classC, classD); + addSubclass(classC, classE); - // set up tbox. Pellet is reasoning; SimpleReasonerTBoxListener is not being used. - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); + // SimpleReasonerTBoxListener is not being used. AnnotationProperty mostSpecificType = tBox.createAnnotationProperty(mostSpecificTypePropertyURI); - - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - OntClass classC = tBox.createClass("http://test.vivo/C"); - classC.setLabel("class C", "en-US"); - OntClass classD = tBox.createClass("http://test.vivo/D"); - classD.setLabel("class D", "en-US"); - OntClass classE = tBox.createClass("http://test.vivo/E"); - classE.setLabel("class E", "en-US"); - - classA.addSubClass(classC); - classC.addSubClass(classD); - classC.addSubClass(classE); // this will receive the abox inferences Model inf = ModelFactory.createDefaultModel(); @@ -755,21 +675,11 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { */ @Test public void recomputeABox1() throws InterruptedException { - - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - - OntProperty P = tBox.createObjectProperty("http://test.vivo/P"); - P.setLabel("property P", "en-US"); - - OntProperty Q = tBox.createObjectProperty("http://test.vivo/Q"); - Q.setLabel("property Q", "en-US"); - - OntProperty S = tBox.createDatatypeProperty("http://test.vivo/"); - S.setLabel("property S", "en-US"); - - OntProperty T = tBox.createDatatypeProperty("http://test.vivo/"); - T.setLabel("property T", "en-US"); - + 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"); diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerTBoxHelper.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerTBoxHelper.java new file mode 100644 index 000000000..0c4b05cfb --- /dev/null +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerTBoxHelper.java @@ -0,0 +1,232 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.reasoner; + +import java.util.HashSet; +import java.util.Set; + +import com.hp.hpl.jena.ontology.ObjectProperty; +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.ontology.OntProperty; +import com.hp.hpl.jena.rdf.model.ModelFactory; + +import edu.cornell.mannlib.vitro.testing.AbstractTestClass; + +/** + * We're using a simple OntModel as the TBox in the SimpleReasoner tests, so we + * don't get tied to a particular TBox reasoner (like Pellet). + * + * But the SimpleReasoner expects certain elementary reasoning, so these methods + * impose that reasoning on the model. + * + * On the model: Thing is a class. + * + * On classes: Every class is equivalent to itself, a subclass of itself, and a + * subclass of Thing. Every class is a subclass of all its ancestors and a + * superclass of all its descendants. Every class has the same superclasses and + * subclasses as do all of its equivalent classes. + * + * On object properties: Every object property is equivalent to itself and a + * subproperty of itself. Every object property has the same inverses as do all + * of its equivalent properties. + * + * ---------------------- + * + * It's a little silly to implement this as a parent class of the unit tests. It + * would have been nicer to find a way that is more object-oriented but still + * explicit in what "reasoning" is performed. This will do for now. + */ +public class SimpleReasonerTBoxHelper extends AbstractTestClass { + private static String URI_THING = "http://www.w3.org/2002/07/owl#Thing"; + + // ---------------------------------------------------------------------- + // The model + // ---------------------------------------------------------------------- + + protected OntModel createTBoxModel() { + OntModel tBox = ModelFactory + .createOntologyModel(OntModelSpec.OWL_DL_MEM); + createClass(tBox, URI_THING, "OWL Thing"); + return tBox; + } + + // ---------------------------------------------------------------------- + // Classes + // ---------------------------------------------------------------------- + + protected OntClass createClass(OntModel tBox, String uri, String label) { + OntClass s = tBox.createClass(uri); + s.setLabel(label, "en-US"); + s.addEquivalentClass(s); + s.addSubClass(s); + thing(tBox).addSubClass(s); + return s; + } + + private OntClass thing(OntModel tBox) { + return tBox.getOntClass(URI_THING); + } + + /** + * Make sure that you establish subclass relationships before setting + * equivalent classes. + */ + protected void setEquivalent(OntClass c1, OntClass c2) { + setEquivalentClasses(equivalences(c1), equivalences(c2)); + setEquivalentClasses(equivalences(c2), equivalences(c1)); + c1.addEquivalentClass(c2); + c2.addEquivalentClass(c1); + copySubClasses(c1, c2); + copySubClasses(c2, c1); + copySuperClasses(c1, c2); + copySuperClasses(c2, c1); + } + + private void setEquivalentClasses(Set equivalences1, + Set equivalences2) { + for (OntClass c1 : equivalences1) { + for (OntClass c2 : equivalences2) { + c1.addEquivalentClass(c2); + } + } + } + + private void copySubClasses(OntClass c1, OntClass c2) { + for (OntClass sub : c1.listSubClasses().toList()) { + c2.addSubClass(sub); + } + } + + private void copySuperClasses(OntClass c1, OntClass c2) { + for (OntClass sup : c1.listSuperClasses().toList()) { + c2.addSuperClass(sup); + } + } + + private Set equivalences(OntClass c1) { + return new HashSet(c1.listEquivalentClasses().toList()); + } + + protected void addSubclass(OntClass parent, OntClass child) { + addSubclass(equivalences(parent), equivalences(child)); + } + + private void addSubclass(Set equivalentParents, + Set equivalentChildren) { + for (OntClass parent : equivalentParents) { + for (OntClass child : equivalentChildren) { + parent.addSubClass(child); + + for (OntClass ancestor : parent.listSuperClasses().toList()) { + ancestor.addSubClass(child); + } + for (OntClass descendant : child.listSubClasses().toList()) { + parent.addSubClass(descendant); + } + } + } + } + + protected void removeSubclass(OntClass parent, OntClass child) { + removeSubclass(equivalences(parent), equivalences(child)); + } + + /** + * This has the potential for problems if we set this up: + * + *
+	 * A -> B -> C
+	 * 
+	 * explicit add A -> C
+	 * 
+	 * remove A -> B
+	 * 
+ * + * But why would we do that? + */ + private void removeSubclass(Set equivalentParents, + Set equivalentChildren) { + for (OntClass parent : equivalentParents) { + for (OntClass child : equivalentChildren) { + parent.removeSubClass(child); + + for (OntClass ancestor : parent.listSuperClasses().toList()) { + ancestor.removeSubClass(child); + } + for (OntClass descendant : child.listSubClasses().toList()) { + parent.removeSubClass(descendant); + } + } + } + } + + // ---------------------------------------------------------------------- + // Object properties + // ---------------------------------------------------------------------- + + protected ObjectProperty createObjectProperty(OntModel tBox, String uri, + String label) { + ObjectProperty p = tBox.createObjectProperty(uri); + p.setLabel(label, "en-US"); + p.addEquivalentProperty(p); + p.addSubProperty(p); + return p; + } + + protected void setEquivalent(OntProperty p1, OntProperty p2) { + setEquivalentProperty(equivalences(p1), equivalences(p2)); + setEquivalentProperty(equivalences(p2), equivalences(p1)); + copyInverses(p1, p2); + copyInverses(p2, p1); + } + + private void setEquivalentProperty(Set equivalences1, + Set equivalences2) { + for (OntProperty p1 : equivalences1) { + for (OntProperty p2 : equivalences2) { + p1.addEquivalentProperty(p2); + } + } + } + + private void copyInverses(OntProperty p1, OntProperty p2) { + for (OntProperty inv : p1.listInverse().toList()) { + p2.addInverseOf(inv); + } + } + + protected void setInverse(OntProperty p1, OntProperty p2) { + setInverse(equivalences(p1), equivalences(p2)); + setInverse(equivalences(p2), equivalences(p1)); + } + + private void setInverse(Set equivalences1, + Set equivalences2) { + for (OntProperty p1 : equivalences1) { + for (OntProperty p2 : equivalences2) { + p1.addInverseOf(p2); + } + } + } + + protected void removeInverse(OntProperty p1, OntProperty p2) { + removeInverse(equivalences(p1), equivalences(p2)); + removeInverse(equivalences(p2), equivalences(p1)); + } + + private void removeInverse(Set equivalences1, + Set equivalences2) { + for (OntProperty p1 : equivalences1) { + for (OntProperty p2 : equivalences2) { + p1.removeInverseProperty(p2); + } + } + } + + private Set equivalences(OntProperty p) { + return new HashSet(p.listEquivalentProperties().toSet()); + } + +} diff --git a/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerTest.java b/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerTest.java index 7b17d1303..237c18622 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerTest.java @@ -7,7 +7,6 @@ import org.junit.Assert; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; -import org.mindswap.pellet.jena.PelletReasonerFactory; import com.hp.hpl.jena.ontology.AnnotationProperty; import com.hp.hpl.jena.ontology.OntClass; @@ -21,11 +20,10 @@ import com.hp.hpl.jena.rdf.model.Statement; import com.hp.hpl.jena.vocabulary.OWL; import com.hp.hpl.jena.vocabulary.RDF; -import edu.cornell.mannlib.vitro.testing.AbstractTestClass; import edu.cornell.mannlib.vitro.webapp.utils.threads.VitroBackgroundThread; -public class SimpleReasonerTest extends AbstractTestClass { +public class SimpleReasonerTest extends SimpleReasonerTBoxHelper { private static final String mostSpecificTypePropertyURI = "http://vitro.mannlib.cornell.edu/ns/vitro/0.7#mostSpecificType"; long delay = 50; @@ -54,15 +52,9 @@ public class SimpleReasonerTest extends AbstractTestClass { public void addABoxTypeAssertion1( boolean sameAsEnabled ){ // Create a Tbox with a simple class hierarchy. B is a subclass of A. - // Pellet will compute TBox inferences - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - + OntModel tBox = createTBoxModel(); + OntClass classA = createClass(tBox, "http://test.vivo/A", "class A"); + OntClass classB = createClass(tBox, "http://test.vivo/B", "class B"); classA.addSubClass(classB); // this is the model to receive inferences @@ -101,30 +93,18 @@ public class SimpleReasonerTest extends AbstractTestClass { 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. Pellet will compute TBox inferences. - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); + // 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); - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - - OntClass classC = tBox.createClass("http://test.vivo/C"); - classC.setLabel("class C", "en-US"); - - OntClass classD = tBox.createClass("http://test.vivo/D"); - classD.setLabel("class D", "en-US"); - - OntClass classE = tBox.createClass("http://test.vivo/E"); - classE.setLabel("class E", "en-US"); - - classC.addSubClass(classD); - classC.addSubClass(classE); - - classA.addSubClass(classB); - classA.addSubClass(classC); - // this is the model to receive inferences Model inf = ModelFactory.createDefaultModel(); @@ -159,12 +139,17 @@ public class SimpleReasonerTest extends AbstractTestClass { * Test inference based on class equivalence */ public void addABoxTypeAssertion3(boolean enableSameAs) throws InterruptedException { - - // Create TBox, ABox and Inference models and register - // the ABox reasoner listeners with the ABox and TBox - // Pellet will compute TBox inferences - - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); + + // Add classes A, B and C to the TBox + // A is equivalent to B + // C 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(classA, classC); + setEquivalent(classA, classB); + OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); Model inf = ModelFactory.createDefaultModel(); @@ -175,22 +160,6 @@ public class SimpleReasonerTest extends AbstractTestClass { SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner); tBox.register(simpleReasonerTBoxListener); - // Add classes A, B and C to the TBox - // A is equivalent to B - // C is a subclass of A - - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - - OntClass classC = tBox.createClass("http://test.vivo/C"); - classC.setLabel("class C", "en-US"); - - classA.addEquivalentClass(classB); - classA.addSubClass(classC); - while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); } @@ -223,11 +192,13 @@ public class SimpleReasonerTest extends AbstractTestClass { */ public void addABoxTypeAssertion4(boolean enableSameAs) throws InterruptedException { - // Create TBox, ABox and Inference models and register - // the ABox reasoner listeners with the ABox and TBox - // Pellet will compute TBox inferences + // Add classes A and B to the TBox + // A is equivalent to B + OntModel tBox = createTBoxModel(); + OntClass classA = createClass(tBox, "http://test.vivo/A", "class A"); + OntClass classB = createClass(tBox, "http://test.vivo/B", "class B"); + setEquivalent(classA, classB); - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); Model inf = ModelFactory.createDefaultModel(); @@ -238,17 +209,6 @@ public class SimpleReasonerTest extends AbstractTestClass { SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner); tBox.register(simpleReasonerTBoxListener); - // Add classes A and B to the TBox - // A is equivalent to B - - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - - classA.addEquivalentClass(classB); - while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); } @@ -277,12 +237,14 @@ public class SimpleReasonerTest extends AbstractTestClass { * Test inference based on class equivalence */ public void addABoxTypeAssertion5(boolean enableSameAs) throws InterruptedException { - - // Create TBox, ABox and Inference models and register - // the ABox reasoner listeners with the ABox and TBox - // Pellet will compute TBox inferences - - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); + + // Add classes classes A and B to the TBox + // A is equivalent to B + OntModel tBox = createTBoxModel(); + OntClass classA = createClass(tBox, "http://test.vivo/A", "class A"); + OntClass classB = createClass(tBox, "http://test.vivo/B", "class B"); + setEquivalent(classA, classB); + OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); Model inf = ModelFactory.createDefaultModel(); @@ -293,17 +255,6 @@ public class SimpleReasonerTest extends AbstractTestClass { SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner); tBox.register(simpleReasonerTBoxListener); - // Add classes classes A and B to the TBox - // A is equivalent to B - - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - - classA.addEquivalentClass(classB); - while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); } @@ -326,11 +277,11 @@ public class SimpleReasonerTest extends AbstractTestClass { } @Test - public void removeABoxTypeAssertion1Test()throws InterruptedException{ + public void removeABoxTypeAssertion1Test() { removeABoxTypeAssertion1(true); } @Test - public void removeABoxTypeAssertion1NoSameAs()throws InterruptedException{ + public void removeABoxTypeAssertion1NoSameAs() { removeABoxTypeAssertion1(false); } /* @@ -344,22 +295,15 @@ public class SimpleReasonerTest extends AbstractTestClass { 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. Pellet will compute TBox inferences. + // and B is a subclass of A. - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - - OntClass classC = tBox.createClass("http://test.vivo/C"); - classC.setLabel("class C", "en-US"); + 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); - classB.addSubClass(classC); - classA.addSubClass(classB); - // this is the model to receive inferences Model inf = ModelFactory.createDefaultModel(); @@ -415,12 +359,15 @@ public class SimpleReasonerTest extends AbstractTestClass { * as a test of equivalentClass statements also. */ public void addTBoxSubClassAssertion1(boolean enableSameAs) throws InterruptedException { - - // Create TBox, ABox and Inference models and register - // the ABox reasoner listeners with the ABox and TBox - // Pellet will compute TBox inferences + // Create the TBox and add classes A, B, C and D + 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"); - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); + // 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(); @@ -431,27 +378,12 @@ public class SimpleReasonerTest extends AbstractTestClass { SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner); tBox.register(simpleReasonerTBoxListener); - // Add classes classes A, B, C and D to the TBox - - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - - OntClass classC = tBox.createClass("http://test.vivo/C"); - classC.setLabel("class C", "en-US"); - - OntClass classD = tBox.createClass("http://test.vivo/D"); - classD.setLabel("class D", "en-US"); - // Add a statement that individual x is of type C to the ABox Resource ind_x = aBox.createResource("http://test.vivo/x"); aBox.add(ind_x, RDF.type, classC); // Add a statement that C is a subclass of A to the TBox - - classA.addSubClass(classC); + addSubclass(classA, classC); while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); @@ -498,11 +430,17 @@ public class SimpleReasonerTest extends AbstractTestClass { */ public void addTBoxSubClassAssertion2(boolean enableSameAs) throws InterruptedException { - // Create TBox, ABox and Inference models and register - // the ABox reasoner listeners with the ABox and TBox - // Pellet will compute TBox inferences + // Create the TBox and add classes A, B, C and D. + // D is a subclass of C. + 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"); + addSubclass(classC, classD); - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); + // 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(); @@ -513,23 +451,6 @@ public class SimpleReasonerTest extends AbstractTestClass { SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner); tBox.register(simpleReasonerTBoxListener); - // Add classes classes A, B, C and D to the TBox - // D is a subclass of C - - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - - OntClass classC = tBox.createClass("http://test.vivo/C"); - classC.setLabel("class C", "en-US"); - - OntClass classD = tBox.createClass("http://test.vivo/D"); - classD.setLabel("class D", "en-US"); - - classC.addSubClass(classD); - while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); } @@ -539,7 +460,7 @@ public class SimpleReasonerTest extends AbstractTestClass { aBox.add(ind_x, RDF.type, classD); // Add a statement that C is a subclass of A to the TBox - classA.addSubClass(classC); + addSubclass(classA, classC); while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); @@ -569,11 +490,30 @@ public class SimpleReasonerTest extends AbstractTestClass { * */ public void removeTBoxSubClassAssertion1(boolean enableSameAs) throws InterruptedException { - // Create TBox, ABox and Inference models and register - // the ABox reasoner listeners with the ABox and TBox - // Pellet will compute TBox inferences + // Create the TBox and add classes A, B, C, D, E, F, G and H. + // B, C and D are subclasses of A. + // E is a subclass of B. + // F and G are subclasses of C. + // H is a subclass of D. + 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"); + OntClass classF = createClass(tBox, "http://test.vivo/F", "class F"); + OntClass classG = createClass(tBox, "http://test.vivo/G", "class G"); + OntClass classH = createClass(tBox, "http://test.vivo/H", "class H"); + addSubclass(classA, classB); + addSubclass(classA, classC); + addSubclass(classA, classD); + addSubclass(classB, classE); + addSubclass(classC, classF); + addSubclass(classC, classG); + addSubclass(classD, classH); - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); + // 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(); @@ -584,44 +524,6 @@ public class SimpleReasonerTest extends AbstractTestClass { SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner); tBox.register(simpleReasonerTBoxListener); - // Add classes A, B, C, D, E, F, G and H to the TBox. - // B, C and D are subclasses of A. - // E is a subclass of B. - // F and G are subclasses of C. - // H is a subclass of D. - - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - - OntClass classC = tBox.createClass("http://test.vivo/C"); - classC.setLabel("class C", "en-US"); - - OntClass classD = tBox.createClass("http://test.vivo/D"); - classD.setLabel("class D", "en-US"); - - OntClass classE = tBox.createClass("http://test.vivo/E"); - classE.setLabel("class E", "en-US"); - - OntClass classF = tBox.createClass("http://test.vivo/F"); - classF.setLabel("class F", "en-US"); - - OntClass classG = tBox.createClass("http://test.vivo/G"); - classG.setLabel("class G", "en-US"); - - OntClass classH = tBox.createClass("http://test.vivo/H"); - classH.setLabel("class H", "en-US"); - - classA.addSubClass(classB); - classA.addSubClass(classC); - classA.addSubClass(classD); - classB.addSubClass(classE); - classC.addSubClass(classF); - classC.addSubClass(classG); - classD.addSubClass(classH); - while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); } @@ -631,7 +533,7 @@ public class SimpleReasonerTest extends AbstractTestClass { aBox.add(ind_x, RDF.type, classE); // Remove the statement that B is a subclass of A from the TBox - classA.removeSubClass(classB); + removeSubclass(classA, classB); while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); @@ -664,13 +566,13 @@ public class SimpleReasonerTest extends AbstractTestClass { simpleReasonerTBoxListener.setStopRequested(); } - @Ignore(" needs PelletListener infrastructure which is not in this suite.") + @Ignore(" needs TBoxReasoner infrastructure which is not in this suite.") @Test public void bcdTest()throws InterruptedException{ bcd(true); } - @Ignore(" needs PelletListener infrastructure which is not in this suite.") + @Ignore(" needs TBoxReasoner infrastructure which is not in this suite.") @Test public void bcdNoSameAsTest()throws InterruptedException{ bcd(false); @@ -682,17 +584,16 @@ public class SimpleReasonerTest extends AbstractTestClass { * inference graph. * */ - // this test would need PelletListener infrastructure, which we're not + // this test would need TBoxReasoner infrastructure, which we're not // testing in this suite. The reason it doesn't work as it is because // the SimpleReasonerTBoxListener is not listening to the tBox inference - // model as Pellet is updating it. I could simulate it by adding to the - // tBox assertions what we can count on Pellet to infer. + // model as the TBoxReasoner is updating it. I could simulate it by adding to the + // tBox assertions what we can count on the TBoxReasoner to infer. public void bcd(boolean enableSameAs) throws InterruptedException { - // Create TBox, ABox and Inference models and register + OntModel tBox = createTBoxModel(); + + // Create ABox and Inference models and register // the ABox reasoner listeners with the ABox and TBox - // Pellet will compute TBox inferences - - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); Model inf = ModelFactory.createDefaultModel(); @@ -704,18 +605,11 @@ public class SimpleReasonerTest extends AbstractTestClass { // Add classes LivingThing, Flora, Brassica to the TBox // Brassica is a subClass of Flora and Flora is a subclass of Brassica - - OntClass LivingThing = tBox.createClass("http://test.vivo/LivingThing"); - LivingThing.setLabel("Living Thing", "en-US"); - - OntClass Flora = tBox.createClass("http://test.vivo/Flora"); - Flora.setLabel("Flora", "en-US"); - - OntClass Brassica = tBox.createClass("http://test.vivo/Brassica"); - Brassica.setLabel("Brassica", "en-US"); - - LivingThing.addSubClass(Flora); - Flora.addSubClass(Brassica); + OntClass LivingThing = createClass(tBox, "http://test.vivo/LivingThing", "Living Thing"); + OntClass Flora = createClass(tBox, "http://test.vivo/Flora", "Flora"); + OntClass Brassica = createClass(tBox, "http://test.vivo/Brassica", "Brassica"); + addSubclass(LivingThing, Flora); + addSubclass(Flora, Brassica); tBox.rebind(); tBox.prepare(); @@ -761,11 +655,23 @@ public class SimpleReasonerTest extends AbstractTestClass { * to an added/removed ABox type assertion. */ public void mstTest1(boolean enableSameAs) throws InterruptedException { - // Create TBox, ABox and Inference models and register + // Set up the Tbox with a class hierarchy. C is a subclass of A + // and Y. D and E are subclasses of C. B is a subclass of D. + 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"); + OntClass classY = createClass(tBox, "http://test.vivo/Y", "class Y"); + addSubclass(classA, classC); + addSubclass(classY, classC); + addSubclass(classC, classD); + addSubclass(classC, classE); + addSubclass(classD, classB); + + // Create ABox and Inference models and register // the ABox reasoner listeners with the ABox and TBox - // Pellet will compute TBox inferences - - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); Model inf = ModelFactory.createDefaultModel(); @@ -776,38 +682,8 @@ public class SimpleReasonerTest extends AbstractTestClass { SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner); tBox.register(simpleReasonerTBoxListener); - // Set up the Tbox with a class hierarchy. C is a subclass of A - // and Y. D and E are subclasses C. B is a subclass of D. - // Pellet will compute TBox inferences. - AnnotationProperty mostSpecificType = tBox.createAnnotationProperty(mostSpecificTypePropertyURI); - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - - OntClass classC = tBox.createClass("http://test.vivo/C"); - classC.setLabel("class C", "en-US"); - - OntClass classD = tBox.createClass("http://test.vivo/D"); - classD.setLabel("class D", "en-US"); - - OntClass classE = tBox.createClass("http://test.vivo/E"); - classE.setLabel("class E", "en-US"); - - OntClass classY = tBox.createClass("http://test.vivo/Y"); - classE.setLabel("class Y", "en-US"); - - classY.addSubClass(classC); - classA.addSubClass(classC); - - classC.addSubClass(classD); - classC.addSubClass(classE); - - classD.addSubClass(classB); - while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); } @@ -848,11 +724,17 @@ public class SimpleReasonerTest extends AbstractTestClass { * to an added ABox type assertion. */ public void mstTest2(boolean enableSameAs) throws InterruptedException { - // Create TBox, ABox and Inference models and register + // Set up the Tbox with a class hierarchy. A, B, and C are all equivalent. + // This implies that they are all subclasses of each other. + 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"); + setEquivalent(classA, classB); + setEquivalent(classB, classC); + + // Create ABox and Inference models and register // the ABox reasoner listeners with the ABox and TBox - // Pellet will compute TBox inferences - - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); Model inf = ModelFactory.createDefaultModel(); @@ -863,25 +745,8 @@ public class SimpleReasonerTest extends AbstractTestClass { SimpleReasonerTBoxListener simpleReasonerTBoxListener = getTBoxListener(simpleReasoner); tBox.register(simpleReasonerTBoxListener); - // Set up the Tbox with a class hierarchy. B is a subclass of A, - // C is a subclass of B, and A is a subclass of C. - // Pellet should infer these three classes to be equivalent. - AnnotationProperty mostSpecificType = tBox.createAnnotationProperty(mostSpecificTypePropertyURI); - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - - OntClass classC = tBox.createClass("http://test.vivo/C"); - classC.setLabel("class C", "en-US"); - - classA.addSubClass(classB); - classB.addSubClass(classC); - classC.addSubClass(classA); - while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); } @@ -911,11 +776,18 @@ public class SimpleReasonerTest extends AbstractTestClass { * to an added/removed TBox assertions. */ public void mstTest3(boolean enableSameAs) throws InterruptedException { - // Create TBox, ABox and Inference models and register + // Set up the Tbox with classes A, B, C, D, E, F and G + 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"); + OntClass classF = createClass(tBox, "http://test.vivo/F", "class F"); + OntClass classG = createClass(tBox, "http://test.vivo/G", "class G"); + + // Create ABox and Inference models and register // the ABox reasoner listeners with the ABox and TBox - // Pellet will compute TBox inferences - - OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); Model inf = ModelFactory.createDefaultModel(); @@ -928,30 +800,6 @@ public class SimpleReasonerTest extends AbstractTestClass { OntClass OWL_THING = tBox.createClass(OWL.Thing.getURI()); AnnotationProperty mostSpecificType = tBox.createAnnotationProperty(mostSpecificTypePropertyURI); - - // Set up the Tbox with classes A, B, C, D, - // E, F and G - - OntClass classA = tBox.createClass("http://test.vivo/A"); - classA.setLabel("class A", "en-US"); - - OntClass classB = tBox.createClass("http://test.vivo/B"); - classB.setLabel("class B", "en-US"); - - OntClass classC = tBox.createClass("http://test.vivo/C"); - classC.setLabel("class C", "en-US"); - - OntClass classD = tBox.createClass("http://test.vivo/D"); - classD.setLabel("class D", "en-US"); - - OntClass classE = tBox.createClass("http://test.vivo/E"); - classE.setLabel("class E", "en-US"); - - OntClass classF = tBox.createClass("http://test.vivo/F"); - classF.setLabel("class F", "en-US"); - - OntClass classG = tBox.createClass("http://test.vivo/G"); - classE.setLabel("class G", "en-US"); while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); @@ -975,17 +823,13 @@ public class SimpleReasonerTest extends AbstractTestClass { Assert.assertTrue(inf.contains(ind_y, mostSpecificType, ResourceFactory.createResource(classD.getURI()))); Assert.assertTrue(inf.contains(ind_y, mostSpecificType, ResourceFactory.createResource(classF.getURI()))); - // Set up a class hierarchy. - // Pellet will compute TBox inferences. - - classA.addSubClass(classB); - classA.addSubClass(classC); - classA.addSubClass(classD); - - classC.addSubClass(classE); - - classD.addSubClass(classF); - classD.addSubClass(classG); + // Set up a class hierarchy. + addSubclass(classA, classB); + addSubclass(classA, classC); + addSubclass(classA, classD); + addSubclass(classC, classE); + addSubclass(classD, classF); + addSubclass(classD, classG); while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay); @@ -996,7 +840,7 @@ public class SimpleReasonerTest extends AbstractTestClass { // If F is removed as a subclass of D, then D should once again be a most specific type // for y. - classD.removeSubClass(classF); + removeSubclass(classD, classF); while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { Thread.sleep(delay);