public Individual fillExistingDataPropertyStatementsForIndividual( Individual entity /*, boolean allowAnyNameSpace*/) { if (entity.getURI() == null) { return entity; } else { OntModel ontModel = getOntModelSelector().getABoxModel(); ontModel.enterCriticalSection(Lock.READ); try { Resource ind = ontModel.getResource(entity.getURI()); List<DataPropertyStatement> edList = new ArrayList<DataPropertyStatement>(); StmtIterator stmtIt = ind.listProperties(); while (stmtIt.hasNext()) { Statement st = (Statement) stmtIt.next(); boolean addToList = /*allowAnyNameSpace ? st.getObject().canAs(Literal.class) :*/ st.getObject().isLiteral() && ((RDF.value.equals(st.getPredicate()) || VitroVocabulary.value.equals(st.getPredicate().getURI())) || !(NONUSER_NAMESPACES.contains(st.getPredicate().getNameSpace()))); if (addToList) { /* now want to expose Cornellemailnetid and potentially other properties so can at least control whether visible boolean isExternalId = false; ClosableIterator externalIdStmtIt = getOntModel().listStatements(st.getPredicate(), DATAPROPERTY_ISEXTERNALID, (Literal)null); try { if (externalIdStmtIt.hasNext()) { isExternalId = true; } } finally { externalIdStmtIt.close(); } if (!isExternalId) { */ DataPropertyStatement ed = new DataPropertyStatementImpl(); Literal lit = (Literal) st.getObject(); fillDataPropertyStatementWithJenaLiteral(ed, lit); ed.setDatapropURI(st.getPredicate().getURI()); ed.setIndividualURI(ind.getURI()); ed.setIndividual(entity); edList.add(ed); /* } */ } } entity.setDataPropertyStatements(edList); return entity; } finally { ontModel.leaveCriticalSection(); } } }
public Collection<PropertyInstance> getAllPropInstByVClasses(List<VClass> vclasses) { List<PropertyInstance> propInsts = new ArrayList<PropertyInstance>(); if (vclasses == null || vclasses.isEmpty()) { return propInsts; } Collections.sort( vclasses, new VClassHierarchyRanker(this.getWebappDaoFactory().getVClassDao())); OntModel ontModel = getOntModelSelector().getTBoxModel(); try { ontModel.enterCriticalSection(Lock.READ); // map object property URI to an array of two resources: // the first is the "allValuesFrom" resource and the second is // "someValuesFrom" Map<String, Resource[]> applicableProperties = new HashMap<String, Resource[]>(); try { for (VClass vclass : vclasses) { if (vclass.isAnonymous()) { continue; } String VClassURI = vclass.getURI(); OntClass ontClass = getOntClass(ontModel, VClassURI); if (ontClass != null) { List<OntClass> relatedClasses = new ArrayList<OntClass>(); relatedClasses.addAll(ontClass.listEquivalentClasses().toList()); relatedClasses.addAll(ontClass.listSuperClasses().toList()); for (OntClass relatedClass : relatedClasses) { // find properties in restrictions if (relatedClass.isRestriction()) { // TODO: check if restriction is something like // maxCardinality 0 or allValuesFrom owl:Nothing, // in which case the property is NOT applicable! Restriction rest = (Restriction) relatedClass.as(Restriction.class); OntProperty onProperty = rest.getOnProperty(); if (onProperty != null && onProperty.canAs(ObjectProperty.class)) { Resource[] ranges = new Resource[2]; if (rest.isAllValuesFromRestriction()) { ranges[0] = (rest.asAllValuesFromRestriction()).getAllValuesFrom(); } else if (rest.isSomeValuesFromRestriction()) { ranges[1] = (rest.asSomeValuesFromRestriction()).getSomeValuesFrom(); } updatePropertyRangeMap(applicableProperties, onProperty.getURI(), ranges); } } } // find properties with class in domain ResIterator pit = ontModel.listSubjectsWithProperty(RDFS.domain, ontClass); while (pit.hasNext()) { Resource prop = pit.nextResource(); if (prop.getNameSpace() != null && !NONUSER_NAMESPACES.contains(prop.getNameSpace())) { StmtIterator rangeSit = prop.listProperties(RDFS.range); Resource rangeRes = null; while (rangeSit.hasNext()) { Statement s = rangeSit.nextStatement(); if (s.getObject().isURIResource()) { rangeRes = (Resource) s.getObject(); } } Resource[] ranges = new Resource[2]; ranges[0] = rangeRes; updatePropertyRangeMap(applicableProperties, prop.getURI(), ranges); } } } } } catch (Exception e) { log.error( "Unable to get applicable properties " + "by examining property restrictions and domains", e); } // make the PropertyInstance objects for (String propertyURI : applicableProperties.keySet()) { ObjectProperty op = ontModel.getObjectProperty(propertyURI); if (op == null) { continue; } String domainURIStr = getURIStr(op.getDomain()); Resource[] foundRanges = applicableProperties.get(propertyURI); Resource rangeRes = (foundRanges[0] != null) ? foundRanges[0] : (op.getRange() == null && foundRanges[1] != null) ? foundRanges[1] : op.getRange(); PropertyInstance pi = new PropertyInstance(); if (rangeRes != null) { String rangeClassURI; if (rangeRes.isAnon()) { rangeClassURI = PSEUDO_BNODE_NS + rangeRes.getId().toString(); } else { rangeClassURI = (String) rangeRes.getURI(); } pi.setRangeClassURI(rangeClassURI); try { pi.setRangeClassName( getWebappDaoFactory().getVClassDao().getVClassByURI(rangeClassURI).getName()); // pi.setRangeClassName(getLabel(getOntModel().getOntResource(rangeClassURI))); } catch (NullPointerException e) { /* probably a union or intersection - need to handle this somehow */ } } else { pi.setRangeClassURI(OWL.Thing.getURI()); // TODO see above } pi.setDomainClassURI(domainURIStr); try { pi.setDomainClassName( getWebappDaoFactory().getVClassDao().getVClassByURI(domainURIStr).getName()); // pi.setDomainClassName(getLabel(getOntModel().getOntResource(op.getDomain().getURI()))); } catch (NullPointerException e) { /* probably a union or intersection - need to handle this somehow */ } pi.setSubjectSide(true); pi.setPropertyURI(op.getURI()); pi.setPropertyName(getLabelOrId(op)); // TODO pi.setRangePublic(getLabelOrId(op)); pi.setDomainPublic(getLabelOrId(op)); propInsts.add(pi); } } finally { ontModel.leaveCriticalSection(); } Collections.sort(propInsts, new PropInstSorter()); return propInsts; }