From 0bab5a3f6f2af08d70fe57d4d6a8f33fa8d9f586 Mon Sep 17 00:00:00 2001 From: sjm222 Date: Fri, 16 Jul 2010 15:17:47 +0000 Subject: [PATCH] infrastructure for handling deletions with sparql constructs --- .../update/OntologyUpdateSettings.java | 17 +++-- .../ontology/update/OntologyUpdater.java | 73 +++++++++++-------- .../servlet/setup/UpdateKnowledgeBase.java | 6 +- 3 files changed, 60 insertions(+), 36 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/OntologyUpdateSettings.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/OntologyUpdateSettings.java index e5d4c7730..0a0a610fd 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/OntologyUpdateSettings.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/OntologyUpdateSettings.java @@ -9,7 +9,8 @@ import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector; public class OntologyUpdateSettings { private String dataDir; - private String sparqlConstructsDir; + private String sparqlConstructAdditionsDir; + private String sparqlConstructDeletionsDir; private String askQueryFile; private String successAssertionsFile; private String successRDFFormat = "N3"; @@ -31,11 +32,17 @@ public class OntologyUpdateSettings { public void setDataDir(String dataDir) { this.dataDir = dataDir; } - public String getSparqlConstructsDir() { - return sparqlConstructsDir; + public String getSparqlConstructAdditionsDir() { + return sparqlConstructAdditionsDir; } - public void setSparqlConstructsDir(String sparqlConstructsDir) { - this.sparqlConstructsDir = sparqlConstructsDir; + public void setSparqlConstructAdditionsDir(String sparqlConstructAdditionsDir) { + this.sparqlConstructAdditionsDir = sparqlConstructAdditionsDir; + } + public String getSparqlConstructDeletionsDir() { + return sparqlConstructDeletionsDir; + } + public void setSparqlConstructDeletionsDir(String sparqlConstructDeletionsDir) { + this.sparqlConstructDeletionsDir = sparqlConstructDeletionsDir; } public String getAskQueryFile() { return askQueryFile; 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 68be7f551..c8ccbe3db 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 @@ -80,7 +80,7 @@ public class OntologyUpdater { private void performUpdate() throws IOException { - performSparqlConstructs(settings.getSparqlConstructsDir(), settings.getOntModelSelector().getABoxModel()); + performSparqlConstructAdditions(settings.getSparqlConstructAdditionsDir(), settings.getOntModelSelector().getABoxModel()); List rawChanges = getAtomicOntologyChanges(); @@ -94,6 +94,44 @@ public class OntologyUpdater { } + + private void performSparqlConstructAdditions(String sparqlConstructDir, OntModel aboxModel) throws IOException { + + Model anonModel = performSparqlConstructs(sparqlConstructDir, aboxModel); + + if (anonModel == null) { + return; + } + + aboxModel.enterCriticalSection(Lock.WRITE); + try { + JenaIngestUtils jiu = new JenaIngestUtils(); + Model additions = jiu.renameBNodes(anonModel, settings.getDefaultNamespace() + "n", + aboxModel); + Model actualAdditions = ModelFactory.createDefaultModel(); + StmtIterator stmtIt = additions.listStatements(); + while (stmtIt.hasNext()) { + Statement stmt = stmtIt.nextStatement(); + if (!aboxModel.contains(stmt)) { + actualAdditions.add(stmt); + } + } + aboxModel.add(actualAdditions); + if (actualAdditions.size() > 0) { + logger.log("Constructed " + actualAdditions.size() + " new " + + "statement" + + ((actualAdditions.size() > 1) ? "s" : "") + + " using SPARQL CONSTRUCT queries."); + } + record.recordAdditions(actualAdditions); + } finally { + aboxModel.leaveCriticalSection(); + } + + + } + + /** * Performs a set of arbitrary SPARQL CONSTRUCT queries on the * data, for changes that cannot be expressed as simple property @@ -102,16 +140,18 @@ public class OntologyUpdater { * @param sparqlConstructDir * @param aboxModel */ - private void performSparqlConstructs(String sparqlConstructDir, + private Model performSparqlConstructs(String sparqlConstructDir, OntModel aboxModel) throws IOException { + Model anonModel = ModelFactory.createDefaultModel(); File sparqlConstructDirectory = new File(sparqlConstructDir); + if (!sparqlConstructDirectory.isDirectory()) { logger.logError(this.getClass().getName() + "performSparqlConstructs() expected to find a directory " + " at " + sparqlConstructDir + ". Unable to execute " + " SPARQL CONSTRUCTS."); - return; + return null; } File[] sparqlFiles = sparqlConstructDirectory.listFiles(); for (int i = 0; i < sparqlFiles.length; i ++) { @@ -147,32 +187,7 @@ public class OntologyUpdater { } } - aboxModel.enterCriticalSection(Lock.WRITE); - try { - JenaIngestUtils jiu = new JenaIngestUtils(); - Model additions = jiu.renameBNodes(anonModel, settings.getDefaultNamespace() + "n", - aboxModel); - Model actualAdditions = ModelFactory.createDefaultModel(); - StmtIterator stmtIt = additions.listStatements(); - while (stmtIt.hasNext()) { - Statement stmt = stmtIt.nextStatement(); - if (!aboxModel.contains(stmt)) { - actualAdditions.add(stmt); - } - } - aboxModel.add(actualAdditions); - if (actualAdditions.size() > 0) { - logger.log("Constructed " + actualAdditions.size() + " new " + - "statement" - + ((actualAdditions.size() > 1) ? "s" : "") + - " using SPARQL CONSTRUCT queries."); - } - record.recordAdditions(actualAdditions); - } finally { - aboxModel.leaveCriticalSection(); - } - - + return anonModel; } diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateKnowledgeBase.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateKnowledgeBase.java index 564190aa0..0e672035a 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateKnowledgeBase.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/servlet/setup/UpdateKnowledgeBase.java @@ -52,7 +52,8 @@ public class UpdateKnowledgeBase implements ServletContextListener { private final String ERROR_LOG_FILE = DATA_DIR + LOG_DIR + "knowledgeBaseUpdate.error.log"; private final String REMOVED_DATA_FILE = DATA_DIR + CHANGED_DATA_DIR + "removedData.n3"; private final String ADDED_DATA_FILE = DATA_DIR + CHANGED_DATA_DIR + "addedData.n3"; - private final String SPARQL_CONSTRUCTS_DIR = DATA_DIR + "sparqlConstructs/"; + private final String SPARQL_CONSTRUCT_ADDITIONS_DIR = DATA_DIR + "sparqlConstructs/additions/"; + private final String SPARQL_CONSTRUCT_DELETIONS_DIR = DATA_DIR + "sparqlConstructs/deletions/"; private final String MISC_REPLACEMENTS_FILE = DATA_DIR + "miscReplacements.rdf"; private final String OLD_TBOX_MODEL_DIR = DATA_DIR + "oldVersion/"; private final String NEW_TBOX_MODEL_DIR = "/WEB-INF/submodels/"; @@ -72,7 +73,8 @@ public class UpdateKnowledgeBase implements ServletContextListener { OntologyUpdateSettings settings = new OntologyUpdateSettings(); settings.setAskQueryFile(ctx.getRealPath(ASK_QUERY_FILE)); settings.setDataDir(ctx.getRealPath(DATA_DIR)); - settings.setSparqlConstructsDir(ctx.getRealPath(SPARQL_CONSTRUCTS_DIR)); + settings.setSparqlConstructAdditionsDir(ctx.getRealPath(SPARQL_CONSTRUCT_ADDITIONS_DIR)); + settings.setSparqlConstructDeletionsDir(ctx.getRealPath(SPARQL_CONSTRUCT_DELETIONS_DIR)); settings.setDiffFile(ctx.getRealPath(DIFF_FILE)); settings.setSuccessAssertionsFile(ctx.getRealPath(SUCCESS_ASSERTIONS_FILE)); settings.setSuccessRDFFormat(SUCCESS_RDF_FORMAT);