improvements and bugfixes for NIHVIVO-205 ontology upgrade

This commit is contained in:
bjl23 2010-04-02 16:10:14 +00:00
parent 36ab5f8357
commit 819f05039d
7 changed files with 222 additions and 158 deletions

View file

@ -3,11 +3,13 @@
package edu.cornell.mannlib.vitro.webapp.ontology.update; package edu.cornell.mannlib.vitro.webapp.ontology.update;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import com.hp.hpl.jena.ontology.OntClass; import com.hp.hpl.jena.ontology.OntClass;
import com.hp.hpl.jena.ontology.OntModel; import com.hp.hpl.jena.ontology.OntModel;
import com.hp.hpl.jena.ontology.OntModelSpec;
import com.hp.hpl.jena.ontology.OntProperty; import com.hp.hpl.jena.ontology.OntProperty;
import com.hp.hpl.jena.rdf.model.Literal; import com.hp.hpl.jena.rdf.model.Literal;
import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.Model;
@ -36,6 +38,9 @@ public class ABoxUpdater {
private OntModel aboxModel; private OntModel aboxModel;
private OntologyChangeLogger logger; private OntologyChangeLogger logger;
private OntologyChangeRecord record; private OntologyChangeRecord record;
private OntClass OWL_THING = (ModelFactory
.createOntologyModel(OntModelSpec.OWL_MEM))
.createClass(OWL.Thing.getURI());
/** /**
* *
@ -127,10 +132,9 @@ public class ABoxUpdater {
// Change class references in the subjects of statements // Change class references in the subjects of statements
StmtIterator iter = aboxModel.listStatements(oldClass, (Property) null, (RDFNode) null); StmtIterator iter = aboxModel.listStatements(oldClass, (Property) null, (RDFNode) null);
//log summary of changes int count = 0;
logger.log("Changing " + iter.toList().size() + " subject referernces to the " + oldClass.getURI() + " class to be " + newClass.getURI()); while (iter.hasNext()) {
count++;
while (iter.hasNext()) {
Statement oldStatement = iter.next(); Statement oldStatement = iter.next();
Statement newStatement = ResourceFactory.createStatement(newClass, oldStatement.getPredicate(), oldStatement.getObject()); Statement newStatement = ResourceFactory.createStatement(newClass, oldStatement.getPredicate(), oldStatement.getObject());
retractions.add(oldStatement); retractions.add(oldStatement);
@ -138,14 +142,18 @@ public class ABoxUpdater {
logChange(oldStatement, false); logChange(oldStatement, false);
logChange(newStatement,true); logChange(newStatement,true);
} }
//log summary of changes
if (count > 0) {
logger.log("Changing " + count + " subject referernces to the " + oldClass.getURI() + " class to be " + newClass.getURI());
}
// Change class references in the objects of statements // Change class references in the objects of statements
iter = aboxModel.listStatements((Resource) null, (Property) null, oldClass); iter = aboxModel.listStatements((Resource) null, (Property) null, oldClass);
//log summary of changes count = 0;
logger.log("Changing " + iter.toList().size() + " object referernces to the " + oldClass.getURI() + " class to be " + newClass.getURI()); while (iter.hasNext()) {
count++;
while (iter.hasNext()) {
Statement oldStatement = iter.next(); Statement oldStatement = iter.next();
Statement newStatement = ResourceFactory.createStatement(oldStatement.getSubject(), oldStatement.getPredicate(), newClass); Statement newStatement = ResourceFactory.createStatement(oldStatement.getSubject(), oldStatement.getPredicate(), newClass);
retractions.add(oldStatement); retractions.add(oldStatement);
@ -155,6 +163,11 @@ public class ABoxUpdater {
logChanges(oldStatement, newStatement); logChanges(oldStatement, newStatement);
} }
//log summary of changes
if (count > 0) {
logger.log("Changing " + count + " object referernces to the " + oldClass.getURI() + " class to be " + newClass.getURI());
}
aboxModel.remove(retractions); aboxModel.remove(retractions);
record.recordRetractions(retractions); record.recordRetractions(retractions);
aboxModel.add(additions); aboxModel.add(additions);
@ -191,7 +204,18 @@ public class ABoxUpdater {
return; return;
} }
ExtendedIterator<OntClass> classIter = addedClass.listSuperClasses(true); List<OntClass> classList = addedClass.listSuperClasses(true).toList();
List<OntClass> namedClassList = new ArrayList<OntClass>();
for (OntClass ontClass : classList) {
if (!ontClass.isAnon()) {
namedClassList.add(ontClass);
}
}
if (namedClassList.isEmpty()) {
namedClassList.add(OWL_THING);
}
Iterator<OntClass> classIter = namedClassList.iterator();
while (classIter.hasNext()) { while (classIter.hasNext()) {
OntClass parentOfAddedClass = classIter.next(); OntClass parentOfAddedClass = classIter.next();
@ -209,11 +233,13 @@ public class ABoxUpdater {
indList += "\n\t" + stmt.getSubject().getURI(); indList += "\n\t" + stmt.getSubject().getURI();
} }
//TODO - take out the detailed logging after our internal testing is completed. if (count > 0) {
logger.log("There are " + count + " individuals in the model that are of type " + parentOfAddedClass.getURI() + "," + //TODO - take out the detailed logging after our internal testing is completed.
" and a new subclass of that class has been added: " + addedClass.getURI() + ". " + logger.log("There are " + count + " individuals in the model that are of type " + parentOfAddedClass.getURI() + "," +
"Please review the following individuals to see whether they should be of type: " + addedClass.getURI() + ":" + " and a new subclass of that class has been added: " + addedClass.getURI() + ". " +
indList ); "Please review the following individuals to see whether they should be of type: " + addedClass.getURI() + ":" +
indList );
}
} }
} }
} }
@ -246,7 +272,17 @@ public class ABoxUpdater {
return; return;
} }
OntClass parent = deletedClass.getSuperClass(); List<OntClass> classList = deletedClass.listSuperClasses(true).toList();
List<OntClass> namedClassList = new ArrayList<OntClass>();
for (OntClass ontClass : classList) {
if (!ontClass.isAnon()) {
namedClassList.add(ontClass);
}
}
OntClass parent = (namedClassList.isEmpty())
? namedClassList.get(0)
: OWL_THING;
OntClass replacementClass = newTboxModel.getOntClass(parent.getURI()); OntClass replacementClass = newTboxModel.getOntClass(parent.getURI());
while (replacementClass == null) { while (replacementClass == null) {
@ -255,7 +291,7 @@ public class ABoxUpdater {
} }
//log summary of changes //log summary of changes
logger.log("CLass " + deletedClass.getURI() + " has been deleted. Any references to it in the knowledge base will be changed to " + logger.log("Class " + deletedClass.getURI() + " has been deleted. Any references to it in the knowledge base will be changed to " +
replacementClass.getURI()); replacementClass.getURI());
AtomicOntologyChange chg = new AtomicOntologyChange(deletedClass.getURI(), replacementClass.getURI(), AtomicChangeType.RENAME); AtomicOntologyChange chg = new AtomicOntologyChange(deletedClass.getURI(), replacementClass.getURI(), AtomicChangeType.RENAME);
@ -281,37 +317,50 @@ public class ABoxUpdater {
private void addProperty(AtomicOntologyChange propObj) throws IOException{ private void addProperty(AtomicOntologyChange propObj) throws IOException{
OntProperty tempProperty = newTboxModel.getOntProperty OntProperty tempProperty = newTboxModel.getOntProperty
(propObj.getDestinationURI()).getSuperProperty(); (propObj.getDestinationURI());
if (tempProperty == null) { if (tempProperty == null) {
logger.logError("Unable to find property " +
propObj.getDestinationURI() +
" in newTBoxModel");
return;
}
OntProperty superProperty = tempProperty.getSuperProperty();
if (superProperty == null) {
return; return;
} }
int count = aboxModel.listStatements( int count = aboxModel.listStatements(
(Resource) null, tempProperty, (RDFNode) null).toSet().size(); (Resource) null, superProperty, (RDFNode) null).toSet().size();
logger.log("The Property " + tempProperty.getURI() + if (count > 0) {
"which occurs " + count + "times in database has " + logger.log("The Property " + superProperty.getURI() +
"a new subProperty " + propObj.getDestinationURI() + " which occurs " + count + " times in database has " +
"added to Core 1.0"); "a new subProperty " + propObj.getDestinationURI() +
logger.log("Please review accordingly"); " in the new ontology version. ");
logger.log("Please review accordingly.");
}
} }
private void deleteProperty(AtomicOntologyChange propObj) throws IOException{ private void deleteProperty(AtomicOntologyChange propObj) throws IOException{
OntProperty deletedProperty = oldTboxModel.getOntProperty(propObj.getSourceURI()); OntProperty deletedProperty = oldTboxModel.getOntProperty(propObj.getSourceURI());
if (deletedProperty == null) { if (deletedProperty == null) {
// TODO - log logger.logError("expected to find property "
+ propObj.getSourceURI() + " in oldTBoxModel");
return; return;
} }
OntProperty replacementProperty = null;
OntProperty parent = deletedProperty.getSuperProperty(); OntProperty parent = deletedProperty.getSuperProperty();
OntProperty replacementProperty = newTboxModel.getOntProperty(parent.getURI()); if (parent != null) {
replacementProperty = newTboxModel.getOntProperty(parent.getURI());
while (replacementProperty == null) {
parent = parent.getSuperProperty(); while (replacementProperty == null) {
if (parent == null) { parent = parent.getSuperProperty();
break; if (parent == null) {
} break;
replacementProperty = newTboxModel.getOntProperty(parent.getURI()); }
} replacementProperty = newTboxModel.getOntProperty(parent.getURI());
}
}
Model deletePropModel = ModelFactory.createDefaultModel(); Model deletePropModel = ModelFactory.createDefaultModel();
@ -325,9 +374,11 @@ public class ABoxUpdater {
aboxModel.leaveCriticalSection(); aboxModel.leaveCriticalSection();
} }
record.recordRetractions(deletePropModel); record.recordRetractions(deletePropModel);
logger.log(deletePropModel.size() + " statements using " + if (deletePropModel.size() > 0) {
propObj.getSourceURI() + " were removed. " + logger.log(deletePropModel.size() + " statements using " +
" Please refer to the removed data model"); propObj.getSourceURI() + " were removed. " +
" Please refer to the removed data model");
}
} else { } else {
AtomicOntologyChange chg = new AtomicOntologyChange(deletedProperty.getURI(), replacementProperty.getURI(), AtomicChangeType.RENAME); AtomicOntologyChange chg = new AtomicOntologyChange(deletedProperty.getURI(), replacementProperty.getURI(), AtomicChangeType.RENAME);
renameProperty(chg); renameProperty(chg);
@ -341,7 +392,8 @@ public class ABoxUpdater {
OntProperty newProperty = newTboxModel.getOntProperty(propObj.getDestinationURI()); OntProperty newProperty = newTboxModel.getOntProperty(propObj.getDestinationURI());
if (oldProperty == null || newProperty == null) { if (oldProperty == null || newProperty == null) {
// TODO - log logger.logError(" expects non-null old property and new property "
+ "URIs");
return; return;
} }
@ -369,10 +421,12 @@ public class ABoxUpdater {
record.recordAdditions(renamePropAddModel); record.recordAdditions(renamePropAddModel);
record.recordRetractions(renamePropRetractModel); record.recordRetractions(renamePropRetractModel);
logger.log(renamePropRetractModel.size() + " statments using " + if (renamePropRetractModel.size() > 0) {
"property " + propObj.getSourceURI() + " were changed to use " + logger.log(renamePropRetractModel.size() + " statments using " +
propObj.getDestinationURI() + " instead. Please refer to the " + "property " + propObj.getSourceURI() + " were changed to use " +
"removed data model and the added data model."); propObj.getDestinationURI() + " instead. Please refer to the " +
"removed data model and the added data model.");
}
} }

View file

@ -24,6 +24,12 @@ import java.util.ArrayList;
public class OntologyChangeParser { public class OntologyChangeParser {
private OntologyChangeLogger logger;
public OntologyChangeParser(OntologyChangeLogger logger) {
this.logger = logger;
}
/** /**
* @param args * @param args
* @throws IOException * @throws IOException
@ -32,6 +38,8 @@ public class OntologyChangeParser {
@SuppressWarnings({ "unchecked", "null", "static-access" }) @SuppressWarnings({ "unchecked", "null", "static-access" })
public ArrayList<AtomicOntologyChange> parseFile(String diffPath) throws IOException{ public ArrayList<AtomicOntologyChange> parseFile(String diffPath) throws IOException{
logger.log("Parsing PromptDiff file at " + diffPath);
AtomicOntologyChange changeObj; AtomicOntologyChange changeObj;
ArrayList<AtomicOntologyChange> changeObjects = new ArrayList<AtomicOntologyChange>(); ArrayList<AtomicOntologyChange> changeObjects = new ArrayList<AtomicOntologyChange>();
int countColumns = 0; int countColumns = 0;
@ -42,56 +50,44 @@ public class OntologyChangeParser {
StringTokenizer stArr = null; StringTokenizer stArr = null;
FileInputStream in = new FileInputStream(new File(diffPath)); FileInputStream in = new FileInputStream(new File(diffPath));
CSVReader readFile = new SimpleReader(); CSVReader readFile = new SimpleReader();
readFile.setSeperator('\t');
List<String[]> rows = readFile.parse(in); List<String[]> rows = readFile.parse(in);
for(int rowNum = 0; rowNum < rows.size(); rowNum++){ for(int rowNum = 0; rowNum < rows.size(); rowNum++){
String[] cols = rows.get(rowNum); String[] cols = rows.get(rowNum);
if (cols.length != 5) {
for(int col =0; col < cols.length; col++){ logger.logError("Invalid PromptDiff data at row " + (rowNum + 1)
String column = cols[col].trim(); + ". Expected 5 columns; found " + cols.length );
stArr = new StringTokenizer(column," "); } else {
countColumns = stArr.countTokens();
changeObj = new AtomicOntologyChange(); changeObj = new AtomicOntologyChange();
if (cols[0] != null && cols[0].length() > 0) {
if(countColumns == 4){ changeObj.setSourceURI(cols[0]);
}
URI = stArr.nextToken(); if (cols[1] != null && cols[1].length() > 0) {
rename = stArr.nextToken(); changeObj.setDestinationURI(cols[1]);
String check = stArr.nextToken(); }
if ("Yes".equals(cols[2])) {
if(check.equalsIgnoreCase("Add")){ changeObj.setAtomicChangeType(AtomicChangeType.RENAME);
} else if ("Delete".equals(cols[3])) {
AtomicChangeType atomicChangeType = changeObj.getAtomicChangeType(); changeObj.setAtomicChangeType(AtomicChangeType.DELETE);
changeObj.setAtomicChangeType(atomicChangeType.ADD); } else if ("Add".equals(cols[3])) {
changeObj.setDestinationURI(URI); changeObj.setAtomicChangeType(AtomicChangeType.ADD);
changeObjects.add(changeObj); } else {
logger.logError("Invalid rename or change type data: '" +
} cols[2] + " " + cols[3] + "'");
else{ }
AtomicChangeType atomicChangeType = changeObj.getAtomicChangeType(); changeObjects.add(changeObj);
changeObj.setAtomicChangeType(atomicChangeType.DELETE);
changeObj.setSourceURI(URI);
changeObjects.add(changeObj);
}
}
else{
sourceURI = stArr.nextToken();
destinationURI = stArr.nextToken();
AtomicChangeType atomicChangeType = changeObj.getAtomicChangeType();
changeObj.setAtomicChangeType(atomicChangeType.RENAME);
changeObj.setSourceURI(sourceURI);
changeObj.setDestinationURI(destinationURI);
changeObjects.add(changeObj);
}
} }
} }
if (changeObjects.size() == 0) {
logger.log("did not find any changes in PromptDiff output file.");
}
return changeObjects; return changeObjects;
} }

View file

@ -9,6 +9,7 @@ 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.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Set; import java.util.Set;
@ -87,7 +88,7 @@ public class OntologyUpdater {
updateTBoxAnnotations(); updateTBoxAnnotations();
// perform additional additions and retractions logger.closeLogs();
} }
/** /**
@ -158,7 +159,8 @@ public class OntologyUpdater {
private List<AtomicOntologyChange> getAtomicOntologyChanges() private List<AtomicOntologyChange> getAtomicOntologyChanges()
throws IOException { throws IOException {
return (new OntologyChangeParser()).parseFile(settings.getDiffFile()); return (new OntologyChangeParser(logger))
.parseFile(settings.getDiffFile());
} }
@ -166,9 +168,9 @@ public class OntologyUpdater {
private void updateABox(AtomicOntologyChangeLists changes) private void updateABox(AtomicOntologyChangeLists changes)
throws IOException { throws IOException {
// TODO get models from somewhere // TODO get models from somewhere
OntModel oldTBoxModel = null; OntModel oldTBoxModel = settings.getOldTBoxModel();
OntModel newTBoxModel = null; OntModel newTBoxModel = settings.getNewTBoxModel();
OntModel ABoxModel = null; OntModel ABoxModel = settings.getOntModelSelector().getABoxModel();
ABoxUpdater aboxUpdater = new ABoxUpdater( ABoxUpdater aboxUpdater = new ABoxUpdater(
oldTBoxModel, newTBoxModel, ABoxModel, logger, record); oldTBoxModel, newTBoxModel, ABoxModel, logger, record);
aboxUpdater.processPropertyChanges(changes.getAtomicPropertyChanges()); aboxUpdater.processPropertyChanges(changes.getAtomicPropertyChanges());
@ -252,19 +254,20 @@ public class OntologyUpdater {
*/ */
private class AtomicOntologyChangeLists { private class AtomicOntologyChangeLists {
private List<AtomicOntologyChange> atomicClassChanges; private List<AtomicOntologyChange> atomicClassChanges =
new ArrayList<AtomicOntologyChange>();
private List<AtomicOntologyChange> atomicPropertyChanges; private List<AtomicOntologyChange> atomicPropertyChanges =
new ArrayList<AtomicOntologyChange>();
public AtomicOntologyChangeLists ( public AtomicOntologyChangeLists (
List<AtomicOntologyChange> changeList, OntModel newTboxModel, List<AtomicOntologyChange> changeList, OntModel newTboxModel,
OntModel oldTboxModel) throws IOException { OntModel oldTboxModel) throws IOException {
String str = null;
Iterator<AtomicOntologyChange> listItr = changeList.iterator(); Iterator<AtomicOntologyChange> listItr = changeList.iterator();
while(listItr.hasNext()){ while(listItr.hasNext()){
AtomicOntologyChange changeObj = listItr.next(); AtomicOntologyChange changeObj = listItr.next();
if(!changeObj.getSourceURI().equals(str)){ if(changeObj.getSourceURI() != null){
if(oldTboxModel.getOntProperty(changeObj. if(oldTboxModel.getOntProperty(changeObj.
getSourceURI()) != null){ getSourceURI()) != null){
atomicPropertyChanges.add(changeObj); atomicPropertyChanges.add(changeObj);
@ -279,7 +282,7 @@ public class OntologyUpdater {
} }
} }
else if(!changeObj.getDestinationURI().equals(str)){ else if(changeObj.getDestinationURI() != null){
if(newTboxModel.getOntProperty(changeObj. if(newTboxModel.getOntProperty(changeObj.
getDestinationURI()) != null){ getDestinationURI()) != null){
atomicPropertyChanges.add(changeObj); atomicPropertyChanges.add(changeObj);
@ -298,7 +301,7 @@ public class OntologyUpdater {
+ "Change Object skipped" ); + "Change Object skipped" );
} }
} }
logger.log("Property and Class change Object lists seperated"); //logger.log("Property and Class change Object lists separated");
} }
public List<AtomicOntologyChange> getAtomicClassChanges() { public List<AtomicOntologyChange> getAtomicClassChanges() {

View file

@ -33,6 +33,7 @@ public class SimpleOntologyChangeLogger implements OntologyChangeLogger {
String methodName = ((StackTraceElement)elements[1]).getMethodName(); String methodName = ((StackTraceElement)elements[1]).getMethodName();
logWriter.write(className + "." + methodName + ": " + logMessage + "\n"); logWriter.write(className + "." + methodName + ": " + logMessage + "\n");
logWriter.flush();
} }
public void logError(String errorMessage) throws IOException { public void logError(String errorMessage) throws IOException {
@ -45,6 +46,7 @@ public class SimpleOntologyChangeLogger implements OntologyChangeLogger {
int lineNumber = ((StackTraceElement)elements[1]).getLineNumber(); int lineNumber = ((StackTraceElement)elements[1]).getLineNumber();
errorWriter.write(className + "." + methodName + " line " + lineNumber + ": " + errorMessage + "\n"); errorWriter.write(className + "." + methodName + " line " + lineNumber + ": " + errorMessage + "\n");
errorWriter.flush();
} }
public void closeLogs() throws IOException { public void closeLogs() throws IOException {

View file

@ -3,6 +3,8 @@ package edu.cornell.mannlib.vitro.webapp.ontology.update;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -25,12 +27,16 @@ public class SimpleOntologyChangeRecord implements OntologyChangeRecord {
public SimpleOntologyChangeRecord( public SimpleOntologyChangeRecord(
String additionsFile, String retractionsFile) { String additionsFile, String retractionsFile) {
this.additionsFile = new File(additionsFile); this.additionsFile = new File(additionsFile);
if (!this.additionsFile.exists()) { try {
throw new RuntimeException(this.getClass().getName() + FileWriter test = new FileWriter(additionsFile);
} catch (IOException ioe) {
throw new RuntimeException(this.getClass().getName() +
" unable to create required file at " + additionsFile); " unable to create required file at " + additionsFile);
} }
this.retractionsFile = new File(retractionsFile); this.retractionsFile = new File(retractionsFile);
if (!this.retractionsFile.exists()) { try {
FileWriter test = new FileWriter(retractionsFile);
} catch (IOException ioe) {
throw new RuntimeException(this.getClass().getName() + throw new RuntimeException(this.getClass().getName() +
" unable to create required file at " + retractionsFile); " unable to create required file at " + retractionsFile);
} }

View file

@ -103,35 +103,35 @@ public class TBoxUpdater {
NodeIterator objects = newTboxModel.listObjectsOfProperty(subject, predicate); NodeIterator objects = newTboxModel.listObjectsOfProperty(subject, predicate);
if ((objects == null) || (objects.toList().size() == 0) ) { if ((objects == null) || (!objects.hasNext()) ) {
logger.logError("Error: found a statement for subject = " + subject.getURI() + retractions.add(siteModel.listStatements(subject, predicate, (RDFNode) null));
" and property = " + predicate.getURI() + //logger.log("Error: found a statement for subject = " + subject.getURI() +
" in the old version but not the new version of the ontology."); // " and property = " + predicate.getURI() +
continue; // " in the old version but not the new version of the ontology.");
continue;
} else if (objects.toList().size() > 1) { }
RDFNode newObject = objects.next();
if (objects.hasNext()) {
logger.logError("Error: found " + objects.toList().size() + logger.logError("Error: found " + objects.toList().size() +
" statements with subject = " + subject.getURI() + " statements with subject = " + subject.getURI() +
" and property = " + predicate.getURI() + " and property = " + predicate.getURI() +
" in the new version of the ontology. (maximum of one is expected)"); " in the new version of the ontology. (maximum of one is expected)");
continue; continue;
} }
RDFNode newObject = objects.next();
if (!newObject.equals(oldObject)) { if (!newObject.equals(oldObject)) {
objects = siteModel.listObjectsOfProperty(subject,predicate); objects = siteModel.listObjectsOfProperty(subject,predicate);
if (objects.toList().size() > 1) { RDFNode siteObject = objects.next();
if (objects.hasNext()) {
logger.logError("Warning: found " + objects.toList().size() + logger.logError("Warning: found " + objects.toList().size() +
" statements with subject = " + subject.getURI() + " statements with subject = " + subject.getURI() +
" and property = " + predicate.getURI() + " and property = " + predicate.getURI() +
" in the site model (maximum of one is expected). +" + " in the site model (maximum of one is expected). +" +
" did not perform any update on this property"); " did not perform any update on this property");
continue; continue;
} }
RDFNode siteObject = objects.next();
if (!siteObject.equals(oldObject)) { if (!siteObject.equals(oldObject)) {
try { try {
@ -149,11 +149,11 @@ public class TBoxUpdater {
additions.add(subject, predicate, newObject); additions.add(subject, predicate, newObject);
logger.log("Changed the value of property " + predicate.getURI() + logger.log("Changed the value of property " + predicate.getURI() +
" of class = " + subject.getURI() + " of subject = " + subject.getURI() +
" from " + " from " +
(newObject.isLiteral() ? ((Resource)oldObject).getURI() : ((Literal)oldObject).getLexicalForm()) + (oldObject.isResource() ? ((Resource)oldObject).getURI() : ((Literal)oldObject).getLexicalForm()) +
" to " + " to " +
(newObject.isLiteral() ? ((Resource)newObject).getURI() : ((Literal)newObject).getLexicalForm()) + (newObject.isResource() ? ((Resource)newObject).getURI() : ((Literal)newObject).getLexicalForm()) +
" in the knowledge base:\n"); " in the knowledge base:\n");
} catch (Exception e) { } catch (Exception e) {
logger.logError("Error trying to change the value of property " + predicate.getURI() + logger.logError("Error trying to change the value of property " + predicate.getURI() +

View file

@ -44,7 +44,7 @@ public class UpdateKnowledgeBase implements ServletContextListener {
private final String DATA_DIR = "/WEB-INF/ontologies/update/"; private final String DATA_DIR = "/WEB-INF/ontologies/update/";
private final String LOG_DIR = "logs/"; private final String LOG_DIR = "logs/";
private final String REMOVED_DATA_DIR = "removedData/"; private final String CHANGED_DATA_DIR = "changedData/";
private final String ASK_QUERY_FILE = DATA_DIR + "ask.sparql"; private final String ASK_QUERY_FILE = DATA_DIR + "ask.sparql";
private final String SUCCESS_ASSERTIONS_FILE = DATA_DIR + "success.n3"; private final String SUCCESS_ASSERTIONS_FILE = DATA_DIR + "success.n3";
private final String SUCCESS_RDF_FORMAT = "N3"; private final String SUCCESS_RDF_FORMAT = "N3";
@ -53,8 +53,10 @@ public class UpdateKnowledgeBase implements ServletContextListener {
"knowledgeBaseUpdate.log"; "knowledgeBaseUpdate.log";
private final String ERROR_LOG_FILE = DATA_DIR + LOG_DIR + private final String ERROR_LOG_FILE = DATA_DIR + LOG_DIR +
"knowledgeBaseUpdate.error.log"; "knowledgeBaseUpdate.error.log";
private final String REMOVED_DATA_FILE = DATA_DIR + REMOVED_DATA_DIR + private final String REMOVED_DATA_FILE = DATA_DIR + CHANGED_DATA_DIR +
"removedData.rdf"; "removedData.rdf";
private final String ADDED_DATA_FILE = DATA_DIR + CHANGED_DATA_DIR +
"addedData.rdf";
private final String SPARQL_CONSTRUCTS_DIR = DATA_DIR + "sparqlConstructs/"; private final String SPARQL_CONSTRUCTS_DIR = DATA_DIR + "sparqlConstructs/";
private final String MISC_REPLACEMENTS_FILE = DATA_DIR + "miscReplacements.rdf"; private final String MISC_REPLACEMENTS_FILE = DATA_DIR + "miscReplacements.rdf";
private final String OLD_TBOX_MODEL_DIR = DATA_DIR + "oldVersion/"; private final String OLD_TBOX_MODEL_DIR = DATA_DIR + "oldVersion/";
@ -63,53 +65,54 @@ public class UpdateKnowledgeBase implements ServletContextListener {
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
// TODO remove when ready
if (true) {
return;
}
ServletContext ctx = sce.getServletContext();
OntModelSelector oms = new SimpleOntModelSelector(
(OntModel) sce.getServletContext().getAttribute(
JenaBaseDao.ASSERTIONS_ONT_MODEL_ATTRIBUTE_NAME));
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));
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));
WebappDaoFactory wadf = (WebappDaoFactory) ctx.getAttribute("webappDaoFactory");
settings.setDefaultNamespace(wadf.getDefaultNamespace());
OntModel oldTBoxModel = loadModelFromDirectory(ctx.getRealPath(OLD_TBOX_MODEL_DIR));
settings.setOldTBoxModel(oldTBoxModel);
settings.setNewTBoxModel(oms.getTBoxModel());
OntModel oldTBoxAnnotationsModel = loadModelFromDirectory(ctx.getRealPath(OLD_TBOX_ANNOTATIONS_DIR));
settings.setOldTBoxAnnotationsModel(oldTBoxAnnotationsModel);
OntModel newTBoxAnnotationsModel = loadModelFromDirectory(ctx.getRealPath(NEW_TBOX_ANNOTATIONS_DIR));
settings.setNewTBoxAnnotationsModel(newTBoxAnnotationsModel);
try { try {
doMiscAppMetadataReplacements(ctx.getRealPath(MISC_REPLACEMENTS_FILE), oms);
(new OntologyUpdater(settings)).update(); ServletContext ctx = sce.getServletContext();
} catch (IOException ioe) {
String errMsg = "IOException updating knowledge base " + OntModelSelector oms = new SimpleOntModelSelector(
"for ontology changes: "; (OntModel) sce.getServletContext().getAttribute(
// Tomcat doesn't always seem to print exceptions thrown from JenaBaseDao.ASSERTIONS_ONT_MODEL_ATTRIBUTE_NAME));
// context listeners
System.out.println(errMsg); OntologyUpdateSettings settings = new OntologyUpdateSettings();
ioe.printStackTrace(); settings.setAskQueryFile(ctx.getRealPath(ASK_QUERY_FILE));
throw new RuntimeException(errMsg, ioe); 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));
settings.setSuccessRDFFormat(ctx.getRealPath(SUCCESS_RDF_FORMAT));
settings.setLogFile(ctx.getRealPath(LOG_FILE));
settings.setErrorLogFile(ctx.getRealPath(ERROR_LOG_FILE));
settings.setAddedDataFile(ctx.getRealPath(ADDED_DATA_FILE));
settings.setRemovedDataFile(ctx.getRealPath(REMOVED_DATA_FILE));
WebappDaoFactory wadf = (WebappDaoFactory) ctx.getAttribute("webappDaoFactory");
settings.setDefaultNamespace(wadf.getDefaultNamespace());
settings.setOntModelSelector(oms);
OntModel oldTBoxModel = loadModelFromDirectory(ctx.getRealPath(OLD_TBOX_MODEL_DIR));
settings.setOldTBoxModel(oldTBoxModel);
settings.setNewTBoxModel(oms.getTBoxModel());
OntModel oldTBoxAnnotationsModel = loadModelFromDirectory(ctx.getRealPath(OLD_TBOX_ANNOTATIONS_DIR));
settings.setOldTBoxAnnotationsModel(oldTBoxAnnotationsModel);
OntModel newTBoxAnnotationsModel = loadModelFromDirectory(ctx.getRealPath(NEW_TBOX_ANNOTATIONS_DIR));
settings.setNewTBoxAnnotationsModel(newTBoxAnnotationsModel);
try {
doMiscAppMetadataReplacements(ctx.getRealPath(MISC_REPLACEMENTS_FILE), oms);
(new OntologyUpdater(settings)).update();
} catch (IOException ioe) {
String errMsg = "IOException updating knowledge base " +
"for ontology changes: ";
// Tomcat doesn't always seem to print exceptions thrown from
// context listeners
System.out.println(errMsg);
ioe.printStackTrace();
throw new RuntimeException(errMsg, ioe);
}
} catch (Throwable t) {
t.printStackTrace();
}
} }