From 4e156952814cdbc36b1fd2734c54514643bb83e1 Mon Sep 17 00:00:00 2001 From: Jim Blake Date: Tue, 2 Dec 2014 14:22:58 -0500 Subject: [PATCH] VIVO-778 Break out just the task of building the pattern list. --- .../dao/jena/pellet/PatternListBuilder.java | 110 ++++++++++++++++ .../dao/jena/pellet/PelletListener.java | 119 ++---------------- .../tboxreasoner/TBoxReasonerDriver.java | 35 +++++- 3 files changed, 157 insertions(+), 107 deletions(-) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/pellet/PatternListBuilder.java diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/pellet/PatternListBuilder.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/pellet/PatternListBuilder.java new file mode 100644 index 000000000..dbc5d0417 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/pellet/PatternListBuilder.java @@ -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 build() { + LinkedList irpl = new LinkedList<>(); + + Set 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; + } +} diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/pellet/PelletListener.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/pellet/PelletListener.java index fc44d2d14..0a706f13a 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/pellet/PelletListener.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/dao/jena/pellet/PelletListener.java @@ -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 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 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(); diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/tboxreasoner/TBoxReasonerDriver.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/tboxreasoner/TBoxReasonerDriver.java index a52d8e06a..09c356adb 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/tboxreasoner/TBoxReasonerDriver.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/tboxreasoner/TBoxReasonerDriver.java @@ -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; + } + + } }