incremental development for nihvivo-3206
This commit is contained in:
parent
b0b2fb98e1
commit
d1b745863c
4 changed files with 53 additions and 32 deletions
|
@ -95,7 +95,7 @@ public class ABoxUpdater {
|
|||
break;
|
||||
case DELETE:
|
||||
if ("Delete".equals(change.getNotes())) {
|
||||
deleteClass(change);
|
||||
deleteIndividualsOfType(change);
|
||||
} else {
|
||||
renameClassToParent(change);
|
||||
}
|
||||
|
@ -325,13 +325,13 @@ public class ABoxUpdater {
|
|||
|
||||
/**
|
||||
*
|
||||
* Remove all instances of and references to a class in the abox of the knowledge base.
|
||||
* Remove all instances of the given class from the abox of the knowledge base.
|
||||
*
|
||||
* @param change - an AtomicOntologyChange object representing a class
|
||||
* delete operation.
|
||||
*
|
||||
*/
|
||||
public void deleteClass(AtomicOntologyChange change) throws IOException {
|
||||
public void deleteIndividualsOfType(AtomicOntologyChange change) throws IOException {
|
||||
|
||||
//logger.log("Processing a class deletion of class " + change.getSourceURI());
|
||||
|
||||
|
@ -348,17 +348,32 @@ public class ABoxUpdater {
|
|||
aboxModel.enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
int count = 0;
|
||||
int refCount = 0;
|
||||
StmtIterator iter = aboxModel.listStatements((Resource) null, RDF.type, deletedClass);
|
||||
|
||||
while (iter.hasNext()) {
|
||||
count++;
|
||||
Statement typeStmt = iter.next();
|
||||
retractions.add(typeStmt);
|
||||
|
||||
StmtIterator iter2 = aboxModel.listStatements(typeStmt.getSubject(), (Property) null, (RDFNode) null);
|
||||
|
||||
while (iter2.hasNext()) {
|
||||
Statement subjstmt = iter2.next();
|
||||
retractions.add(subjstmt);
|
||||
}
|
||||
|
||||
StmtIterator iter3 = aboxModel.listStatements((Resource) null, (Property) null, typeStmt.getSubject());
|
||||
|
||||
while (iter3.hasNext()) {
|
||||
Statement objstmt = iter3.next();
|
||||
retractions.add(objstmt);
|
||||
refCount++;
|
||||
}
|
||||
}
|
||||
|
||||
//log summary of changes
|
||||
if (count > 0) {
|
||||
logger.log("Removed " + count + " " + deletedClass.getURI() + " type assertion" + ((count > 1) ? "s" : ""));
|
||||
logger.log("Removed " + count + " individual " + ((count > 1) ? "s" : "" + " of type " + deletedClass.getURI()) + " (refs = " + refCount + ")");
|
||||
}
|
||||
|
||||
aboxModel.remove(retractions);
|
||||
|
|
|
@ -32,11 +32,8 @@ import edu.cornell.mannlib.vitro.webapp.servlet.setup.JenaDataSourceSetupBase;
|
|||
import edu.cornell.mannlib.vitro.webapp.utils.jena.JenaIngestUtils;
|
||||
|
||||
/**
|
||||
* Performs knowledge base updates if necessary to align with a
|
||||
* Performs knowledge base updates necessary to align with a
|
||||
* new ontology version.
|
||||
*
|
||||
* @author bjl23
|
||||
*
|
||||
*/
|
||||
public class KnowledgeBaseUpdater {
|
||||
|
||||
|
@ -90,9 +87,9 @@ public class KnowledgeBaseUpdater {
|
|||
private void performUpdate() throws IOException {
|
||||
|
||||
log.info("\tperforming SPARQL construct additions (abox)");
|
||||
performSparqlConstructAdditions(settings.getSparqlConstructAdditionsDir(), settings.getAssertionOntModelSelector().getABoxModel());
|
||||
performSparqlConstructAdditions(settings.getSparqlConstructAdditionsDir(), settings.getUnionOntModelSelector().getABoxModel() , settings.getAssertionOntModelSelector().getABoxModel());
|
||||
log.info("\tperforming SPARQL construct deletions (inferences)");
|
||||
performSparqlConstructRetractions(settings.getSparqlConstructDeletionsDir(), settings.getInferenceOntModelSelector().getABoxModel());
|
||||
performSparqlConstructRetractions(settings.getSparqlConstructDeletionsDir(), settings.getUnionOntModelSelector().getABoxModel() , settings.getAssertionOntModelSelector().getABoxModel());
|
||||
|
||||
List<AtomicOntologyChange> rawChanges = getAtomicOntologyChanges();
|
||||
|
||||
|
@ -107,51 +104,50 @@ public class KnowledgeBaseUpdater {
|
|||
updateABox(changes);
|
||||
}
|
||||
|
||||
private void performSparqlConstructAdditions(String sparqlConstructDir, OntModel aboxModel) throws IOException {
|
||||
private void performSparqlConstructAdditions(String sparqlConstructDir, OntModel readModel, OntModel writeModel) throws IOException {
|
||||
|
||||
Model anonModel = performSparqlConstructs(sparqlConstructDir, aboxModel, true);
|
||||
Model anonModel = performSparqlConstructs(sparqlConstructDir, readModel, true);
|
||||
|
||||
if (anonModel == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
aboxModel.enterCriticalSection(Lock.WRITE);
|
||||
writeModel.enterCriticalSection(Lock.WRITE);
|
||||
try {
|
||||
JenaIngestUtils jiu = new JenaIngestUtils();
|
||||
Model additions = jiu.renameBNodes(anonModel, settings.getDefaultNamespace() + "n", aboxModel);
|
||||
Model additions = jiu.renameBNodes(anonModel, settings.getDefaultNamespace() + "n", writeModel);
|
||||
Model actualAdditions = ModelFactory.createDefaultModel();
|
||||
StmtIterator stmtIt = additions.listStatements();
|
||||
|
||||
while (stmtIt.hasNext()) {
|
||||
Statement stmt = stmtIt.nextStatement();
|
||||
if (!aboxModel.contains(stmt)) {
|
||||
if (!writeModel.contains(stmt)) {
|
||||
actualAdditions.add(stmt);
|
||||
}
|
||||
}
|
||||
|
||||
aboxModel.add(actualAdditions);
|
||||
writeModel.add(actualAdditions);
|
||||
record.recordAdditions(actualAdditions);
|
||||
} finally {
|
||||
aboxModel.leaveCriticalSection();
|
||||
writeModel.leaveCriticalSection();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
private void performSparqlConstructRetractions(String sparqlConstructDir, OntModel readModel, OntModel writeModel) throws IOException {
|
||||
|
||||
private void performSparqlConstructRetractions(String sparqlConstructDir, OntModel model) throws IOException {
|
||||
|
||||
Model retractions = performSparqlConstructs(sparqlConstructDir, model, false);
|
||||
Model retractions = performSparqlConstructs(sparqlConstructDir, readModel, false);
|
||||
|
||||
if (retractions == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
model.enterCriticalSection(Lock.WRITE);
|
||||
writeModel.enterCriticalSection(Lock.WRITE);
|
||||
|
||||
try {
|
||||
model.remove(retractions);
|
||||
writeModel.remove(retractions);
|
||||
record.recordRetractions(retractions);
|
||||
} finally {
|
||||
model.leaveCriticalSection();
|
||||
writeModel.leaveCriticalSection();
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -165,7 +161,8 @@ public class KnowledgeBaseUpdater {
|
|||
* @param aboxModel
|
||||
*/
|
||||
private Model performSparqlConstructs(String sparqlConstructDir,
|
||||
OntModel aboxModel, boolean add) throws IOException {
|
||||
OntModel readModel,
|
||||
boolean add) throws IOException {
|
||||
|
||||
Model anonModel = ModelFactory.createDefaultModel();
|
||||
File sparqlConstructDirectory = new File(sparqlConstructDir);
|
||||
|
@ -182,32 +179,33 @@ public class KnowledgeBaseUpdater {
|
|||
for (int i = 0; i < sparqlFiles.length; i ++) {
|
||||
File sparqlFile = sparqlFiles[i];
|
||||
try {
|
||||
BufferedReader reader =
|
||||
new BufferedReader(new FileReader(sparqlFile));
|
||||
BufferedReader reader = new BufferedReader(new FileReader(sparqlFile));
|
||||
StringBuffer fileContents = new StringBuffer();
|
||||
String ln;
|
||||
|
||||
while ( (ln = reader.readLine()) != null) {
|
||||
fileContents.append(ln).append('\n');
|
||||
}
|
||||
|
||||
try {
|
||||
log.debug("\t\tprocessing SPARQL construct query from file " + sparqlFiles[i].getName());
|
||||
Query q = QueryFactory.create(fileContents.toString(), Syntax.syntaxARQ);
|
||||
aboxModel.enterCriticalSection(Lock.WRITE);
|
||||
readModel.enterCriticalSection(Lock.READ);
|
||||
try {
|
||||
QueryExecution qe = QueryExecutionFactory.create(q, aboxModel);
|
||||
QueryExecution qe = QueryExecutionFactory.create(q, readModel);
|
||||
long numBefore = anonModel.size();
|
||||
qe.execConstruct(anonModel);
|
||||
long numAfter = anonModel.size();
|
||||
long num = numAfter - numBefore;
|
||||
|
||||
if (num > 0) {
|
||||
logger.log((add ? "Added " : "Removed ") + num + (add ? "" : " inferred") +
|
||||
logger.log((add ? "Added " : "Removed ") + num +
|
||||
" statement" + ((num > 1) ? "s" : "") +
|
||||
" using the SPARQL construct query from file " + sparqlFiles[i].getName());
|
||||
}
|
||||
qe.close();
|
||||
} finally {
|
||||
aboxModel.leaveCriticalSection();
|
||||
readModel.leaveCriticalSection();
|
||||
}
|
||||
} catch (Exception e) {
|
||||
logger.logError(this.getClass().getName() +
|
||||
|
|
|
@ -23,6 +23,7 @@ public class UpdateSettings {
|
|||
private String defaultNamespace;
|
||||
private OntModelSelector assertionOntModelSelector;
|
||||
private OntModelSelector inferenceOntModelSelector;
|
||||
private OntModelSelector unionOntModelSelector;
|
||||
private OntModel oldTBoxModel;
|
||||
private OntModel newTBoxModel;
|
||||
private OntModel oldTBoxAnnotationsModel;
|
||||
|
@ -82,6 +83,9 @@ public class UpdateSettings {
|
|||
public OntModelSelector getInferenceOntModelSelector() {
|
||||
return inferenceOntModelSelector;
|
||||
}
|
||||
public OntModelSelector getUnionOntModelSelector() {
|
||||
return unionOntModelSelector;
|
||||
}
|
||||
public String getLogFile() {
|
||||
return logFile;
|
||||
}
|
||||
|
@ -118,6 +122,9 @@ public class UpdateSettings {
|
|||
public void setInferenceOntModelSelector(OntModelSelector ontModelSelector) {
|
||||
this.inferenceOntModelSelector = ontModelSelector;
|
||||
}
|
||||
public void setUnionOntModelSelector(OntModelSelector ontModelSelector) {
|
||||
this.unionOntModelSelector = ontModelSelector;
|
||||
}
|
||||
public OntModel getOldTBoxModel() {
|
||||
return oldTBoxModel;
|
||||
}
|
||||
|
|
|
@ -90,6 +90,7 @@ public class UpdateKnowledgeBase implements ServletContextListener {
|
|||
settings.setDefaultNamespace(wadf.getDefaultNamespace());
|
||||
settings.setAssertionOntModelSelector(ModelContext.getBaseOntModelSelector(ctx));
|
||||
settings.setInferenceOntModelSelector(ModelContext.getInferenceOntModelSelector(ctx));
|
||||
settings.setUnionOntModelSelector(ModelContext.getUnionOntModelSelector(ctx));
|
||||
|
||||
try {
|
||||
OntModel oldTBoxModel = loadModelFromDirectory(ctx.getRealPath(OLD_TBOX_MODEL_DIR));
|
||||
|
|
Loading…
Add table
Reference in a new issue