From 802e5bc1ab239b7a2ff2632a38088021d38c36e7 Mon Sep 17 00:00:00 2001 From: sjm222 Date: Tue, 3 Aug 2010 14:25:31 +0000 Subject: [PATCH] merging in 1.1 branch changes --- .../webapp/ontology/update/ABoxUpdater.java | 2 +- .../ontology/update/OntologyUpdater.java | 32 ++++++- .../webapp/ontology/update/TBoxUpdater.java | 87 ++++++++++++------- 3 files changed, 88 insertions(+), 33 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/ABoxUpdater.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/ABoxUpdater.java index fd56e9835..e60953504 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/ABoxUpdater.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/ABoxUpdater.java @@ -267,7 +267,7 @@ public class ABoxUpdater { if (count > 0) { //TODO - take out the detailed logging after our internal testing is completed. - logger.log("There " + ((count > 1) ? "are" : "is") + " " + count + " individuals in the model that are of type " + parentOfAddedClass.getURI() + "," + + logger.log("There " + ((count > 1) ? "are" : "is") + " " + count + " individual" + ((count > 1) ? "s" : "") + " in the model that " + ((count > 1) ? "are" : "is") + " of type " + parentOfAddedClass.getURI() + "," + " and a new subclass of that class has been added: " + addedClass.getURI() + ". " + "Please review " + ((count > 1) ? "these" : "this") + " individual" + ((count > 1) ? "s" : "") + " to see whether " + ((count > 1) ? "they" : "it") + " should be of type: " + addedClass.getURI() ); } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/OntologyUpdater.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/OntologyUpdater.java index c8ccbe3db..383f9e655 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/OntologyUpdater.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/OntologyUpdater.java @@ -81,6 +81,7 @@ public class OntologyUpdater { private void performUpdate() throws IOException { performSparqlConstructAdditions(settings.getSparqlConstructAdditionsDir(), settings.getOntModelSelector().getABoxModel()); + performSparqlConstructRetractions(settings.getSparqlConstructDeletionsDir(), settings.getOntModelSelector().getABoxModel()); List rawChanges = getAtomicOntologyChanges(); @@ -128,9 +129,38 @@ public class OntologyUpdater { aboxModel.leaveCriticalSection(); } - } + private void performSparqlConstructRetractions(String sparqlConstructDir, OntModel aboxModel) throws IOException { + + Model retractions = performSparqlConstructs(sparqlConstructDir, aboxModel); + + if (retractions == null) { + return; + } + + aboxModel.enterCriticalSection(Lock.WRITE); + try { + Model actualRetractions = ModelFactory.createDefaultModel(); + StmtIterator stmtIt = retractions.listStatements(); + while (stmtIt.hasNext()) { + Statement stmt = stmtIt.nextStatement(); + if (aboxModel.contains(stmt)) { + actualRetractions.add(stmt); + } + } + aboxModel.remove(actualRetractions); + if (actualRetractions.size() > 0) { + logger.log("Removed " + actualRetractions.size() + " statement" + + ((actualRetractions.size() > 1) ? "s" : "") + + " using SPARQL CONSTRUCT queries."); + } + record.recordRetractions(actualRetractions); + } finally { + aboxModel.leaveCriticalSection(); + } + + } /** * Performs a set of arbitrary SPARQL CONSTRUCT queries on the diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/TBoxUpdater.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/TBoxUpdater.java index ce2f1d125..bdd799ae0 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/TBoxUpdater.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/TBoxUpdater.java @@ -31,6 +31,7 @@ public class TBoxUpdater { private OntModel siteModel; private OntologyChangeLogger logger; private OntologyChangeRecord record; + private boolean detailLogs = false; /** * @@ -159,32 +160,54 @@ public class TBoxUpdater { " in the new version of the annotations ontology. (maximum of one is expected)"); continue; } + + // If a subject-property pair occurs in the old annotation TBox and the new annotations + // TBox, but not in the site model, then it is considered an erroneous deletion and + // the value from the new TBox is added into the site model. + // sjm: 7-16-2010. We want this here now to add back in annotations mistakenly dropped + // in the .9 to 1.0 migration, but I'm not sure we would want this here permanently. + // Shouldn't a site be allowed to delete annotations if they want to? + + NodeIterator siteObjects = siteModel.listObjectsOfProperty(subject,predicate); + + if (siteObjects == null || !siteObjects.hasNext()) { + try { + additions.add(subject, predicate, newObject); + + if (detailLogs) { + logger.log( "adding Statement: subject = " + subject.getURI() + + " property = " + predicate.getURI() + + " object = " + (newObject.isLiteral() ? ((Literal)newObject).getLexicalForm() + : ((Resource)newObject).getURI())); + } + } catch (Exception e) { + logger.logError("Error trying to add statement with property " + predicate.getURI() + + " of class = " + subject.getURI() + " in the knowledge base:\n" + e.getMessage()); + } + + continue; + } + if (!newObject.equals(oldObject)) { - NodeIterator siteObjects = siteModel.listObjectsOfProperty(subject,predicate); - - RDFNode siteObject = null; - - if (siteObjects != null && siteObjects.hasNext()) { - - siteObject = siteObjects.next(); - - i = 1; - while (siteObjects.hasNext()) { - i++; - siteObjects.next(); - } - - if (i > 1) { - logger.log("WARNING: found " + i + - " statements with subject = " + subject.getURI() + - " and property = " + predicate.getURI() + - " in the site annotations model. (maximum of one is expected). "); - continue; - } + + RDFNode siteObject = siteObjects.next(); + + i = 1; + while (siteObjects.hasNext()) { + i++; + siteObjects.next(); + } + + if (i > 1) { + logger.log("WARNING: found " + i + + " statements with subject = " + subject.getURI() + + " and property = " + predicate.getURI() + + " in the site annotations model. (maximum of one is expected). "); + continue; } - if (siteObject == null || siteObject.equals(oldObject)) { + if (siteObject.equals(oldObject)) { try { StmtIterator it = siteModel.listStatements(subject, predicate, (RDFNode)null); while (it.hasNext()) { @@ -199,13 +222,15 @@ public class TBoxUpdater { try { additions.add(subject, predicate, newObject); - logger.log("Changed the value of property " + predicate.getURI() + + if (detailLogs) { + logger.log("Changed the value of property " + predicate.getURI() + " of subject = " + subject.getURI() + " from " + (oldObject.isResource() ? ((Resource)oldObject).getURI() : ((Literal)oldObject).getLexicalForm()) + " to " + (newObject.isResource() ? ((Resource)newObject).getURI() : ((Literal)newObject).getLexicalForm()) + " in the knowledge base:\n"); + } } catch (Exception e) { logger.logError("Error trying to change the value of property " + predicate.getURI() + " of class = " + subject.getURI() + " in the knowledge base:\n" + e.getMessage()); @@ -230,7 +255,7 @@ public class TBoxUpdater { long numRemoved = actualRetractions.size() - actualAdditions.size(); if (numRemoved > 0) { logger.log("Removed " + numRemoved + - " superfluous vitro annotation property setting" + ((numRemoved > 1) ? "s" : "") + " from the knowledge base."); + " outdated vitro annotation property setting" + ((numRemoved > 1) ? "s" : "") + " from the knowledge base."); } // Copy annotation property settings that were introduced in the new ontology @@ -244,20 +269,20 @@ public class TBoxUpdater { Statement stmt = newStmtIt.next(); if (!siteModel.contains(stmt)) { newAnnotationSettingsToAdd.add(stmt); - - // TODO remove this for production - logger.log( "adding Statement: subject = " + stmt.getSubject().getURI() + - " property = " + stmt.getPredicate().getURI() + - " object = " + (stmt.getObject().isLiteral() ? ((Literal)stmt.getObject()).getLexicalForm() + + if (detailLogs) { + logger.log( "adding Statement: subject = " + stmt.getSubject().getURI() + + " property = " + stmt.getPredicate().getURI() + + " object = " + (stmt.getObject().isLiteral() ? ((Literal)stmt.getObject()).getLexicalForm() : ((Resource)stmt.getObject()).getURI())); + } } } siteModel.add(newAnnotationSettingsToAdd); record.recordAdditions(newAnnotationSettingsToAdd); - // log the additions - //summary + // log the additions - summary if (newAnnotationSettingsToAdd.size() > 0) { boolean plural = (newAnnotationSettingsToAdd.size() > 1); logger.log("Added " + newAnnotationSettingsToAdd.size() + " new annotation property setting" + (plural ? "s" : "") + " to the knowledge base. This includes " +