From 82dfc8d0ca04b08b2e21e148843bd775f7b74e7a Mon Sep 17 00:00:00 2001 From: bjl23 Date: Tue, 30 Mar 2010 17:41:01 +0000 Subject: [PATCH] initial glue code for NIHVIVO-205 'Ontology Upgrade...' --- .../ontology/update/OntologyUpdater.java | 118 ++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/OntologyUpdater.java 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 new file mode 100644 index 000000000..9e19b0aa1 --- /dev/null +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/ontology/update/OntologyUpdater.java @@ -0,0 +1,118 @@ +/* $This file is distributed under the terms of the license in /doc/license.txt$ */ + +package edu.cornell.mannlib.vitro.webapp.ontology.update; + +import java.io.InputStream; +import java.util.List; + +import javax.servlet.ServletContext; + +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.shared.Lock; + +import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector; + +/** + * Performs knowledge base updates if necessary to align with a + * new ontology version. + * + * This class assumes that config and mapping data files are available + * at DATA_DIR. + * + * @author bjl23 + * + */ +public class OntologyUpdater { + + private final String DATA_DIR = "/WEB-INF/ontologies/update/"; + private final String ASK_QUERY_FILE = DATA_DIR + "ask.sparql"; + private final String SUCCESS_ASSERTIONS = DATA_DIR + "success.n3"; + private final String SUCCESS_RDF_FORMAT = "N3"; + private final String DIFF_FILE = DATA_DIR + "diff.tab.txt"; + + private ServletContext context; + private OntModelSelector ontModelSelector; + + public OntologyUpdater(ServletContext context, + OntModelSelector ontModelSelector) { + this.context = context; + this.ontModelSelector = ontModelSelector; + } + + public void update() { + // Check to see if the update is necessary. + if (updateRequired()) { + performUpdate(); + // add assertions to the knowledge base showing that the + // update was successful, so we don't need to run it again. + assertSuccess(); + } + } + + private void performUpdate() { + List changes = getAtomicOntologyChanges(); + + //preprocessChanges(changes); + //updateTBox(changes); + + updateABox(changes); + + updateAnnotations(); + + // perform additional additions and retractions + } + + private List getAtomicOntologyChanges() { + return null; //Anup's code is called here + } + + private void updateABox(List changes) { + // perform operations based on change objects + // run additional SPARQL CONSTRUCTS + + } + + private void updateAnnotations() { + // Stella's code is called here + } + + /** + * Executes a SPARQL ASK query to determine whether the knowledge base + * needs to be updated to conform to a new ontology version + */ + private boolean updateRequired() { + String sparqlQueryStr = loadSparqlQuery(ASK_QUERY_FILE); + if (sparqlQueryStr == null) { + return false; + } + Model m = ontModelSelector.getApplicationMetadataModel(); + Query query = QueryFactory.create(sparqlQueryStr); + QueryExecution qexec = QueryExecutionFactory.create(query, m); + return qexec.execAsk(); + } + + /** + * loads a SPARQL ASK query from a text file + * @param filePath + * @return the query string or null if file not found + */ + private String loadSparqlQuery(String filePath) { + return null; // TODO implement me! + } + + private void assertSuccess() { + Model m = ontModelSelector.getApplicationMetadataModel(); + InputStream inStream = context.getResourceAsStream(SUCCESS_ASSERTIONS); + m.enterCriticalSection(Lock.WRITE); + try { + m.read(inStream, SUCCESS_RDF_FORMAT); + } finally { + m.leaveCriticalSection(); + } + } + +}