some refactoring of SimpleReasoner
This commit is contained in:
parent
b84407eba2
commit
5a7f70e1d7
2 changed files with 201 additions and 198 deletions
|
@ -144,6 +144,7 @@ public class SimpleReasoner extends StatementListener {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// don't stop the edit if there's an exception
|
// don't stop the edit if there's an exception
|
||||||
log.error("Exception while computing inferences: " + e.getMessage());
|
log.error("Exception while computing inferences: " + e.getMessage());
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -164,6 +165,7 @@ public class SimpleReasoner extends StatementListener {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// don't stop the edit if there's an exception
|
// don't stop the edit if there's an exception
|
||||||
log.error("Exception while retracting inferences: ", e);
|
log.error("Exception while retracting inferences: ", e);
|
||||||
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -339,7 +341,6 @@ public class SimpleReasoner extends StatementListener {
|
||||||
StmtIterator iter = null;
|
StmtIterator iter = null;
|
||||||
|
|
||||||
aboxModel.enterCriticalSection(Lock.READ);
|
aboxModel.enterCriticalSection(Lock.READ);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
iter = aboxModel.listStatements(individual, RDF.type, (RDFNode) null);
|
iter = aboxModel.listStatements(individual, RDF.type, (RDFNode) null);
|
||||||
|
|
||||||
|
@ -370,13 +371,11 @@ public class SimpleReasoner extends StatementListener {
|
||||||
|
|
||||||
cls = tboxModel.getOntClass(stmt.getObject().asResource().getURI());
|
cls = tboxModel.getOntClass(stmt.getObject().asResource().getURI());
|
||||||
if (cls != null) {
|
if (cls != null) {
|
||||||
|
|
||||||
List<OntClass> parents = (cls.listSuperClasses(false)).toList();
|
List<OntClass> parents = (cls.listSuperClasses(false)).toList();
|
||||||
parents.addAll((cls.listEquivalentClasses()).toList());
|
parents.addAll((cls.listEquivalentClasses()).toList());
|
||||||
Iterator<OntClass> parentIt = parents.iterator();
|
Iterator<OntClass> parentIt = parents.iterator();
|
||||||
|
|
||||||
if (parentIt.hasNext()) {
|
if (parentIt.hasNext()) {
|
||||||
List<Resource> sameIndividuals = getSameIndividuals(stmt.getSubject().asResource(), inferenceModel);
|
|
||||||
while (parentIt.hasNext()) {
|
while (parentIt.hasNext()) {
|
||||||
OntClass parentClass = parentIt.next();
|
OntClass parentClass = parentIt.next();
|
||||||
|
|
||||||
|
@ -387,28 +386,7 @@ public class SimpleReasoner extends StatementListener {
|
||||||
if (parentClass.isAnon()) continue;
|
if (parentClass.isAnon()) continue;
|
||||||
|
|
||||||
Statement infStmt = ResourceFactory.createStatement(stmt.getSubject(), RDF.type, parentClass);
|
Statement infStmt = ResourceFactory.createStatement(stmt.getSubject(), RDF.type, parentClass);
|
||||||
aboxModel.enterCriticalSection(Lock.READ);
|
addInference(infStmt,inferenceModel,true);
|
||||||
try {
|
|
||||||
inferenceModel.enterCriticalSection(Lock.WRITE);
|
|
||||||
try {
|
|
||||||
if (!inferenceModel.contains(infStmt) && !aboxModel.contains(infStmt)) {
|
|
||||||
inferenceModel.add(infStmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
|
||||||
while (sameIter.hasNext()) {
|
|
||||||
Resource subject = sameIter.next();
|
|
||||||
if (!inferenceModel.contains(subject,infStmt.getPredicate(),infStmt.getObject()) && !aboxModel.contains(subject,infStmt.getPredicate(),infStmt.getObject())) {
|
|
||||||
inferenceModel.add(subject,infStmt.getPredicate(),infStmt.getObject());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
} finally {
|
|
||||||
inferenceModel.leaveCriticalSection();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
aboxModel.leaveCriticalSection();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -427,6 +405,15 @@ public class SimpleReasoner extends StatementListener {
|
||||||
} finally {
|
} finally {
|
||||||
tboxModel.leaveCriticalSection();
|
tboxModel.leaveCriticalSection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
inferenceModel.enterCriticalSection(Lock.WRITE);
|
||||||
|
try {
|
||||||
|
if (inferenceModel.contains(stmt)) {
|
||||||
|
inferenceModel.remove(stmt);
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
inferenceModel.leaveCriticalSection();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -471,24 +458,17 @@ public class SimpleReasoner extends StatementListener {
|
||||||
|
|
||||||
while (infIter.hasNext()) {
|
while (infIter.hasNext()) {
|
||||||
Statement infStmt = infIter.next();
|
Statement infStmt = infIter.next();
|
||||||
aboxModel.enterCriticalSection(Lock.READ);
|
addInference(infStmt,inferenceModel,false);
|
||||||
try {
|
}
|
||||||
|
|
||||||
inferenceModel.enterCriticalSection(Lock.WRITE);
|
inferenceModel.enterCriticalSection(Lock.WRITE);
|
||||||
try {
|
try {
|
||||||
if (inferenceModel.contains(stmt)) {
|
if (inferenceModel.contains(stmt)) {
|
||||||
inferenceModel.remove(stmt);
|
inferenceModel.remove(stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!inferenceModel.contains(infStmt) && !aboxModel.contains(infStmt) ) {
|
|
||||||
inferenceModel.add(infStmt);
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
inferenceModel.leaveCriticalSection();
|
inferenceModel.leaveCriticalSection();
|
||||||
}
|
}
|
||||||
} finally {
|
|
||||||
aboxModel.leaveCriticalSection();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -531,6 +511,7 @@ public class SimpleReasoner extends StatementListener {
|
||||||
|
|
||||||
ArrayList<Resource> sameIndividuals = new ArrayList<Resource>();
|
ArrayList<Resource> sameIndividuals = new ArrayList<Resource>();
|
||||||
aboxModel.enterCriticalSection(Lock.READ);
|
aboxModel.enterCriticalSection(Lock.READ);
|
||||||
|
try {
|
||||||
inferenceModel.enterCriticalSection(Lock.READ);
|
inferenceModel.enterCriticalSection(Lock.READ);
|
||||||
try {
|
try {
|
||||||
Iterator<Statement> iter = unionModel.listStatements(ind, OWL.sameAs, (RDFNode) null);
|
Iterator<Statement> iter = unionModel.listStatements(ind, OWL.sameAs, (RDFNode) null);
|
||||||
|
@ -542,6 +523,8 @@ public class SimpleReasoner extends StatementListener {
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
inferenceModel.leaveCriticalSection();
|
inferenceModel.leaveCriticalSection();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
aboxModel.leaveCriticalSection();
|
aboxModel.leaveCriticalSection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -555,6 +538,7 @@ public class SimpleReasoner extends StatementListener {
|
||||||
* the inference graph and then recompute the inferences for x and
|
* the inference graph and then recompute the inferences for x and
|
||||||
* y based on their respective assertions. that x owl:sameAs y, then all asserted and inferred
|
* y based on their respective assertions. that x owl:sameAs y, then all asserted and inferred
|
||||||
*/
|
*/
|
||||||
|
//TODO - I think I need to handle sameAs chains here
|
||||||
public void removedABoxSameAsAssertion(Statement stmt, Model inferenceModel) {
|
public void removedABoxSameAsAssertion(Statement stmt, Model inferenceModel) {
|
||||||
|
|
||||||
Resource subject = null;
|
Resource subject = null;
|
||||||
|
@ -582,19 +566,8 @@ public class SimpleReasoner extends StatementListener {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
inferenceModel.enterCriticalSection(Lock.WRITE);
|
Statement infStmt = ResourceFactory.createStatement(object,OWL.sameAs,subject);
|
||||||
try {
|
removeInference(infStmt,inferenceModel,false);
|
||||||
if (inferenceModel.contains(stmt)) {
|
|
||||||
inferenceModel.remove(stmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!inferenceModel.contains(object, OWL.sameAs, subject) && !aboxModel.contains(object, OWL.sameAs, subject) ) {
|
|
||||||
inferenceModel.add(object, OWL.sameAs, subject);
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
inferenceModel.leaveCriticalSection();
|
|
||||||
}
|
|
||||||
|
|
||||||
recomputeInferencesForIndividual(subject, inferenceModel);
|
recomputeInferencesForIndividual(subject, inferenceModel);
|
||||||
recomputeInferencesForIndividual(object, inferenceModel);
|
recomputeInferencesForIndividual(object, inferenceModel);
|
||||||
}
|
}
|
||||||
|
@ -643,42 +616,29 @@ public class SimpleReasoner extends StatementListener {
|
||||||
* Performs incremental property-based reasoning.
|
* Performs incremental property-based reasoning.
|
||||||
*
|
*
|
||||||
* Materializes inferences based on the owl:inverseOf relationship.
|
* Materializes inferences based on the owl:inverseOf relationship.
|
||||||
|
* and owl:sameAs
|
||||||
*
|
*
|
||||||
* If it is added that x prop1 y, and prop2 is an inverseOf prop1
|
* If it is added that x prop1 y, and prop2 is an inverseOf prop1
|
||||||
* then add y prop2 x to the inference graph, if it is not already in
|
* then add y prop2 x to the inference graph, if it is not already in
|
||||||
* the assertions graph.
|
* the assertions graph.
|
||||||
|
*
|
||||||
*/
|
*/
|
||||||
public void addedABoxAssertion(Statement stmt, Model inferenceModel) {
|
public void addedABoxAssertion(Statement stmt, Model inferenceModel) {
|
||||||
List<Resource> sameIndividuals = getSameIndividuals(stmt.getSubject().asResource(), inferenceModel);
|
|
||||||
List<OntProperty> inverseProperties = getInverseProperties(stmt);
|
List<OntProperty> inverseProperties = getInverseProperties(stmt);
|
||||||
Iterator<OntProperty> inverseIter = inverseProperties.iterator();
|
Iterator<OntProperty> inverseIter = inverseProperties.iterator();
|
||||||
|
|
||||||
while (inverseIter.hasNext()) {
|
while (inverseIter.hasNext()) {
|
||||||
Property inverseProp = inverseIter.next();
|
Property inverseProp = inverseIter.next();
|
||||||
|
|
||||||
Statement infStmt = ResourceFactory.createStatement(stmt.getObject().asResource(), inverseProp, stmt.getSubject());
|
Statement infStmt = ResourceFactory.createStatement(stmt.getObject().asResource(), inverseProp, stmt.getSubject());
|
||||||
|
addInference(infStmt,inferenceModel,true);
|
||||||
aboxModel.enterCriticalSection(Lock.READ);
|
|
||||||
try {
|
|
||||||
inferenceModel.enterCriticalSection(Lock.WRITE);
|
|
||||||
try {
|
|
||||||
if (!inferenceModel.contains(infStmt) && !aboxModel.contains(infStmt) ) {
|
|
||||||
inferenceModel.add(infStmt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<Resource> sameIndividuals = getSameIndividuals(stmt.getSubject().asResource(), inferenceModel);
|
||||||
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
||||||
while (sameIter.hasNext()) {
|
while (sameIter.hasNext()) {
|
||||||
Resource subject = sameIter.next();
|
Resource subject = sameIter.next();
|
||||||
if (!inferenceModel.contains(subject,infStmt.getPredicate(),infStmt.getObject()) && !aboxModel.contains(subject,infStmt.getPredicate(),infStmt.getObject())) {
|
Statement sameStmt = ResourceFactory.createStatement(subject,stmt.getPredicate(),stmt.getObject());
|
||||||
inferenceModel.add(subject,infStmt.getPredicate(),infStmt.getObject());
|
addInference(sameStmt,inferenceModel,false);
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
inferenceModel.leaveCriticalSection();
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
aboxModel.leaveCriticalSection();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
inferenceModel.enterCriticalSection(Lock.WRITE);
|
inferenceModel.enterCriticalSection(Lock.WRITE);
|
||||||
|
@ -686,14 +646,6 @@ public class SimpleReasoner extends StatementListener {
|
||||||
if (inferenceModel.contains(stmt)) {
|
if (inferenceModel.contains(stmt)) {
|
||||||
inferenceModel.remove(stmt);
|
inferenceModel.remove(stmt);
|
||||||
}
|
}
|
||||||
|
|
||||||
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
|
||||||
while (sameIter.hasNext()) {
|
|
||||||
Resource subject = sameIter.next();
|
|
||||||
if (!inferenceModel.contains(subject,stmt.getPredicate(),stmt.getObject()) && !aboxModel.contains(subject,stmt.getPredicate(),stmt.getObject())) {
|
|
||||||
inferenceModel.add(subject,stmt.getPredicate(),stmt.getObject());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
} finally {
|
||||||
inferenceModel.leaveCriticalSection();
|
inferenceModel.leaveCriticalSection();
|
||||||
}
|
}
|
||||||
|
@ -706,9 +658,7 @@ 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;
|
OntClass cls = null;
|
||||||
|
|
||||||
|
@ -786,47 +736,28 @@ public class SimpleReasoner extends StatementListener {
|
||||||
*/
|
*/
|
||||||
public void removedABoxAssertion(Statement stmt, Model inferenceModel) {
|
public void removedABoxAssertion(Statement stmt, Model inferenceModel) {
|
||||||
|
|
||||||
List<Resource> sameIndividuals = getSameIndividuals(stmt.getSubject().asResource(), inferenceModel);
|
|
||||||
List<OntProperty> inverseProperties = getInverseProperties(stmt);
|
List<OntProperty> inverseProperties = getInverseProperties(stmt);
|
||||||
Iterator<OntProperty> inverseIter = inverseProperties.iterator();
|
Iterator<OntProperty> inverseIter = inverseProperties.iterator();
|
||||||
|
|
||||||
while (inverseIter.hasNext()) {
|
while (inverseIter.hasNext()) {
|
||||||
OntProperty inverseProp = inverseIter.next();
|
OntProperty inverseProp = inverseIter.next();
|
||||||
|
|
||||||
Statement infStmt = ResourceFactory.createStatement(stmt.getObject().asResource(), inverseProp, stmt.getSubject());
|
Statement infStmt = ResourceFactory.createStatement(stmt.getObject().asResource(), inverseProp, stmt.getSubject());
|
||||||
|
removeInference(infStmt,inferenceModel,true);
|
||||||
inferenceModel.enterCriticalSection(Lock.WRITE);
|
|
||||||
try {
|
|
||||||
if (!entailedStatement(infStmt) && inferenceModel.contains(infStmt)) {
|
|
||||||
inferenceModel.remove(infStmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
|
||||||
while (sameIter.hasNext()) {
|
|
||||||
Statement infStmtSame = ResourceFactory.createStatement(sameIter.next(), infStmt.getPredicate(), infStmt.getObject());
|
|
||||||
if (!entailedStatement(infStmtSame) && inferenceModel.contains(infStmtSame)) {
|
|
||||||
inferenceModel.remove(infStmtSame);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
inferenceModel.leaveCriticalSection();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
inferenceModel.enterCriticalSection(Lock.WRITE);
|
|
||||||
try {
|
|
||||||
// if a statement has been removed that is otherwise entailed,
|
|
||||||
// add it to the inference graph.
|
|
||||||
if (entailedStatement(stmt) && !inferenceModel.contains(stmt)) {
|
|
||||||
inferenceModel.add(stmt);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
List<Resource> sameIndividuals = getSameIndividuals(stmt.getSubject().asResource(), inferenceModel);
|
||||||
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
||||||
while (sameIter.hasNext()) {
|
while (sameIter.hasNext()) {
|
||||||
Statement stmtSame = ResourceFactory.createStatement(sameIter.next(), stmt.getPredicate(), stmt.getObject());
|
Statement stmtSame = ResourceFactory.createStatement(sameIter.next(), stmt.getPredicate(), stmt.getObject());
|
||||||
if (!entailedStatement(stmtSame) && inferenceModel.contains(stmtSame)) {
|
removeInference(stmtSame,inferenceModel,false);
|
||||||
inferenceModel.remove(stmtSame);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if a statement has been removed that is otherwise entailed,
|
||||||
|
// add it to the inference graph.
|
||||||
|
inferenceModel.enterCriticalSection(Lock.WRITE);
|
||||||
|
try {
|
||||||
|
if (entailedStatement(stmt) && !inferenceModel.contains(stmt)) {
|
||||||
|
inferenceModel.add(stmt);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
inferenceModel.leaveCriticalSection();
|
inferenceModel.leaveCriticalSection();
|
||||||
|
@ -868,29 +799,16 @@ public class SimpleReasoner extends StatementListener {
|
||||||
// reasoning or sameAs reasoning; otherwise returns false.
|
// reasoning or sameAs reasoning; otherwise returns false.
|
||||||
protected boolean entailedStatement(Statement stmt) {
|
protected boolean entailedStatement(Statement stmt) {
|
||||||
|
|
||||||
List<OntProperty> inverses = new ArrayList<OntProperty>();
|
// class subsumption
|
||||||
|
|
||||||
tboxModel.enterCriticalSection(Lock.READ);
|
// Inverse properties
|
||||||
try {
|
List<OntProperty> inverses = getInverseProperties(stmt);
|
||||||
OntProperty prop = tboxModel.getOntProperty(stmt.getPredicate().asResource().getURI());
|
Iterator<OntProperty> iIter = inverses.iterator();
|
||||||
inverses.addAll(prop.listInverse().toList());
|
if (iIter.hasNext()) {
|
||||||
} finally {
|
|
||||||
tboxModel.leaveCriticalSection();
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterator<OntProperty> oIter = inverses.iterator();
|
|
||||||
if (oIter.hasNext()) {
|
|
||||||
aboxModel.enterCriticalSection(Lock.READ);
|
aboxModel.enterCriticalSection(Lock.READ);
|
||||||
try {
|
try {
|
||||||
while (oIter.hasNext()) {
|
while (iIter.hasNext()) {
|
||||||
Property invProp = oIter.next();
|
Property invProp = iIter.next();
|
||||||
|
|
||||||
// not reasoning on properties in the OWL, RDF or RDFS namespace
|
|
||||||
if ((invProp.getNameSpace()).equals(OWL.NS) ||
|
|
||||||
(invProp.getNameSpace()).equals(RDFS.getURI()) ||
|
|
||||||
(invProp.getNameSpace()).equals(RDF.getURI())) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
Statement invStmt = ResourceFactory.createStatement(stmt.getObject().asResource(), invProp, stmt.getSubject());
|
Statement invStmt = ResourceFactory.createStatement(stmt.getObject().asResource(), invProp, stmt.getSubject());
|
||||||
if (aboxModel.contains(invStmt)) {
|
if (aboxModel.contains(invStmt)) {
|
||||||
|
@ -902,6 +820,7 @@ public class SimpleReasoner extends StatementListener {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// individuals sameAs each other
|
||||||
List<Resource> sameIndividuals = getSameIndividuals(stmt.getSubject().asResource(),inferenceModel);
|
List<Resource> sameIndividuals = getSameIndividuals(stmt.getSubject().asResource(),inferenceModel);
|
||||||
Iterator<Resource> rIter = sameIndividuals.iterator();
|
Iterator<Resource> rIter = sameIndividuals.iterator();
|
||||||
if (rIter.hasNext()) {
|
if (rIter.hasNext()) {
|
||||||
|
@ -997,6 +916,13 @@ public class SimpleReasoner extends StatementListener {
|
||||||
aboxModel.leaveCriticalSection();
|
aboxModel.leaveCriticalSection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (Resource subject : subjectList) {
|
||||||
|
Statement infStmt = ResourceFactory.createStatement(subject, RDF.type, superClass);
|
||||||
|
addInference(infStmt, inferenceModel, true);
|
||||||
|
setMostSpecificTypes(infStmt.getSubject(), inferenceModel, new HashSet<String>());
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
for (Resource subject : subjectList) {
|
for (Resource subject : subjectList) {
|
||||||
Statement infStmt = ResourceFactory.createStatement(subject, RDF.type, superClass);
|
Statement infStmt = ResourceFactory.createStatement(subject, RDF.type, superClass);
|
||||||
|
|
||||||
|
@ -1014,6 +940,7 @@ public class SimpleReasoner extends StatementListener {
|
||||||
aboxModel.leaveCriticalSection();
|
aboxModel.leaveCriticalSection();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -1081,33 +1008,11 @@ public class SimpleReasoner extends StatementListener {
|
||||||
|
|
||||||
if (inferences.isEmpty()) return;
|
if (inferences.isEmpty()) return;
|
||||||
|
|
||||||
aboxModel.enterCriticalSection(Lock.READ);
|
|
||||||
try {
|
|
||||||
StmtIterator iter = inferences.listStatements();
|
StmtIterator iter = inferences.listStatements();
|
||||||
|
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
Statement infStmt = iter.next();
|
Statement infStmt = iter.next();
|
||||||
List<Resource> sameIndividuals = getSameIndividuals(infStmt.getSubject().asResource(), inferenceModel);
|
addInference(infStmt, inferenceModel, true);
|
||||||
|
|
||||||
inferenceModel.enterCriticalSection(Lock.WRITE);
|
|
||||||
try {
|
|
||||||
if (!inferenceModel.contains(infStmt) && !aboxModel.contains(infStmt)) {
|
|
||||||
inferenceModel.add(infStmt);
|
|
||||||
}
|
|
||||||
|
|
||||||
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
|
||||||
while (sameIter.hasNext()) {
|
|
||||||
Resource subject = sameIter.next();
|
|
||||||
if (!inferenceModel.contains(subject,infStmt.getPredicate(),infStmt.getObject()) && !aboxModel.contains(subject,infStmt.getPredicate(),infStmt.getObject())) {
|
|
||||||
inferenceModel.add(subject,infStmt.getPredicate(),infStmt.getObject());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
inferenceModel.leaveCriticalSection();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} finally {
|
|
||||||
aboxModel.leaveCriticalSection();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1162,16 +1067,102 @@ public class SimpleReasoner extends StatementListener {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
removeInference(infStmt,inferenceModel,true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Remove an inference from the inference model
|
||||||
|
*
|
||||||
|
* Removes the inference if it is not entailed by the abox model
|
||||||
|
* and if the inference model contains it.
|
||||||
|
*
|
||||||
|
* Removes the corresponding inference for each same individual
|
||||||
|
* if that inference is not entailed by the abox model.
|
||||||
|
*/
|
||||||
|
protected void removeInference(Statement infStmt, Model inferenceModel, boolean handleSameAs) {
|
||||||
|
|
||||||
inferenceModel.enterCriticalSection(Lock.WRITE);
|
inferenceModel.enterCriticalSection(Lock.WRITE);
|
||||||
try {
|
try {
|
||||||
if (inferenceModel.contains(infStmt)) {
|
if ( !entailedStatement(infStmt) && inferenceModel.contains(infStmt)) {
|
||||||
inferenceModel.remove(infStmt);
|
inferenceModel.remove(infStmt);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
inferenceModel.leaveCriticalSection();
|
inferenceModel.leaveCriticalSection();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (handleSameAs) {
|
||||||
|
inferenceModel.enterCriticalSection(Lock.WRITE);
|
||||||
|
try {
|
||||||
|
List<Resource> sameIndividuals = getSameIndividuals(infStmt.getSubject().asResource(), inferenceModel);
|
||||||
|
|
||||||
|
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
||||||
|
while (sameIter.hasNext()) {
|
||||||
|
Statement infStmtSame = ResourceFactory.createStatement(sameIter.next(), infStmt.getPredicate(), infStmt.getObject());
|
||||||
|
if (!entailedStatement(infStmtSame) && inferenceModel.contains(infStmtSame)) {
|
||||||
|
inferenceModel.remove(infStmtSame);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} finally {
|
||||||
|
inferenceModel.leaveCriticalSection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Add an inference from the inference model
|
||||||
|
*
|
||||||
|
* Adds the inference to the inference model if it is not already in
|
||||||
|
* the inference model and not in the abox model.
|
||||||
|
*/
|
||||||
|
protected void addInference(Statement infStmt, Model inferenceModel, boolean handleSameAs) {
|
||||||
|
|
||||||
|
aboxModel.enterCriticalSection(Lock.READ);
|
||||||
|
try {
|
||||||
|
inferenceModel.enterCriticalSection(Lock.WRITE);
|
||||||
|
try {
|
||||||
|
if (!inferenceModel.contains(infStmt) && !aboxModel.contains(infStmt)) {
|
||||||
|
inferenceModel.add(infStmt);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (handleSameAs) {
|
||||||
|
List<Resource> sameIndividuals = getSameIndividuals(infStmt.getSubject().asResource(), inferenceModel);
|
||||||
|
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
||||||
|
while (sameIter.hasNext()) {
|
||||||
|
Resource subject = sameIter.next();
|
||||||
|
if (!inferenceModel.contains(subject,infStmt.getPredicate(),infStmt.getObject()) && !aboxModel.contains(subject,infStmt.getPredicate(),infStmt.getObject())) {
|
||||||
|
inferenceModel.add(subject,infStmt.getPredicate(),infStmt.getObject());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
inferenceModel.leaveCriticalSection();
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
aboxModel.leaveCriticalSection();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
System.out.println("hello!!! ONE");
|
||||||
|
if (handleSameAs) {
|
||||||
|
System.out.println("hello!!! TWO");
|
||||||
|
aboxModel.enterCriticalSection(Lock.READ);
|
||||||
|
System.out.println("hello!!! TRES");
|
||||||
|
try {
|
||||||
|
List<Resource> sameIndividuals = getSameIndividuals(infStmt.getSubject().asResource(), inferenceModel);
|
||||||
|
Iterator<Resource> sameIter = sameIndividuals.iterator();
|
||||||
|
while (sameIter.hasNext()) {
|
||||||
|
Resource subject = sameIter.next();
|
||||||
|
if (!inferenceModel.contains(subject,infStmt.getPredicate(),infStmt.getObject()) && !aboxModel.contains(subject,infStmt.getPredicate(),infStmt.getObject())) {
|
||||||
|
inferenceModel.add(subject,infStmt.getPredicate(),infStmt.getObject());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} finally {
|
||||||
|
aboxModel.leaveCriticalSection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
*/
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Find the most specific types (classes) of an individual and
|
* Find the most specific types (classes) of an individual and
|
||||||
|
|
|
@ -295,11 +295,11 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass {
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* adding an inverseOf assertion for individuals who are sameAs
|
* adding and removing an inverseOf assertion for individuals who
|
||||||
* each other.
|
* are sameAs each other.
|
||||||
*/
|
*/
|
||||||
@Test
|
@Test
|
||||||
public void addTBoxInverseAssertion1() throws InterruptedException {
|
public void tBoxInverseAssertion1() throws InterruptedException {
|
||||||
|
|
||||||
// Create TBox, ABox and Inference models and register
|
// Create TBox, ABox and Inference models and register
|
||||||
// the ABox reasoner listeners with the ABox and TBox
|
// the ABox reasoner listeners with the ABox and TBox
|
||||||
|
@ -329,9 +329,6 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass {
|
||||||
aBox.add(a,P,b);
|
aBox.add(a,P,b);
|
||||||
aBox.add(a, OWL.sameAs,b);
|
aBox.add(a, OWL.sameAs,b);
|
||||||
|
|
||||||
// Assert P and Q as inverses and wait for SimpleReasoner TBox
|
|
||||||
// thread to end
|
|
||||||
|
|
||||||
Q.addInverseOf(P);
|
Q.addInverseOf(P);
|
||||||
|
|
||||||
tBox.rebind();
|
tBox.rebind();
|
||||||
|
@ -341,14 +338,29 @@ public class SimpleReasonerSameAsTest extends AbstractTestClass {
|
||||||
Thread.sleep(delay);
|
Thread.sleep(delay);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Verify inferences
|
|
||||||
Assert.assertTrue(inf.contains(b,Q,a));
|
Assert.assertTrue(inf.contains(b,Q,a));
|
||||||
Assert.assertTrue(inf.contains(b,OWL.sameAs,a));
|
Assert.assertTrue(inf.contains(b,OWL.sameAs,a));
|
||||||
Assert.assertTrue(inf.contains(b,P,b));
|
Assert.assertTrue(inf.contains(b,P,b));
|
||||||
Assert.assertTrue(inf.contains(a,Q,a));
|
Assert.assertTrue(inf.contains(a,Q,a));
|
||||||
|
|
||||||
|
Q.removeInverseProperty(P);
|
||||||
|
|
||||||
|
tBox.rebind();
|
||||||
|
tBox.prepare();
|
||||||
|
|
||||||
|
while (!VitroBackgroundThread.getLivingThreads().isEmpty()) {
|
||||||
|
Thread.sleep(delay);
|
||||||
|
}
|
||||||
|
|
||||||
|
Assert.assertFalse(inf.contains(b,Q,a));
|
||||||
|
Assert.assertTrue(inf.contains(b,OWL.sameAs,a));
|
||||||
|
Assert.assertTrue(inf.contains(b,P,b));
|
||||||
|
Assert.assertFalse(inf.contains(a,Q,a));
|
||||||
|
|
||||||
simpleReasonerTBoxListener.setStopRequested();
|
simpleReasonerTBoxListener.setStopRequested();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Basic scenario around recomputing the ABox inferences
|
* Basic scenario around recomputing the ABox inferences
|
||||||
*/
|
*/
|
||||||
|
|
Loading…
Add table
Reference in a new issue