NIHVIVO-3481 fixed 'create classgroups automatically' option
This commit is contained in:
parent
83c4e54a34
commit
549d489bac
2 changed files with 557 additions and 517 deletions
|
@ -34,6 +34,7 @@ import edu.cornell.mannlib.vitro.webapp.controller.Controllers;
|
||||||
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
import edu.cornell.mannlib.vitro.webapp.controller.VitroRequest;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
import edu.cornell.mannlib.vitro.webapp.dao.WebappDaoFactory;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaModelUtils;
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.JenaModelUtils;
|
||||||
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.ModelContext;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.OntModelSelector;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaSpecialModelMaker;
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.VitroJenaSpecialModelMaker;
|
||||||
import edu.cornell.mannlib.vitro.webapp.dao.jena.event.BulkUpdateEvent;
|
import edu.cornell.mannlib.vitro.webapp.dao.jena.event.BulkUpdateEvent;
|
||||||
|
@ -80,8 +81,8 @@ public class RDFUploadController extends BaseEditController {
|
||||||
|
|
||||||
String languageStr = request.getParameter("language");
|
String languageStr = request.getParameter("language");
|
||||||
|
|
||||||
boolean makeClassgroups =
|
boolean makeClassgroups = ("true".equals(request.getParameter(
|
||||||
(request.getParameter("makeClassgroups") != null);
|
"makeClassgroups")));
|
||||||
|
|
||||||
// add directly to the ABox model without reading first into
|
// add directly to the ABox model without reading first into
|
||||||
// a temporary in-memory model
|
// a temporary in-memory model
|
||||||
|
@ -154,32 +155,29 @@ public class RDFUploadController extends BaseEditController {
|
||||||
long aboxstmtCount = 0L;
|
long aboxstmtCount = 0L;
|
||||||
|
|
||||||
JenaModelUtils xutil = new JenaModelUtils();
|
JenaModelUtils xutil = new JenaModelUtils();
|
||||||
|
|
||||||
OntModel tboxModel = getTBoxModel(
|
OntModel tboxModel = getTBoxModel(
|
||||||
request.getSession(), getServletContext());
|
request.getSession(), getServletContext());
|
||||||
OntModel aboxModel = getABoxModel(
|
OntModel aboxModel = getABoxModel(
|
||||||
request.getSession(), getServletContext());
|
request.getSession(), getServletContext());
|
||||||
OntModel tboxChangeModel = null;
|
OntModel tboxChangeModel = null;
|
||||||
Model aboxChangeModel = null;
|
Model aboxChangeModel = null;
|
||||||
|
OntModelSelector ontModelSelector = ModelContext.getOntModelSelector(
|
||||||
|
getServletContext());
|
||||||
|
|
||||||
if (tboxModel != null) {
|
if (tboxModel != null) {
|
||||||
boolean AGGRESSIVE = true;
|
boolean AGGRESSIVE = true;
|
||||||
tboxChangeModel = xutil.extractTBox(uploadModel, AGGRESSIVE);
|
tboxChangeModel = xutil.extractTBox(uploadModel, AGGRESSIVE);
|
||||||
// aggressively seek all statements that are part of the TBox
|
// aggressively seek all statements that are part of the TBox
|
||||||
tboxstmtCount = operateOnModel(
|
tboxstmtCount = operateOnModel(request.getFullWebappDaoFactory(),
|
||||||
request.getFullWebappDaoFactory(),
|
tboxModel, tboxChangeModel, ontModelSelector,
|
||||||
tboxModel,tboxChangeModel,
|
remove, makeClassgroups, loginBean.getUserURI());
|
||||||
remove,
|
|
||||||
makeClassgroups,
|
|
||||||
loginBean.getUserURI());
|
|
||||||
}
|
}
|
||||||
if (aboxModel != null) {
|
if (aboxModel != null) {
|
||||||
aboxChangeModel = uploadModel.remove(tboxChangeModel);
|
aboxChangeModel = uploadModel.remove(tboxChangeModel);
|
||||||
aboxstmtCount = operateOnModel(
|
aboxstmtCount = operateOnModel(request.getFullWebappDaoFactory(),
|
||||||
request.getFullWebappDaoFactory(),
|
aboxModel, aboxChangeModel, ontModelSelector,
|
||||||
aboxModel,
|
remove, makeClassgroups, loginBean.getUserURI());
|
||||||
aboxChangeModel,
|
|
||||||
remove,
|
|
||||||
makeClassgroups,
|
|
||||||
loginBean.getUserURI());
|
|
||||||
}
|
}
|
||||||
request.setAttribute("uploadDesc", uploadDesc + ". " + verb + " " +
|
request.setAttribute("uploadDesc", uploadDesc + ". " + verb + " " +
|
||||||
(tboxstmtCount + aboxstmtCount) + " statements.");
|
(tboxstmtCount + aboxstmtCount) + " statements.");
|
||||||
|
@ -239,11 +237,10 @@ public class RDFUploadController extends BaseEditController {
|
||||||
private long operateOnModel(WebappDaoFactory webappDaoFactory,
|
private long operateOnModel(WebappDaoFactory webappDaoFactory,
|
||||||
OntModel mainModel,
|
OntModel mainModel,
|
||||||
Model changesModel,
|
Model changesModel,
|
||||||
|
OntModelSelector ontModelSelector,
|
||||||
boolean remove,
|
boolean remove,
|
||||||
boolean makeClassgroups,
|
boolean makeClassgroups,
|
||||||
String userURI) {
|
String userURI) {
|
||||||
mainModel.enterCriticalSection(Lock.WRITE);
|
|
||||||
try {
|
|
||||||
|
|
||||||
EditEvent startEvent = null, endEvent = null;
|
EditEvent startEvent = null, endEvent = null;
|
||||||
|
|
||||||
|
@ -255,18 +252,33 @@ public class RDFUploadController extends BaseEditController {
|
||||||
endEvent = new EditEvent(userURI, false);
|
endEvent = new EditEvent(userURI, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Model[] classgroupModel = null;
|
||||||
|
|
||||||
|
if (makeClassgroups) {
|
||||||
|
classgroupModel = JenaModelUtils.makeClassGroupsFromRootClasses(
|
||||||
|
webappDaoFactory, changesModel);
|
||||||
|
OntModel appMetadataModel = ontModelSelector
|
||||||
|
.getApplicationMetadataModel();
|
||||||
|
appMetadataModel.enterCriticalSection(Lock.WRITE);
|
||||||
|
try {
|
||||||
|
appMetadataModel.add(classgroupModel[0]);
|
||||||
|
} finally {
|
||||||
|
appMetadataModel.leaveCriticalSection();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
mainModel.enterCriticalSection(Lock.WRITE);
|
||||||
|
try {
|
||||||
|
|
||||||
mainModel.getBaseModel().notifyEvent(startEvent);
|
mainModel.getBaseModel().notifyEvent(startEvent);
|
||||||
try {
|
try {
|
||||||
if (makeClassgroups) {
|
|
||||||
Model classgroupModel =
|
|
||||||
JenaModelUtils.makeClassGroupsFromRootClasses(
|
|
||||||
webappDaoFactory, changesModel, changesModel);
|
|
||||||
mainModel.add(classgroupModel);
|
|
||||||
}
|
|
||||||
if (remove) {
|
if (remove) {
|
||||||
mainModel.remove(changesModel);
|
mainModel.remove(changesModel);
|
||||||
} else {
|
} else {
|
||||||
mainModel.add(changesModel);
|
mainModel.add(changesModel);
|
||||||
|
if (classgroupModel != null) {
|
||||||
|
mainModel.add(classgroupModel[1]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
mainModel.getBaseModel().notifyEvent(endEvent);
|
mainModel.getBaseModel().notifyEvent(endEvent);
|
||||||
|
|
|
@ -11,7 +11,6 @@ import java.util.Set;
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype;
|
|
||||||
import com.hp.hpl.jena.ontology.Individual;
|
import com.hp.hpl.jena.ontology.Individual;
|
||||||
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;
|
||||||
|
@ -25,6 +24,7 @@ import com.hp.hpl.jena.query.QueryFactory;
|
||||||
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.Property;
|
import com.hp.hpl.jena.rdf.model.Property;
|
||||||
|
import com.hp.hpl.jena.rdf.model.RDFNode;
|
||||||
import com.hp.hpl.jena.rdf.model.Resource;
|
import com.hp.hpl.jena.rdf.model.Resource;
|
||||||
import com.hp.hpl.jena.rdf.model.ResourceFactory;
|
import com.hp.hpl.jena.rdf.model.ResourceFactory;
|
||||||
import com.hp.hpl.jena.shared.Lock;
|
import com.hp.hpl.jena.shared.Lock;
|
||||||
|
@ -55,13 +55,26 @@ public class JenaModelUtils {
|
||||||
nonIndividualTypeURIs.add(RDF.Property.getURI());
|
nonIndividualTypeURIs.add(RDF.Property.getURI());
|
||||||
}
|
}
|
||||||
|
|
||||||
public synchronized static void makeClassGroupsFromRootClasses(WebappDaoFactory wadf, Model ontModel) {
|
/**
|
||||||
makeClassGroupsFromRootClasses(wadf, ontModel, ontModel);
|
* Creates a set of vitro:ClassGroup resources for each root class in
|
||||||
}
|
* an ontology. Also creates annotations to place each root class and all
|
||||||
|
* of its children in the appropriate groups. In the case of multiple
|
||||||
|
* inheritance, classgroup assignment will be arbitrary.
|
||||||
|
* @param wadf
|
||||||
|
* @param tboxModel containing ontology classes
|
||||||
|
* @return resultArray of OntModels, where resultArray[0] is the model containing
|
||||||
|
* the triples about the classgroups, and resultArray[1] is the model containing
|
||||||
|
* annotation triples assigning ontology classes to classgroups.
|
||||||
|
*/
|
||||||
|
public synchronized static OntModel[] makeClassGroupsFromRootClasses(
|
||||||
|
WebappDaoFactory wadf, Model tboxModel) {
|
||||||
|
|
||||||
public synchronized static OntModel makeClassGroupsFromRootClasses(WebappDaoFactory wadf, Model baseModel, Model vitroInternalsSubmodel) {
|
OntModel ontModel = ModelFactory.createOntologyModel(
|
||||||
OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM,baseModel);
|
OntModelSpec.OWL_DL_MEM, tboxModel);
|
||||||
OntModel modelForClassgroups = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
|
OntModel modelForClassgroups = ModelFactory.createOntologyModel(
|
||||||
|
OntModelSpec.OWL_DL_MEM);
|
||||||
|
OntModel modelForClassgroupAnnotations = ModelFactory.createOntologyModel(
|
||||||
|
OntModelSpec.OWL_DL_MEM);
|
||||||
SimpleOntModelSelector oms = new SimpleOntModelSelector();
|
SimpleOntModelSelector oms = new SimpleOntModelSelector();
|
||||||
oms.setTBoxModel(ontModel);
|
oms.setTBoxModel(ontModel);
|
||||||
oms.setApplicationMetadataModel(modelForClassgroups);
|
oms.setApplicationMetadataModel(modelForClassgroups);
|
||||||
|
@ -69,38 +82,53 @@ public class JenaModelUtils {
|
||||||
config.setDefaultNamespace(wadf.getDefaultNamespace());
|
config.setDefaultNamespace(wadf.getDefaultNamespace());
|
||||||
WebappDaoFactory myWebappDaoFactory = new WebappDaoFactoryJena(
|
WebappDaoFactory myWebappDaoFactory = new WebappDaoFactoryJena(
|
||||||
new SimpleOntModelSelector(ontModel), config, null);
|
new SimpleOntModelSelector(ontModel), config, null);
|
||||||
OntModel tempModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM);
|
|
||||||
Resource classGroupClass = ResourceFactory.createResource(VitroVocabulary.CLASSGROUP);
|
Resource classGroupClass = ResourceFactory.createResource(
|
||||||
Property inClassGroupProperty = ResourceFactory.createProperty(VitroVocabulary.IN_CLASSGROUP);
|
VitroVocabulary.CLASSGROUP);
|
||||||
|
Property inClassGroupProperty = ResourceFactory.createProperty(
|
||||||
|
VitroVocabulary.IN_CLASSGROUP);
|
||||||
|
|
||||||
ontModel.enterCriticalSection(Lock.READ);
|
ontModel.enterCriticalSection(Lock.READ);
|
||||||
try {
|
try {
|
||||||
try {
|
try {
|
||||||
for (Iterator rootClassIt = myWebappDaoFactory.getVClassDao().getRootClasses().iterator(); rootClassIt.hasNext(); ) {
|
for (Iterator rootClassIt = myWebappDaoFactory.getVClassDao()
|
||||||
|
.getRootClasses().iterator(); rootClassIt.hasNext(); ) {
|
||||||
VClass rootClass = (VClass) rootClassIt.next();
|
VClass rootClass = (VClass) rootClassIt.next();
|
||||||
Individual classGroup = tempModel.createIndividual(wadf.getDefaultNamespace()+"vitroClassGroup"+rootClass.getLocalName(), classGroupClass);
|
|
||||||
classGroup.addProperty(tempModel.getProperty(VitroVocabulary.DISPLAY_RANK_ANNOT),"50",XSDDatatype.XSDint);
|
Individual classGroup = modelForClassgroups.createIndividual(
|
||||||
|
wadf.getDefaultNamespace() + "vitroClassGroup" +
|
||||||
|
rootClass.getLocalName(), classGroupClass);
|
||||||
classGroup.setLabel(rootClass.getName(), null);
|
classGroup.setLabel(rootClass.getName(), null);
|
||||||
OntClass rootClassOntClass = ontModel.getOntClass(rootClass.getURI());
|
|
||||||
tempModel.add(rootClassOntClass, inClassGroupProperty, classGroup);
|
Resource rootClassRes = modelForClassgroupAnnotations.getResource(
|
||||||
for (Iterator childIt = myWebappDaoFactory.getVClassDao().getAllSubClassURIs(rootClass.getURI()).iterator(); childIt.hasNext(); ) {
|
rootClass.getURI());
|
||||||
|
modelForClassgroupAnnotations.add(
|
||||||
|
rootClassRes, inClassGroupProperty, classGroup);
|
||||||
|
for (Iterator<String> childIt = myWebappDaoFactory.getVClassDao()
|
||||||
|
.getAllSubClassURIs(rootClass.getURI()).iterator();
|
||||||
|
childIt.hasNext(); ) {
|
||||||
String childURI = (String) childIt.next();
|
String childURI = (String) childIt.next();
|
||||||
OntClass childClass = ontModel.getOntClass(childURI);
|
Resource childClass = modelForClassgroupAnnotations
|
||||||
|
.getResource(childURI);
|
||||||
|
if (!modelForClassgroupAnnotations.contains(
|
||||||
|
childClass, inClassGroupProperty, (RDFNode) null)) {
|
||||||
childClass.addProperty(inClassGroupProperty, classGroup);
|
childClass.addProperty(inClassGroupProperty, classGroup);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (Exception e) {
|
|
||||||
log.error("Unable to create class groups automatically based on class hierarchy");
|
|
||||||
}
|
}
|
||||||
vitroInternalsSubmodel.enterCriticalSection(Lock.WRITE);
|
} catch (Exception e) {
|
||||||
try {
|
String errMsg = "Unable to create class groups automatically " +
|
||||||
vitroInternalsSubmodel.add(tempModel);
|
"based on class hierarchy";
|
||||||
} finally {
|
log.error(errMsg, e);
|
||||||
vitroInternalsSubmodel.leaveCriticalSection();
|
throw new RuntimeException(errMsg, e);
|
||||||
}
|
}
|
||||||
} finally {
|
} finally {
|
||||||
ontModel.leaveCriticalSection();
|
ontModel.leaveCriticalSection();
|
||||||
}
|
}
|
||||||
return modelForClassgroups;
|
OntModel[] resultArray = new OntModel[2];
|
||||||
|
resultArray[0] = modelForClassgroups;
|
||||||
|
resultArray[1] = modelForClassgroupAnnotations;
|
||||||
|
return resultArray;
|
||||||
}
|
}
|
||||||
|
|
||||||
private final OntModelSpec DEFAULT_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
|
private final OntModelSpec DEFAULT_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue