development for NIHVIVO-2578 and NIHVIVO-2314

This commit is contained in:
stellamit 2011-05-03 16:39:06 +00:00
parent 66b02af3e8
commit de5c5ff7de
2 changed files with 78 additions and 18 deletions

View file

@ -15,6 +15,7 @@ import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec; import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntProperty; import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.rdf.listeners.StatementListener; import com.hp.hpl.jena.rdf.listeners.StatementListener;
import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory; import com.hp.hpl.jena.rdf.model.ModelFactory;
import com.hp.hpl.jena.rdf.model.RDFNode; import com.hp.hpl.jena.rdf.model.RDFNode;
@ -46,6 +47,11 @@ public class SimpleReasoner extends StatementListener {
private Model inferenceRebuildModel; private Model inferenceRebuildModel;
private Model scratchpadModel; private Model scratchpadModel;
private static final String topObjectPropertyURI = "http://www.w3.org/2002/07/owl#topObjectProperty";
private static final String bottomObjectPropertyURI = "http://www.w3.org/2002/07/owl#bottomObjectProperty";
private static final String topDataPropertyURI = "http://www.w3.org/2002/07/owl#topDataProperty";
private static final String bottomDataPropertyURI = "http://www.w3.org/2002/07/owl#bottomDataProperty";
/** /**
* @param tboxModel - input. This model contains both asserted and inferred TBox axioms * @param tboxModel - input. This model contains both asserted and inferred TBox axioms
* @param aboxModel - input. This model contains asserted ABox statements * @param aboxModel - input. This model contains asserted ABox statements
@ -279,7 +285,14 @@ public class SimpleReasoner extends StatementListener {
if (prop != null) { if (prop != null) {
//TODO: have trouble parametizing the template with ? extends OntProperty // not reasoning on properties in the OWL, RDF or RDFS namespace
if ((prop.getNameSpace()).equals(OWL.NS) ||
(prop.getNameSpace()).equals("http://www.w3.org/2000/01/rdf-schema#") ||
(prop.getNameSpace()).equals("http://www.w3.org/1999/02/22-rdf-syntax-ns#")) {
return;
}
//TODO: have trouble paramterizing the template with ? extends OntProperty
List superProperties = prop.listSuperProperties(false).toList(); List superProperties = prop.listSuperProperties(false).toList();
superProperties.addAll(prop.listEquivalentProperties().toList()); superProperties.addAll(prop.listEquivalentProperties().toList());
Iterator<OntProperty> superIt = superProperties.iterator(); Iterator<OntProperty> superIt = superProperties.iterator();
@ -288,15 +301,18 @@ public class SimpleReasoner extends StatementListener {
OntProperty superProp = superIt.next(); OntProperty superProp = superIt.next();
if ( !((prop.isObjectProperty() && superProp.isObjectProperty()) || (prop.isDatatypeProperty() && superProp.isDatatypeProperty())) ) { if ( !((prop.isObjectProperty() && superProp.isObjectProperty()) || (prop.isDatatypeProperty() && superProp.isDatatypeProperty())) ) {
log.warn("sub-property and super-property do not have the same type. No inferencing will be performed. sub-property: " + prop.getURI() + " super-property:" + superProp.getURI()); log.warn("sub-property and super-property do not have the same type. No inferencing will be performed. sub-property: " + prop.getURI() + " super-property:" + superProp.getURI());
return; continue;
}
if (superProp.getURI().equals(topObjectPropertyURI) || superProp.getURI().equals(topDataPropertyURI)) {
continue;
} }
Statement infStmt = ResourceFactory.createStatement(stmt.getSubject(), superProp, stmt.getObject()); Statement infStmt = ResourceFactory.createStatement(stmt.getSubject(), superProp, stmt.getObject());
inferenceModel.enterCriticalSection(Lock.WRITE); inferenceModel.enterCriticalSection(Lock.WRITE);
try { try {
if (!inferenceModel.contains(infStmt) && !infStmt.equals(stmt) ) { if (!inferenceModel.contains(infStmt) && !infStmt.equals(stmt) ) {
//log.debug("Adding this inferred statement: " + infStmt.toString() );
inferenceModel.add(infStmt); inferenceModel.add(infStmt);
} }
} finally { } finally {
@ -404,7 +420,6 @@ public class SimpleReasoner extends StatementListener {
inferenceModel.enterCriticalSection(Lock.WRITE); inferenceModel.enterCriticalSection(Lock.WRITE);
try { try {
if (inferenceModel.contains(infStmt)) { if (inferenceModel.contains(infStmt)) {
//log.debug("Removing this inferred statement: " + infStmt.toString() + " - " + infStmt.getSubject().toString() + " - " + infStmt.getPredicate().toString() + " - " + infStmt.getObject().toString());
inferenceModel.remove(infStmt); inferenceModel.remove(infStmt);
} }
} finally { } finally {
@ -494,7 +509,6 @@ public class SimpleReasoner extends StatementListener {
inferenceModel.enterCriticalSection(Lock.WRITE); inferenceModel.enterCriticalSection(Lock.WRITE);
if (!inferenceModel.contains(infStmt)) { if (!inferenceModel.contains(infStmt)) {
//log.debug("Adding this inferred statement: " + infStmt.toString() );
inferenceModel.add(infStmt); inferenceModel.add(infStmt);
} }
} }
@ -537,7 +551,6 @@ public class SimpleReasoner extends StatementListener {
inferenceModel.enterCriticalSection(Lock.WRITE); inferenceModel.enterCriticalSection(Lock.WRITE);
if (inferenceModel.contains(infStmt)) { if (inferenceModel.contains(infStmt)) {
//log.debug("Removing this inferred statement: " + infStmt.toString() );
inferenceModel.remove(infStmt); inferenceModel.remove(infStmt);
} }
} }
@ -579,7 +592,6 @@ public class SimpleReasoner extends StatementListener {
inferenceModel.enterCriticalSection(Lock.WRITE); inferenceModel.enterCriticalSection(Lock.WRITE);
if (!inferenceModel.contains(infStmt)) { if (!inferenceModel.contains(infStmt)) {
//log.debug("Adding this inferred statement: " + infStmt.toString() );
inferenceModel.add(infStmt); inferenceModel.add(infStmt);
} }
} }
@ -628,7 +640,6 @@ public class SimpleReasoner extends StatementListener {
inferenceModel.enterCriticalSection(Lock.WRITE); inferenceModel.enterCriticalSection(Lock.WRITE);
if (inferenceModel.contains(infStmt)) { if (inferenceModel.contains(infStmt)) {
//log.debug("Adding this inferred statement: " + infStmt.toString() );
inferenceModel.remove(infStmt); inferenceModel.remove(infStmt);
} }
} }
@ -674,31 +685,75 @@ public class SimpleReasoner extends StatementListener {
// recompute the inferences // recompute the inferences
inferenceRebuildModel.enterCriticalSection(Lock.WRITE); inferenceRebuildModel.enterCriticalSection(Lock.WRITE);
aboxModel.enterCriticalSection(Lock.READ); aboxModel.enterCriticalSection(Lock.READ);
tboxModel.enterCriticalSection(Lock.READ);
try { try {
inferenceRebuildModel.removeAll(); inferenceRebuildModel.removeAll();
StmtIterator iter = aboxModel.listStatements((Resource) null, RDF.type, (RDFNode) null); StmtIterator iter = aboxModel.listStatements((Resource) null, RDF.type, (RDFNode) null);
log.info("Computing class-based ABox inferences");
while (iter.hasNext()) { while (iter.hasNext()) {
Statement stmt = iter.next(); Statement stmt = iter.next();
addedABoxTypeAssertion(stmt, inferenceRebuildModel); addedABoxTypeAssertion(stmt, inferenceRebuildModel);
} }
log.info("Computing property-based ABox inferences");
iter = tboxModel.listStatements((Resource) null, RDFS.subPropertyOf, (RDFNode) null);
int numStmts = 0;
while (iter.hasNext()) {
Statement stmt = iter.next();
if (stmt.getSubject().getURI().equals(bottomObjectPropertyURI) || stmt.getSubject().getURI().equals(bottomDataPropertyURI) ||
(stmt.getObject().isResource() && (stmt.getObject().asResource().getURI().equals(topObjectPropertyURI) ||
stmt.getObject().asResource().getURI().equals(topDataPropertyURI))) ) {
continue;
}
if ( stmt.getSubject().equals(stmt.getObject()) ) {
continue;
}
addedTBoxStatement(stmt, inferenceRebuildModel);
numStmts++;
if ((numStmts % 500) == 0) {
log.info("Still computing property-based ABox inferences...");
}
}
iter = tboxModel.listStatements((Resource) null, OWL.equivalentProperty, (RDFNode) null);
while (iter.hasNext()) {
Statement stmt = iter.next();
if ( stmt.getSubject().equals(stmt.getObject()) ) {
continue;
}
addedTBoxStatement(stmt, inferenceRebuildModel);
numStmts++;
if ((numStmts % 500) == 0) {
log.info("Still computing property-based ABox inferences...");
}
}
} catch (Exception e) { } catch (Exception e) {
log.error("Exception while recomputing ABox inference model", e); log.error("Exception while recomputing ABox inference model", e);
} finally { } finally {
aboxModel.leaveCriticalSection(); aboxModel.leaveCriticalSection();
inferenceRebuildModel.leaveCriticalSection(); tboxModel.leaveCriticalSection();
inferenceRebuildModel.leaveCriticalSection();
} }
// reflect the recomputed inferences into the application inference // reflect the recomputed inferences into the application inference
// model. // model.
inferenceRebuildModel.enterCriticalSection(Lock.READ); inferenceRebuildModel.enterCriticalSection(Lock.READ);
scratchpadModel.enterCriticalSection(Lock.WRITE); scratchpadModel.enterCriticalSection(Lock.WRITE);
log.info("Updating ABox inference model");
// Remove everything from the current inference model that is not
// in the recomputed inference model
try { try {
// Remove everything from the current inference model that is not
// in the recomputed inference model
inferenceModel.enterCriticalSection(Lock.READ); inferenceModel.enterCriticalSection(Lock.READ);
try { try {
@ -759,7 +814,7 @@ public class SimpleReasoner extends StatementListener {
scratchpadModel.leaveCriticalSection(); scratchpadModel.leaveCriticalSection();
} }
log.info("ABox inferences recomputed."); log.info("ABox inference model updated");
} }
public static SimpleReasoner getSimpleReasonerFromServletContext(ServletContext ctx) { public static SimpleReasoner getSimpleReasonerFromServletContext(ServletContext ctx) {
@ -775,4 +830,11 @@ public class SimpleReasoner extends StatementListener {
public static boolean isABoxReasoningAsynchronous(ServletContext ctx) { public static boolean isABoxReasoningAsynchronous(ServletContext ctx) {
return (getSimpleReasonerFromServletContext(ctx) == null); return (getSimpleReasonerFromServletContext(ctx) == null);
} }
public static String stmtString(Statement statement) {
return " [subject = " + statement.getSubject().getURI() +
"] [property = " + statement.getPredicate().getURI() +
"] [object = " + (statement.getObject().isLiteral() ? ((Literal)statement.getObject()).getLexicalForm() + " (Literal)"
: ((Resource)statement.getObject()).getURI() + " (Resource)") + "]";
}
} }

View file

@ -4,7 +4,6 @@ package edu.cornell.mannlib.vitro.webapp.reasoner;
import org.apache.log4j.Level; import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -23,7 +22,6 @@ import com.hp.hpl.jena.vocabulary.RDF;
import edu.cornell.mannlib.vitro.testing.AbstractTestClass; import edu.cornell.mannlib.vitro.testing.AbstractTestClass;
import edu.cornell.mannlib.vitro.webapp.reasoner.support.SimpleReasonerTBoxListener; import edu.cornell.mannlib.vitro.webapp.reasoner.support.SimpleReasonerTBoxListener;
import freemarker.ext.dump.BaseDumpDirective;
public class SimpleReasonerTest extends AbstractTestClass { public class SimpleReasonerTest extends AbstractTestClass {