From 549d489bac8395c3ee8dec35333397fb4363420a Mon Sep 17 00:00:00 2001 From: brianjlowe Date: Mon, 12 Dec 2011 16:48:50 +0000 Subject: [PATCH] NIHVIVO-3481 fixed 'create classgroups automatically' option --- .../controller/jena/RDFUploadController.java | 458 ++++++------- .../vitro/webapp/dao/jena/JenaModelUtils.java | 616 +++++++++--------- 2 files changed, 557 insertions(+), 517 deletions(-) diff --git a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/jena/RDFUploadController.java b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/jena/RDFUploadController.java index 46abde934..4a988732b 100644 --- a/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/jena/RDFUploadController.java +++ b/webapp/src/edu/cornell/mannlib/vitro/webapp/controller/jena/RDFUploadController.java @@ -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.dao.WebappDaoFactory; 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.VitroJenaSpecialModelMaker; import edu.cornell.mannlib.vitro.webapp.dao.jena.event.BulkUpdateEvent; @@ -41,156 +42,153 @@ import edu.cornell.mannlib.vitro.webapp.dao.jena.event.EditEvent; import edu.cornell.mannlib.vitro.webapp.filestorage.uploadrequest.FileUploadServletRequest; public class RDFUploadController extends BaseEditController { - + private static int maxFileSizeInBytes = 1024 * 1024 * 2000; //2000mb private static FileItem fileStream = null; private static final String INGEST_MENU_JSP = "/jenaIngest/ingestMenu.jsp"; private static final String LOAD_RDF_DATA_JSP="/jenaIngest/loadRDFData.jsp"; private static final String LIST_MODELS_JSP = "/jenaIngest/listModels.jsp"; - - public void doPost(HttpServletRequest rawRequest, - HttpServletResponse response) throws ServletException, IOException { + + public void doPost(HttpServletRequest rawRequest, + HttpServletResponse response) throws ServletException, IOException { if (!isAuthorizedToDisplayPage(rawRequest, response, new Actions( - new UseAdvancedDataToolsPages()))) { - return; + new UseAdvancedDataToolsPages()))) { + return; } - FileUploadServletRequest req = FileUploadServletRequest.parseRequest( + FileUploadServletRequest req = FileUploadServletRequest.parseRequest( rawRequest, maxFileSizeInBytes); - if (req.hasFileUploadException()) { - forwardToFileUploadError( - req.getFileUploadException().getLocalizedMessage(), + if (req.hasFileUploadException()) { + forwardToFileUploadError( + req.getFileUploadException().getLocalizedMessage(), req, response); - return; - } + return; + } - Map> fileStreams = req.getFiles(); - - VitroRequest request = new VitroRequest(req); - LoginStatusBean loginBean = LoginStatusBean.getBean(request); - - String modelName = req.getParameter("modelName"); - if(modelName!=null){ - loadRDF(req,request,response); - return; - } - - boolean remove = "remove".equals(request.getParameter("mode")); - String verb = remove?"Removed":"Added"; - - String languageStr = request.getParameter("language"); - - boolean makeClassgroups = - (request.getParameter("makeClassgroups") != null); - - // add directly to the ABox model without reading first into - // a temporary in-memory model - boolean directRead = ("directAddABox".equals(request.getParameter( + Map> fileStreams = req.getFiles(); + + VitroRequest request = new VitroRequest(req); + LoginStatusBean loginBean = LoginStatusBean.getBean(request); + + String modelName = req.getParameter("modelName"); + if(modelName!=null){ + loadRDF(req,request,response); + return; + } + + boolean remove = "remove".equals(request.getParameter("mode")); + String verb = remove?"Removed":"Added"; + + String languageStr = request.getParameter("language"); + + boolean makeClassgroups = ("true".equals(request.getParameter( + "makeClassgroups"))); + + // add directly to the ABox model without reading first into + // a temporary in-memory model + boolean directRead = ("directAddABox".equals(request.getParameter( "mode"))); - String uploadDesc =""; - - OntModel uploadModel = (directRead) - ? getABoxModel(request.getSession(), getServletContext()) - : ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); - - /* ********************* GET RDF by URL ********************** */ - String RDFUrlStr = request.getParameter("rdfUrl"); - if (RDFUrlStr != null && RDFUrlStr.length() > 0) { - try { - uploadModel.enterCriticalSection(Lock.WRITE); - try { - uploadModel.read(RDFUrlStr, languageStr); - // languageStr may be null and default would be RDF/XML - } finally { - uploadModel.leaveCriticalSection(); - } - uploadDesc = verb + " RDF from " + RDFUrlStr; - } catch (JenaException ex){ - forwardToFileUploadError("Could not parse file to " + + String uploadDesc =""; + + OntModel uploadModel = (directRead) + ? getABoxModel(request.getSession(), getServletContext()) + : ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); + + /* ********************* GET RDF by URL ********************** */ + String RDFUrlStr = request.getParameter("rdfUrl"); + if (RDFUrlStr != null && RDFUrlStr.length() > 0) { + try { + uploadModel.enterCriticalSection(Lock.WRITE); + try { + uploadModel.read(RDFUrlStr, languageStr); + // languageStr may be null and default would be RDF/XML + } finally { + uploadModel.leaveCriticalSection(); + } + uploadDesc = verb + " RDF from " + RDFUrlStr; + } catch (JenaException ex){ + forwardToFileUploadError("Could not parse file to " + languageStr + ": " + ex.getMessage(), req, response); - return; - }catch (Exception e) { + return; + }catch (Exception e) { forwardToFileUploadError("Could not load from URL: " + - e.getMessage(), req, response); + e.getMessage(), req, response); return; - } - } else { - /* **************** upload RDF from POST ********************* */ - if( fileStreams.get("rdfStream") != null - && fileStreams.get("rdfStream").size() > 0 ) { - FileItem rdfStream = fileStreams.get("rdfStream").get(0); - try { - uploadModel.enterCriticalSection(Lock.WRITE); - try { - uploadModel.read( - rdfStream.getInputStream(), null, languageStr); - } finally { - uploadModel.leaveCriticalSection(); - } - uploadDesc = verb + " RDF from file " + rdfStream.getName(); - } catch (IOException e) { - forwardToFileUploadError("Could not read file: " + - e.getLocalizedMessage(), req, response); - return; - }catch (JenaException ex){ - forwardToFileUploadError("Could not parse file to " + - languageStr + ": " + ex.getMessage(), - req, response); - return; - }catch (Exception e) { - forwardToFileUploadError("Could not load from file: " + - e.getMessage(), req, response); - return; - }finally{ - rdfStream.delete(); - } - } - } - - /* ********** Do the model changes *********** */ - if( !directRead && uploadModel != null ){ - long tboxstmtCount = 0L; - long aboxstmtCount = 0L; + } + } else { + /* **************** upload RDF from POST ********************* */ + if( fileStreams.get("rdfStream") != null + && fileStreams.get("rdfStream").size() > 0 ) { + FileItem rdfStream = fileStreams.get("rdfStream").get(0); + try { + uploadModel.enterCriticalSection(Lock.WRITE); + try { + uploadModel.read( + rdfStream.getInputStream(), null, languageStr); + } finally { + uploadModel.leaveCriticalSection(); + } + uploadDesc = verb + " RDF from file " + rdfStream.getName(); + } catch (IOException e) { + forwardToFileUploadError("Could not read file: " + + e.getLocalizedMessage(), req, response); + return; + }catch (JenaException ex){ + forwardToFileUploadError("Could not parse file to " + + languageStr + ": " + ex.getMessage(), + req, response); + return; + }catch (Exception e) { + forwardToFileUploadError("Could not load from file: " + + e.getMessage(), req, response); + return; + }finally{ + rdfStream.delete(); + } + } + } + + /* ********** Do the model changes *********** */ + if( !directRead && uploadModel != null ){ + long tboxstmtCount = 0L; + long aboxstmtCount = 0L; - JenaModelUtils xutil = new JenaModelUtils(); - OntModel tboxModel = getTBoxModel( - request.getSession(), getServletContext()); - OntModel aboxModel = getABoxModel( - request.getSession(), getServletContext()); - OntModel tboxChangeModel=null; - Model aboxChangeModel=null; - if (tboxModel != null) { - boolean AGGRESSIVE = true; - tboxChangeModel = xutil.extractTBox(uploadModel, AGGRESSIVE); - // aggressively seek all statements that are part of the TBox - tboxstmtCount = operateOnModel( - request.getFullWebappDaoFactory(), - tboxModel,tboxChangeModel, - remove, - makeClassgroups, - loginBean.getUserURI()); - } - if (aboxModel != null) { - aboxChangeModel = uploadModel.remove(tboxChangeModel); - aboxstmtCount = operateOnModel( - request.getFullWebappDaoFactory(), - aboxModel, - aboxChangeModel, - remove, - makeClassgroups, - loginBean.getUserURI()); - } - request.setAttribute("uploadDesc", uploadDesc + ". " + verb + " " + + JenaModelUtils xutil = new JenaModelUtils(); + + OntModel tboxModel = getTBoxModel( + request.getSession(), getServletContext()); + OntModel aboxModel = getABoxModel( + request.getSession(), getServletContext()); + OntModel tboxChangeModel = null; + Model aboxChangeModel = null; + OntModelSelector ontModelSelector = ModelContext.getOntModelSelector( + getServletContext()); + + if (tboxModel != null) { + boolean AGGRESSIVE = true; + tboxChangeModel = xutil.extractTBox(uploadModel, AGGRESSIVE); + // aggressively seek all statements that are part of the TBox + tboxstmtCount = operateOnModel(request.getFullWebappDaoFactory(), + tboxModel, tboxChangeModel, ontModelSelector, + remove, makeClassgroups, loginBean.getUserURI()); + } + if (aboxModel != null) { + aboxChangeModel = uploadModel.remove(tboxChangeModel); + aboxstmtCount = operateOnModel(request.getFullWebappDaoFactory(), + aboxModel, aboxChangeModel, ontModelSelector, + remove, makeClassgroups, loginBean.getUserURI()); + } + request.setAttribute("uploadDesc", uploadDesc + ". " + verb + " " + (tboxstmtCount + aboxstmtCount) + " statements."); - } else { - request.setAttribute("uploadDesc", "RDF upload successful."); - } - + } else { + request.setAttribute("uploadDesc", "RDF upload successful."); + } + RequestDispatcher rd = request.getRequestDispatcher( - Controllers.BASIC_JSP); + Controllers.BASIC_JSP); request.setAttribute( - "bodyJsp", "/templates/edit/specific/upload_rdf_result.jsp"); + "bodyJsp", "/templates/edit/specific/upload_rdf_result.jsp"); request.setAttribute("title","Ingest RDF Data"); try { @@ -200,31 +198,31 @@ public class RDFUploadController extends BaseEditController { } } - public void loadRDF(FileUploadServletRequest req, + public void loadRDF(FileUploadServletRequest req, VitroRequest request, HttpServletResponse response) throws ServletException, IOException { - Map> fileStreams = req.getFiles(); - String filePath = fileStreams.get("filePath").get(0).getName(); - fileStream = fileStreams.get("filePath").get(0); - String modelName = req.getParameter("modelName"); - String docLoc = req.getParameter("docLoc"); - String languageStr = request.getParameter("language"); - ModelMaker maker = getVitroJenaModelMaker(request); - - if (docLoc!=null && modelName != null) { - doLoadRDFData(modelName,docLoc,filePath,languageStr,maker); - //request.setAttribute("title","Ingest Menu"); - //request.setAttribute("bodyJsp",INGEST_MENU_JSP); - request.setAttribute("title","Available Models"); - request.setAttribute("bodyJsp",LIST_MODELS_JSP); - } else { - request.setAttribute("title","Load RDF Data"); - request.setAttribute("bodyJsp",LOAD_RDF_DATA_JSP); - } - - RequestDispatcher rd = request.getRequestDispatcher( - Controllers.BASIC_JSP); + Map> fileStreams = req.getFiles(); + String filePath = fileStreams.get("filePath").get(0).getName(); + fileStream = fileStreams.get("filePath").get(0); + String modelName = req.getParameter("modelName"); + String docLoc = req.getParameter("docLoc"); + String languageStr = request.getParameter("language"); + ModelMaker maker = getVitroJenaModelMaker(request); + + if (docLoc!=null && modelName != null) { + doLoadRDFData(modelName,docLoc,filePath,languageStr,maker); + //request.setAttribute("title","Ingest Menu"); + //request.setAttribute("bodyJsp",INGEST_MENU_JSP); + request.setAttribute("title","Available Models"); + request.setAttribute("bodyJsp",LIST_MODELS_JSP); + } else { + request.setAttribute("title","Load RDF Data"); + request.setAttribute("bodyJsp",LOAD_RDF_DATA_JSP); + } + + RequestDispatcher rd = request.getRequestDispatcher( + Controllers.BASIC_JSP); try { rd.forward(request, response); @@ -233,40 +231,54 @@ public class RDFUploadController extends BaseEditController { log.error(errMsg, e); throw new ServletException(errMsg, e); } - - } - + + } + private long operateOnModel(WebappDaoFactory webappDaoFactory, - OntModel mainModel, - Model changesModel, - boolean remove, - boolean makeClassgroups, - String userURI) { + OntModel mainModel, + Model changesModel, + OntModelSelector ontModelSelector, + boolean remove, + boolean makeClassgroups, + String userURI) { + + EditEvent startEvent = null, endEvent = null; + + if (remove) { + startEvent = new BulkUpdateEvent(userURI, true); + endEvent = new BulkUpdateEvent(userURI, false); + } else { + startEvent = new EditEvent(userURI, true); + 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 { - - EditEvent startEvent = null, endEvent = null; - - if (remove) { - startEvent = new BulkUpdateEvent(userURI, true); - endEvent = new BulkUpdateEvent(userURI, false); - } else { - startEvent = new EditEvent(userURI, true); - endEvent = new EditEvent(userURI, false); - } - + mainModel.getBaseModel().notifyEvent(startEvent); - try { - if (makeClassgroups) { - Model classgroupModel = - JenaModelUtils.makeClassGroupsFromRootClasses( - webappDaoFactory, changesModel, changesModel); - mainModel.add(classgroupModel); - } + try { if (remove) { mainModel.remove(changesModel); } else { mainModel.add(changesModel); + if (classgroupModel != null) { + mainModel.add(classgroupModel[1]); + } } } finally { mainModel.getBaseModel().notifyEvent(endEvent); @@ -279,45 +291,45 @@ public class RDFUploadController extends BaseEditController { private void doLoadRDFData(String modelName, String docLoc, - String filePath, - String language, - ModelMaker modelMaker) { - Model m = modelMaker.getModel(modelName); - m.enterCriticalSection(Lock.WRITE); - try { - if ( (docLoc != null) && (docLoc.length()>0) ) { - m.read(docLoc, language); - } else if ( (filePath != null) && (filePath.length()>0) ) { - File file = new File(filePath); - File[] files; - if (file.isDirectory()) { - files = file.listFiles(); - } else { - files = new File[1]; - files[0] = file; - } - for (int i=0; i0) ) { + m.read(docLoc, language); + } else if ( (filePath != null) && (filePath.length()>0) ) { + File file = new File(filePath); + File[] files; + if (file.isDirectory()) { + files = file.listFiles(); + } else { + files = new File[1]; + files[0] = file; + } + for (int i=0; i nonIndividualTypeURIs ; - - static { - nonIndividualTypeURIs = new HashSet(); - nonIndividualTypeURIs.add(OWL.Class.getURI()); - nonIndividualTypeURIs.add(OWL.Restriction.getURI()); - nonIndividualTypeURIs.add(OWL.ObjectProperty.getURI()); - nonIndividualTypeURIs.add(OWL.DatatypeProperty.getURI()); - nonIndividualTypeURIs.add(OWL.AnnotationProperty.getURI()); - nonIndividualTypeURIs.add(OWL.Ontology.getURI()); - nonIndividualTypeURIs.add(RDFS.Class.getURI()); - nonIndividualTypeURIs.add(RDF.Property.getURI()); - } - - public synchronized static void makeClassGroupsFromRootClasses(WebappDaoFactory wadf, Model ontModel) { - makeClassGroupsFromRootClasses(wadf, ontModel, ontModel); - } - - public synchronized static OntModel makeClassGroupsFromRootClasses(WebappDaoFactory wadf, Model baseModel, Model vitroInternalsSubmodel) { - OntModel ontModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM,baseModel); - OntModel modelForClassgroups = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); - SimpleOntModelSelector oms = new SimpleOntModelSelector(); - oms.setTBoxModel(ontModel); - oms.setApplicationMetadataModel(modelForClassgroups); - WebappDaoFactoryConfig config = new WebappDaoFactoryConfig(); - config.setDefaultNamespace(wadf.getDefaultNamespace()); - WebappDaoFactory myWebappDaoFactory = new WebappDaoFactoryJena( - new SimpleOntModelSelector(ontModel), config, null); - OntModel tempModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_DL_MEM); - Resource classGroupClass = ResourceFactory.createResource(VitroVocabulary.CLASSGROUP); - Property inClassGroupProperty = ResourceFactory.createProperty(VitroVocabulary.IN_CLASSGROUP); - ontModel.enterCriticalSection(Lock.READ); - try { - try { - for (Iterator rootClassIt = myWebappDaoFactory.getVClassDao().getRootClasses().iterator(); rootClassIt.hasNext(); ) { - 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); - classGroup.setLabel(rootClass.getName(),null); - OntClass rootClassOntClass = ontModel.getOntClass(rootClass.getURI()); - tempModel.add(rootClassOntClass, inClassGroupProperty, classGroup); - for (Iterator childIt = myWebappDaoFactory.getVClassDao().getAllSubClassURIs(rootClass.getURI()).iterator(); childIt.hasNext(); ) { - String childURI = (String) childIt.next(); - OntClass childClass = ontModel.getOntClass(childURI); - childClass.addProperty(inClassGroupProperty, classGroup); - } - } - } catch (Exception e) { - log.error("Unable to create class groups automatically based on class hierarchy"); - } - vitroInternalsSubmodel.enterCriticalSection(Lock.WRITE); - try { - vitroInternalsSubmodel.add(tempModel); - } finally { - vitroInternalsSubmodel.leaveCriticalSection(); - } - } finally { - ontModel.leaveCriticalSection(); - } - return modelForClassgroups; - } - - private final OntModelSpec DEFAULT_ONT_MODEL_SPEC = OntModelSpec.OWL_MEM; - private final boolean NORMAL = false; - private final boolean AGGRESSIVE = true; + private static final Set nonIndividualTypeURIs ; + + static { + nonIndividualTypeURIs = new HashSet(); + nonIndividualTypeURIs.add(OWL.Class.getURI()); + nonIndividualTypeURIs.add(OWL.Restriction.getURI()); + nonIndividualTypeURIs.add(OWL.ObjectProperty.getURI()); + nonIndividualTypeURIs.add(OWL.DatatypeProperty.getURI()); + nonIndividualTypeURIs.add(OWL.AnnotationProperty.getURI()); + nonIndividualTypeURIs.add(OWL.Ontology.getURI()); + nonIndividualTypeURIs.add(RDFS.Class.getURI()); + nonIndividualTypeURIs.add(RDF.Property.getURI()); + } + + /** + * 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) { + + OntModel ontModel = ModelFactory.createOntologyModel( + OntModelSpec.OWL_DL_MEM, tboxModel); + OntModel modelForClassgroups = ModelFactory.createOntologyModel( + OntModelSpec.OWL_DL_MEM); + OntModel modelForClassgroupAnnotations = ModelFactory.createOntologyModel( + OntModelSpec.OWL_DL_MEM); + SimpleOntModelSelector oms = new SimpleOntModelSelector(); + oms.setTBoxModel(ontModel); + oms.setApplicationMetadataModel(modelForClassgroups); + WebappDaoFactoryConfig config = new WebappDaoFactoryConfig(); + config.setDefaultNamespace(wadf.getDefaultNamespace()); + WebappDaoFactory myWebappDaoFactory = new WebappDaoFactoryJena( + new SimpleOntModelSelector(ontModel), config, null); + + Resource classGroupClass = ResourceFactory.createResource( + VitroVocabulary.CLASSGROUP); + Property inClassGroupProperty = ResourceFactory.createProperty( + VitroVocabulary.IN_CLASSGROUP); + + ontModel.enterCriticalSection(Lock.READ); + try { + try { + for (Iterator rootClassIt = myWebappDaoFactory.getVClassDao() + .getRootClasses().iterator(); rootClassIt.hasNext(); ) { + VClass rootClass = (VClass) rootClassIt.next(); + + Individual classGroup = modelForClassgroups.createIndividual( + wadf.getDefaultNamespace() + "vitroClassGroup" + + rootClass.getLocalName(), classGroupClass); + classGroup.setLabel(rootClass.getName(), null); + + Resource rootClassRes = modelForClassgroupAnnotations.getResource( + rootClass.getURI()); + modelForClassgroupAnnotations.add( + rootClassRes, inClassGroupProperty, classGroup); + for (Iterator childIt = myWebappDaoFactory.getVClassDao() + .getAllSubClassURIs(rootClass.getURI()).iterator(); + childIt.hasNext(); ) { + String childURI = (String) childIt.next(); + Resource childClass = modelForClassgroupAnnotations + .getResource(childURI); + if (!modelForClassgroupAnnotations.contains( + childClass, inClassGroupProperty, (RDFNode) null)) { + childClass.addProperty(inClassGroupProperty, classGroup); + } + } + } + } catch (Exception e) { + String errMsg = "Unable to create class groups automatically " + + "based on class hierarchy"; + log.error(errMsg, e); + throw new RuntimeException(errMsg, e); + } + } finally { + ontModel.leaveCriticalSection(); + } + 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 boolean NORMAL = false; + private final boolean AGGRESSIVE = true; - public OntModel extractTBox( Model inputModel ) { - return extractTBox(inputModel, null); - } - - public OntModel extractTBox( Model inputModel, boolean MODE ) { - Dataset dataset = DatasetFactory.create(inputModel); + public OntModel extractTBox( Model inputModel ) { + return extractTBox(inputModel, null); + } + + public OntModel extractTBox( Model inputModel, boolean MODE ) { + Dataset dataset = DatasetFactory.create(inputModel); return extractTBox(dataset, null, null, MODE); } - - public OntModel extractTBox( Model inputModel, String namespace ) { - Dataset dataset = DatasetFactory.create(inputModel); + + public OntModel extractTBox( Model inputModel, String namespace ) { + Dataset dataset = DatasetFactory.create(inputModel); return extractTBox( dataset, namespace, null, NORMAL ); - } - - public OntModel extractTBox( Dataset dataset, String namespace, String graphURI) { - return extractTBox( dataset, namespace, graphURI, NORMAL); - } - - public OntModel extractTBox( Dataset dataset, String namespace, String graphURI, boolean mode ) { - OntModel tboxModel = ModelFactory.createOntologyModel(DEFAULT_ONT_MODEL_SPEC); - - List queryStrList = new LinkedList(); - - // Use SPARQL DESCRIBE queries to extract the RDF for named ontology entities - - queryStrList.add( makeDescribeQueryStr( OWL.Class.getURI(), namespace, graphURI ) ); - queryStrList.add( makeDescribeQueryStr( OWL.Restriction.getURI(), namespace, graphURI ) ); - queryStrList.add( makeDescribeQueryStr( OWL.ObjectProperty.getURI(), namespace, graphURI ) ); - queryStrList.add( makeDescribeQueryStr( OWL.DatatypeProperty.getURI(), namespace, graphURI ) ); - queryStrList.add( makeDescribeQueryStr( OWL.AnnotationProperty.getURI(), namespace, graphURI ) ); - // if we're using to a hash namespace, the URI of the Ontology resource will be - // that namespace minus the final hash mark. - if ( namespace != null && namespace.endsWith("#") ) { - queryStrList.add( makeDescribeQueryStr( OWL.Ontology.getURI(), namespace.substring(0,namespace.length()-1), graphURI ) ); - } else { - queryStrList.add( makeDescribeQueryStr( OWL.Ontology.getURI(), namespace, graphURI ) ); - } - - // Perform the SPARQL DESCRIBEs - for ( String queryStr : queryStrList ) { - Query tboxSparqlQuery = QueryFactory.create(queryStr); - QueryExecution qe = QueryExecutionFactory.create(tboxSparqlQuery,dataset); - try { - dataset.getLock().enterCriticalSection(Lock.READ); - qe.execDescribe(tboxModel); - } finally { - dataset.getLock().leaveCriticalSection(); - } - } - - // Perform possibly-redundant extraction to try ensure we don't miss - // individual axioms floating around. We still might miss things; - // this approach isn't perfect. - if (mode = AGGRESSIVE) { - tboxModel.add(construct(dataset, namespace, graphURI, RDFS.subClassOf)); - tboxModel.add(construct(dataset, namespace, graphURI, RDFS.subPropertyOf)); - tboxModel.add(construct(dataset, namespace, graphURI, OWL.equivalentClass)); - tboxModel.add(construct(dataset, namespace, graphURI, OWL.unionOf)); - tboxModel.add(construct(dataset, namespace, graphURI, OWL.intersectionOf)); - tboxModel.add(construct(dataset, namespace, graphURI, OWL.complementOf)); - tboxModel.add(construct(dataset, namespace, graphURI, OWL.onProperty)); - tboxModel.add(construct(dataset, namespace, graphURI, OWL.allValuesFrom)); - tboxModel.add(construct(dataset, namespace, graphURI, OWL.someValuesFrom)); - tboxModel.add(construct(dataset, namespace, graphURI, OWL.hasValue)); - tboxModel.add(construct(dataset, namespace, graphURI, OWL.minCardinality)); - tboxModel.add(construct(dataset, namespace, graphURI, OWL.maxCardinality)); - tboxModel.add(construct(dataset, namespace, graphURI, OWL.cardinality)); - tboxModel.add(construct(dataset, namespace, graphURI, OWL.disjointWith)); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( - VitroVocabulary.DISPLAY_LIMIT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + } + + public OntModel extractTBox( Dataset dataset, String namespace, String graphURI) { + return extractTBox( dataset, namespace, graphURI, NORMAL); + } + + public OntModel extractTBox( Dataset dataset, String namespace, String graphURI, boolean mode ) { + OntModel tboxModel = ModelFactory.createOntologyModel(DEFAULT_ONT_MODEL_SPEC); + + List queryStrList = new LinkedList(); + + // Use SPARQL DESCRIBE queries to extract the RDF for named ontology entities + + queryStrList.add( makeDescribeQueryStr( OWL.Class.getURI(), namespace, graphURI ) ); + queryStrList.add( makeDescribeQueryStr( OWL.Restriction.getURI(), namespace, graphURI ) ); + queryStrList.add( makeDescribeQueryStr( OWL.ObjectProperty.getURI(), namespace, graphURI ) ); + queryStrList.add( makeDescribeQueryStr( OWL.DatatypeProperty.getURI(), namespace, graphURI ) ); + queryStrList.add( makeDescribeQueryStr( OWL.AnnotationProperty.getURI(), namespace, graphURI ) ); + // if we're using to a hash namespace, the URI of the Ontology resource will be + // that namespace minus the final hash mark. + if ( namespace != null && namespace.endsWith("#") ) { + queryStrList.add( makeDescribeQueryStr( OWL.Ontology.getURI(), namespace.substring(0,namespace.length()-1), graphURI ) ); + } else { + queryStrList.add( makeDescribeQueryStr( OWL.Ontology.getURI(), namespace, graphURI ) ); + } + + // Perform the SPARQL DESCRIBEs + for ( String queryStr : queryStrList ) { + Query tboxSparqlQuery = QueryFactory.create(queryStr); + QueryExecution qe = QueryExecutionFactory.create(tboxSparqlQuery,dataset); + try { + dataset.getLock().enterCriticalSection(Lock.READ); + qe.execDescribe(tboxModel); + } finally { + dataset.getLock().leaveCriticalSection(); + } + } + + // Perform possibly-redundant extraction to try ensure we don't miss + // individual axioms floating around. We still might miss things; + // this approach isn't perfect. + if (mode = AGGRESSIVE) { + tboxModel.add(construct(dataset, namespace, graphURI, RDFS.subClassOf)); + tboxModel.add(construct(dataset, namespace, graphURI, RDFS.subPropertyOf)); + tboxModel.add(construct(dataset, namespace, graphURI, OWL.equivalentClass)); + tboxModel.add(construct(dataset, namespace, graphURI, OWL.unionOf)); + tboxModel.add(construct(dataset, namespace, graphURI, OWL.intersectionOf)); + tboxModel.add(construct(dataset, namespace, graphURI, OWL.complementOf)); + tboxModel.add(construct(dataset, namespace, graphURI, OWL.onProperty)); + tboxModel.add(construct(dataset, namespace, graphURI, OWL.allValuesFrom)); + tboxModel.add(construct(dataset, namespace, graphURI, OWL.someValuesFrom)); + tboxModel.add(construct(dataset, namespace, graphURI, OWL.hasValue)); + tboxModel.add(construct(dataset, namespace, graphURI, OWL.minCardinality)); + tboxModel.add(construct(dataset, namespace, graphURI, OWL.maxCardinality)); + tboxModel.add(construct(dataset, namespace, graphURI, OWL.cardinality)); + tboxModel.add(construct(dataset, namespace, graphURI, OWL.disjointWith)); + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + VitroVocabulary.DISPLAY_LIMIT))); + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.DISPLAY_RANK_ANNOT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.IN_CLASSGROUP))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.IN_CLASSGROUP))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.PROPERTY_INPROPERTYGROUPANNOT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.PROHIBITED_FROM_UPDATE_BELOW_ROLE_LEVEL_ANNOT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.HIDDEN_FROM_DISPLAY_BELOW_ROLE_LEVEL_ANNOT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.DESCRIPTION_ANNOT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.SHORTDEF))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.EXAMPLE_ANNOT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.vitroURI + "extendedLinkedData"))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.PROPERTY_OFFERCREATENEWOPTIONANNOT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.PROPERTY_COLLATEBYSUBCLASSANNOT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.PROPERTY_CUSTOM_LIST_VIEW_ANNOT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.PROPERTY_CUSTOMDISPLAYVIEWANNOT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.PROPERTY_CUSTOMENTRYFORMANNOT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.PROPERTY_CUSTOMSEARCHVIEWANNOT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.PROPERTY_CUSTOMSHORTVIEWANNOT))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.PROPERTY_ENTITYSORTDIRECTION))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.PROPERTY_ENTITYSORTFIELD))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.PROPERTY_OBJECTINDIVIDUALSORTPROPERTY))); - tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( + tboxModel.add(construct(dataset, namespace, graphURI, ResourceFactory.createResource( VitroVocabulary.PROPERTY_SELECTFROMEXISTINGANNOT))); - } - return tboxModel; - } - - private Model construct(Dataset dataset, - String namespace, - String graphURI, - Resource property) { - dataset.getLock().enterCriticalSection(Lock.READ); - try { - StringBuffer buff = new StringBuffer(); - buff.append("PREFIX afn: \n") - .append("CONSTRUCT { \n") - .append(" ?res <" + property.getURI() + "> ?o } WHERE { \n"); - if (graphURI != null) { - buff.append(" GRAPH " + graphURI + " { \n"); - } - buff.append(" ?res <" + property.getURI() + "> ?o \n"); - buff.append(getNamespaceFilter(namespace)); - if (graphURI != null) { - buff.append(" } \n"); - } - buff.append("}"); - Query constructProp = QueryFactory.create(buff.toString()); - QueryExecution qe = QueryExecutionFactory.create(constructProp, dataset); - try { - return qe.execConstruct(); - } finally { - qe.close(); - } - } finally { - dataset.getLock().leaveCriticalSection(); - } - } - - private String makeDescribeQueryStr( String typeURI, String namespace ) { - return makeDescribeQueryStr( typeURI, namespace, null ); - } - - private String makeDescribeQueryStr( String typeURI, String namespace, String graphURI ) { - - StringBuffer describeQueryStrBuff = new StringBuffer() - .append("PREFIX rdf: \n") - .append("PREFIX afn: \n") - .append("DESCRIBE ?res WHERE { \n"); - if (graphURI != null) { - describeQueryStrBuff - .append("GRAPH " + graphURI + "{ \n"); - } - describeQueryStrBuff - .append(" ?res rdf:type <").append(typeURI).append("> . \n"); - - describeQueryStrBuff - .append(" FILTER (!isBlank(?res)) \n") - - .append(getNamespaceFilter(namespace)); - - if (graphURI != null) { - describeQueryStrBuff - .append("} \n"); - } - - describeQueryStrBuff.append("} \n"); - - return describeQueryStrBuff.toString(); - - } - - private String getNamespaceFilter(String namespace) { - StringBuffer buff = new StringBuffer(); - if (namespace == null) { + } + return tboxModel; + } + + private Model construct(Dataset dataset, + String namespace, + String graphURI, + Resource property) { + dataset.getLock().enterCriticalSection(Lock.READ); + try { + StringBuffer buff = new StringBuffer(); + buff.append("PREFIX afn: \n") + .append("CONSTRUCT { \n") + .append(" ?res <" + property.getURI() + "> ?o } WHERE { \n"); + if (graphURI != null) { + buff.append(" GRAPH " + graphURI + " { \n"); + } + buff.append(" ?res <" + property.getURI() + "> ?o \n"); + buff.append(getNamespaceFilter(namespace)); + if (graphURI != null) { + buff.append(" } \n"); + } + buff.append("}"); + Query constructProp = QueryFactory.create(buff.toString()); + QueryExecution qe = QueryExecutionFactory.create(constructProp, dataset); + try { + return qe.execConstruct(); + } finally { + qe.close(); + } + } finally { + dataset.getLock().leaveCriticalSection(); + } + } + + private String makeDescribeQueryStr( String typeURI, String namespace ) { + return makeDescribeQueryStr( typeURI, namespace, null ); + } + + private String makeDescribeQueryStr( String typeURI, String namespace, String graphURI ) { + + StringBuffer describeQueryStrBuff = new StringBuffer() + .append("PREFIX rdf: \n") + .append("PREFIX afn: \n") + .append("DESCRIBE ?res WHERE { \n"); + if (graphURI != null) { + describeQueryStrBuff + .append("GRAPH " + graphURI + "{ \n"); + } + describeQueryStrBuff + .append(" ?res rdf:type <").append(typeURI).append("> . \n"); + + describeQueryStrBuff + .append(" FILTER (!isBlank(?res)) \n") + + .append(getNamespaceFilter(namespace)); + + if (graphURI != null) { + describeQueryStrBuff + .append("} \n"); + } + + describeQueryStrBuff.append("} \n"); + + return describeQueryStrBuff.toString(); + + } + + private String getNamespaceFilter(String namespace) { + StringBuffer buff = new StringBuffer(); + if (namespace == null) { // exclude resources in the Vitro internal namespace or in the // OWL namespace, but allow all others buff @@ -307,73 +335,73 @@ public class JenaModelUtils { .append(namespace) .append("\")) \n"); } - return buff.toString(); - } - - public Model extractABox(Model inputModel){ - Dataset dataset = DatasetFactory.create(inputModel); - return extractABox(dataset, null, null); - } - - public Model extractABox( Dataset unionDataset, Dataset baseOrInfDataset, String graphURI ) { - - Model aboxModel = ModelFactory.createDefaultModel(); + return buff.toString(); + } + + public Model extractABox(Model inputModel){ + Dataset dataset = DatasetFactory.create(inputModel); + return extractABox(dataset, null, null); + } + + public Model extractABox( Dataset unionDataset, Dataset baseOrInfDataset, String graphURI ) { + + Model aboxModel = ModelFactory.createDefaultModel(); - // iterate through all classes and DESCRIBE each of their instances - // Note that this could be simplified if we knew that the model was a - // reasoning model: we could then simply describe all instances of - // owl:Thing. + // iterate through all classes and DESCRIBE each of their instances + // Note that this could be simplified if we knew that the model was a + // reasoning model: we could then simply describe all instances of + // owl:Thing. - //OntModel ontModel = ( inputModel instanceof OntModel ) - //? (OntModel)inputModel - //: ModelFactory.createOntologyModel( DEFAULT_ONT_MODEL_SPEC, inputModel ); - OntModel ontModel = extractTBox(unionDataset, null, graphURI); - - try { - ontModel.enterCriticalSection(Lock.READ); - Iterator classIt = ontModel.listNamedClasses(); - QueryExecution qe = null; - while ( classIt.hasNext() ) { - - OntClass ontClass = (OntClass) classIt.next(); - //if ( !(ontClass.getNameSpace().startsWith(OWL.getURI()) ) - // && !(ontClass.getNameSpace().startsWith(VitroVocabulary.vitroURI)) ) { - if(!(ontClass.getNameSpace().startsWith(OWL.getURI()))){ - - String queryStr = makeDescribeQueryStr( ontClass.getURI(), null, graphURI ); - - Query aboxSparqlQuery = QueryFactory.create(queryStr); - if(baseOrInfDataset != null){ - qe = QueryExecutionFactory.create(aboxSparqlQuery,baseOrInfDataset); - } - else{ - qe = QueryExecutionFactory.create(aboxSparqlQuery,unionDataset); - } - if(baseOrInfDataset != null){ - try { - baseOrInfDataset.getLock().enterCriticalSection(Lock.READ); - qe.execDescribe(aboxModel); // puts the statements about each resource into aboxModel. - } finally { - baseOrInfDataset.getLock().leaveCriticalSection(); - } - } - else{ - try { - unionDataset.getLock().enterCriticalSection(Lock.READ); - qe.execDescribe(aboxModel); // puts the statements about each resource into aboxModel. - } finally { - unionDataset.getLock().leaveCriticalSection(); - } - } - - } - } - } finally { - ontModel.leaveCriticalSection(); - } - - return aboxModel; - - } - + //OntModel ontModel = ( inputModel instanceof OntModel ) + //? (OntModel)inputModel + //: ModelFactory.createOntologyModel( DEFAULT_ONT_MODEL_SPEC, inputModel ); + OntModel ontModel = extractTBox(unionDataset, null, graphURI); + + try { + ontModel.enterCriticalSection(Lock.READ); + Iterator classIt = ontModel.listNamedClasses(); + QueryExecution qe = null; + while ( classIt.hasNext() ) { + + OntClass ontClass = (OntClass) classIt.next(); + //if ( !(ontClass.getNameSpace().startsWith(OWL.getURI()) ) + // && !(ontClass.getNameSpace().startsWith(VitroVocabulary.vitroURI)) ) { + if(!(ontClass.getNameSpace().startsWith(OWL.getURI()))){ + + String queryStr = makeDescribeQueryStr( ontClass.getURI(), null, graphURI ); + + Query aboxSparqlQuery = QueryFactory.create(queryStr); + if(baseOrInfDataset != null){ + qe = QueryExecutionFactory.create(aboxSparqlQuery,baseOrInfDataset); + } + else{ + qe = QueryExecutionFactory.create(aboxSparqlQuery,unionDataset); + } + if(baseOrInfDataset != null){ + try { + baseOrInfDataset.getLock().enterCriticalSection(Lock.READ); + qe.execDescribe(aboxModel); // puts the statements about each resource into aboxModel. + } finally { + baseOrInfDataset.getLock().leaveCriticalSection(); + } + } + else{ + try { + unionDataset.getLock().enterCriticalSection(Lock.READ); + qe.execDescribe(aboxModel); // puts the statements about each resource into aboxModel. + } finally { + unionDataset.getLock().leaveCriticalSection(); + } + } + + } + } + } finally { + ontModel.leaveCriticalSection(); + } + + return aboxModel; + + } + }