update for display model migration

This commit is contained in:
hjkhjk54 2012-07-06 00:05:43 +00:00
parent 858ccb5cdd
commit d3ad1c1a5a
3 changed files with 299 additions and 22 deletions

View file

@ -10,6 +10,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.io.StringWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@ -29,10 +30,16 @@ import com.hp.hpl.jena.query.QueryFactory;
import com.hp.hpl.jena.query.Syntax; import com.hp.hpl.jena.query.Syntax;
import com.hp.hpl.jena.rdf.model.Model; import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory; 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.rdf.model.Statement; import com.hp.hpl.jena.rdf.model.Statement;
import com.hp.hpl.jena.rdf.model.StmtIterator; import com.hp.hpl.jena.rdf.model.StmtIterator;
import com.hp.hpl.jena.shared.Lock; import com.hp.hpl.jena.shared.Lock;
import com.hp.hpl.jena.vocabulary.RDF;
import com.hp.hpl.jena.vocabulary.RDFS;
import edu.cornell.mannlib.vitro.webapp.dao.DisplayVocabulary;
import edu.cornell.mannlib.vitro.webapp.rdfservice.ChangeSet; import edu.cornell.mannlib.vitro.webapp.rdfservice.ChangeSet;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFService;
import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException; import edu.cornell.mannlib.vitro.webapp.rdfservice.RDFServiceException;
@ -364,6 +371,145 @@ public class KnowledgeBaseUpdater {
} }
} }
//update migration model
public void migrateDisplayModel() {
OntModel displayModel = this.settings.getDisplayModel();
Model addStatements = ModelFactory.createDefaultModel();
Model removeStatements = ModelFactory.createDefaultModel();
//remove old tbox and display metadata statements and add statements from new versions
replaceTboxAndDisplayMetadata(displayModel, addStatements, removeStatements);
//Update statements for data getter class types that have changed in 1.5
updateDataGetterClassNames(displayModel, addStatements, removeStatements);
//add cannot delete flags to pages that shouldn't allow deletion on page list
addCannotDeleteFlagDisplayModel(displayModel, addStatements, removeStatements);
//removes requiresTemplate statement for people page
updatePeoplePageDisplayModel(displayModel, addStatements, removeStatements);
//add page list
addPageListDisplayModel(displayModel, addStatements, removeStatements);
//update data getter labels
updateDataGetterLabels(displayModel, addStatements, removeStatements);
displayModel.enterCriticalSection(Lock.WRITE);
try {
if(log.isDebugEnabled()) {
StringWriter sw = new StringWriter();
addStatements.write(sw);
log.debug("Statements to be added are: ");
log.debug(sw.toString());
sw.close();
sw = new StringWriter();
removeStatements.write(sw);
log.debug("Statements to be removed are: ");
log.debug(sw.toString());
sw.close();
}
displayModel.remove(removeStatements);
displayModel.add(addStatements);
} catch(Exception ex) {
log.error("An error occurred in migrating display model ", ex);
} finally {
displayModel.leaveCriticalSection();
}
}
//replace
private void replaceTboxAndDisplayMetadata(OntModel displayModel, Model addStatements, Model removeStatements) {
OntModel oldDisplayModelTboxModel = this.settings.getOldDisplayModelTboxModel();
OntModel oldDisplayModelDisplayMetadataModel = this.settings.getOldDisplayModelDisplayMetadataModel();
OntModel newDisplayModelTboxModel = this.settings.getNewDisplayModelTboxModel();
OntModel newDisplayModelDisplayMetadataModel = this.settings.getNewDisplayModelDisplayMetadataModel();
OntModel loadedAtStartup = this.settings.getLoadedAtStartupDisplayModel();
OntModel oldVivoListView = this.settings.getVivoListViewConfigDisplayModel();
//Remove old display model tbox and display metadata statements from display model
removeStatements.add(oldDisplayModelTboxModel);
removeStatements.add(oldDisplayModelDisplayMetadataModel);
//the old startup folder only contained by oldVivoListView
removeStatements.add(oldVivoListView);
//Add statements from new tbox and display metadata
addStatements.add(newDisplayModelTboxModel);
addStatements.add(newDisplayModelDisplayMetadataModel);
//this should include the list view in addition to other files
addStatements.add(loadedAtStartup);
}
//update statements for data getter classes
private void updateDataGetterClassNames(OntModel displayModel, Model addStatements, Model removeStatements) {
Resource classGroupOldType = ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.ClassGroupPageData");
Resource browseOldType = ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.BrowseDataGetter");
Resource individualsForClassesOldType = ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.IndividualsForClassesDataGetter");
Resource internalClassesOldType = ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.pageDataGetter.InternalClassesDataGetter");
Resource classGroupNewType = ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.ClassGroupPageData");
Resource browseNewType = ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.BrowseDataGetter");
Resource individualsForClassesNewType = ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.IndividualsForClassesDataGetter");
Resource internalClassesNewType = ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.InternalClassesDataGetter");
//Find statements where type is ClassGroupData
updateAddRemoveDataGetterStatements(displayModel, removeStatements, addStatements, classGroupOldType, classGroupNewType);
//Find statements where type is BrowseDataGetter
updateAddRemoveDataGetterStatements(displayModel, removeStatements, addStatements, browseOldType, browseNewType);
//Find statements where type is individuals for classes
updateAddRemoveDataGetterStatements(displayModel, removeStatements, addStatements, individualsForClassesOldType, individualsForClassesNewType);
//Find statements where type is internal class
updateAddRemoveDataGetterStatements(displayModel, removeStatements, addStatements, internalClassesOldType, internalClassesNewType);
}
private void updateAddRemoveDataGetterStatements(OntModel displayModel,
Model removeStatements, Model addStatements,
Resource oldType, Resource newType) {
removeStatements.add(displayModel.listStatements(null, RDF.type, oldType));
StmtIterator oldStatements = displayModel.listStatements(null, RDF.type, oldType);
while(oldStatements.hasNext()) {
Statement stmt = oldStatements.nextStatement();
addStatements.add(stmt.getSubject(), RDF.type, newType);
}
}
//add cannotDeleteFlag to display model
private void addCannotDeleteFlagDisplayModel(OntModel displayModel, Model addStatements, Model removeStatements) {
Resource homePage = displayModel.getResource(DisplayVocabulary.HOME_PAGE_URI);
addStatements.add(homePage,
ResourceFactory.createProperty(DisplayVocabulary.DISPLAY_NS + "cannotDeletePage"),
ResourceFactory.createPlainLiteral("true"));
}
//remove requires template
private void updatePeoplePageDisplayModel(OntModel displayModel, Model addStatements, Model removeStatements) {
Resource peoplePage = displayModel.getResource(DisplayVocabulary.DISPLAY_NS + "People");
if(peoplePage != null) {
removeStatements.add(peoplePage, DisplayVocabulary.REQUIRES_BODY_TEMPLATE,
ResourceFactory.createPlainLiteral("menupage--classgroup-people.ftl"));
}
}
//add page list sparql query
private void addPageListDisplayModel(OntModel displayModel, Model addStatements, Model removeStatements) {
OntModel newDisplayModel = this.settings.getNewDisplayModelFromFile();
//Get all statements about pageList and pageListData
Resource pageList = newDisplayModel.getResource(DisplayVocabulary.DISPLAY_NS + "pageList");
Resource pageListData = newDisplayModel.getResource(DisplayVocabulary.DISPLAY_NS + "pageListData");
addStatements.add(newDisplayModel.listStatements(pageList, null, (RDFNode) null));
addStatements.add(newDisplayModel.listStatements(pageListData, null, (RDFNode) null));
}
//update any new labels
private void updateDataGetterLabels(OntModel displayModel, Model addStatements, Model removeStatements) {
OntModel newDisplayModel = this.settings.getNewDisplayModelFromFile();
List<Resource> resourcesForLabels = new ArrayList<Resource>();
resourcesForLabels.add(ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.ClassGroupPageData"));
resourcesForLabels.add(ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.BrowseDataGetter"));
resourcesForLabels.add(ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.IndividualsForClassesDataGetter"));
resourcesForLabels.add(ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.InternalClassesDataGetter"));
resourcesForLabels.add(ResourceFactory.createResource("java:edu.cornell.mannlib.vitro.webapp.utils.dataGetter.SparqlQueryDataGetter"));
for(Resource r: resourcesForLabels) {
addStatements.add(newDisplayModel.listStatements(r, RDFS.label, (RDFNode)null));
}
}
/** /**
* A class that allows to access two different ontology change lists, * A class that allows to access two different ontology change lists,
* one for class changes and the other for property changes. The * one for class changes and the other for property changes. The
@ -427,5 +573,7 @@ public class KnowledgeBaseUpdater {
public List<AtomicOntologyChange> getAtomicPropertyChanges() { public List<AtomicOntologyChange> getAtomicPropertyChanges() {
return atomicPropertyChanges; return atomicPropertyChanges;
} }
} }
} }

View file

@ -28,7 +28,15 @@ public class UpdateSettings {
private OntModel newTBoxModel; private OntModel newTBoxModel;
private OntModel oldTBoxAnnotationsModel; private OntModel oldTBoxAnnotationsModel;
private OntModel newTBoxAnnotationsModel; private OntModel newTBoxAnnotationsModel;
//display model tbox and display model display metadata
private OntModel oldDisplayModelTboxModel;
private OntModel oldDisplayModelDisplayMetadataModel;
private OntModel newDisplayModelTboxModel;
private OntModel newDisplayModelDisplayMetadataModel;
private OntModel displayModel;
private OntModel newDisplayModelFromFile;
private OntModel loadedAtStartupDisplayModel;
private OntModel oldDisplayModelVivoListViewConfig;
public String getDataDir() { public String getDataDir() {
return dataDir; return dataDir;
} }
@ -149,4 +157,72 @@ public class UpdateSettings {
public void setNewTBoxAnnotationsModel(OntModel newTBoxAnnotationsModel) { public void setNewTBoxAnnotationsModel(OntModel newTBoxAnnotationsModel) {
this.newTBoxAnnotationsModel = newTBoxAnnotationsModel; this.newTBoxAnnotationsModel = newTBoxAnnotationsModel;
} }
//Old and new display model methods
public void setOldDisplayModelTboxModel(OntModel oldDisplayModelTboxModel) {
this.oldDisplayModelTboxModel = oldDisplayModelTboxModel;
}
public void setNewDisplayModelTboxModel(OntModel newDisplayModelTboxModel) {
this.newDisplayModelTboxModel = newDisplayModelTboxModel;
}
public void setOldDisplayModelDisplayMetadataModel(OntModel oldDisplayModelDisplayMetadataModel) {
this.oldDisplayModelDisplayMetadataModel = oldDisplayModelDisplayMetadataModel;
}
public void setNewDisplayModelDisplayMetadataModel(OntModel newDisplayModelDisplayMetadataModel) {
this.newDisplayModelDisplayMetadataModel = newDisplayModelDisplayMetadataModel;
}
public void setDisplayModel(OntModel displayModel) {
this.displayModel = displayModel;
}
public OntModel getOldDisplayModelTboxModel() {
return this.oldDisplayModelTboxModel;
}
public OntModel getNewDisplayModelTboxModel() {
return this.newDisplayModelTboxModel;
}
public OntModel getOldDisplayModelDisplayMetadataModel() {
return this.oldDisplayModelDisplayMetadataModel;
}
public OntModel getNewDisplayModelDisplayMetadataModel() {
return this.newDisplayModelDisplayMetadataModel;
}
public OntModel getDisplayModel() {
return this.displayModel;
}
public void setNewDisplayModelFromFile(OntModel newDisplayModel) {
this.newDisplayModelFromFile = newDisplayModel;
}
public OntModel getNewDisplayModelFromFile() {
return this.newDisplayModelFromFile;
}
public void setLoadedAtStartupDisplayModel(OntModel loadedModel) {
this.loadedAtStartupDisplayModel = loadedModel;
}
public OntModel getLoadedAtStartupDisplayModel() {
return this.loadedAtStartupDisplayModel;
}
public void setVivoListViewConfigDisplayModel(OntModel loadedModel) {
this.oldDisplayModelVivoListViewConfig = loadedModel;
}
public OntModel getVivoListViewConfigDisplayModel() {
return this.oldDisplayModelVivoListViewConfig;
}
} }

View file

@ -60,7 +60,15 @@ public class UpdateKnowledgeBase implements ServletContextListener {
private static final String NEW_TBOX_MODEL_DIR = "/WEB-INF/filegraph/tbox/"; private static final String NEW_TBOX_MODEL_DIR = "/WEB-INF/filegraph/tbox/";
private static final String OLD_TBOX_ANNOTATIONS_DIR = DATA_DIR + "oldAnnotations/"; private static final String OLD_TBOX_ANNOTATIONS_DIR = DATA_DIR + "oldAnnotations/";
private static final String NEW_TBOX_ANNOTATIONS_DIR = "/WEB-INF/ontologies/user/tbox/"; private static final String NEW_TBOX_ANNOTATIONS_DIR = "/WEB-INF/ontologies/user/tbox/";
//For display model migration
private static final String OLD_DISPLAYMODEL_TBOX_PATH = DATA_DIR + "oldDisplayModel/displayTBOX.n3";
private static final String NEW_DISPLAYMODEL_TBOX_PATH = "/WEB-INF/ontologies/app/menuload/displayTBOX.n3";
private static final String OLD_DISPLAYMODEL_DISPLAYMETADATA_PATH = DATA_DIR + "oldDisplayModel/displayDisplay.n3";
private static final String NEW_DISPLAYMODEL_DISPLAYMETADATA_PATH = "/WEB-INF/ontologies/app/menuload/displayDisplay.n3";
private static final String NEW_DISPLAYMODEL_PATH = "/WEB-INF/ontologies/app/menu.n3";
private static final String LOADED_STARTUPT_DISPLAYMODEL_DIR = "/WEB-INF/ontologies/app/loadedAtStartup/";
private static final String OLD_DISPLAYMODEL_VIVOLISTVIEW_PATH = DATA_DIR + "oldDisplayModel/vivoListViewConfig.rdf";
public void contextInitialized(ServletContextEvent sce) { public void contextInitialized(ServletContextEvent sce) {
try { try {
ServletContext ctx = sce.getServletContext(); ServletContext ctx = sce.getServletContext();
@ -85,7 +93,7 @@ public class UpdateKnowledgeBase implements ServletContextListener {
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)); settings.setUnionOntModelSelector(ModelContext.getUnionOntModelSelector(ctx));
settings.setDisplayModel(ModelContext.getDisplayModel(ctx));
try { try {
OntModel oldTBoxModel = loadModelFromDirectory(ctx.getRealPath(OLD_TBOX_MODEL_DIR)); OntModel oldTBoxModel = loadModelFromDirectory(ctx.getRealPath(OLD_TBOX_MODEL_DIR));
settings.setOldTBoxModel(oldTBoxModel); settings.setOldTBoxModel(oldTBoxModel);
@ -95,6 +103,27 @@ public class UpdateKnowledgeBase implements ServletContextListener {
settings.setOldTBoxAnnotationsModel(oldTBoxAnnotationsModel); settings.setOldTBoxAnnotationsModel(oldTBoxAnnotationsModel);
OntModel newTBoxAnnotationsModel = loadModelFromDirectory(ctx.getRealPath(NEW_TBOX_ANNOTATIONS_DIR)); OntModel newTBoxAnnotationsModel = loadModelFromDirectory(ctx.getRealPath(NEW_TBOX_ANNOTATIONS_DIR));
settings.setNewTBoxAnnotationsModel(newTBoxAnnotationsModel); settings.setNewTBoxAnnotationsModel(newTBoxAnnotationsModel);
//Display model tbox and display metadata
//old display model tbox model
OntModel oldDisplayModelTboxModel = loadModelFromFile(ctx.getRealPath(OLD_DISPLAYMODEL_TBOX_PATH));
settings.setOldDisplayModelTboxModel(oldDisplayModelTboxModel);
//new display model tbox model
OntModel newDisplayModelTboxModel = loadModelFromFile(ctx.getRealPath(NEW_DISPLAYMODEL_TBOX_PATH));
settings.setNewDisplayModelTboxModel(newDisplayModelTboxModel);
//old display model display model metadata
OntModel oldDisplayModelDisplayMetadataModel = loadModelFromFile(ctx.getRealPath(OLD_DISPLAYMODEL_DISPLAYMETADATA_PATH));
settings.setOldDisplayModelDisplayMetadataModel(oldDisplayModelDisplayMetadataModel);
//new display model display model metadata
OntModel newDisplayModelDisplayMetadataModel = loadModelFromFile(ctx.getRealPath(NEW_DISPLAYMODEL_DISPLAYMETADATA_PATH));
settings.setNewDisplayModelDisplayMetadataModel(newDisplayModelDisplayMetadataModel);
//Get new display model
OntModel newDisplayModelFromFile = loadModelFromFile(ctx.getRealPath(NEW_DISPLAYMODEL_PATH));
settings.setNewDisplayModelFromFile(newDisplayModelFromFile);
OntModel loadedAtStartupFiles = loadModelFromDirectory(ctx.getRealPath(LOADED_STARTUPT_DISPLAYMODEL_DIR));
settings.setLoadedAtStartupDisplayModel(loadedAtStartupFiles);
OntModel oldDisplayModelVivoListView = loadModelFromFile(ctx.getRealPath(OLD_DISPLAYMODEL_VIVOLISTVIEW_PATH));
settings.setVivoListViewConfigDisplayModel(oldDisplayModelVivoListView);
} catch (ModelDirectoryNotFoundException e) { } catch (ModelDirectoryNotFoundException e) {
log.info("Knowledge base update directories not found. " + log.info("Knowledge base update directories not found. " +
"No update will be performed."); "No update will be performed.");
@ -108,7 +137,7 @@ public class UpdateKnowledgeBase implements ServletContextListener {
if (ontologyUpdater.updateRequired(ctx)) { if (ontologyUpdater.updateRequired(ctx)) {
ctx.setAttribute(KBM_REQURIED_AT_STARTUP, Boolean.TRUE); ctx.setAttribute(KBM_REQURIED_AT_STARTUP, Boolean.TRUE);
ontologyUpdater.update(ctx); ontologyUpdater.update(ctx);
// migrateDisplayModel(ctx); migrateDisplayModel(ontologyUpdater);
} }
} catch (IOException ioe) { } catch (IOException ioe) {
String errMsg = "IOException updating knowledge base " + String errMsg = "IOException updating knowledge base " +
@ -127,8 +156,10 @@ public class UpdateKnowledgeBase implements ServletContextListener {
} }
} }
private void migrateDisplayModel(ServletContext ctx) { //Multiple changes from 1.4 to 1.5 will occur
return;
private void migrateDisplayModel(KnowledgeBaseUpdater ontologyUpdater) {
ontologyUpdater.migrateDisplayModel();
} }
private OntModel loadModelFromDirectory(String directoryPath) { private OntModel loadModelFromDirectory(String directoryPath) {
@ -141,26 +172,42 @@ public class UpdateKnowledgeBase implements ServletContextListener {
} }
File[] rdfFiles = directory.listFiles(); File[] rdfFiles = directory.listFiles();
for (int i = 0; i < rdfFiles.length; i++) { for (int i = 0; i < rdfFiles.length; i++) {
try { readFile(rdfFiles[i], om, directoryPath);
File f = rdfFiles[i];
FileInputStream fis = new FileInputStream(f);
try {
if (f.getName().endsWith(".n3")) {
om.read(fis, null, "N3");
} else {
om.read(fis, null, "RDF/XML");
}
} catch (Exception e) {
log.error("Unable to load RDF from " + f.getName(), e);
}
} catch (FileNotFoundException fnfe) {
log.error(rdfFiles[i].getName() + " not found. Unable to load" +
" RDF from this location: " + directoryPath);
}
} }
return om; return om;
} }
//load file from file path
private OntModel loadModelFromFile(String filePath) {
OntModel om = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM);
File file = new File(filePath);
if (!file.isFile()) {
throw new ModelFileNotFoundException(filePath + " must be a file " +
"containing RDF files.");
}
readFile(file, om, filePath);
return om;
}
private void readFile(File f, OntModel om, String path) {
try {
FileInputStream fis = new FileInputStream(f);
try {
if (f.getName().endsWith(".n3")) {
om.read(fis, null, "N3");
} else {
om.read(fis, null, "RDF/XML");
}
} catch (Exception e) {
log.error("Unable to load RDF from " + f.getName(), e);
}
} catch (FileNotFoundException fnfe) {
log.error(f.getName() + " not found. Unable to load" +
" RDF from this location: " + path);
}
}
public void contextDestroyed(ServletContextEvent arg0) { public void contextDestroyed(ServletContextEvent arg0) {
// nothing to do // nothing to do
} }
@ -180,4 +227,10 @@ public class UpdateKnowledgeBase implements ServletContextListener {
super(msg); super(msg);
} }
} }
private class ModelFileNotFoundException extends RuntimeException {
public ModelFileNotFoundException(String msg) {
super(msg);
}
}
} }