NIHVIVO-2426 knowledge base migration
This commit is contained in:
parent
32e69d257d
commit
19f69c62ba
6 changed files with 49 additions and 103 deletions
|
@ -90,8 +90,8 @@ public class KnowledgeBaseUpdater {
|
|||
|
||||
private void performUpdate() throws IOException {
|
||||
|
||||
performSparqlConstructAdditions(settings.getSparqlConstructAdditionsDir(), settings.getOntModelSelector().getABoxModel());
|
||||
performSparqlConstructRetractions(settings.getSparqlConstructDeletionsDir(), settings.getOntModelSelector().getABoxModel());
|
||||
performSparqlConstructAdditions(settings.getSparqlConstructAdditionsDir(), settings.getAssertionOntModelSelector().getABoxModel());
|
||||
performSparqlConstructRetractions(settings.getSparqlConstructDeletionsDir(), settings.getInferenceOntModelSelector().getABoxModel());
|
||||
|
||||
List<AtomicOntologyChange> rawChanges = getAtomicOntologyChanges();
|
||||
|
||||
|
@ -127,49 +127,27 @@ public class KnowledgeBaseUpdater {
|
|||
|
||||
aboxModel.add(actualAdditions);
|
||||
record.recordAdditions(actualAdditions);
|
||||
/*
|
||||
if (actualAdditions.size() > 0) {
|
||||
logger.log("Constructed " + actualAdditions.size() + " new " +
|
||||
"statement"
|
||||
+ ((actualAdditions.size() > 1) ? "s" : "") +
|
||||
" using SPARQL construct queries.");
|
||||
}
|
||||
*/
|
||||
|
||||
} finally {
|
||||
aboxModel.leaveCriticalSection();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
private void performSparqlConstructRetractions(String sparqlConstructDir, OntModel aboxModel) throws IOException {
|
||||
private void performSparqlConstructRetractions(String sparqlConstructDir, OntModel model) throws IOException {
|
||||
|
||||
Model retractions = performSparqlConstructs(sparqlConstructDir, aboxModel, false);
|
||||
Model retractions = performSparqlConstructs(sparqlConstructDir, model, false);
|
||||
|
||||
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);
|
||||
record.recordRetractions(actualRetractions);
|
||||
/*
|
||||
if (actualRetractions.size() > 0) {
|
||||
logger.log("Removed " + actualRetractions.size() + " statement" + ((actualRetractions.size() > 1) ? "s" : "") + " using SPARQL CONSTRUCT queries.");
|
||||
}
|
||||
*/
|
||||
model.enterCriticalSection(Lock.WRITE);
|
||||
|
||||
try {
|
||||
model.remove(retractions);
|
||||
record.recordRetractions(retractions);
|
||||
} finally {
|
||||
aboxModel.leaveCriticalSection();
|
||||
model.leaveCriticalSection();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -218,7 +196,7 @@ public class KnowledgeBaseUpdater {
|
|||
long num = numAfter - numBefore;
|
||||
|
||||
if (num > 0) {
|
||||
logger.log((add ? "Added " : "Removed ") + num +
|
||||
logger.log((add ? "Added " : "Removed ") + num + (add ? "" : " inferred") +
|
||||
" statement" + ((num > 1) ? "s" : "") +
|
||||
" using the SPARQL construct query from file " + sparqlFiles[i].getName());
|
||||
}
|
||||
|
@ -254,7 +232,7 @@ public class KnowledgeBaseUpdater {
|
|||
|
||||
OntModel oldTBoxModel = settings.getOldTBoxModel();
|
||||
OntModel newTBoxModel = settings.getNewTBoxModel();
|
||||
OntModel ABoxModel = settings.getOntModelSelector().getABoxModel();
|
||||
OntModel ABoxModel = settings.getAssertionOntModelSelector().getABoxModel();
|
||||
ABoxUpdater aboxUpdater = new ABoxUpdater(
|
||||
oldTBoxModel, newTBoxModel, ABoxModel,
|
||||
settings.getNewTBoxAnnotationsModel(), logger, record);
|
||||
|
@ -267,10 +245,10 @@ public class KnowledgeBaseUpdater {
|
|||
|
||||
TBoxUpdater tboxUpdater = new TBoxUpdater(settings.getOldTBoxAnnotationsModel(),
|
||||
settings.getNewTBoxAnnotationsModel(),
|
||||
settings.getOntModelSelector().getABoxModel(), logger, record);
|
||||
settings.getAssertionOntModelSelector().getTBoxModel(), logger, record);
|
||||
|
||||
tboxUpdater.updateDefaultAnnotationValues();
|
||||
tboxUpdater.updateAnnotationModel();
|
||||
//tboxUpdater.updateAnnotationModel();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -286,9 +264,9 @@ public class KnowledgeBaseUpdater {
|
|||
return required;
|
||||
}
|
||||
|
||||
Model m = settings.getOntModelSelector().getApplicationMetadataModel();
|
||||
Model abox = settings.getAssertionOntModelSelector().getABoxModel();
|
||||
Query query = QueryFactory.create(sparqlQueryStr);
|
||||
QueryExecution isUpdated = QueryExecutionFactory.create(query, m);
|
||||
QueryExecution isUpdated = QueryExecutionFactory.create(query, abox);
|
||||
|
||||
// if the ASK query DOES have a solution (i.e. the assertions exist
|
||||
// showing that the update has already been performed), then the update
|
||||
|
@ -298,10 +276,11 @@ public class KnowledgeBaseUpdater {
|
|||
required = false;
|
||||
} else {
|
||||
required = true;
|
||||
Model tbox = settings.getAssertionOntModelSelector().getTBoxModel();
|
||||
String sparqlQueryStr2 = loadSparqlQuery(settings.getAskEmptyQueryFile());
|
||||
if (sparqlQueryStr2 != null) {
|
||||
Query query2 = QueryFactory.create(sparqlQueryStr2);
|
||||
QueryExecution isNotEmpty = QueryExecutionFactory.create(query2, m);
|
||||
QueryExecution isNotEmpty = QueryExecutionFactory.create(query2, tbox);
|
||||
required = isNotEmpty.execAsk();
|
||||
}
|
||||
}
|
||||
|
@ -332,7 +311,8 @@ public class KnowledgeBaseUpdater {
|
|||
private void assertSuccess() throws FileNotFoundException, IOException {
|
||||
try {
|
||||
|
||||
Model m = settings.getOntModelSelector().getApplicationMetadataModel();
|
||||
//Model m = settings.getAssertionOntModelSelector().getApplicationMetadataModel();
|
||||
Model m = settings.getAssertionOntModelSelector().getABoxModel();
|
||||
File successAssertionsFile = new File(settings.getSuccessAssertionsFile());
|
||||
InputStream inStream = new FileInputStream(successAssertionsFile);
|
||||
m.enterCriticalSection(Lock.WRITE);
|
||||
|
|
|
@ -206,10 +206,12 @@ public class TBoxUpdater {
|
|||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
|
@ -23,7 +23,8 @@ public class UpdateSettings {
|
|||
private String addedDataFile;
|
||||
private String removedDataFile;
|
||||
private String defaultNamespace;
|
||||
private OntModelSelector ontModelSelector;
|
||||
private OntModelSelector assertionOntModelSelector;
|
||||
private OntModelSelector inferenceOntModelSelector;
|
||||
private OntModel oldTBoxModel;
|
||||
private OntModel newTBoxModel;
|
||||
private OntModel oldTBoxAnnotationsModel;
|
||||
|
@ -89,8 +90,11 @@ public class UpdateSettings {
|
|||
public void setDiffFile(String diffFile) {
|
||||
this.diffFile = diffFile;
|
||||
}
|
||||
public OntModelSelector getOntModelSelector() {
|
||||
return ontModelSelector;
|
||||
public OntModelSelector getAssertionOntModelSelector() {
|
||||
return assertionOntModelSelector;
|
||||
}
|
||||
public OntModelSelector getInferenceOntModelSelector() {
|
||||
return inferenceOntModelSelector;
|
||||
}
|
||||
public String getLogFile() {
|
||||
return logFile;
|
||||
|
@ -122,8 +126,11 @@ public class UpdateSettings {
|
|||
public void setDefaultNamespace(String defaultNamespace) {
|
||||
this.defaultNamespace = defaultNamespace;
|
||||
}
|
||||
public void setOntModelSelector(OntModelSelector ontModelSelector) {
|
||||
this.ontModelSelector = ontModelSelector;
|
||||
public void setAssertionOntModelSelector(OntModelSelector ontModelSelector) {
|
||||
this.assertionOntModelSelector = ontModelSelector;
|
||||
}
|
||||
public void setInferenceOntModelSelector(OntModelSelector ontModelSelector) {
|
||||
this.inferenceOntModelSelector = ontModelSelector;
|
||||
}
|
||||
public OntModel getOldTBoxModel() {
|
||||
return oldTBoxModel;
|
||||
|
|
|
@ -12,6 +12,7 @@ import java.util.Set;
|
|||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import javax.servlet.ServletContext;
|
||||
import javax.servlet.ServletContextEvent;
|
||||
import javax.servlet.ServletContextListener;
|
||||
|
||||
|
@ -83,21 +84,15 @@ public class FileGraphSetup implements ServletContextListener {
|
|||
t.printStackTrace();
|
||||
}
|
||||
|
||||
if (aboxChanged || tboxChanged) {
|
||||
if ( !JenaDataSourceSetup.updateRequired(sce.getServletContext(), baseOms.getTBoxModel())) {
|
||||
if ( (aboxChanged || tboxChanged) && !isUpdateRequired(sce.getServletContext()) ) {
|
||||
log.info("a full recompute of the Abox will be performed because" +
|
||||
" the filegraph abox(s) and/or tbox(s) have changed, or are being read for the first time." );
|
||||
" the filegraph abox(s) and/or tbox(s) have changed." );
|
||||
SimpleReasonerSetup.setRecomputeRequired(sce.getServletContext());
|
||||
} else {
|
||||
log.info("A knowledgebase update is required. A full recompute of the Abox will not be" +
|
||||
" performed; instead inferences will be updated incrementally as the knowledge base is updated.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* Reads the graphs stored as files in sub-directories of
|
||||
* FileGraphSetup.PATH_ROOT and for each graph:
|
||||
* 1. updates the SDB store to reflect the current contents of the graph.
|
||||
* 2. adds the graph as an in-memory submodel of the base in-memory graph
|
||||
*
|
||||
|
@ -255,4 +250,9 @@ public class FileGraphSetup implements ServletContextListener {
|
|||
public void contextDestroyed( ServletContextEvent sce ) {
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
private static boolean isUpdateRequired(ServletContext ctx) {
|
||||
return (ctx.getAttribute(UpdateKnowledgeBase.KBM_REQURIED_AT_STARTUP) != null);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -593,45 +593,5 @@ public class JenaDataSourceSetup extends JenaDataSourceSetupBase implements java
|
|||
public static Store getApplicationStore(ServletContext ctx) {
|
||||
return (Store) ctx.getAttribute(STORE_ATTR);
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes a SPARQL ASK query to determine whether the knowledge base
|
||||
* needs to be updated to conform to a new ontology version
|
||||
*/
|
||||
public static boolean updateRequired(ServletContext ctx, OntModel m) {
|
||||
|
||||
boolean required = false;
|
||||
|
||||
try {
|
||||
String sparqlQueryStr = KnowledgeBaseUpdater.loadSparqlQuery(UpdateKnowledgeBase.getAskQueryPath(ctx));
|
||||
if (sparqlQueryStr == null) {
|
||||
return required;
|
||||
}
|
||||
|
||||
Query query = QueryFactory.create(sparqlQueryStr);
|
||||
QueryExecution isUpdated = QueryExecutionFactory.create(query, m);
|
||||
|
||||
// if the ASK query DOES have a solution (i.e. the assertions exist
|
||||
// showing that the update has already been performed), then the update
|
||||
// is NOT required.
|
||||
|
||||
if (isUpdated.execAsk()) {
|
||||
required = false;
|
||||
} else {
|
||||
required = true;
|
||||
String sparqlQueryStr2 = KnowledgeBaseUpdater.loadSparqlQuery(UpdateKnowledgeBase.getAskEmptyQueryPath(ctx));
|
||||
if (sparqlQueryStr2 != null) {
|
||||
Query query2 = QueryFactory.create(sparqlQueryStr2);
|
||||
QueryExecution isNotEmpty = QueryExecutionFactory.create(query2, m);
|
||||
required = isNotEmpty.execAsk();
|
||||
}
|
||||
}
|
||||
} catch (IOException e) {
|
||||
log.error("error while trying to determine if a knowledgbase update is required", e);
|
||||
return false;
|
||||
}
|
||||
|
||||
return required;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -42,6 +42,7 @@ import edu.cornell.mannlib.vitro.webapp.search.IndexConstants;
|
|||
*/
|
||||
public class UpdateKnowledgeBase implements ServletContextListener {
|
||||
|
||||
public static final String KBM_REQURIED_AT_STARTUP = "KNOWLEDGE_BASE_MIGRATION_REQUIRED_AT_STARTUP";
|
||||
private final static Log log = LogFactory.getLog(UpdateKnowledgeBase.class);
|
||||
|
||||
private static final String DATA_DIR = "/WEB-INF/ontologies/update/";
|
||||
|
@ -49,7 +50,6 @@ public class UpdateKnowledgeBase implements ServletContextListener {
|
|||
private static final String CHANGED_DATA_DIR = "changedData/";
|
||||
private static final String ASK_QUERY_FILE = DATA_DIR + "ask.sparql";
|
||||
private static final String ASK_EMPTY_QUERY_FILE = DATA_DIR + "askEmpty.sparql";
|
||||
private static final String ASK_EVER_QUERY_FILE = DATA_DIR + "askEver.sparql";
|
||||
private static final String SUCCESS_ASSERTIONS_FILE = DATA_DIR + "success.n3";
|
||||
private static final String SUCCESS_RDF_FORMAT = "N3";
|
||||
private static final String DIFF_FILE = DATA_DIR + "diff.tab.txt";
|
||||
|
@ -72,14 +72,12 @@ public class UpdateKnowledgeBase implements ServletContextListener {
|
|||
try {
|
||||
|
||||
ServletContext ctx = sce.getServletContext();
|
||||
OntModelSelector assertionsOms = ModelContext.getBaseOntModelSelector(ctx);
|
||||
|
||||
String logFileName = DATA_DIR + LOG_DIR + timestampedFileName("knowledgeBaseUpdate", "log");
|
||||
String errorLogFileName = DATA_DIR + LOG_DIR + timestampedFileName("knowledgeBaseUpdate.error", "log");
|
||||
|
||||
UpdateSettings settings = new UpdateSettings();
|
||||
settings.setAskQueryFile(getAskQueryPath(ctx));
|
||||
settings.setAskEverQueryFile(getAskEverQueryPath(ctx));
|
||||
settings.setAskEmptyQueryFile(getAskEmptyQueryPath(ctx));
|
||||
settings.setDataDir(ctx.getRealPath(DATA_DIR));
|
||||
settings.setSparqlConstructAdditionsDir(ctx.getRealPath(SPARQL_CONSTRUCT_ADDITIONS_DIR));
|
||||
|
@ -93,8 +91,9 @@ public class UpdateKnowledgeBase implements ServletContextListener {
|
|||
settings.setRemovedDataFile(ctx.getRealPath(REMOVED_DATA_FILE));
|
||||
WebappDaoFactory wadf = (WebappDaoFactory) ctx.getAttribute("webappDaoFactory");
|
||||
settings.setDefaultNamespace(wadf.getDefaultNamespace());
|
||||
settings.setAssertionOntModelSelector(ModelContext.getBaseOntModelSelector(ctx));
|
||||
settings.setInferenceOntModelSelector(ModelContext.getInferenceOntModelSelector(ctx));
|
||||
|
||||
settings.setOntModelSelector(assertionsOms);
|
||||
try {
|
||||
OntModel oldTBoxModel = loadModelFromDirectory(ctx.getRealPath(OLD_TBOX_MODEL_DIR));
|
||||
settings.setOldTBoxModel(oldTBoxModel);
|
||||
|
@ -117,6 +116,7 @@ public class UpdateKnowledgeBase implements ServletContextListener {
|
|||
try {
|
||||
if (ontologyUpdater.updateRequired()) {
|
||||
ctx.setAttribute(IndexConstants.INDEX_REBUILD_REQUESTED_AT_STARTUP, Boolean.TRUE);
|
||||
ctx.setAttribute(KBM_REQURIED_AT_STARTUP, Boolean.TRUE);
|
||||
//doMiscAppMetadataReplacements(ctx.getRealPath(MISC_REPLACEMENTS_FILE), oms);
|
||||
reloadDisplayModel(ctx);
|
||||
}
|
||||
|
@ -243,17 +243,14 @@ public class UpdateKnowledgeBase implements ServletContextListener {
|
|||
public void contextDestroyed(ServletContextEvent arg0) {
|
||||
// nothing to do
|
||||
}
|
||||
|
||||
public static String getAskQueryPath(ServletContext ctx) {
|
||||
return ctx.getRealPath(ASK_QUERY_FILE);
|
||||
|
||||
}
|
||||
public static String getAskEverQueryPath(ServletContext ctx) {
|
||||
return ctx.getRealPath(ASK_EVER_QUERY_FILE);
|
||||
|
||||
}
|
||||
public static String getAskEmptyQueryPath(ServletContext ctx) {
|
||||
return ctx.getRealPath(ASK_EMPTY_QUERY_FILE);
|
||||
|
||||
}
|
||||
|
||||
private static String timestampedFileName(String prefix, String suffix) {
|
||||
|
|
Loading…
Add table
Reference in a new issue