work on NIHVIVO-205 ontology upgrade

This commit is contained in:
bjl23 2010-03-31 14:48:44 +00:00
parent 6d2968177c
commit 66a7042506
4 changed files with 134 additions and 33 deletions

View file

@ -78,6 +78,12 @@
<listener-class>edu.cornell.mannlib.vitro.webapp.servlet.setup.AttachSubmodels</listener-class> <listener-class>edu.cornell.mannlib.vitro.webapp.servlet.setup.AttachSubmodels</listener-class>
</listener> </listener>
<!-- Invokes process to perform updates to align with ontology changes if needed -->
<listener>
<listener-class>edu.cornell.mannlib.vitro.webapp.servlet.setup.UpdateKnowledgeBase</listener-class>
</listener>
<!-- Pellet setup enables reasoning. Inferences are cached in a separate DB graph. --> <!-- Pellet setup enables reasoning. Inferences are cached in a separate DB graph. -->
<!-- Changing the class name sets the kindss of inferences that are materialized. --> <!-- Changing the class name sets the kindss of inferences that are materialized. -->
<!-- See documentation for details. --> <!-- See documentation for details. -->

View file

@ -0,0 +1,72 @@
package edu.cornell.mannlib.vitro.webapp.ontology.update;
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
public class OntologyUpdateSettings {
private String dataDir;
private String askQueryFile;
private String successAssertionsFile;
private String successRDFFormat = "N3";
private String diffFile;
private String logFile;
private String errorLogFile;
private String removedDataFile;
private OntModelSelector ontModelSelector;
public String getDataDir() {
return dataDir;
}
public void setDataDir(String dataDir) {
this.dataDir = dataDir;
}
public String getAskQueryFile() {
return askQueryFile;
}
public void setAskQueryFile(String askQueryFile) {
this.askQueryFile = askQueryFile;
}
public String getSuccessAssertionsFile() {
return successAssertionsFile;
}
public void setSuccessAssertionsFile(String successAssertionsFile) {
this.successAssertionsFile = successAssertionsFile;
}
public String getSuccessRDFFormat() {
return successRDFFormat;
}
public void setSuccessRDFFormat(String successRDFFormat) {
this.successRDFFormat = successRDFFormat;
}
public String getDiffFile() {
return diffFile;
}
public void setDiffFile(String diffFile) {
this.diffFile = diffFile;
}
public OntModelSelector getOntModelSelector() {
return ontModelSelector;
}
public String getLogFile() {
return logFile;
}
public void setLogFile(String logFile) {
this.logFile = logFile;
}
public String getErrorLogFile() {
return errorLogFile;
}
public void setErrorLogFile(String errorLogFile) {
this.errorLogFile = errorLogFile;
}
public String getRemovedDataFile() {
return removedDataFile;
}
public void setRemovedDataFile(String removedDataFile) {
this.removedDataFile = removedDataFile;
}
public void setOntModelSelector(OntModelSelector ontModelSelector) {
this.ontModelSelector = ontModelSelector;
}
}

View file

@ -4,14 +4,13 @@ package edu.cornell.mannlib.vitro.webapp.ontology.update;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.File; import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
import javax.servlet.ServletContext;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -19,12 +18,9 @@ import com.hp.hpl.jena.query.Query;
import com.hp.hpl.jena.query.QueryExecution; import com.hp.hpl.jena.query.QueryExecution;
import com.hp.hpl.jena.query.QueryExecutionFactory; import com.hp.hpl.jena.query.QueryExecutionFactory;
import com.hp.hpl.jena.query.QueryFactory; import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.shared.Lock; 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 * Performs knowledge base updates if necessary to align with a
* new ontology version. * new ontology version.
@ -37,21 +33,14 @@ import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
*/ */
public class OntologyUpdater { 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_FILE = DATA_DIR + "success.n3";
private final String SUCCESS_RDF_FORMAT = "N3";
private final String DIFF_FILE = DATA_DIR + "diff.tab.txt";
private final Log log = LogFactory.getLog(OntologyUpdater.class); private final Log log = LogFactory.getLog(OntologyUpdater.class);
private ServletContext context; private OntologyUpdateSettings settings;
private OntModelSelector ontModelSelector;
public OntologyUpdater(ServletContext context, public OntologyUpdater(OntologyUpdateSettings settings) {
OntModelSelector ontModelSelector) { this.settings = settings;
this.context = context;
this.ontModelSelector = ontModelSelector;
} }
public void update() throws IOException { public void update() throws IOException {
@ -67,12 +56,12 @@ public class OntologyUpdater {
private void performUpdate() { private void performUpdate() {
List<AtomicOntologyChange> changes = getAtomicOntologyChanges(); List<AtomicOntologyChange> changes = getAtomicOntologyChanges();
//preprocessChanges(changes);
//updateTBox(changes); //updateTBox(changes);
//preprocessChanges(changes);
updateABox(changes); updateABox(changes);
updateAnnotations(); updateTBoxAnnotations();
// perform additional additions and retractions // perform additional additions and retractions
} }
@ -84,10 +73,9 @@ public class OntologyUpdater {
private void updateABox(List<AtomicOntologyChange> changes) { private void updateABox(List<AtomicOntologyChange> changes) {
// perform operations based on change objects // perform operations based on change objects
// run additional SPARQL CONSTRUCTS // run additional SPARQL CONSTRUCTS
} }
private void updateAnnotations() { private void updateTBoxAnnotations() {
// Stella's code is called here // Stella's code is called here
} }
@ -96,11 +84,11 @@ public class OntologyUpdater {
* needs to be updated to conform to a new ontology version * needs to be updated to conform to a new ontology version
*/ */
private boolean updateRequired() throws IOException { private boolean updateRequired() throws IOException {
String sparqlQueryStr = loadSparqlQuery(ASK_QUERY_FILE); String sparqlQueryStr = loadSparqlQuery(settings.getAskQueryFile());
if (sparqlQueryStr == null) { if (sparqlQueryStr == null) {
return false; return false;
} }
Model m = ontModelSelector.getApplicationMetadataModel(); Model m = settings.getOntModelSelector().getApplicationMetadataModel();
Query query = QueryFactory.create(sparqlQueryStr); Query query = QueryFactory.create(sparqlQueryStr);
QueryExecution qexec = QueryExecutionFactory.create(query, m); QueryExecution qexec = QueryExecutionFactory.create(query, m);
@ -117,7 +105,7 @@ public class OntologyUpdater {
* @return the query string or null if file not found * @return the query string or null if file not found
*/ */
private String loadSparqlQuery(String filePath) throws IOException { private String loadSparqlQuery(String filePath) throws IOException {
File file = new File(context.getRealPath(filePath)); File file = new File(settings.getAskQueryFile());
if (!file.exists()) { if (!file.exists()) {
return null; return null;
} }
@ -130,15 +118,21 @@ public class OntologyUpdater {
return fileContents.toString(); return fileContents.toString();
} }
private void assertSuccess() { private void assertSuccess() throws FileNotFoundException {
Model m = ontModelSelector.getApplicationMetadataModel(); try {
InputStream inStream = context.getResourceAsStream(SUCCESS_ASSERTIONS_FILE); Model m = settings.getOntModelSelector().getApplicationMetadataModel();
m.enterCriticalSection(Lock.WRITE); File successAssertionsFile =
try { new File(settings.getSuccessAssertionsFile());
m.read(inStream, SUCCESS_RDF_FORMAT); InputStream inStream = new FileInputStream(successAssertionsFile);
} finally { m.enterCriticalSection(Lock.WRITE);
m.leaveCriticalSection(); try {
} m.read(inStream, settings.getSuccessRDFFormat());
} finally {
m.leaveCriticalSection();
}
} catch (Exception e) {
// TODO: log something to the error log
}
} }
} }

View file

@ -4,6 +4,7 @@ package edu.cornell.mannlib.vitro.webapp.servlet.setup;
import java.io.IOException; import java.io.IOException;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener; import javax.servlet.ServletContextListener;
@ -12,6 +13,7 @@ import com.hp.hpl.jena.ontology.OntModel;
import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaBaseDao; import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaBaseDao;
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector; import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
import edu.cornell.mannlib.vitro.webapp.dao.jena.SimpleOntModelSelector; import edu.cornell.mannlib.vitro.webapp.dao.jena.SimpleOntModelSelector;
import edu.cornell.mannlib.vitro.webapp.ontology.update.OntologyUpdateSettings;
import edu.cornell.mannlib.vitro.webapp.ontology.update.OntologyUpdater; import edu.cornell.mannlib.vitro.webapp.ontology.update.OntologyUpdater;
/** /**
@ -22,14 +24,41 @@ import edu.cornell.mannlib.vitro.webapp.ontology.update.OntologyUpdater;
*/ */
public class UpdateKnowledgeBase implements ServletContextListener { public class UpdateKnowledgeBase implements ServletContextListener {
private final String DATA_DIR = "/WEB-INF/ontologies/update/";
private final String LOG_DIR = "logs/";
private final String REMOVED_DATA_DIR = "removedData/";
private final String ASK_QUERY_FILE = DATA_DIR + "ask.sparql";
private final String SUCCESS_ASSERTIONS_FILE = DATA_DIR + "success.n3";
private final String SUCCESS_RDF_FORMAT = "N3";
private final String DIFF_FILE = DATA_DIR + "diff.tab.txt";
private final String LOG_FILE = DATA_DIR + LOG_DIR +
"knowledgeBaseUpdate.log";
private final String ERROR_LOG_FILE = DATA_DIR + LOG_DIR +
"knowledgeBaseUpdate.error.log";
private final String REMOVED_DATA_FILE = DATA_DIR + REMOVED_DATA_DIR +
"removedData.rdf";
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
ServletContext ctx = sce.getServletContext();
OntModelSelector oms = new SimpleOntModelSelector( OntModelSelector oms = new SimpleOntModelSelector(
(OntModel) sce.getServletContext().getAttribute( (OntModel) sce.getServletContext().getAttribute(
JenaBaseDao.ASSERTIONS_ONT_MODEL_ATTRIBUTE_NAME)); JenaBaseDao.ASSERTIONS_ONT_MODEL_ATTRIBUTE_NAME));
OntologyUpdateSettings settings = new OntologyUpdateSettings();
settings.setAskQueryFile(ctx.getRealPath(ASK_QUERY_FILE));
settings.setDataDir(ctx.getRealPath(DATA_DIR));
settings.setDiffFile(ctx.getRealPath(DIFF_FILE));
settings.setSuccessAssertionsFile(
ctx.getRealPath(SUCCESS_ASSERTIONS_FILE));
settings.setSuccessRDFFormat(ctx.getRealPath(SUCCESS_RDF_FORMAT));
settings.setLogFile(ctx.getRealPath(LOG_FILE));
settings.setErrorLogFile(ctx.getRealPath(ERROR_LOG_FILE));
settings.setRemovedDataFile(ctx.getRealPath(REMOVED_DATA_FILE));
try { try {
(new OntologyUpdater(sce.getServletContext(), oms)).update(); (new OntologyUpdater(settings)).update();
} catch (IOException ioe) { } catch (IOException ioe) {
throw new RuntimeException("IOException updating knowledge base " + throw new RuntimeException("IOException updating knowledge base " +
"for ontology changes", ioe); "for ontology changes", ioe);