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

The following examples show how to use org.apache.jena.rdf.model.Resource#isURIResource() . 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: SHACLCWriter.java    From shacl with Apache License 2.0 6 votes vote down vote up
private void writeShapes(IndentedWriter out, Model model) {
	
	List<Resource> shapes = new LinkedList<>();
	for(Resource shape : JenaUtil.getAllInstances(SH.NodeShape.inModel(model))) {
		if(shape.isURIResource()) {
			shapes.add(shape);
		}
	}
	// Collections.sort(shapes, ResourceComparator.get());
	
	for(int i = 0; i < shapes.size(); i++) {
		if(i > 0) {
			out.println();
		}
		writeShape(out, shapes.get(i));
	}
}
 
Example 2
Source File: SPDXFile.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * @param r1
 * @param r2
 * @return
 */
private boolean resourcesEqual(Resource r1,
		Resource r2) {
	if (r2 == null) {
		return false;
	}
	if (r1.isAnon()) {
		if (!r2.isAnon()) {
			return false;
		}
		return r1.getId().equals(r2.getId());
	} else {
		if (!r2.isURIResource()) {
			return false;
		}
		return r1.getURI().equals(r2.getURI());
	} 
}
 
Example 3
Source File: RdfModelObject.java    From tools with Apache License 2.0 6 votes vote down vote up
/**
 * Returns true if the two resources represent the same node
 * @param r1
 * @param r2
 * @return
 */
protected boolean resourcesEqual(Resource r1,
		Resource r2) {
	if (r1 == null) {
		return (r2 == null);
	}
	if (r2 == null) {
		return false;
	}
	if (r1.isAnon()) {
		if (!r2.isAnon()) {
			return false;
		}
		return r1.getId().equals(r2.getId());
	} else {
		if (!r2.isURIResource()) {
			return false;
		}
		return r1.getURI().equals(r2.getURI());
	} 
}
 
Example 4
Source File: RDFLabels.java    From shacl with Apache License 2.0 5 votes vote down vote up
/**
 * Gets the label for a given Resource.
 * @param resource  the Resource to get the label of
 * @return the label (never null)
 */
public String getLabel(Resource resource) {
	if(resource.isURIResource() && resource.getModel() != null) {
		String qname = resource.getModel().qnameFor(resource.getURI());
		if(qname != null) {
			return qname;
		}
		else {
			return "<" + resource.getURI() + ">";
		}
	}
	else if(resource.isAnon() && resource.getModel() != null && resource.hasProperty(RDF.first)) {
		StringBuffer buffer = new StringBuffer("[");
		Iterator<RDFNode> members = resource.as(RDFList.class).iterator();
		while(members.hasNext()) {
			RDFNode member = members.next();
			buffer.append(RDFLabels.get().getNodeLabel(member));
			if(members.hasNext()) {
				buffer.append(", ");
			}
		}
		buffer.append("]");
		return buffer.toString();
	}
	else if(resource.isAnon()) {
		return getBlankNodeLabel(resource);
	}
	else {
		return resource.toString();
	}
}
 
Example 5
Source File: ClosedConstraintExecutor.java    From shacl with Apache License 2.0 5 votes vote down vote up
ClosedConstraintExecutor(Constraint constraint) {
	this.closed = constraint.getShapeResource().hasProperty(SH.closed, JenaDatatypes.TRUE);
	RDFList list = JenaUtil.getListProperty(constraint.getShapeResource(), SH.ignoredProperties);
	if(list != null) {
		list.iterator().forEachRemaining(allowedProperties::add);
	}
	for(Resource ps : JenaUtil.getResourceProperties(constraint.getShapeResource(), SH.property)) {
		Resource path = ps.getPropertyResourceValue(SH.path);
		if(path.isURIResource()) {
			allowedProperties.add(path);
		}
	}
}
 
Example 6
Source File: SHACLCWriter.java    From shacl with Apache License 2.0 5 votes vote down vote up
private String getPropertyTypes(Resource property) {
	List<String> types = new LinkedList<>();
	for(Resource clas : JenaUtil.getResourceProperties(property, SH.class_)) {
		types.add(iri(clas));
	}
	for(Resource datatype : JenaUtil.getResourceProperties(property, SH.datatype)) {
		types.add(iri(datatype));
	}
	for(Resource node : JenaUtil.getResourceProperties(property, SH.node)) {
		if(node.isURIResource()) {
			types.add("@" + iri(node));
		}
	}
	Resource nodeKind = property.getPropertyResourceValue(SH.nodeKind);
	if(nodeKind != null) {
		types.add(nodeKind.getLocalName());
	}
	Collections.sort(types);
	StringBuffer sb = new StringBuffer();
	for(int i = 0; i < types.size(); i++) {
		if(i > 0) {
			sb.append(" ");
		}
		sb.append(types.get(i));
	}
	return sb.toString();
}
 
Example 7
Source File: SHACLPaths.java    From shacl with Apache License 2.0 5 votes vote down vote up
/**
 * Renders a given path into a given StringBuffer, using the prefixes supplied by the
 * Path's Model.
 * @param sb  the StringBuffer to write into
 * @param path  the path resource
 */
public static void appendPath(StringBuffer sb, Resource path) {
	if(path.isURIResource()) {
		sb.append(FmtUtils.stringForNode(path.asNode(), path.getModel()));
	}
	else {
		appendPathBlankNode(sb, path, SEQUENCE_PATH_SEPARATOR);
	}
}
 
Example 8
Source File: SHACLPaths.java    From shacl with Apache License 2.0 5 votes vote down vote up
private static void appendNestedPath(StringBuffer sb, Resource path, String separator) {
	if(path.isURIResource()) {
		sb.append(FmtUtils.stringForNode(path.asNode(), path.getModel()));
	}
	else {
		sb.append("(");
		appendPathBlankNode(sb, path, separator);
		sb.append(")");
	}
}
 
Example 9
Source File: SHACLPaths.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static Object getJenaPath(Resource path) throws QueryParseException {
	if(path.isURIResource()) {
		return path;
	}
	else {
		String pathString = SHACLPaths.getPathString(path);
		return SHACLPaths.getJenaPath(pathString, path.getModel());
	}
}
 
Example 10
Source File: SHACLFunctions.java    From shacl with Apache License 2.0 5 votes vote down vote up
private static void perhapsRegisterFunction(SHConstraintComponent component, Property predicate) {
	for(Resource validator : JenaUtil.getResourceProperties(component, predicate)) {
		if(validator.isURIResource() && 
				!FunctionRegistry.get().isRegistered(validator.getURI()) &&
				JenaUtil.hasIndirectType(validator, SH.SPARQLAskValidator)) {
			FunctionFactory arqFunction = new SHACLSPARQLARQFunction(component, validator);
			if(arqFunction != null) {
				FunctionRegistry.get().put(validator.getURI(), arqFunction);
			}
		}
	}
}
 
Example 11
Source File: SHPropertyShapeImpl.java    From shacl with Apache License 2.0 5 votes vote down vote up
@Override
public Property getPredicate() {
	Resource r = getPropertyResourceValue(SH.path);
	if(r != null && r.isURIResource()) {
		return JenaUtil.asProperty(r);
	}
	else {
		return null;
	}
}
 
Example 12
Source File: SPDXFile.java    From tools with Apache License 2.0 5 votes vote down vote up
public static String fileTypeResourceToString(Resource fileTypeResource) throws InvalidSPDXAnalysisException {
	if (!fileTypeResource.isURIResource()) {
		throw(new InvalidSPDXAnalysisException("File type resource must be a URI."));
	}
	String retval = fileTypeResource.getURI();
	if (retval == null) {
		throw(new InvalidSPDXAnalysisException("Not a recognized file type for an SPDX document."));
	}
	return retval;
}
 
Example 13
Source File: SPDXChecksum.java    From tools with Apache License 2.0 5 votes vote down vote up
public static String algorithmResourceToString(Resource algorithmResource) throws InvalidSPDXAnalysisException {
	String uri = algorithmResource.getURI();
	if (!algorithmResource.isURIResource()) {
		throw(new InvalidSPDXAnalysisException("Algorithm resource must be a URI"));
	}
	String retval = URI_TO_ALGORITHM.get(uri);
	if (retval == null) {
		throw(new InvalidSPDXAnalysisException("Invalid algorithm resource."));
	}
	return retval;
}
 
Example 14
Source File: DB2DescribeHandler.java    From quetzal with Eclipse Public License 2.0 5 votes vote down vote up
private static Resource otherModel(Resource r, Model model) {
	if (r.isURIResource())
		return model.createResource(r.getURI());
	if (r.isAnon())
		return model.createResource(r.getId());
	return r;
}
 
Example 15
Source File: AbstractSPARQLExecutor.java    From shacl with Apache License 2.0 4 votes vote down vote up
@Override
public void executeConstraint(Constraint constraint, ValidationEngine engine, Collection<RDFNode> focusNodes) {
	
	QuerySolutionMap bindings = new QuerySolutionMap();
	addBindings(constraint, bindings);
	bindings.add(SH.currentShapeVar.getVarName(), constraint.getShapeResource());
	bindings.add(SH.shapesGraphVar.getVarName(), ResourceFactory.createResource(engine.getShapesGraphURI().toString()));
	
	Resource path = constraint.getShapeResource().getPath();
	if(path != null && path.isURIResource()) {
		bindings.add(SH.PATHVar.getName(), path);
	}
	
	URI oldShapesGraphURI = HasShapeFunction.getShapesGraphURI();
	ShapesGraph oldShapesGraph = HasShapeFunction.getShapesGraph();
	if(!engine.getShapesGraphURI().equals(oldShapesGraphURI)) {
		HasShapeFunction.setShapesGraph(engine.getShapesGraph(), engine.getShapesGraphURI());
	}
	
	Model oldNestedResults = HasShapeFunction.getResultsModel();
	Model nestedResults = JenaUtil.createMemoryModel();
	HasShapeFunction.setResultsModel(nestedResults);
	
	try {
		long startTime = System.currentTimeMillis();
		Resource messageHolder = getSPARQLExecutable(constraint);
		for(RDFNode focusNode : focusNodes) {
			bindings.add(SH.thisVar.getVarName(), focusNode); // Overwrite any previous binding
			QueryExecution qexec = SPARQLSubstitutions.createQueryExecution(query, engine.getDataset(), bindings);
			executeSelectQuery(engine, constraint, messageHolder, nestedResults, focusNode, qexec, bindings);
			engine.checkCanceled();
		}			
		if(ExecStatisticsManager.get().isRecording()) {
			long endTime = System.currentTimeMillis();
			long duration = endTime - startTime;
			String label = getLabel(constraint);
			Iterator<String> varNames = bindings.varNames();
			if(varNames.hasNext()) {
				queryString += "\nBindings:";
				while(varNames.hasNext()) {
					String varName = varNames.next();
					queryString += "\n- ?" + varName + ": " + bindings.get(varName);
				}
			}
			ExecStatistics stats = new ExecStatistics(label, queryString, duration, startTime, constraint.getComponent().asNode());
			ExecStatisticsManager.get().add(Collections.singletonList(stats));
		}
	}
	finally {
		HasShapeFunction.setShapesGraph(oldShapesGraph, oldShapesGraphURI);
		HasShapeFunction.setResultsModel(oldNestedResults);
	}
}
 
Example 16
Source File: ShapePathReader.java    From RDFUnit with Apache License 2.0 4 votes vote down vote up
private static Path readPath(Resource resource) {
    if (resource.isURIResource()) {
        return new P_Link(resource.asNode());
    }

    if (RdfListUtils.isList(resource)) {
        return RdfListUtils.getListItemsOrEmpty(resource).stream()
                .filter(RDFNode::isResource)
                .map(RDFNode::asResource)
                .map(ShapePathReader::readPath)
                .reduce(PathFactory::pathSeq)
                .orElseThrow(() -> new IllegalArgumentException("Sequence path invalid"));
    }

    Resource inverse = resource.getPropertyResourceValue(SHACL.inversePath);
    if ( inverse != null ) {
        return PathFactory.pathInverse(readPath(inverse));
    }

    Resource alternate = resource.getPropertyResourceValue(SHACL.alternativePath);
    if ( alternate != null && RdfListUtils.isList(alternate)) {
        return RdfListUtils.getListItemsOrEmpty(alternate).stream()
                .filter(RDFNode::isResource)
                .map(RDFNode::asResource)
                .map(ShapePathReader::readPath)
                .reduce(PathFactory::pathAlt)
                .orElseThrow(() -> new IllegalArgumentException("Sequence path invalid"));
    }

    Resource zeroOrOne = resource.getPropertyResourceValue(SHACL.zeroOrOnePath);
    if ( zeroOrOne != null ) {
        return PathFactory.pathZeroOrOne(readPath(zeroOrOne));
    }

    Resource zeroOrMore = resource.getPropertyResourceValue(SHACL.zeroOrMorePath);
    if ( zeroOrMore != null ) {
        return PathFactory.pathZeroOrMore1(readPath(zeroOrMore));
    }

    Resource oneOrMore = resource.getPropertyResourceValue(SHACL.oneOrMorePath);
    if ( oneOrMore != null ) {
        return PathFactory.pathOneOrMore1(readPath(oneOrMore));
    }
    throw new IllegalArgumentException("Wrong SHACL Path");
}