NIHVIVO03967 retraction of inferences by SimpleBridgingRule in batch mode
This commit is contained in:
parent
ddeaf7d7d2
commit
1b8bcb37f0
4 changed files with 2 additions and 286 deletions
|
@ -3,6 +3,7 @@
|
|||
package org.vivoweb.reasoner.plugin;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.reasoner.ReasonerPlugin;
|
||||
import edu.cornell.mannlib.vitro.webapp.reasoner.plugin.SimpleBridgingRule;
|
||||
|
||||
public class DCCreatorForDocuments extends SimpleBridgingRule implements ReasonerPlugin {
|
||||
|
||||
|
|
|
@ -17,6 +17,7 @@ import com.hp.hpl.jena.vocabulary.RDF;
|
|||
import com.hp.hpl.jena.vocabulary.RDFS;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.reasoner.ReasonerPlugin;
|
||||
import edu.cornell.mannlib.vitro.webapp.reasoner.plugin.SimplePropertyAndTypeRule;
|
||||
|
||||
public class DCTitleForDocuments extends SimplePropertyAndTypeRule implements ReasonerPlugin {
|
||||
|
||||
|
|
|
@ -1,149 +0,0 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package org.vivoweb.reasoner.plugin;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
||||
import com.hp.hpl.jena.ontology.OntModel;
|
||||
import com.hp.hpl.jena.query.Query;
|
||||
import com.hp.hpl.jena.query.QueryExecution;
|
||||
import com.hp.hpl.jena.query.QueryExecutionFactory;
|
||||
import com.hp.hpl.jena.query.QueryFactory;
|
||||
import com.hp.hpl.jena.rdf.model.Model;
|
||||
import com.hp.hpl.jena.rdf.model.ModelFactory;
|
||||
import com.hp.hpl.jena.rdf.model.Property;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
import com.hp.hpl.jena.rdf.model.ResourceFactory;
|
||||
import com.hp.hpl.jena.rdf.model.Statement;
|
||||
import com.hp.hpl.jena.rdf.model.StmtIterator;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.reasoner.ReasonerPlugin;
|
||||
import edu.cornell.mannlib.vitro.webapp.reasoner.SimpleReasoner;
|
||||
|
||||
/**
|
||||
* handles rules of the form
|
||||
* assertedProp1(?x, ?y) ^ assertedProp2(?y, ?z) -> inferredProp(?x, ?z)
|
||||
*
|
||||
* @author bjl23
|
||||
*
|
||||
*/
|
||||
public abstract class SimpleBridgingRule implements ReasonerPlugin {
|
||||
|
||||
private static final Log log = LogFactory.getLog(SimpleBridgingRule.class);
|
||||
|
||||
private Property assertedProp1;
|
||||
private Property assertedProp2;
|
||||
private String queryStr;
|
||||
private SimpleReasoner simpleReasoner;
|
||||
|
||||
protected SimpleBridgingRule(String assertedProp1, String assertedProp2, String inferredProp) {
|
||||
this.assertedProp1 = ResourceFactory.createProperty(assertedProp1);
|
||||
this.assertedProp2 = ResourceFactory.createProperty(assertedProp2);
|
||||
|
||||
this.queryStr = "CONSTRUCT { \n" +
|
||||
" ?x <" + inferredProp + "> ?z \n" +
|
||||
"} WHERE { \n" +
|
||||
" ?x <" + assertedProp1 + "> ?y . \n" +
|
||||
" ?y <" + assertedProp2 + "> ?z \n" +
|
||||
"}";
|
||||
}
|
||||
|
||||
public boolean isInterestedInAddedStatement(Statement stmt) {
|
||||
return isRelevantPredicate(stmt);
|
||||
}
|
||||
|
||||
public boolean isInterestedInRemovedStatement(Statement stmt) {
|
||||
return isRelevantPredicate(stmt);
|
||||
}
|
||||
|
||||
public void addedABoxStatement(Statement stmt,
|
||||
Model aboxAssertionsModel,
|
||||
Model aboxInferencesModel,
|
||||
OntModel TBoxInferencesModel) {
|
||||
if (ignore(stmt)) {
|
||||
return;
|
||||
}
|
||||
Model inf = constructInferences(stmt, aboxAssertionsModel);
|
||||
StmtIterator sit = inf.listStatements();
|
||||
while(sit.hasNext()) {
|
||||
Statement s = sit.nextStatement();
|
||||
if (simpleReasoner != null) simpleReasoner.addInference(s,aboxInferencesModel);
|
||||
}
|
||||
}
|
||||
|
||||
private boolean ignore(Statement stmt) {
|
||||
return (
|
||||
(stmt.getSubject().isAnon() || stmt.getObject().isAnon())
|
||||
// can't deal with blank nodes
|
||||
||
|
||||
(!stmt.getObject().isResource())
|
||||
// don't deal with literal values
|
||||
);
|
||||
}
|
||||
|
||||
private Model constructInferences(Statement stmt, Model aboxAssertionsModel) {
|
||||
String queryStr = new String(this.queryStr);
|
||||
if (stmt.getPredicate().equals(assertedProp1)) {
|
||||
queryStr = queryStr.replace(
|
||||
"?x", "<" + stmt.getSubject().getURI() + ">");
|
||||
queryStr = queryStr.replace(
|
||||
"?y", "<" + ((Resource) stmt.getObject()).getURI() + ">");
|
||||
} else if (stmt.getPredicate().equals(assertedProp2)) {
|
||||
queryStr = queryStr.replace(
|
||||
"?y", "<" + stmt.getSubject().getURI() + ">");
|
||||
queryStr = queryStr.replace(
|
||||
"?z", "<" + ((Resource) stmt.getObject()).getURI() + ">");
|
||||
} else {
|
||||
// should never be here
|
||||
return ModelFactory.createDefaultModel();
|
||||
}
|
||||
Query query = QueryFactory.create(queryStr);
|
||||
QueryExecution qe = QueryExecutionFactory.create(query, aboxAssertionsModel);
|
||||
try {
|
||||
return qe.execConstruct();
|
||||
} finally {
|
||||
qe.close();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public void removedABoxStatement(Statement stmt,
|
||||
Model aboxAssertionsModel,
|
||||
Model aboxInferencesModel,
|
||||
OntModel TBoxInferencesModel) {
|
||||
if (ignore(stmt)) {
|
||||
return;
|
||||
}
|
||||
// The following should probably be improved, as it is likely to be
|
||||
// inefficient.
|
||||
// The SPARQL query will currently depend on the existence of the triple
|
||||
// that has just been removed, so we'll union it in temporarily.
|
||||
// TODO: make the SPARQL query construction smarter.
|
||||
Model m = ModelFactory.createDefaultModel();
|
||||
m.add(stmt);
|
||||
Model union = ModelFactory.createUnion(m, aboxAssertionsModel);
|
||||
Model inf = constructInferences(stmt, union);
|
||||
StmtIterator sit = inf.listStatements();
|
||||
while(sit.hasNext()) {
|
||||
Statement s = sit.nextStatement();
|
||||
if (simpleReasoner != null) simpleReasoner.removeInference(s,aboxInferencesModel);
|
||||
}
|
||||
|
||||
aboxInferencesModel.remove(constructInferences(stmt, union));
|
||||
}
|
||||
|
||||
private boolean isRelevantPredicate(Statement stmt) {
|
||||
return (assertedProp1.equals(stmt.getPredicate())
|
||||
|| assertedProp2.equals(stmt.getPredicate()));
|
||||
}
|
||||
|
||||
public void setSimpleReasoner(SimpleReasoner simpleReasoner) {
|
||||
this.simpleReasoner = simpleReasoner;
|
||||
}
|
||||
|
||||
public SimpleReasoner getSimpleReasoner() {
|
||||
return this.simpleReasoner;
|
||||
}
|
||||
}
|
||||
|
|
@ -1,137 +0,0 @@
|
|||
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
|
||||
|
||||
package org.vivoweb.reasoner.plugin;
|
||||
|
||||
import com.hp.hpl.jena.ontology.OntModel;
|
||||
import com.hp.hpl.jena.rdf.model.Model;
|
||||
import com.hp.hpl.jena.rdf.model.Property;
|
||||
import com.hp.hpl.jena.rdf.model.RDFNode;
|
||||
import com.hp.hpl.jena.rdf.model.Resource;
|
||||
import com.hp.hpl.jena.rdf.model.ResourceFactory;
|
||||
import com.hp.hpl.jena.rdf.model.Statement;
|
||||
import com.hp.hpl.jena.rdf.model.StmtIterator;
|
||||
import com.hp.hpl.jena.vocabulary.RDF;
|
||||
import com.hp.hpl.jena.vocabulary.RDFS;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.reasoner.ReasonerPlugin;
|
||||
import edu.cornell.mannlib.vitro.webapp.reasoner.SimpleReasoner;
|
||||
|
||||
/**
|
||||
* handles rules of the form
|
||||
* assertedProp(?x, ?y) ^ type(?x) -> inferredProp(?x, ?y)
|
||||
*
|
||||
* @author bjl23
|
||||
*
|
||||
*/
|
||||
public abstract class SimplePropertyAndTypeRule implements ReasonerPlugin {
|
||||
|
||||
private Property ASSERTED_PROP;
|
||||
private Resource TYPE;
|
||||
private Property INFERRED_PROP;
|
||||
private SimpleReasoner simpleReasoner;
|
||||
|
||||
protected SimplePropertyAndTypeRule(String assertedProp, String type, String inferredProp) {
|
||||
TYPE = ResourceFactory.createResource(type);
|
||||
ASSERTED_PROP = ResourceFactory.createProperty(assertedProp);
|
||||
INFERRED_PROP = ResourceFactory.createProperty(inferredProp);
|
||||
}
|
||||
|
||||
public boolean isInterestedInAddedStatement(Statement stmt) {
|
||||
return (RDF.type.equals(stmt.getPredicate()) || isRelevantPredicate(stmt));
|
||||
}
|
||||
|
||||
public boolean isInterestedInRemovedStatement(Statement stmt) {
|
||||
return (RDF.type.equals(stmt.getPredicate()) || isRelevantPredicate(stmt));
|
||||
}
|
||||
|
||||
public void addedABoxStatement(Statement stmt,
|
||||
Model aboxAssertionsModel,
|
||||
Model aboxInferencesModel,
|
||||
OntModel TBoxInferencesModel) {
|
||||
boolean relevantType = isRelevantType(stmt, TBoxInferencesModel);
|
||||
boolean relevantPredicate = isRelevantPredicate(stmt);
|
||||
|
||||
if (relevantType) {
|
||||
StmtIterator stmtIt = aboxAssertionsModel.listStatements(
|
||||
stmt.getSubject(), ASSERTED_PROP, (RDFNode)null);
|
||||
while (stmtIt.hasNext()) {
|
||||
Statement s = stmtIt.nextStatement();
|
||||
tryToInfer(stmt.getSubject(),
|
||||
INFERRED_PROP,
|
||||
s.getObject(),
|
||||
aboxAssertionsModel,
|
||||
aboxInferencesModel);
|
||||
}
|
||||
} else if (relevantPredicate) {
|
||||
if(aboxAssertionsModel.contains(
|
||||
stmt.getSubject(), RDF.type, TYPE)
|
||||
|| aboxInferencesModel.contains(
|
||||
stmt.getSubject(), RDF.type, TYPE)) {
|
||||
tryToInfer(stmt.getSubject(),
|
||||
INFERRED_PROP,
|
||||
stmt.getObject(),
|
||||
aboxAssertionsModel,
|
||||
aboxInferencesModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void tryToInfer(Resource subject,
|
||||
Property predicate,
|
||||
RDFNode object,
|
||||
Model aboxAssertionsModel,
|
||||
Model aboxInferencesModel) {
|
||||
// this should be part of a superclass or some class that provides
|
||||
// reasoning framework functions
|
||||
Statement s = ResourceFactory.createStatement(subject, predicate, object);
|
||||
if (simpleReasoner != null) {
|
||||
simpleReasoner.addInference(s,aboxInferencesModel);
|
||||
}
|
||||
}
|
||||
|
||||
public void removedABoxStatement(Statement stmt,
|
||||
Model aboxAssertionsModel,
|
||||
Model aboxInferencesModel,
|
||||
OntModel TBoxInferencesModel) {
|
||||
|
||||
if (isRelevantPredicate(stmt)) {
|
||||
// if (aboxAssertionsModel.contains(
|
||||
// stmt.getSubject(), RDF.type, BIBO_DOCUMENT)
|
||||
// || aboxInferencesModel.contains(
|
||||
// stmt.getSubject(), RDF.type, BIBO_DOCUMENT)) {
|
||||
if (simpleReasoner != null) {
|
||||
simpleReasoner.removeInference(ResourceFactory.createStatement(stmt.getSubject(), INFERRED_PROP, stmt.getObject()), aboxInferencesModel);
|
||||
}
|
||||
// }
|
||||
} else if (isRelevantType(stmt, TBoxInferencesModel)) {
|
||||
if(!aboxInferencesModel.contains(
|
||||
stmt.getSubject(), RDF.type, TYPE)) {
|
||||
StmtIterator groundIt = aboxAssertionsModel.listStatements(
|
||||
stmt.getSubject(), ASSERTED_PROP, (RDFNode) null);
|
||||
while (groundIt.hasNext()) {
|
||||
Statement groundStmt = groundIt.nextStatement();
|
||||
simpleReasoner.removeInference(ResourceFactory.createStatement(groundStmt.getSubject(), INFERRED_PROP, groundStmt.getObject()), aboxInferencesModel);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isRelevantType(Statement stmt, Model TBoxInferencesModel) {
|
||||
return (RDF.type.equals(stmt.getPredicate())
|
||||
&& (TYPE.equals(stmt.getObject())
|
||||
|| TBoxInferencesModel.contains(
|
||||
(Resource) stmt.getObject(), RDFS.subClassOf, TYPE)));
|
||||
}
|
||||
|
||||
private boolean isRelevantPredicate(Statement stmt) {
|
||||
return (ASSERTED_PROP.equals(stmt.getPredicate()));
|
||||
}
|
||||
|
||||
public void setSimpleReasoner(SimpleReasoner simpleReasoner) {
|
||||
this.simpleReasoner = simpleReasoner;
|
||||
}
|
||||
|
||||
public SimpleReasoner getSimpleReasoner() {
|
||||
return this.simpleReasoner;
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue