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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//log.debug("added TBox assertion = " + stmt.toString());
|
if (!stmt.getObject().isResource()) {
|
||||||
|
log.warn("The object of this assertion is not a resource: " + stmtString(stmt));
|
||||||
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));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -258,6 +251,16 @@ public class SimpleReasoner extends StatementListener {
|
||||||
addedSubClass(object,subject,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());
|
OntProperty prop1 = tboxModel.getOntProperty((stmt.getSubject()).getURI());
|
||||||
if (prop1 == null) {
|
if (prop1 == null) {
|
||||||
log.debug("didn't find subject property in the tbox: " + (stmt.getSubject()).getURI());
|
log.debug("didn't find subject property in the tbox: " + (stmt.getSubject()).getURI());
|
||||||
|
@ -290,15 +293,8 @@ public class SimpleReasoner extends StatementListener {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
//log.debug("removed TBox assertion = " + stmt.toString());
|
if (!stmt.getObject().isResource()) {
|
||||||
|
log.warn("The object of this assertion is not a resource: " + stmtString(stmt));
|
||||||
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));
|
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -329,6 +325,16 @@ public class SimpleReasoner extends StatementListener {
|
||||||
removedSubClass(object,subject,inferenceModel);
|
removedSubClass(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());
|
OntProperty prop1 = tboxModel.getOntProperty((stmt.getSubject()).getURI());
|
||||||
if (prop1 == null) {
|
if (prop1 == null) {
|
||||||
log.debug("didn't find subject property in the tbox: " + (stmt.getSubject()).getURI());
|
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);
|
tboxModel.enterCriticalSection(Lock.READ);
|
||||||
try {
|
try {
|
||||||
OntClass cls = null;
|
Resource cls = null;
|
||||||
if ( (stmt.getObject().asResource()).getURI() != 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 (cls != null) {
|
||||||
List<OntClass> parents = null;
|
List<Resource> parents = getParents(cls,tboxModel);
|
||||||
try {
|
|
||||||
parents = (cls.listSuperClasses(false)).toList();
|
|
||||||
parents.addAll((cls.listEquivalentClasses()).toList());
|
|
||||||
} catch (ConversionException ce) {
|
|
||||||
parents = getParents(cls,tboxModel);
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterator<OntClass> parentIt = parents.iterator();
|
Iterator<Resource> parentIt = parents.iterator();
|
||||||
|
|
||||||
if (parentIt.hasNext()) {
|
if (parentIt.hasNext()) {
|
||||||
while (parentIt.hasNext()) {
|
while (parentIt.hasNext()) {
|
||||||
OntClass parentClass = parentIt.next();
|
Resource parentClass = parentIt.next();
|
||||||
|
|
||||||
// VIVO doesn't materialize statements that assert anonymous types
|
// VIVO doesn't materialize statements that assert anonymous types
|
||||||
// for individuals. Also, sharing an identical anonymous node is
|
// for individuals. Also, sharing an identical anonymous node is
|
||||||
|
@ -400,7 +400,7 @@ public class SimpleReasoner extends StatementListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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;
|
return;
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
|
@ -426,29 +426,23 @@ public class SimpleReasoner extends StatementListener {
|
||||||
protected void removedABoxTypeAssertion(Statement stmt, Model inferenceModel) {
|
protected void removedABoxTypeAssertion(Statement stmt, Model inferenceModel) {
|
||||||
tboxModel.enterCriticalSection(Lock.READ);
|
tboxModel.enterCriticalSection(Lock.READ);
|
||||||
try {
|
try {
|
||||||
OntClass cls = null;
|
Resource cls = null;
|
||||||
|
|
||||||
if ( (stmt.getObject().asResource()).getURI() != 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 (cls != null) {
|
||||||
if (entailedType(stmt.getSubject(),cls)) {
|
if (entailedType(stmt.getSubject(),cls)) {
|
||||||
addInference(stmt,inferenceModel,true);
|
addInference(stmt,inferenceModel,true);
|
||||||
}
|
}
|
||||||
|
|
||||||
List<OntClass> parents = null;
|
List<Resource> parents = getParents(cls,tboxModel);
|
||||||
try {
|
|
||||||
parents = (cls.listSuperClasses(false)).toList();
|
|
||||||
parents.addAll((cls.listEquivalentClasses()).toList());
|
|
||||||
} catch (ConversionException ce) {
|
|
||||||
parents = getParents(cls,tboxModel);
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterator<OntClass> parentIt = parents.iterator();
|
Iterator<Resource> parentIt = parents.iterator();
|
||||||
|
|
||||||
while (parentIt.hasNext()) {
|
while (parentIt.hasNext()) {
|
||||||
|
|
||||||
OntClass parentClass = parentIt.next();
|
Resource parentClass = parentIt.next();
|
||||||
|
|
||||||
// VIVO doesn't materialize statements that assert anonymous types
|
// VIVO doesn't materialize statements that assert anonymous types
|
||||||
// for individuals. Also, sharing an identical anonymous node is
|
// for individuals. Also, sharing an identical anonymous node is
|
||||||
|
@ -889,22 +883,18 @@ public class SimpleReasoner extends StatementListener {
|
||||||
|
|
||||||
// Returns true if it is entailed by class subsumption that
|
// Returns true if it is entailed by class subsumption that
|
||||||
// subject is of type cls; otherwise returns false.
|
// 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);
|
List<Resource> sameIndividuals = getSameIndividuals(subject,inferenceModel);
|
||||||
sameIndividuals.add(subject);
|
sameIndividuals.add(subject);
|
||||||
|
|
||||||
tboxModel.enterCriticalSection(Lock.READ);
|
List<Resource> subClasses = getSubClasses(cls);
|
||||||
try {
|
|
||||||
aboxModel.enterCriticalSection(Lock.READ);
|
aboxModel.enterCriticalSection(Lock.READ);
|
||||||
try {
|
try {
|
||||||
List<OntClass> subclasses = null;
|
Iterator<Resource> iter = subClasses.iterator();
|
||||||
subclasses = (cls.listSubClasses(false)).toList();
|
|
||||||
subclasses.addAll((cls.listEquivalentClasses()).toList());
|
|
||||||
Iterator<OntClass> iter = subclasses.iterator();
|
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
OntClass childClass = iter.next();
|
Resource childClass = iter.next();
|
||||||
if (childClass.equals(cls)) continue; // TODO - determine whether this is needed
|
if (childClass.equals(cls)) continue;
|
||||||
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
||||||
while (sameIter.hasNext()) {
|
while (sameIter.hasNext()) {
|
||||||
Statement stmt = ResourceFactory.createStatement(sameIter.next(), RDF.type, childClass);
|
Statement stmt = ResourceFactory.createStatement(sameIter.next(), RDF.type, childClass);
|
||||||
|
@ -917,6 +907,63 @@ public class SimpleReasoner extends StatementListener {
|
||||||
} finally {
|
} finally {
|
||||||
aboxModel.leaveCriticalSection();
|
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 {
|
} finally {
|
||||||
tboxModel.leaveCriticalSection();
|
tboxModel.leaveCriticalSection();
|
||||||
}
|
}
|
||||||
|
@ -1162,7 +1209,7 @@ public class SimpleReasoner extends StatementListener {
|
||||||
if ( (stmt.getObject().asResource()).getURI() != null ) {
|
if ( (stmt.getObject().asResource()).getURI() != null ) {
|
||||||
ontClass = tboxModel.getOntClass(stmt.getObject().asResource().getURI());
|
ontClass = tboxModel.getOntClass(stmt.getObject().asResource().getURI());
|
||||||
} else {
|
} 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;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1170,7 +1217,7 @@ public class SimpleReasoner extends StatementListener {
|
||||||
if ( !(stmt.getObject().asResource().getNameSpace()).equals(OWL.NS)) {
|
if ( !(stmt.getObject().asResource().getNameSpace()).equals(OWL.NS)) {
|
||||||
if (!unknownTypes.contains(stmt.getObject().asResource().getURI())) {
|
if (!unknownTypes.contains(stmt.getObject().asResource().getURI())) {
|
||||||
unknownTypes.add(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.");
|
(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) {
|
if (add) {
|
||||||
typeURIs.add(type.getURI());
|
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()) {
|
while (eIter.hasNext()) {
|
||||||
OntClass equivClass = eIter.next();
|
Resource equivClass = eIter.next();
|
||||||
if (equivClass.isAnon()) continue;
|
if (equivClass.isAnon()) continue;
|
||||||
typeURIs.add(equivClass.getURI());
|
typeURIs.add(equivClass.getURI());
|
||||||
}
|
}
|
||||||
|
@ -1267,29 +1326,31 @@ public class SimpleReasoner extends StatementListener {
|
||||||
return;
|
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);
|
tboxModel.enterCriticalSection(Lock.READ);
|
||||||
try {
|
try {
|
||||||
StmtIterator iter = tboxModel.listStatements(cls, RDFS.subClassOf, (RDFNode) null);
|
StmtIterator iter = tboxModel.listStatements(cls, RDFS.subClassOf, (RDFNode) null);
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
Statement stmt = iter.next();
|
Statement stmt = iter.next();
|
||||||
if (!stmt.getObject().isAnon() && stmt.getObject().canAs(OntClass.class)) {
|
if (stmt.getObject() == null || !stmt.getObject().isResource() || stmt.getObject().asResource().isAnon()) {
|
||||||
if (!parents.contains(stmt.getObject().as(OntClass.class))) {
|
continue;
|
||||||
parents.add(stmt.getObject().as(OntClass.class));
|
|
||||||
}
|
}
|
||||||
|
if (!parents.contains(stmt.getObject().asResource())) {
|
||||||
|
parents.add(stmt.getObject().asResource());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
iter = tboxModel.listStatements(cls, OWL.equivalentClass, (RDFNode) null);
|
iter = tboxModel.listStatements(cls, OWL.equivalentClass, (RDFNode) null);
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
Statement stmt = iter.next();
|
Statement stmt = iter.next();
|
||||||
if (!stmt.getObject().isAnon() && stmt.getObject().canAs(OntClass.class)) {
|
if (stmt.getObject() == null || !stmt.getObject().isResource() || stmt.getObject().asResource().isAnon()) {
|
||||||
if (!parents.contains(stmt.getObject().as(OntClass.class))) {
|
continue;
|
||||||
parents.add(stmt.getObject().as(OntClass.class));
|
|
||||||
}
|
}
|
||||||
|
if (!parents.contains(stmt.getObject().asResource())) {
|
||||||
|
parents.add(stmt.getObject().asResource());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
|
Loading…
Add table
Reference in a new issue