public void testCreateAnonResourceWithNull() { final Resource r = model.createResource((String) null); Assert.assertTrue(r.isAnon()); Assert.assertNull(r.getURI()); Assert.assertNull(r.getNameSpace()); Assert.assertNull(r.getLocalName()); }
protected String jcrMixinNameFromRdfResource(final Resource mixinResource) throws RepositoryException { final String namespace = getJcrNamespaceForRDFNamespace(mixinResource.getNameSpace()); String namespacePrefix = null; final Map<String, String> streamNSMap = checkNotNull(stream().namespaces(), "Use an empty map of namespaces, not null!"); if (streamNSMap.containsValue(namespace)) { LOGGER.debug("Found namespace: {} in stream namespace mapping.", namespace); for (final Map.Entry<String, String> entry : streamNSMap.entrySet()) { final String streamNamespace = entry.getValue(); if (namespace.equals(streamNamespace)) { LOGGER.debug( "Found namespace: {} in stream namespace mapping with prefix: {}.", namespace, namespacePrefix); namespacePrefix = entry.getKey(); } } } else { try { namespacePrefix = session().getNamespacePrefix(namespace); LOGGER.debug( "Found namespace: {} in repository namespace mapping with prefix: {}.", namespace, namespacePrefix); } catch (final NamespaceException e) { throw new MalformedRdfException( "Unable to resolve registered namespace for resource " + mixinResource.toString(), e); } } final String mixinName = namespacePrefix + ":" + mixinResource.getLocalName(); LOGGER.debug("Constructed JCR mixin name: {}", mixinName); return mixinName; }
/** * Determine if a predicate is an internal property of a node (and should not be modified from * external sources) * * @param subjectNode * @param predicate * @return */ public boolean isInternalProperty(final Node subjectNode, final Resource predicate) { switch (predicate.getNameSpace()) { case REPOSITORY_NAMESPACE: case JCR_NAMESPACE: case LDP_NAMESPACE: return true; default: return false; } }
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(); } } }
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; }
/** @see com.hp.hpl.jena.rdf.model.Resource#getNameSpace() */ public String getNameSpace() { synchronized (model) { return wrapped.getNameSpace(); } }