private boolean isAbstract(OntProperty p) { List<RDFNode> list = p.listPropertyValues(RDF.type).toList(); for (RDFNode node : list) { if (node.canAs(Resource.class)) { Resource r = node.asResource(); if (BindVocabulary.AbstractProperty.getURI().equals(r.getURI())) return true; } } return false; }
private void handleSubpropertyOf(OntProperty p, OntProperty ancestor) { List<RDFNode> list = ancestor.listPropertyValues(RDFS.subPropertyOf).toList(); for (RDFNode node : list) { if (!node.canAs(OntProperty.class)) continue; OntProperty superProperty = node.as(OntProperty.class); if (superProperty.equals(ancestor)) continue; addFields(p, superProperty); handleSubpropertyOf(p, superProperty); } }
/** * Attempts to find the most plausible RDF type for a given property. * * @param property the property to get the type of * @return either owl:DatatypeProperty or owl:ObjectProperty */ private Resource getPropertyType(Resource property) { StmtIterator it = model.listStatements(property, RDFS.range, (RDFNode) null); if (it.hasNext()) { while (it.hasNext()) { Statement s = it.nextStatement(); RDFNode n = s.getObject(); if (n.canAs(Resource.class) && model.contains((Resource) n.as(Resource.class), RDF.type, OWL.Class)) { return OWL.ObjectProperty; } } } return OWL.DatatypeProperty; }
private Field createListField(Frame frame, OntProperty p, OntResource range) { Resource intersection = range.getPropertyResourceValue(OWL.intersectionOf); if (intersection == null) return null; if (intersection.canAs(RDFList.class)) { List<RDFNode> intersectionList = intersection.as(RDFList.class).asJavaList(); for (RDFNode node : intersectionList) { if (node.canAs(OntClass.class)) { OntClass intersectionMember = node.as(OntClass.class); if (RDF.first.equals(intersectionMember.getPropertyResourceValue(OWL.onProperty))) { // The intersectionMember has an owl:onProperty property whose value is rdf:first Resource elementRdfType = intersectionMember.getPropertyResourceValue(OWL.allValuesFrom); if (elementRdfType != null) { String elementTypeURI = elementRdfType.getURI(); if (elementTypeURI != null) { RdfType elementType = manager.getTypeByURI(elementTypeURI); if (elementType != null) { ListType listType = manager.getListTypeByElementUri(elementTypeURI); if (listType == null) { listType = new ListType(manager, intersectionMember, elementType); manager.add(listType); } return new Field(frame, p, listType); } } } } } } } return null; }
private void unifyRDFSVersion(String ns) { for (Iterator it = Jena.cloneIt(model.listStatements()); it.hasNext(); ) { Statement s = (Statement) it.next(); Resource newSubject = s.getSubject(); Property newPredicate = s.getPredicate(); RDFNode newObject = s.getObject(); boolean changed = false; if (ns.equals(newSubject.getNameSpace())) { changed = true; newSubject = model.getResource(RDFS.getURI() + newSubject.getLocalName()); } if (ns.equals(newPredicate.getNameSpace())) { changed = true; newPredicate = model.getProperty(RDFS.getURI() + newPredicate.getLocalName()); } if (newObject.canAs(Resource.class)) { Resource oldResource = (Resource) newObject.as(Resource.class); if (ns.equals(oldResource.getNameSpace())) { changed = true; newObject = model.getResource(RDFS.getURI() + oldResource.getLocalName()); } } if (changed) { model.add(newSubject, newPredicate, newObject); if (log.isLoggable(Level.FINE)) { log.fine( "Replaced deprecated triple " + s + " with " + newSubject + ", " + newPredicate + ", " + newObject); } it.remove(); } } }
private void handlePropertySubclassOf(OntProperty p) { List<RDFNode> list = p.listPropertyValues(RDFS.subClassOf).toList(); for (RDFNode node : list) { if (!node.canAs(Resource.class)) continue; Resource resource = node.asResource(); Resource onPropertyValue = resource.getPropertyResourceValue(OWL.onProperty); if (!RDFS.domain.equals(onPropertyValue)) continue; Resource someValuesFrom = resource.getPropertyResourceValue(OWL.someValuesFrom); if (someValuesFrom == null) continue; String uri = someValuesFrom.getURI(); if (uri != null) { OntResource type = someValuesFrom.as(OntResource.class); addField(type, p, null); } else { Resource unionList = someValuesFrom.getPropertyResourceValue(OWL.unionOf); while (unionList != null) { Resource first = unionList.getPropertyResourceValue(RDF.first); if (first != null) { String typeURI = first.getURI(); if (typeURI == null) { logger.warn( "Cannot handle union that contains an anonymous class in the domain of " + p.getURI()); } else { OntResource type = first.as(OntResource.class); addField(type, p, null); } } unionList = unionList.getPropertyResourceValue(RDF.rest); if (RDF.nil.equals(unionList)) { break; } } } } }
/** * If _n_ is a ReifiedStatement, create a local copy of it, which will force the underlying * reifier to take note of the mapping. */ private void noteIfReified(RDFNode n) { if (n.canAs(ReifiedStatement.class)) { ReifiedStatement rs = n.as(ReifiedStatement.class); createReifiedStatement(rs.getURI(), rs.getStatement()); } }
/* (non-Javadoc) * @see com.hp.hpl.jena.rdf.model.RDFNode#canAs(java.lang.Class) */ @Override public <T extends RDFNode> boolean canAs(Class<T> view) { return rdfNode.canAs(view); }
// renames percentage properties and classes // activeProperties -> if true, then rename properties // activeClasses -> if true, then rename classes public OntModel renameResource( boolean activeProperties, boolean activeClasses, float percentage, boolean activeRandomString, boolean activeTranslateString, boolean activeSynonym, int activeStringOperation) { List<Statement> statements = null; // the list of all statements HashMap<String, String> propertiesIdentifiers = null; // the HashMap of the properties identifiers HashMap<String, String> classesIdentifiers = null; // the HashMap of the classes identifiers OntModel newModel = ModelFactory.createOntologyModel(OntModelSpec.OWL_MEM); // create new Model // get properties and classes identifiers if (activeProperties) propertiesIdentifiers = getPropertiesIdentifiers( percentage, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation); if (activeClasses) classesIdentifiers = getClassesIdentifiers( percentage, activeRandomString, activeTranslateString, activeSynonym, activeStringOperation); // iterate and modify the identifiers for (Statement stm : modifiedModel.listStatements().toList()) { Resource subject = stm.getSubject(); // the subject Property predicate = stm.getPredicate(); // the predicate RDFNode object = stm.getObject(); // the object boolean isPred, isSubj, isObj; isPred = isSubj = isObj = false; String subjuri = subject.getURI(); String subjectLocalName = getLocalName(subjuri); Resource subj = null; // if it is the subject of the statement if (subjectLocalName != null) { String subjectNameSpace = getNameSpace(subjuri); if (activeProperties) { if (propertiesIdentifiers.containsKey(subjectLocalName)) { // if the namespace of the subject is the same as the namespace of the property // identifier if (subjectNameSpace.equals(modifiedOntologyNS)) { // that we want to remove isSubj = true; subj = newModel.createResource( subjectNameSpace + propertiesIdentifiers.get(subjectLocalName)); } } } if (activeClasses) { if (classesIdentifiers.containsKey(subjectLocalName)) { // if the namespace of the subject is the same as the namespace of the property // identifier // that we want to remove if (subjectNameSpace.equals(modifiedOntologyNS)) { isSubj = true; subj = newModel.createResource( subjectNameSpace + classesIdentifiers.get(subjectLocalName)); } } } } // if it is the predicate of the statement String preduri = predicate.getURI(); String predicateLocalName = getLocalName(preduri); String predicateNameSpace = getNameSpace(preduri); Property pred = null; if (activeProperties) { if (propertiesIdentifiers.containsKey(predicateLocalName)) { // if the namespace of the predicate is the same as the namespace of the property // identifier // that we want to remove if (predicateNameSpace.equals(modifiedOntologyNS)) { isPred = true; pred = newModel.createProperty( predicateNameSpace, propertiesIdentifiers.get(predicateLocalName)); } } } if (activeClasses) { if (classesIdentifiers.containsKey(predicateLocalName)) { // if the namespace of the predicate is the same as the namespace of the property // identifier // that we want to remove if (predicateNameSpace.equals(modifiedOntologyNS)) { isPred = true; pred = newModel.createProperty( predicateNameSpace, classesIdentifiers.get(predicateLocalName)); } } } Resource obj = null; // if it is the object of the statement if (object.canAs(Resource.class)) if (object.isURIResource()) { String uri = object.asResource().getURI(); String objectLocalName = getLocalName(uri); String objectNameSpace = getNameSpace(uri); if (activeProperties) { if (propertiesIdentifiers.containsKey(objectLocalName)) { // if the namespace of the object is the same as the namespace of the property // identifier // that we want to remove if (objectNameSpace.equals(modifiedOntologyNS)) { isObj = true; obj = newModel.createResource( objectNameSpace + propertiesIdentifiers.get(objectLocalName)); } } } if (activeClasses) { if (classesIdentifiers.containsKey(objectLocalName)) { // if the namespace of the object is the same as the namespace of the property // identifier that we want to remove if (objectNameSpace.equals(modifiedOntologyNS)) { isObj = true; obj = newModel.createResource( objectNameSpace + classesIdentifiers.get(objectLocalName)); } } } } if (isSubj) { if (isPred) { if (isObj) newModel.add(subj, pred, obj); else newModel.add(subj, pred, object); } else { if (isObj) newModel.add(subj, predicate, obj); else newModel.add(subj, predicate, object); } } else { if (isPred) { if (isObj) newModel.add(subject, pred, obj); else newModel.add(subject, pred, object); } else { if (isObj) newModel.add(subject, predicate, obj); else newModel.add(subject, predicate, object); } } } if (activeClasses) { buildClassHierarchy(); // we update the class hierarchy according to the new modifications classHierarchy.updateClassHierarchy(alignment); // classHierarchy.printClassHierarchy(); } return newModel; }