merging SimpleReasoner from 1.5 branch
This commit is contained in:
parent
00cbf83d69
commit
39f852dc38
1 changed files with 145 additions and 84 deletions
|
@ -220,15 +220,8 @@ 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));
|
||||
return;
|
||||
}
|
||||
|
||||
if ( stmt.getSubject().getURI() == null ) {
|
||||
log.warn("The subject 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;
|
||||
}
|
||||
|
||||
|
@ -258,6 +251,16 @@ public class SimpleReasoner extends StatementListener {
|
|||
addedSubClass(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());
|
||||
|
@ -290,15 +293,8 @@ 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));
|
||||
return;
|
||||
}
|
||||
|
||||
if ( stmt.getSubject().getURI() == null ) {
|
||||
log.warn("The subject 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;
|
||||
}
|
||||
|
||||
|
@ -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<OntClass> parents = null;
|
||||
try {
|
||||
parents = (cls.listSuperClasses(false)).toList();
|
||||
parents.addAll((cls.listEquivalentClasses()).toList());
|
||||
} catch (ConversionException ce) {
|
||||
parents = getParents(cls,tboxModel);
|
||||
}
|
||||
List<Resource> parents = getParents(cls,tboxModel);
|
||||
|
||||
Iterator<OntClass> parentIt = parents.iterator();
|
||||
Iterator<Resource> 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<OntClass> parents = null;
|
||||
try {
|
||||
parents = (cls.listSuperClasses(false)).toList();
|
||||
parents.addAll((cls.listEquivalentClasses()).toList());
|
||||
} catch (ConversionException ce) {
|
||||
parents = getParents(cls,tboxModel);
|
||||
}
|
||||
List<Resource> parents = getParents(cls,tboxModel);
|
||||
|
||||
Iterator<OntClass> parentIt = parents.iterator();
|
||||
Iterator<Resource> 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,34 +883,87 @@ 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<Resource> sameIndividuals = getSameIndividuals(subject,inferenceModel);
|
||||
sameIndividuals.add(subject);
|
||||
|
||||
tboxModel.enterCriticalSection(Lock.READ);
|
||||
List<Resource> subClasses = getSubClasses(cls);
|
||||
aboxModel.enterCriticalSection(Lock.READ);
|
||||
try {
|
||||
aboxModel.enterCriticalSection(Lock.READ);
|
||||
try {
|
||||
List<OntClass> subclasses = null;
|
||||
subclasses = (cls.listSubClasses(false)).toList();
|
||||
subclasses.addAll((cls.listEquivalentClasses()).toList());
|
||||
Iterator<OntClass> iter = subclasses.iterator();
|
||||
while (iter.hasNext()) {
|
||||
OntClass childClass = iter.next();
|
||||
if (childClass.equals(cls)) continue; // TODO - determine whether this is needed
|
||||
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
||||
while (sameIter.hasNext()) {
|
||||
Statement stmt = ResourceFactory.createStatement(sameIter.next(), RDF.type, childClass);
|
||||
if (aboxModel.contains(stmt)) {
|
||||
return true;
|
||||
}
|
||||
Iterator<Resource> iter = subClasses.iterator();
|
||||
while (iter.hasNext()) {
|
||||
Resource childClass = iter.next();
|
||||
if (childClass.equals(cls)) continue;
|
||||
Iterator<Resource> 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();
|
||||
}
|
||||
return false;
|
||||
} finally {
|
||||
aboxModel.leaveCriticalSection();
|
||||
}
|
||||
}
|
||||
|
||||
protected List<Resource> getSubClasses(Resource cls) {
|
||||
List<Resource> subClasses = new ArrayList<Resource>();
|
||||
tboxModel.enterCriticalSection(Lock.READ);
|
||||
try {
|
||||
Iterator<Statement> 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<Resource> getSuperClasses(Resource cls) {
|
||||
List<Resource> superClasses = new ArrayList<Resource>();
|
||||
tboxModel.enterCriticalSection(Lock.READ);
|
||||
try {
|
||||
Iterator<Statement> 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();
|
||||
}
|
||||
|
@ -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<OntClass> eIter = type.listEquivalentClasses();
|
||||
ArrayList<Resource> equivalentClasses = new ArrayList<Resource>();
|
||||
|
||||
Iterator<Statement> 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<Resource> 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<OntClass> getParents(OntClass cls, OntModel tboxModel) {
|
||||
protected List<Resource> getParents(Resource cls, OntModel tboxModel) {
|
||||
|
||||
List<OntClass> parents = new ArrayList<OntClass>();
|
||||
List<Resource> parents = new ArrayList<Resource>();
|
||||
|
||||
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) {
|
||||
|
|
Loading…
Add table
Reference in a new issue