resolves NIHVIVO-263 'Write SPARQL CONSTRUCTS...'
This commit is contained in:
parent
c28519a98e
commit
853df19c8b
3 changed files with 133 additions and 6 deletions
|
@ -1,10 +1,13 @@
|
|||
package edu.cornell.mannlib.vitro.webapp.ontology.update;
|
||||
|
||||
import com.hp.hpl.jena.ontology.OntModel;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
|
||||
|
||||
public class OntologyUpdateSettings {
|
||||
|
||||
private String dataDir;
|
||||
private String sparqlConstructsDir;
|
||||
private String askQueryFile;
|
||||
private String successAssertionsFile;
|
||||
private String successRDFFormat = "N3";
|
||||
|
@ -13,7 +16,12 @@ public class OntologyUpdateSettings {
|
|||
private String errorLogFile;
|
||||
private String addedDataFile;
|
||||
private String removedDataFile;
|
||||
private String defaultNamespace;
|
||||
private OntModelSelector ontModelSelector;
|
||||
private OntModel oldTBoxModel;
|
||||
private OntModel newTBoxModel;
|
||||
private OntModel oldTBoxAnnotationsModel;
|
||||
private OntModel newTBoxAnnotationsModel;
|
||||
|
||||
public String getDataDir() {
|
||||
return dataDir;
|
||||
|
@ -21,6 +29,12 @@ public class OntologyUpdateSettings {
|
|||
public void setDataDir(String dataDir) {
|
||||
this.dataDir = dataDir;
|
||||
}
|
||||
public String getSparqlConstructsDir() {
|
||||
return sparqlConstructsDir;
|
||||
}
|
||||
public void setSparqlConstructsDir(String sparqlConstructsDir) {
|
||||
this.sparqlConstructsDir = sparqlConstructsDir;
|
||||
}
|
||||
public String getAskQueryFile() {
|
||||
return askQueryFile;
|
||||
}
|
||||
|
@ -72,8 +86,38 @@ public class OntologyUpdateSettings {
|
|||
public void setRemovedDataFile(String removedDataFile) {
|
||||
this.removedDataFile = removedDataFile;
|
||||
}
|
||||
public String getDefaultNamespace() {
|
||||
return defaultNamespace;
|
||||
}
|
||||
public void setDefaultNamespace(String defaultNamespace) {
|
||||
this.defaultNamespace = defaultNamespace;
|
||||
}
|
||||
public void setOntModelSelector(OntModelSelector ontModelSelector) {
|
||||
this.ontModelSelector = ontModelSelector;
|
||||
}
|
||||
public OntModel getOldTBoxModel() {
|
||||
return oldTBoxModel;
|
||||
}
|
||||
public void setOldTBoxModel(OntModel oldTBoxModel) {
|
||||
this.oldTBoxModel = oldTBoxModel;
|
||||
}
|
||||
public OntModel getNewTBoxModel() {
|
||||
return newTBoxModel;
|
||||
}
|
||||
public void setNewTBoxModel(OntModel newTBoxModel) {
|
||||
this.newTBoxModel = newTBoxModel;
|
||||
}
|
||||
public OntModel getOldTBoxAnnotationsModel() {
|
||||
return oldTBoxAnnotationsModel;
|
||||
}
|
||||
public void setOldTBoxAnnotationsModel(OntModel oldTBoxAnnotationsModel) {
|
||||
this.oldTBoxAnnotationsModel = oldTBoxAnnotationsModel;
|
||||
}
|
||||
public OntModel getNewTBoxAnnotationsModel() {
|
||||
return newTBoxAnnotationsModel;
|
||||
}
|
||||
public void setNewTBoxAnnotationsModel(OntModel newTBoxAnnotationsModel) {
|
||||
this.newTBoxAnnotationsModel = newTBoxAnnotationsModel;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -11,6 +11,7 @@ import java.io.IOException;
|
|||
import java.io.InputStream;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
|
@ -22,12 +23,16 @@ 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.query.Syntax;
|
||||
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.shared.Lock;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.utils.jena.JenaIngestUtils;
|
||||
|
||||
/**
|
||||
* Performs knowledge base updates if necessary to align with a
|
||||
* new ontology version.
|
||||
|
@ -47,6 +52,9 @@ public class OntologyUpdater {
|
|||
|
||||
private OntModel OLD_TBOX_MODEL = null; // TODO change this
|
||||
|
||||
private OntModel oldTBoxAnnotationsModel;
|
||||
private OntModel newTBoxAnnotationsModel;
|
||||
|
||||
public OntologyUpdater(OntologyUpdateSettings settings) {
|
||||
this.settings = settings;
|
||||
this.logger = new SimpleOntologyChangeLogger(settings.getLogFile(),
|
||||
|
@ -66,6 +74,10 @@ public class OntologyUpdater {
|
|||
}
|
||||
|
||||
private void performUpdate() throws IOException {
|
||||
|
||||
performSparqlConstructs(settings.getSparqlConstructsDir(),
|
||||
settings.getOntModelSelector().getABoxModel());
|
||||
|
||||
List<AtomicOntologyChange> rawChanges = getAtomicOntologyChanges();
|
||||
|
||||
AtomicOntologyChangeLists changes =
|
||||
|
@ -83,6 +95,72 @@ public class OntologyUpdater {
|
|||
// perform additional additions and retractions
|
||||
}
|
||||
|
||||
/**
|
||||
* Performs a set of arbitrary SPARQL CONSTRUCT queries on the
|
||||
* data, for changes that cannot be expressed as simple property
|
||||
* or class additions, deletions, or renamings.
|
||||
* Blank nodes created by the queries are given random URIs.
|
||||
* @param sparqlConstructDir
|
||||
* @param aboxModel
|
||||
*/
|
||||
private void 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;
|
||||
}
|
||||
File[] sparqlFiles = sparqlConstructDirectory.listFiles();
|
||||
for (int i = 0; i < sparqlFiles.length; i ++) {
|
||||
File sparqlFile = sparqlFiles[i];
|
||||
try {
|
||||
BufferedReader reader =
|
||||
new BufferedReader(new FileReader(sparqlFile));
|
||||
StringBuffer fileContents = new StringBuffer();
|
||||
String ln;
|
||||
while ( (ln = reader.readLine()) != null) {
|
||||
fileContents.append(ln).append('\n');
|
||||
}
|
||||
try {
|
||||
Query q = QueryFactory.create(fileContents.toString(),
|
||||
Syntax.syntaxARQ);
|
||||
aboxModel.enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
QueryExecution qe = QueryExecutionFactory.create(q,
|
||||
aboxModel);
|
||||
qe.execConstruct(anonModel);
|
||||
} finally {
|
||||
aboxModel.leaveCriticalSection();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.logError(this.getClass().getName() +
|
||||
".performSparqlConstructs() unable to execute " +
|
||||
"query at " + sparqlFile);
|
||||
}
|
||||
} catch (FileNotFoundException fnfe) {
|
||||
logger.logError(this.getClass().getName() +
|
||||
".performSparqlConstructs() could not find " +
|
||||
" SPARQL CONSTRUCT file " + sparqlFile + ". Skipping.");
|
||||
}
|
||||
}
|
||||
|
||||
aboxModel.enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
JenaIngestUtils jiu = new JenaIngestUtils();
|
||||
jiu.renameBNodes(anonModel, settings.getDefaultNamespace() + "n",
|
||||
aboxModel);
|
||||
} finally {
|
||||
aboxModel.leaveCriticalSection();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
private List<AtomicOntologyChange> getAtomicOntologyChanges()
|
||||
throws IOException {
|
||||
return (new OntologyChangeParser()).parseFile(settings.getDiffFile());
|
||||
|
@ -103,8 +181,10 @@ public class OntologyUpdater {
|
|||
// run additional SPARQL CONSTRUCTS
|
||||
}
|
||||
|
||||
private void updateTBoxAnnotations() {
|
||||
// Stella's code is called here
|
||||
private void updateTBoxAnnotations() throws IOException {
|
||||
(new TBoxUpdater(oldTBoxAnnotationsModel, newTBoxAnnotationsModel,
|
||||
settings.getOntModelSelector().getTBoxModel(), logger, record))
|
||||
.updateVitroPropertyDefaultValues();
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -10,6 +10,7 @@ import javax.servlet.ServletContextListener;
|
|||
|
||||
import com.hp.hpl.jena.ontology.OntModel;
|
||||
|
||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||
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.SimpleOntModelSelector;
|
||||
|
@ -37,10 +38,11 @@ public class UpdateKnowledgeBase implements ServletContextListener {
|
|||
"knowledgeBaseUpdate.error.log";
|
||||
private final String REMOVED_DATA_FILE = DATA_DIR + REMOVED_DATA_DIR +
|
||||
"removedData.rdf";
|
||||
private final String SPARQL_CONSTRUCTS_DIR = DATA_DIR + "sparqlConstructs/";
|
||||
|
||||
public void contextInitialized(ServletContextEvent sce) {
|
||||
|
||||
/*
|
||||
|
||||
|
||||
ServletContext ctx = sce.getServletContext();
|
||||
|
||||
|
@ -51,6 +53,7 @@ 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.setDiffFile(ctx.getRealPath(DIFF_FILE));
|
||||
settings.setSuccessAssertionsFile(
|
||||
ctx.getRealPath(SUCCESS_ASSERTIONS_FILE));
|
||||
|
@ -58,6 +61,8 @@ public class UpdateKnowledgeBase implements ServletContextListener {
|
|||
settings.setLogFile(ctx.getRealPath(LOG_FILE));
|
||||
settings.setErrorLogFile(ctx.getRealPath(ERROR_LOG_FILE));
|
||||
settings.setRemovedDataFile(ctx.getRealPath(REMOVED_DATA_FILE));
|
||||
WebappDaoFactory wadf = (WebappDaoFactory) ctx.getAttribute("webappDaoFactory");
|
||||
settings.setDefaultNamespace(wadf.getDefaultNamespace());
|
||||
|
||||
try {
|
||||
(new OntologyUpdater(settings)).update();
|
||||
|
@ -71,8 +76,6 @@ public class UpdateKnowledgeBase implements ServletContextListener {
|
|||
throw new RuntimeException(errMsg, ioe);
|
||||
}
|
||||
|
||||
*/
|
||||
|
||||
}
|
||||
|
||||
public void contextDestroyed(ServletContextEvent arg0) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue