From 39f852dc382b8beb32a9060ae06d82c8d84f2efd Mon Sep 17 00:00:00 2001 From: stellamit Date: Thu, 12 Jul 2012 20:35:20 +0000 Subject: [PATCH] merging SimpleReasoner from 1.5 branch --- .../vitro/webapp/reasoner/SimpleReasoner.java | 229 +++++++++++------- 1 file changed, 145 insertions(+), 84 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 75eedeb5c..0cb91e71b 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasoner.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/reasoner/SimpleReasoner.java @@ -220,18 +220,11 @@ public class SimpleReasoner extends StatementListener { return; } - //log.debug("added TBox assertion = " + stmt.toString()); - - if ( stmt.getObject().isResource() && (stmt.getObject().asResource()).getURI() == null ) { - log.warn("The object of this assertion has a null URI: " + stmtString(stmt)); + if (!stmt.getObject().isResource()) { + log.warn("The object of this assertion is not a resource: " + stmtString(stmt)); return; } - - if ( stmt.getSubject().getURI() == null ) { - log.warn("The subject of this assertion has a null URI: " + stmtString(stmt)); - return; - } - + if (stmt.getPredicate().equals(RDFS.subClassOf) || stmt.getPredicate().equals(OWL.equivalentClass)) { // ignore anonymous classes if (stmt.getSubject().isAnon() || stmt.getObject().isAnon()) { @@ -257,7 +250,17 @@ public class SimpleReasoner extends StatementListener { addedSubClass(subject,object,inferenceModel); addedSubClass(object,subject,inferenceModel); } - } else { + } else { + if ( stmt.getObject().asResource().getURI() == null ) { + log.warn("The object of this assertion has a null URI: " + stmtString(stmt)); + return; + } + + if ( stmt.getSubject().getURI() == null ) { + log.warn("The subject of this assertion has a null URI: " + stmtString(stmt)); + return; + } + OntProperty prop1 = tboxModel.getOntProperty((stmt.getSubject()).getURI()); if (prop1 == null) { log.debug("didn't find subject property in the tbox: " + (stmt.getSubject()).getURI()); @@ -290,18 +293,11 @@ public class SimpleReasoner extends StatementListener { return; } - //log.debug("removed TBox assertion = " + stmt.toString()); - - if ( stmt.getObject().isResource() && (stmt.getObject().asResource()).getURI() == null ) { - log.warn("The object of this assertion has a null URI: " + stmtString(stmt)); + if (!stmt.getObject().isResource()) { + log.warn("The object of this assertion is not a resource: " + stmtString(stmt)); return; } - - if ( stmt.getSubject().getURI() == null ) { - log.warn("The subject of this assertion has a null URI: " + stmtString(stmt)); - return; - } - + if ( stmt.getPredicate().equals(RDFS.subClassOf) || stmt.getPredicate().equals(OWL.equivalentClass) ) { // ignore anonymous classes @@ -329,6 +325,16 @@ public class SimpleReasoner extends StatementListener { removedSubClass(object,subject,inferenceModel); } } else { + if ( stmt.getObject().asResource().getURI() == null ) { + log.warn("The object of this assertion has a null URI: " + stmtString(stmt)); + return; + } + + if ( stmt.getSubject().getURI() == null ) { + log.warn("The subject of this assertion has a null URI: " + stmtString(stmt)); + return; + } + OntProperty prop1 = tboxModel.getOntProperty((stmt.getSubject()).getURI()); if (prop1 == null) { log.debug("didn't find subject property in the tbox: " + (stmt.getSubject()).getURI()); @@ -361,24 +367,18 @@ public class SimpleReasoner extends StatementListener { tboxModel.enterCriticalSection(Lock.READ); try { - OntClass cls = null; + Resource cls = null; if ( (stmt.getObject().asResource()).getURI() != null ) { - cls = tboxModel.getOntClass(stmt.getObject().asResource().getURI()); + cls = tboxModel.getResource(stmt.getObject().asResource().getURI()); if (cls != null) { - List parents = null; - try { - parents = (cls.listSuperClasses(false)).toList(); - parents.addAll((cls.listEquivalentClasses()).toList()); - } catch (ConversionException ce) { - parents = getParents(cls,tboxModel); - } + List parents = getParents(cls,tboxModel); - Iterator parentIt = parents.iterator(); + Iterator parentIt = parents.iterator(); if (parentIt.hasNext()) { while (parentIt.hasNext()) { - OntClass parentClass = parentIt.next(); + Resource parentClass = parentIt.next(); // VIVO doesn't materialize statements that assert anonymous types // for individuals. Also, sharing an identical anonymous node is @@ -400,7 +400,7 @@ public class SimpleReasoner extends StatementListener { } } } else { - log.warn("The object of this rdf:type assertion has a null URI: " + stmtString(stmt)); + log.debug("The object of this rdf:type assertion has a null URI, no reasoning will be done based on this assertion: " + stmtString(stmt)); return; } } finally { @@ -426,29 +426,23 @@ public class SimpleReasoner extends StatementListener { protected void removedABoxTypeAssertion(Statement stmt, Model inferenceModel) { tboxModel.enterCriticalSection(Lock.READ); try { - OntClass cls = null; + Resource cls = null; if ( (stmt.getObject().asResource()).getURI() != null ) { - cls = tboxModel.getOntClass(stmt.getObject().asResource().getURI()); + cls = tboxModel.getResource(stmt.getObject().asResource().getURI()); if (cls != null) { if (entailedType(stmt.getSubject(),cls)) { addInference(stmt,inferenceModel,true); } - List parents = null; - try { - parents = (cls.listSuperClasses(false)).toList(); - parents.addAll((cls.listEquivalentClasses()).toList()); - } catch (ConversionException ce) { - parents = getParents(cls,tboxModel); - } + List parents = getParents(cls,tboxModel); - Iterator parentIt = parents.iterator(); + Iterator parentIt = parents.iterator(); while (parentIt.hasNext()) { - OntClass parentClass = parentIt.next(); + Resource parentClass = parentIt.next(); // VIVO doesn't materialize statements that assert anonymous types // for individuals. Also, sharing an identical anonymous node is @@ -889,39 +883,92 @@ public class SimpleReasoner extends StatementListener { // Returns true if it is entailed by class subsumption that // subject is of type cls; otherwise returns false. - protected boolean entailedType(Resource subject, OntClass cls) { + protected boolean entailedType(Resource subject, Resource cls) { List sameIndividuals = getSameIndividuals(subject,inferenceModel); sameIndividuals.add(subject); - tboxModel.enterCriticalSection(Lock.READ); - 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; - } + List subClasses = getSubClasses(cls); + aboxModel.enterCriticalSection(Lock.READ); + try { + Iterator iter = subClasses.iterator(); + while (iter.hasNext()) { + Resource childClass = iter.next(); + if (childClass.equals(cls)) continue; + 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(); - } + } + return false; + } finally { + aboxModel.leaveCriticalSection(); + } } - + + protected List getSubClasses(Resource cls) { + List subClasses = new ArrayList(); + tboxModel.enterCriticalSection(Lock.READ); + try { + Iterator iter = tboxModel.listStatements((Resource) null, RDFS.subClassOf, cls); + while (iter.hasNext()) { + Statement stmt = iter.next(); + if (stmt.getSubject() == null || stmt.getSubject().asResource().getURI() == null) continue; + if (!subClasses.contains(stmt.getSubject())) { + subClasses.add(stmt.getSubject()); + } + } + + iter = tboxModel.listStatements((Resource) null, OWL.equivalentClass, cls); + while (iter.hasNext()) { + Statement stmt = iter.next(); + if (stmt.getSubject() == null || stmt.getSubject().getURI() == null) continue; + if (!subClasses.contains(stmt.getSubject())) { + subClasses.add(stmt.getSubject()); + } + } + + return subClasses; + } finally { + tboxModel.leaveCriticalSection(); + } + } + + protected List getSuperClasses(Resource cls) { + List superClasses = new ArrayList(); + tboxModel.enterCriticalSection(Lock.READ); + try { + Iterator iter = tboxModel.listStatements(cls, RDFS.subClassOf, (RDFNode) null); + while (iter.hasNext()) { + Statement stmt = iter.next(); + + if (stmt.getObject() != null && stmt.getObject().isResource()) { + Resource superCls = stmt.getObject().asResource(); + + if (superCls.isAnon() || superClasses.contains(superCls)) { + continue; + } + superClasses.add(superCls); + } + } + + iter = tboxModel.listStatements((Resource) null, OWL.equivalentClass, cls); + while (iter.hasNext()) { + Statement stmt = iter.next(); + if (stmt.getSubject() == null || stmt.getSubject().asResource().getURI() == null) continue; + if (!superClasses.contains(stmt.getSubject())) { + superClasses.add(stmt.getSubject()); + } + } + return superClasses; + } finally { + tboxModel.leaveCriticalSection(); + } + } + // Returns true if the triple is entailed by inverse property // reasoning or sameAs reasoning; otherwise returns false. protected boolean entailedStatement(Statement stmt) { @@ -1162,7 +1209,7 @@ public class SimpleReasoner extends StatementListener { if ( (stmt.getObject().asResource()).getURI() != null ) { ontClass = tboxModel.getOntClass(stmt.getObject().asResource().getURI()); } else { - log.warn("The object of this rdf:type assertion has a null URI: " + stmtString(stmt)); + log.debug("The object of this rdf:type assertion has a null URI: " + stmtString(stmt)); continue; } @@ -1170,7 +1217,7 @@ public class SimpleReasoner extends StatementListener { if ( !(stmt.getObject().asResource().getNameSpace()).equals(OWL.NS)) { if (!unknownTypes.contains(stmt.getObject().asResource().getURI())) { unknownTypes.add(stmt.getObject().asResource().getURI()); - log.warn("Didn't find the target class (the object of an asserted or inferred rdf:type statement) in the TBox: " + + log.debug("Didn't find the target class (the object of an asserted or inferred rdf:type statement) in the TBox: " + (stmt.getObject().asResource()).getURI() + ". No mostSpecificType computation will be done based on " + (stmt.getObject().asResource()).getURI() + " type statements."); } } @@ -1208,10 +1255,22 @@ public class SimpleReasoner extends StatementListener { if (add) { typeURIs.add(type.getURI()); - Iterator eIter = type.listEquivalentClasses(); + ArrayList equivalentClasses = new ArrayList(); + + Iterator iter = tboxModel.listStatements((Resource) null, OWL.equivalentClass, type); + while (iter.hasNext()) { + Statement stmt = iter.next(); + Resource res = stmt.getSubject(); + if ((res == null) || res.isAnon() || equivalentClasses.contains(res) ) { + continue; + } + equivalentClasses.add(res); + } + + Iterator eIter = equivalentClasses.iterator(); while (eIter.hasNext()) { - OntClass equivClass = eIter.next(); + Resource equivClass = eIter.next(); if (equivClass.isAnon()) continue; typeURIs.add(equivClass.getURI()); } @@ -1267,29 +1326,31 @@ public class SimpleReasoner extends StatementListener { return; } - protected List getParents(OntClass cls, OntModel tboxModel) { + protected List getParents(Resource cls, OntModel tboxModel) { - List parents = new ArrayList(); + List parents = new ArrayList(); tboxModel.enterCriticalSection(Lock.READ); try { StmtIterator iter = tboxModel.listStatements(cls, RDFS.subClassOf, (RDFNode) null); while (iter.hasNext()) { Statement stmt = iter.next(); - if (!stmt.getObject().isAnon() && stmt.getObject().canAs(OntClass.class)) { - if (!parents.contains(stmt.getObject().as(OntClass.class))) { - parents.add(stmt.getObject().as(OntClass.class)); - } + if (stmt.getObject() == null || !stmt.getObject().isResource() || stmt.getObject().asResource().isAnon()) { + continue; + } + if (!parents.contains(stmt.getObject().asResource())) { + parents.add(stmt.getObject().asResource()); } } iter = tboxModel.listStatements(cls, OWL.equivalentClass, (RDFNode) null); while (iter.hasNext()) { Statement stmt = iter.next(); - if (!stmt.getObject().isAnon() && stmt.getObject().canAs(OntClass.class)) { - if (!parents.contains(stmt.getObject().as(OntClass.class))) { - parents.add(stmt.getObject().as(OntClass.class)); - } + if (stmt.getObject() == null || !stmt.getObject().isResource() || stmt.getObject().asResource().isAnon()) { + continue; + } + if (!parents.contains(stmt.getObject().asResource())) { + parents.add(stmt.getObject().asResource()); } } } catch (Exception e) {