Java Code Examples for org.apache.jena.rdf.model.Resource#equals()

The following examples show how to use org.apache.jena.rdf.model.Resource#equals() . 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
/**
 * 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 2
Source File: JenaUtil.java    From shacl with Apache License 2.0 6 votes vote down vote up
private static boolean hasSuperClass(Resource subClass, Resource superClass, Set<Resource> reached) {
	StmtIterator it = subClass.listProperties(RDFS.subClassOf);
	while(it.hasNext()) {
		Statement s = it.next();
		if(superClass.equals(s.getObject())) {
			it.close();
			return true;
		}
		else if(!reached.contains(s.getResource())) {
			reached.add(s.getResource());
			if(hasSuperClass(s.getResource(), superClass, reached)) {
				it.close();
				return true;
			}
		}
	}
	return false;
}
 
Example 3
Source File: TestCase.java    From shacl with Apache License 2.0 6 votes vote down vote up
public boolean usesDifferentEnvironmentFrom(TestCase other) {
	
	if(getResource().hasProperty(DASH.testModifiesEnvironment)) {
		return true;
	}
	if(other.getResource().hasProperty(DASH.testModifiesEnvironment)) {
		return true;
	}
	
	Resource e1 = getResource().getPropertyResourceValue(DASH.testEnvironment);
	Resource e2 = other.getResource().getPropertyResourceValue(DASH.testEnvironment);
	if(e1 != null && e2 != null) {
		return !e1.equals(e2);
	}
	else if(e1 == null && e2 == null) {
		return false;
	}
	else {
		return true;
	}
}
 
Example 4
Source File: IntersectionExpression.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public Resource getOutputShape(Resource contextShape) {
	if(inputs.size() == 0) {
		return null;
	}
	Resource s = inputs.get(0).getOutputShape(contextShape);
	if(s == null) {
		return null;
	}
	for(int i = 1; i < inputs.size(); i++) {
		Resource o = inputs.get(i).getOutputShape(contextShape);
		if(!s.equals(o)) {
			return null;
		}
	}
	return s;
}
 
Example 5
Source File: UnionExpression.java    From shacl with Apache License 2.0 6 votes vote down vote up
@Override
public Resource getOutputShape(Resource contextShape) {
	if(inputs.size() == 0) {
		return null;
	}
	Resource s = inputs.get(0).getOutputShape(contextShape);
	if(s == null) {
		return null;
	}
	for(int i = 1; i < inputs.size(); i++) {
		Resource o = inputs.get(i).getOutputShape(contextShape);
		if(!s.equals(o)) {
			return null;
		}
	}
	return s;
}
 
Example 6
Source File: SystemTriples.java    From shacl with Apache License 2.0 5 votes vote down vote up
private static List<Resource> collectMissingSuperClasses(Resource metaClass,
		Resource superClass) {
	List<Resource> toAdd = new ArrayList<Resource>();
	StmtIterator it = vocabulary.listStatements(null, RDF.type, metaClass);
	while (it.hasNext()) {
		Resource c = it.nextStatement().getSubject();
		if (!c.equals(superClass)) {
			if (c.getProperty(RDFS.subClassOf) == null) {
				toAdd.add(c);
			}
		}
	}
	return toAdd;
}
 
Example 7
Source File: HypermediaFilter.java    From Processor with Apache License 2.0 5 votes vote down vote up
@Override
public ContainerResponse filter(ContainerRequest request, ContainerResponse response)
{
    if (request == null) throw new IllegalArgumentException("ContainerRequest cannot be null");
    if (response == null) throw new IllegalArgumentException("ContainerResponse cannot be null");
    
    // do not process hypermedia if the response is a redirect or 201 Created or 404 Not Found
    if (response.getStatusType().getFamily().equals(REDIRECTION) || response.getStatusType().equals(CREATED) ||
            response.getStatusType().equals(NOT_FOUND) || response.getStatusType().equals(INTERNAL_SERVER_ERROR) || 
            response.getEntity() == null || (!(response.getEntity() instanceof Dataset)))
        return response;
    
    TemplateCall templateCall = getTemplateCall();
    if (templateCall == null) return response;
    
    Resource state = templateCall.build();
    Resource absolutePath = state.getModel().createResource(request.getAbsolutePath().toString());
    if (!state.equals(absolutePath)) state.addProperty(C.stateOf, absolutePath);

    Resource requestUri = state.getModel().createResource(request.getRequestUri().toString());
    if (!state.equals(requestUri)) // add hypermedia if there are query parameters
        state.addProperty(C.viewOf, requestUri). // needed to lookup response state by request URI without redirection
            addProperty(RDF.type, C.View);

    if (log.isDebugEnabled()) log.debug("Added Number of HATEOAS statements added: {}", state.getModel().size());
    Dataset newEntity = ((Dataset)response.getEntity());
    newEntity.getDefaultModel().add(state.getModel());
    response.setEntity(newEntity);
    
    return response;
}