VIVO-778 Break out just the task of building the pattern list.

This commit is contained in:
Jim Blake 2014-12-02 14:22:58 -05:00
parent d513dcf9ae
commit 4e15695281
3 changed files with 157 additions and 107 deletions

View file

@ -0,0 +1,110 @@
/* $This file is distributed under the terms of the license in /doc/license.txt$ */
package edu.cornell.mannlib.vitro.webapp.dao.jena.pellet;
import static com.hp.hpl.jena.rdf.model.ResourceFactory.createProperty;
import java.util.LinkedList;
import java.util.Set;
import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;
import edu.cornell.mannlib.vitro.webapp.tboxreasoner.ReasonerConfiguration;
import edu.cornell.mannlib.vitro.webapp.tboxreasoner.ReasonerStatementPattern;
import edu.cornell.mannlib.vitro.webapp.utils.jena.criticalsection.LockableModel;
import edu.cornell.mannlib.vitro.webapp.utils.jena.criticalsection.LockableOntModel;
import edu.cornell.mannlib.vitro.webapp.utils.jena.criticalsection.LockedModel;
import edu.cornell.mannlib.vitro.webapp.utils.jena.criticalsection.LockedOntModel;
/**
* The list of patterns for filtering the models will include:
*
* All patterns specified by the ReasonerConfiguration,
*
* One pattern for each deleted property, to match the use of that property as a
* predicate.
*/
public class PatternListBuilder {
private static final String OWL_NS = "http://www.w3.org/2002/07/owl#";
private final ReasonerConfiguration reasonerConfiguration;
private final LockableOntModel lockableReasonerModel;
private final LockableModel lockableDeletedObjectProperties;
private final LockableModel lockableDeletedDataProperties;
public PatternListBuilder(ReasonerConfiguration reasonerConfiguration,
OntModel reasonerModel, Model deletedObjectProperties,
Model deletedDataProperties) {
this.reasonerConfiguration = reasonerConfiguration;
this.lockableReasonerModel = new LockableOntModel(reasonerModel);
this.lockableDeletedObjectProperties = new LockableModel(
deletedObjectProperties);
this.lockableDeletedDataProperties = new LockableModel(
deletedDataProperties);
}
/**
* @return
*/
public LinkedList<ReasonerStatementPattern> build() {
LinkedList<ReasonerStatementPattern> irpl = new LinkedList<>();
Set<ReasonerStatementPattern> allowSet = reasonerConfiguration
.getInferenceReceivingPatternAllowSet();
if (allowSet != null) {
irpl.addAll(allowSet);
} else {
irpl.add(ReasonerStatementPattern.ANY_OBJECT_PROPERTY);
}
if (reasonerConfiguration.getQueryForAllObjectProperties()) {
try (LockedOntModel reasonerModel = lockableReasonerModel.read()) {
for (ObjectProperty objProp : reasonerModel
.listObjectProperties().toList()) {
if (!(OWL_NS.equals(objProp.getNameSpace()))) {
irpl.add(ReasonerStatementPattern
.objectPattern(objProp));
}
}
}
try (LockedModel deletedObjectProperties = lockableDeletedObjectProperties
.write()) {
for (Resource subj : deletedObjectProperties.listSubjects()
.toList()) {
irpl.add(ReasonerStatementPattern
.objectPattern(createProperty(subj.getURI())));
}
deletedObjectProperties.removeAll();
}
}
if (reasonerConfiguration.getQueryForAllDatatypeProperties()) {
try (LockedOntModel reasonerModel = lockableReasonerModel.read()) {
for (DatatypeProperty dataProp : reasonerModel
.listDatatypeProperties().toList()) {
if (!(OWL_NS.equals(dataProp.getNameSpace()))) {
// TODO: THIS WILL WORK, BUT NEED TO GENERALIZE THE
// PATTERN CLASSES
irpl.add(ReasonerStatementPattern
.objectPattern(dataProp));
}
}
}
try (LockedModel deletedDataProperties = lockableDeletedDataProperties
.write()) {
for (Resource subj : deletedDataProperties.listSubjects()
.toList()) {
irpl.add(ReasonerStatementPattern
.objectPattern(createProperty(subj.getURI())));
}
deletedDataProperties.removeAll();
}
}
return irpl;
}
}

View file

@ -2,7 +2,6 @@
package edu.cornell.mannlib.vitro.webapp.dao.jena.pellet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Set;
@ -10,19 +9,14 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mindswap.pellet.exceptions.InconsistentOntologyException;
import org.mindswap.pellet.jena.PelletInfGraph;
import org.mindswap.pellet.jena.PelletReasonerFactory;
import com.hp.hpl.jena.ontology.DatatypeProperty;
import com.hp.hpl.jena.ontology.ObjectProperty;
import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;
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.shared.Lock;
import com.hp.hpl.jena.util.iterator.ClosableIterator;
import edu.cornell.mannlib.vitro.webapp.dao.jena.event.EditEvent;
import edu.cornell.mannlib.vitro.webapp.tboxreasoner.ConfiguredReasonerListener;
@ -54,20 +48,18 @@ public class PelletListener implements TBoxReasonerDriver {
private Model deletedObjectProperties;
private Model deletedDataProperties;
private boolean isConsistent = true;
private boolean inErrorState = false;
private String explanation = "";
private Status status = Status.SUCCESS;
public boolean isConsistent() {
return this.isConsistent;
return this.status.isConsistent();
}
public String getExplanation() {
return this.explanation;
return this.status.getExplanation();
}
public boolean isInErrorState() {
return this.inErrorState;
return this.status.isInErrorState();
}
public boolean isReasoning() {
@ -95,8 +87,6 @@ public class PelletListener implements TBoxReasonerDriver {
this.dirty = dirt;
}
private int inferenceRounds = 0;
private boolean foreground = false;
private static final boolean FOREGROUND = true;
private static final boolean BACKGROUND = false;
@ -217,96 +207,16 @@ public class PelletListener implements TBoxReasonerDriver {
public void run() {
while (pelletListener.isDirty()) {
//pipeOpen = false;
try {
pelletListener.setDirty(false);
inferenceRounds++;
log.info("Getting new inferences");
long startTime = System.currentTimeMillis();
LinkedList<ReasonerStatementPattern> irpl = new LinkedList<>();
if (inferenceReceivingPatternAllowSet != null) {
irpl.addAll(inferenceReceivingPatternAllowSet);
} else {
irpl.add(ReasonerStatementPattern.ANY_OBJECT_PROPERTY);
}
if (reasonerConfiguration.getQueryForAllObjectProperties()) {
pelletModel.enterCriticalSection(Lock.READ);
try {
ClosableIterator closeIt = pelletModel.listObjectProperties();
try {
for (Iterator objPropIt = closeIt; objPropIt.hasNext();) {
ObjectProperty objProp = (ObjectProperty) objPropIt.next();
if ( !("http://www.w3.org/2002/07/owl#".equals(objProp.getNameSpace())) ) {
irpl.add(ReasonerStatementPattern.objectPattern(objProp));
}
}
} finally {
closeIt.close();
}
} finally {
pelletModel.leaveCriticalSection();
}
deletedObjectProperties.enterCriticalSection(Lock.WRITE);
try {
ClosableIterator sit = deletedObjectProperties.listSubjects();
try {
while (sit.hasNext()) {
Resource subj = (Resource) sit.next();
irpl.add(ReasonerStatementPattern.objectPattern(ResourceFactory.createProperty(subj.getURI())));
}
} finally {
sit.close();
}
deletedObjectProperties.removeAll();
} finally {
deletedObjectProperties.leaveCriticalSection();
}
}
if (reasonerConfiguration.getQueryForAllDatatypeProperties()) {
pelletModel.enterCriticalSection(Lock.READ);
try {
ClosableIterator closeIt = pelletModel.listDatatypeProperties();
try {
for (Iterator dataPropIt = closeIt; dataPropIt.hasNext();) {
DatatypeProperty dataProp = (DatatypeProperty) dataPropIt.next();
if ( !("http://www.w3.org/2002/07/owl#".equals(dataProp.getNameSpace())) ) {
// TODO: THIS WILL WORK, BUT NEED TO GENERALIZE THE PATTERN CLASSES
irpl.add(ReasonerStatementPattern.objectPattern(dataProp));
}
}
} finally {
closeIt.close();
}
} finally {
pelletModel.leaveCriticalSection();
}
deletedDataProperties.enterCriticalSection(Lock.WRITE);
try {
ClosableIterator sit = deletedDataProperties.listSubjects();
try {
while (sit.hasNext()) {
Resource subj = (Resource) sit.next();
irpl.add(ReasonerStatementPattern.objectPattern(ResourceFactory.createProperty(subj.getURI())));
}
} finally {
sit.close();
}
deletedDataProperties.removeAll();
} finally {
deletedDataProperties.leaveCriticalSection();
}
}
// force new reasoner (disabled)
if (false && !reasonerConfiguration.isIncrementalReasoningEnabled()) {
Model baseModel = pelletModel.getBaseModel();
pelletModel = ModelFactory.createOntologyModel(PelletReasonerFactory.THE_SPEC);
pelletModel.getDocumentManager().setProcessImports(false);
pelletModel.add(baseModel);
}
PatternListBuilder patternListBuilder = new PatternListBuilder(
reasonerConfiguration, pelletModel,
deletedObjectProperties, deletedDataProperties);
LinkedList<ReasonerStatementPattern> irpl = patternListBuilder
.build();
pelletModel.enterCriticalSection(Lock.WRITE);
try {
@ -320,22 +230,19 @@ public class PelletListener implements TBoxReasonerDriver {
pelletModel, inferenceModel, fullModel, listener);
inferenceModelUpdater.update(irpl);
this.pelletListener.isConsistent = true;
this.pelletListener.inErrorState = false;
this.pelletListener.explanation = "";
if (log.isDebugEnabled()) {
this.pelletListener.status = Status.SUCCESS;
if (log.isInfoEnabled()) {
log.info("Added "+inferenceModelUpdater.getAddCount()+" statements entailed by assertions");
log.info("Retracted "+inferenceModelUpdater.getRetractCount()+" statements no longer entailed by assertions");
log.info("Done getting new inferences: "+(System.currentTimeMillis()-startTime)/1000+" seconds");
}
} catch (InconsistentOntologyException ioe) {
this.pelletListener.isConsistent = false;
String explanation = ((PelletInfGraph)pelletModel.getGraph()).getKB().getExplanation();
this.pelletListener.explanation = explanation;
this.pelletListener.status = Status.inconsistent(explanation);
log.error(ioe);
log.error(explanation);
} catch (Exception e) {
this.pelletListener.inErrorState = true;
this.pelletListener.status = Status.ERROR;
log.error("Exception during inference", e);
} finally {
pelletListener.endReasoning();

View file

@ -5,7 +5,8 @@ package edu.cornell.mannlib.vitro.webapp.tboxreasoner;
import com.hp.hpl.jena.rdf.model.Statement;
/**
* What calls can the ConfiguredReasonerListener make to drive the TBox reasoner?
* What calls can the ConfiguredReasonerListener make to drive the TBox
* reasoner?
*/
public interface TBoxReasonerDriver {
void runSynchronizer();
@ -18,4 +19,36 @@ public interface TBoxReasonerDriver {
void deleteObjectProperty(Statement stmt);
public static class Status {
public static final Status SUCCESS = new Status(true, false, "");
public static final Status ERROR = new Status(true, true, "");
public static final Status inconsistent(String explanation) {
return new Status(false, false, explanation);
}
private final boolean consistent;
private final boolean inErrorState;
private final String explanation;
private Status(boolean consistent, boolean inErrorState,
String explanation) {
this.consistent = consistent;
this.inErrorState = inErrorState;
this.explanation = explanation;
}
public boolean isConsistent() {
return consistent;
}
public boolean isInErrorState() {
return inErrorState;
}
public String getExplanation() {
return explanation;
}
}
}