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; }
private void addFieldFromRestriction( Map<String, Field> fieldMap, Frame frame, OntClass restriction) { int minCardinality = 0; int maxCardinality = -1; OntResource range = null; Resource resource = restriction.getPropertyResourceValue(OWL2.onProperty); String uri = resource.getURI(); Field priorField = fieldMap.get(uri); if (priorField != null) { maxCardinality = priorField.getMaxCardinality(); } OntProperty property = null; if (restriction.getPropertyResourceValue(OWL2.onProperty).canAs(OntProperty.class)) { property = resource.as(OntProperty.class); } else { property = model.createOntProperty(resource.getURI()); } if (property != null && property.canAs(FunctionalProperty.class)) { maxCardinality = 1; } if (restriction.hasProperty(OWL.minCardinality)) { minCardinality = restriction.getProperty(OWL.minCardinality).getInt(); } if (restriction.hasProperty(OWL.maxCardinality)) { maxCardinality = restriction.getProperty(OWL.maxCardinality).getInt(); } Resource valueType = restriction.getPropertyResourceValue(OWL.allValuesFrom); if (valueType != null) { range = valueType.as(OntResource.class); } Resource hasValue = restriction.getPropertyResourceValue(OWL.hasValue); NamedIndividual individualValue = null; if (hasValue != null && hasValue.getURI() != null) { String individualURI = hasValue.getURI(); individualValue = new NamedIndividual(hasValue.as(OntResource.class)); } // Resource onClass = restriction.getPropertyResourceValue(OWL2.onClass); // if (onClass != null) { // range = onClass.as(OntResource.class); // if (restriction.hasProperty(OWL2.minQualifiedCardinality)) { // minCardinality = restriction.getProperty(OWL2.minQualifiedCardinality).getInt(); // } // if (restriction.hasProperty(OWL2.maxQualifiedCardinality)) { // maxCardinality = restriction.getProperty(OWL2.maxQualifiedCardinality).getInt(); // } // // } else { // if (restriction.hasProperty(OWL.minCardinality)) { // minCardinality = restriction.getProperty(OWL.minCardinality).getInt(); // } // if (restriction.hasProperty(OWL.maxCardinality)) { // maxCardinality = restriction.getProperty(OWL.maxCardinality).getInt(); // } // } if (range == null) { Resource value = property.getPropertyResourceValue(RDFS.range); if (value == null) { logger.warn( "Ignoring field " + resource.getLocalName() + " on class " + frame.getLocalName() + ": the range is not defined."); return; } range = property.getPropertyResourceValue(RDFS.range).as(OntResource.class); } String comment = restriction.getComment(null); if (priorField != null && priorField.getDeclaringFrame() == frame) { priorField.setComment(comment); priorField.setMinCardinality(minCardinality); priorField.setMaxCardinality(maxCardinality); priorField.setValueRestriction(individualValue); return; } Field field = new Field(frame, property, range, minCardinality, maxCardinality); field.setComment(comment); field.setValueRestriction(individualValue); fieldMap.put(uri, field); frame.getDeclaredFields().add(field); }