From 66b12eb4be79bdb68adfe3212d8d600be12ca7b2 Mon Sep 17 00:00:00 2001 From: stellamit Date: Mon, 11 Jun 2012 00:19:54 +0000 Subject: [PATCH] NIHVIVO-3697 sameAs support in SimpleReasoner --- .../vitro/webapp/reasoner/SimpleReasoner.java | 52 ++++++++++--------- .../reasoner/SimpleReasonerSameAsTest.java | 32 +++++------- 2 files changed, 40 insertions(+), 44 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasoner.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasoner.java index d32851ec5..b23e67017 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasoner.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasoner.java @@ -890,31 +890,35 @@ public class SimpleReasoner extends StatementListener { // subject is of type cls; otherwise returns false. protected boolean entailedType(Resource subject, OntClass cls) { + List sameIndividuals = getSameIndividuals(subject,inferenceModel); + sameIndividuals.add(subject); + tboxModel.enterCriticalSection(Lock.READ); - aboxModel.enterCriticalSection(Lock.READ); - - try { - List subclasses = null; - subclasses = (cls.listSubClasses(false)).toList(); - subclasses.addAll((cls.listEquivalentClasses()).toList()); - - Iterator iter = subclasses.iterator(); - - while (iter.hasNext()) { - OntClass childClass = iter.next(); - if (childClass.equals(cls)) continue; - Statement stmt = ResourceFactory.createStatement(subject, RDF.type, childClass); - if (aboxModel.contains(stmt)) return true; - } - - return false; - } catch (Exception e) { - log.debug("exception in method entailedType: " + e.getMessage()); - return false; - } finally { - aboxModel.leaveCriticalSection(); - tboxModel.leaveCriticalSection(); - } + try { + aboxModel.enterCriticalSection(Lock.READ); + try { + List subclasses = null; + subclasses = (cls.listSubClasses(false)).toList(); + subclasses.addAll((cls.listEquivalentClasses()).toList()); + + Iterator iter = subclasses.iterator(); + while (iter.hasNext()) { + OntClass childClass = iter.next(); + if (childClass.equals(cls)) continue; // TODO - determine whether this is needed + Iterator sameIter = sameIndividuals.iterator(); + while (sameIter.hasNext()) { + Statement stmt = ResourceFactory.createStatement(sameIter.next(), RDF.type, childClass); + if (aboxModel.contains(stmt)) + return true; + } + } + return false; + } finally { + aboxModel.leaveCriticalSection(); + } + } finally { + tboxModel.leaveCriticalSection(); + } } // Returns true if the triple is entailed by inverse property 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 b7b3574d4..7f1b083c0 100644 --- a/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerSameAsTest.java +++ b/webapp/test/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasonerSameAsTest.java @@ -555,21 +555,9 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { */ //@Test public void mostSpecificTypeTest1() throws InterruptedException { - // Create TBox, ABox and Inference models and register - // the ABox reasoner listeners with the ABox and TBox - // Pellet will compute TBox inferences + + // set up tbox. Pellet is reasoning; SimpleReasonerTBoxListener is not being used. OntModel tBox = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC); - 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 the Tbox with a class hierarchy. C is a - // subclass of A. D and E are subclasses C. - // Pellet will compute TBox inferences. AnnotationProperty mostSpecificType = tBox.createAnnotationProperty(mostSpecificTypePropertyURI); OntClass classA = tBox.createClass("http://test.vivo/A"); @@ -584,10 +572,16 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { classA.addSubClass(classC); classC.addSubClass(classD); classC.addSubClass(classE); - - while (!VitroBackgroundThread.getLivingThreads().isEmpty()) { - Thread.sleep(delay); - } + + // this will receive the abox inferences + Model inf = ModelFactory.createDefaultModel(); + + // abox + OntModel aBox = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); + + // set up SimpleReasoner and register it with abox + SimpleReasoner simpleReasoner = new SimpleReasoner(tBox, aBox, inf); + aBox.register(simpleReasoner); // add & remove ABox type statements and verify inferences Resource a = aBox.createResource("http://test.vivo/a"); @@ -636,8 +630,6 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass { Assert.assertFalse(inf.contains(b, mostSpecificType, ResourceFactory.createResource(classD.getURI()))); Assert.assertFalse(inf.contains(c, mostSpecificType, ResourceFactory.createResource(classD.getURI()))); Assert.assertFalse(inf.contains(d, mostSpecificType, ResourceFactory.createResource(classD.getURI()))); - - simpleReasonerTBoxListener.setStopRequested(); } /*