Java Code Examples for org.apache.jena.rdf.model.Statement#getResource()

The following examples show how to use org.apache.jena.rdf.model.Statement#getResource() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JenaUtil.java    From shacl with Apache License 2.0 6 votes vote down vote up
private static Resource getFirstRange(Resource property, Set<Resource> reached) {
	Resource directRange = getFirstDirectRange(property);
	if(directRange != null) {
		return directRange;
	}
	StmtIterator it = property.listProperties(RDFS.subPropertyOf);
	while (it.hasNext()) {
		Statement ss = it.next();
		if (ss.getObject().isURIResource()) {
			Resource superProperty = ss.getResource();
			if (!reached.contains(superProperty)) {
				reached.add(superProperty);
				Resource r = getFirstRange(superProperty, reached);
				if (r != null) {
					it.close();
					return r;
				}
			}
		}
	}
	return null;
}
 
Example 2
Source File: JenaUtil.java    From shacl with Apache License 2.0 6 votes vote down vote up
/**
 * Checks whether a given Resource is an instance of a given type, or
 * a subclass thereof.  Make sure that the expectedType parameter is associated
 * with the right Model, because the system will try to walk up the superclasses
 * of expectedType.  The expectedType may have no Model, in which case
 * the method will use the instance's Model.
 * @param instance  the Resource to test
 * @param expectedType  the type that instance is expected to have
 * @return true if resource has rdf:type expectedType
 */
public static boolean hasIndirectType(Resource instance, Resource expectedType) {
	
	if(expectedType.getModel() == null) {
		expectedType = expectedType.inModel(instance.getModel());
	}
	
	StmtIterator it = instance.listProperties(RDF.type);
	while(it.hasNext()) {
		Statement s = it.next();
		if(s.getObject().isResource()) {
			Resource actualType = s.getResource();
			if(actualType.equals(expectedType) || JenaUtil.hasSuperClass(actualType, expectedType)) {
				it.close();
				return true;
			}
		}
	}
	return false;
}
 
Example 3
Source File: Skolemizer.java    From Processor with Apache License 2.0 6 votes vote down vote up
protected Resource getResource(Resource resource, String name)
{
    if (resource == null) throw new IllegalArgumentException("Resource cannot be null");
    
    StmtIterator it = resource.listProperties();
    try
    {
        while (it.hasNext())
        {
            Statement stmt = it.next();
            if (stmt.getObject().isAnon() && stmt.getPredicate().getLocalName().equals(name))
            {
                if (log.isTraceEnabled()) log.trace("Found Resource {} for property name: {} ", stmt.getResource(), name);
                return stmt.getResource();
            }
        }
    }
    finally
    {
        it.close();
    }
    
    return null;
}
 
Example 4
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static Resource getResourcePropertyWithType(Resource subject, Property predicate, Resource type) {
	StmtIterator it = subject.listProperties(predicate);
	while(it.hasNext()) {
		Statement s = it.next();
		if(s.getObject().isResource() && JenaUtil.hasIndirectType(s.getResource(), type)) {
			it.close();
			return s.getResource();
		}
	}
	return null;
}
 
Example 5
Source File: JenaUtil.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static Resource getURIResourceProperty(Resource subject, Property predicate) {
	Statement s = subject.getProperty(predicate);
	if(s != null && s.getObject().isURIResource()) {
		return s.getResource();
	}
	else {
		return null;
	}
}
 
Example 6
Source File: AbstractEngine.java    From shacl with Apache License 2.0 5 votes vote down vote up
/**
 * Ensures that the data graph includes any entailed triples inferred by the regime
 * specified using sh:entailment in the shapes graph.
 * Should be called prior to validation.
 * Throws an Exception if unsupported entailments are found.
 * If multiple sh:entailments are present then their order is undefined but they all get applied.
 * @throws InterruptedException if the monitor has canceled it
 */
public void applyEntailments() throws InterruptedException {
	Model shapesModel = dataset.getNamedModel(shapesGraphURI.toString());
	for(Statement s : shapesModel.listStatements(null, SH.entailment, (RDFNode)null).toList()) {
		if(s.getObject().isURIResource()) {
			if(SHACLEntailment.get().getEngine(s.getResource().getURI()) != null) {
				this.dataset = SHACLEntailment.get().withEntailment(dataset, shapesGraphURI, shapesGraph, s.getResource(), monitor);
			}
			else {
				throw new UnsupportedOperationException("Unsupported entailment regime " + s.getResource());
			}
		}
	}
}
 
Example 7
Source File: Exporter.java    From fcrepo-import-export with Apache License 2.0 4 votes vote down vote up
/**
 * Initiates export of versions for the given resource if it is a versioned resourced
 * 
 * @param uri resource uri
 * @throws FcrepoOperationFailedException
 * @throws IOException
 */
private void exportVersions(final URI uri) throws FcrepoOperationFailedException, IOException {
    // Do not check for versions if disabled, already exporting a version, or the repo root
    if (!config.includeVersions() || uri.equals(repositoryRoot)) {
        return;
    }

    // Resolve the timemap endpoint for this resource
    final URI timemapURI;
    try (FcrepoResponse response = client().head(uri).disableRedirects().perform()) {
        checkValidResponse(response, uri, config.getUsername());
        if (response.getLinkHeaders("type").stream()
            .filter(x -> x.toString().equals(MEMENTO.getURI()))
            .count() > 0) {
            logger.trace("Resource {} is a memento and therefore not versioned:  ", uri);
            return;
        } else if (response.getLinkHeaders("type").stream()
            .filter(x -> x.toString().equals(TIMEMAP.getURI()))
            .count() > 0) {
            logger.trace("Resource {} is a timemap and therefore not versioned:  ", uri);
            return;
        }

        timemapURI = response.getLinkHeaders("timemap").stream().findFirst().orElse(null);
        if (timemapURI == null) {
            logger.trace("Resource {} is not versioned:  no rel=\"timemap\" Link header present on {}", uri);
            return;
        }
    }

    export(timemapURI);

    try (FcrepoResponse response = client().get(timemapURI).accept(config.getRdfLanguage()).perform()) {
       // Verify that timemapURI can be accessed, which will fail if the resource is not versioned
        checkValidResponse(response, timemapURI, config.getUsername());
        // Extract uris of mementos for export
        final Model model = createDefaultModel().read(response.getBody(), null, config.getRdfLanguage());
        final StmtIterator versionsIt = model.listStatements();
        while (versionsIt.hasNext()) {
            final Statement versionSt = versionsIt.next();
            if (versionSt.getPredicate().equals(CONTAINS)) {
                final Resource versionResc = versionSt.getResource();
                exportLogger.info("Exporting version: {}", versionResc.getURI());
                logger.info("Exporting version {} for {}", versionResc.getURI(), uri);
                export(URI.create(versionResc.getURI()));
            }
        }
    }
}
 
Example 8
Source File: SHRuleImpl.java    From shacl with Apache License 2.0 4 votes vote down vote up
@Override
public Resource getPredicate() {
	Statement s = getProperty(SH.predicate);
	return s != null && s.getObject().isResource() ? s.getResource() : null;
}