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;
|
break;
|
||||||
case DELETE:
|
case DELETE:
|
||||||
if ("Delete".equals(change.getNotes())) {
|
if ("Delete".equals(change.getNotes())) {
|
||||||
deleteClass(change);
|
deleteIndividualsOfType(change);
|
||||||
} else {
|
} else {
|
||||||
renameClassToParent(change);
|
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
|
* @param change - an AtomicOntologyChange object representing a class
|
||||||
* delete operation.
|
* 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());
|
//logger.log("Processing a class deletion of class " + change.getSourceURI());
|
||||||
|
|
||||||
|
@ -348,17 +348,32 @@ public class ABoxUpdater {
|
||||||
aboxModel.enterCriticalSection(Lock.WRITE);
|
aboxModel.enterCriticalSection(Lock.WRITE);
|
||||||
try {
|
try {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
|
int refCount = 0;
|
||||||
StmtIterator iter = aboxModel.listStatements((Resource) null, RDF.type, deletedClass);
|
StmtIterator iter = aboxModel.listStatements((Resource) null, RDF.type, deletedClass);
|
||||||
|
|
||||||
while (iter.hasNext()) {
|
while (iter.hasNext()) {
|
||||||
count++;
|
count++;
|
||||||
Statement typeStmt = iter.next();
|
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
|
//log summary of changes
|
||||||
if (count > 0) {
|
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);
|
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;
|
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.
|
* new ontology version.
|
||||||
*
|
|
||||||
* @author bjl23
|
|
||||||
*
|
|
||||||
*/
|
*/
|
||||||
public class KnowledgeBaseUpdater {
|
public class KnowledgeBaseUpdater {
|
||||||
|
|
||||||
|
@ -90,9 +87,9 @@ public class KnowledgeBaseUpdater {
|
||||||
private void performUpdate() throws IOException {
|
private void performUpdate() throws IOException {
|
||||||
|
|
||||||
log.info("\tperforming SPARQL construct additions (abox)");
|
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)");
|
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();
|
List<AtomicOntologyChange> rawChanges = getAtomicOntologyChanges();
|
||||||
|
|
||||||
|
@ -107,51 +104,50 @@ public class KnowledgeBaseUpdater {
|
||||||
updateABox(changes);
|
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) {
|
if (anonModel == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
aboxModel.enterCriticalSection(Lock.WRITE);
|
writeModel.enterCriticalSection(Lock.WRITE);
|
||||||
try {
|
try {
|
||||||
JenaIngestUtils jiu = new JenaIngestUtils();
|
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();
|
Model actualAdditions = ModelFactory.createDefaultModel();
|
||||||
StmtIterator stmtIt = additions.listStatements();
|
StmtIterator stmtIt = additions.listStatements();
|
||||||
|
|
||||||
while (stmtIt.hasNext()) {
|
while (stmtIt.hasNext()) {
|
||||||
Statement stmt = stmtIt.nextStatement();
|
Statement stmt = stmtIt.nextStatement();
|
||||||
if (!aboxModel.contains(stmt)) {
|
if (!writeModel.contains(stmt)) {
|
||||||
actualAdditions.add(stmt);
|
actualAdditions.add(stmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
aboxModel.add(actualAdditions);
|
writeModel.add(actualAdditions);
|
||||||
record.recordAdditions(actualAdditions);
|
record.recordAdditions(actualAdditions);
|
||||||
} finally {
|
} finally {
|
||||||
aboxModel.leaveCriticalSection();
|
writeModel.leaveCriticalSection();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void performSparqlConstructRetractions(String sparqlConstructDir, OntModel model) throws IOException {
|
private void performSparqlConstructRetractions(String sparqlConstructDir, OntModel readModel, OntModel writeModel) throws IOException {
|
||||||
|
|
||||||
Model retractions = performSparqlConstructs(sparqlConstructDir, model, false);
|
Model retractions = performSparqlConstructs(sparqlConstructDir, readModel, false);
|
||||||
|
|
||||||
if (retractions == null) {
|
if (retractions == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
model.enterCriticalSection(Lock.WRITE);
|
writeModel.enterCriticalSection(Lock.WRITE);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
model.remove(retractions);
|
writeModel.remove(retractions);
|
||||||
record.recordRetractions(retractions);
|
record.recordRetractions(retractions);
|
||||||
} finally {
|
} finally {
|
||||||
model.leaveCriticalSection();
|
writeModel.leaveCriticalSection();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -165,7 +161,8 @@ public class KnowledgeBaseUpdater {
|
||||||
* @param aboxModel
|
* @param aboxModel
|
||||||
*/
|
*/
|
||||||
private Model performSparqlConstructs(String sparqlConstructDir,
|
private Model performSparqlConstructs(String sparqlConstructDir,
|
||||||
OntModel aboxModel, boolean add) throws IOException {
|
OntModel readModel,
|
||||||
|
boolean add) throws IOException {
|
||||||
|
|
||||||
Model anonModel = ModelFactory.createDefaultModel();
|
Model anonModel = ModelFactory.createDefaultModel();
|
||||||
File sparqlConstructDirectory = new File(sparqlConstructDir);
|
File sparqlConstructDirectory = new File(sparqlConstructDir);
|
||||||
|
@ -182,32 +179,33 @@ public class KnowledgeBaseUpdater {
|
||||||
for (int i = 0; i < sparqlFiles.length; i ++) {
|
for (int i = 0; i < sparqlFiles.length; i ++) {
|
||||||
File sparqlFile = sparqlFiles[i];
|
File sparqlFile = sparqlFiles[i];
|
||||||
try {
|
try {
|
||||||
BufferedReader reader =
|
BufferedReader reader = new BufferedReader(new FileReader(sparqlFile));
|
||||||
new BufferedReader(new FileReader(sparqlFile));
|
|
||||||
StringBuffer fileContents = new StringBuffer();
|
StringBuffer fileContents = new StringBuffer();
|
||||||
String ln;
|
String ln;
|
||||||
|
|
||||||
while ( (ln = reader.readLine()) != null) {
|
while ( (ln = reader.readLine()) != null) {
|
||||||
fileContents.append(ln).append('\n');
|
fileContents.append(ln).append('\n');
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
log.debug("\t\tprocessing SPARQL construct query from file " + sparqlFiles[i].getName());
|
log.debug("\t\tprocessing SPARQL construct query from file " + sparqlFiles[i].getName());
|
||||||
Query q = QueryFactory.create(fileContents.toString(), Syntax.syntaxARQ);
|
Query q = QueryFactory.create(fileContents.toString(), Syntax.syntaxARQ);
|
||||||
aboxModel.enterCriticalSection(Lock.WRITE);
|
readModel.enterCriticalSection(Lock.READ);
|
||||||
try {
|
try {
|
||||||
QueryExecution qe = QueryExecutionFactory.create(q, aboxModel);
|
QueryExecution qe = QueryExecutionFactory.create(q, readModel);
|
||||||
long numBefore = anonModel.size();
|
long numBefore = anonModel.size();
|
||||||
qe.execConstruct(anonModel);
|
qe.execConstruct(anonModel);
|
||||||
long numAfter = anonModel.size();
|
long numAfter = anonModel.size();
|
||||||
long num = numAfter - numBefore;
|
long num = numAfter - numBefore;
|
||||||
|
|
||||||
if (num > 0) {
|
if (num > 0) {
|
||||||
logger.log((add ? "Added " : "Removed ") + num + (add ? "" : " inferred") +
|
logger.log((add ? "Added " : "Removed ") + num +
|
||||||
" statement" + ((num > 1) ? "s" : "") +
|
" statement" + ((num > 1) ? "s" : "") +
|
||||||
" using the SPARQL construct query from file " + sparqlFiles[i].getName());
|
" using the SPARQL construct query from file " + sparqlFiles[i].getName());
|
||||||
}
|
}
|
||||||
qe.close();
|
qe.close();
|
||||||
} finally {
|
} finally {
|
||||||
aboxModel.leaveCriticalSection();
|
readModel.leaveCriticalSection();
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
logger.logError(this.getClass().getName() +
|
logger.logError(this.getClass().getName() +
|
||||||
|
|
|
@ -23,6 +23,7 @@ public class UpdateSettings {
|
||||||
private String defaultNamespace;
|
private String defaultNamespace;
|
||||||
private OntModelSelector assertionOntModelSelector;
|
private OntModelSelector assertionOntModelSelector;
|
||||||
private OntModelSelector inferenceOntModelSelector;
|
private OntModelSelector inferenceOntModelSelector;
|
||||||
|
private OntModelSelector unionOntModelSelector;
|
||||||
private OntModel oldTBoxModel;
|
private OntModel oldTBoxModel;
|
||||||
private OntModel newTBoxModel;
|
private OntModel newTBoxModel;
|
||||||
private OntModel oldTBoxAnnotationsModel;
|
private OntModel oldTBoxAnnotationsModel;
|
||||||
|
@ -82,6 +83,9 @@ public class UpdateSettings {
|
||||||
public OntModelSelector getInferenceOntModelSelector() {
|
public OntModelSelector getInferenceOntModelSelector() {
|
||||||
return inferenceOntModelSelector;
|
return inferenceOntModelSelector;
|
||||||
}
|
}
|
||||||
|
public OntModelSelector getUnionOntModelSelector() {
|
||||||
|
return unionOntModelSelector;
|
||||||
|
}
|
||||||
public String getLogFile() {
|
public String getLogFile() {
|
||||||
return logFile;
|
return logFile;
|
||||||
}
|
}
|
||||||
|
@ -118,6 +122,9 @@ public class UpdateSettings {
|
||||||
public void setInferenceOntModelSelector(OntModelSelector ontModelSelector) {
|
public void setInferenceOntModelSelector(OntModelSelector ontModelSelector) {
|
||||||
this.inferenceOntModelSelector = ontModelSelector;
|
this.inferenceOntModelSelector = ontModelSelector;
|
||||||
}
|
}
|
||||||
|
public void setUnionOntModelSelector(OntModelSelector ontModelSelector) {
|
||||||
|
this.unionOntModelSelector = ontModelSelector;
|
||||||
|
}
|
||||||
public OntModel getOldTBoxModel() {
|
public OntModel getOldTBoxModel() {
|
||||||
return oldTBoxModel;
|
return oldTBoxModel;
|
||||||
}
|
}
|
||||||
|
|
|
@ -90,6 +90,7 @@ public class UpdateKnowledgeBase implements ServletContextListener {
|
||||||
settings.setDefaultNamespace(wadf.getDefaultNamespace());
|
settings.setDefaultNamespace(wadf.getDefaultNamespace());
|
||||||
settings.setAssertionOntModelSelector(ModelContext.getBaseOntModelSelector(ctx));
|
settings.setAssertionOntModelSelector(ModelContext.getBaseOntModelSelector(ctx));
|
||||||
settings.setInferenceOntModelSelector(ModelContext.getInferenceOntModelSelector(ctx));
|
settings.setInferenceOntModelSelector(ModelContext.getInferenceOntModelSelector(ctx));
|
||||||
|
settings.setUnionOntModelSelector(ModelContext.getUnionOntModelSelector(ctx));
|
||||||
|
|
||||||
try {
|
try {
|
||||||
OntModel oldTBoxModel = loadModelFromDirectory(ctx.getRealPath(OLD_TBOX_MODEL_DIR));
|
OntModel oldTBoxModel = loadModelFromDirectory(ctx.getRealPath(OLD_TBOX_MODEL_DIR));
|
||||||
|
|
Loading…
Add table
Reference in a new issue