org.apache.jena.sparql.path.Path Java Examples

The following examples show how to use org.apache.jena.sparql.path.Path. 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: Shape.java    From shacl with Apache License 2.0 6 votes vote down vote up
public Shape(ShapesGraph shapesGraph, SHShape shape) {
	this.shape = shape;
	this.shapesGraph = shapesGraph;
	Resource path = shape.getPath();
	this.path = path;
	this.deactivated = shape.isDeactivated();
	this.severity = shape.getSeverity();
	if(path != null) {			
		if(path.isAnon()) {
			jenaPath = (Path) SHACLPaths.getJenaPath(SHACLPaths.getPathString(path), path.getModel());
		}
		else {
			predicate = JenaUtil.asProperty(path);
		}
	}
	else {
		nodeShape = true;
	}
	collectTargets();
}
 
Example #2
Source File: SHACLPaths.java    From shacl with Apache License 2.0 6 votes vote down vote up
/**
 * Attempts to parse a given string into a Jena Path.
 * Throws an Exception if the string cannot be parsed.
 * @param string  the string to parse
 * @param model  the Model to operate on (for prefixes)
 * @return a Path or a Resource if this is a URI
 */
public static Object getJenaPath(String string, Model model) throws QueryParseException {
	Query query = ARQFactory.get().createQuery(model, "ASK { ?a \n" + string + "\n ?b }");
	Element element = query.getQueryPattern();
	if(element instanceof ElementGroup) {
		Element e = ((ElementGroup)element).getElements().get(0);
		if(e instanceof ElementPathBlock) {
			Path path = ((ElementPathBlock) e).getPattern().get(0).getPath();
			if(path instanceof P_Link && ((P_Link)path).isForward()) {
				return model.asRDFNode(((P_Link)path).getNode());
			}
			else {
				return path;
			}
		}
		else if(e instanceof ElementTriplesBlock) {
			return model.asRDFNode(((ElementTriplesBlock) e).getPattern().get(0).getPredicate());
		}
	}
	throw new QueryParseException("Not a SPARQL 1.1 Path expression", 2, 1);
}
 
Example #3
Source File: PathNormalizer.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(P_Alt pathAlt) {
    pathAlt.getLeft().visit(this);
    Path left = result;
    pathAlt.getRight().visit(this);
    Path right = result;
    result = new P_Alt(left, right);
}
 
Example #4
Source File: PathNormalizer.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(P_Seq pathSeq) {
    pathSeq.getLeft().visit(this);
    Path left = result;
    pathSeq.getRight().visit(this);
    Path right = result;
    result = new P_Seq(left, right);
}
 
Example #5
Source File: SPARQLExtPathWriter.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private void visitPath(Path path, boolean needParensThisTime) {
    if (alwaysInnerParens) {
        needParensThisTime = true;
    }
    boolean b = needParens;
    needParens = needParensThisTime;
    path.visit(this);
    needParens = b;
}
 
Example #6
Source File: SPARQLExtPathWriter.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
private void printPathMod(String mod, Path path) {
    boolean doParens = (needParens || alwaysInnerParens);
    if (doParens) {
        out.print("(");
    }
    path.visit(this);
    if (doParens) {
        out.print(")");
    }
    out.print(mod);
}
 
Example #7
Source File: SPARQLExtPathWriter.java    From sparql-generate with Apache License 2.0 5 votes vote down vote up
@Override
public void visit(P_Inverse inversePath) {
    out.print("^");
    Path p = inversePath.getSubPath();
    boolean parens = true;
    if (p instanceof P_Link) {
        parens = false;
    }
    visitPath(p, parens);
}
 
Example #8
Source File: SHACLPaths.java    From shacl with Apache License 2.0 5 votes vote down vote up
public static void addValueNodes(RDFNode focusNode, Path path, Collection<RDFNode> results) {
	Set<Node> seen = new HashSet<>();
	Iterator<Node> it = PathEval.eval(focusNode.getModel().getGraph(), focusNode.asNode(), path, Context.emptyContext);
	while(it.hasNext()) {
		Node node = it.next();
		if(!seen.contains(node)) {
			seen.add(node);
			results.add(focusNode.getModel().asRDFNode(node));
		}
	}
}
 
Example #9
Source File: PathEvaluator.java    From shacl with Apache License 2.0 5 votes vote down vote up
/**
 * Constructs a PathEvaluator for an arbitrary SPARQL path (except single forward properties).
 * @param path  the path
 * @param shapesModel  the shapes Model
 */
public PathEvaluator(Path path, Model shapesModel) {
	this.jenaPath = path;
	isInverse = jenaPath instanceof P_Inverse && ((P_Inverse)jenaPath).getSubPath() instanceof P_Link;
	if(isInverse) {
		P_Link link = (P_Link) ((P_Inverse)jenaPath).getSubPath();
		predicate = shapesModel.getProperty(link.getNode().getURI());
	}
}
 
Example #10
Source File: PathExpression.java    From shacl with Apache License 2.0 5 votes vote down vote up
public PathExpression(RDFNode expr, Resource path, NodeExpression input) {
	super(expr, input);
	this.path = path;
	if(path.isAnon()) {
		pathString = SHACLPaths.getPathString(path);
		Path jenaPath = (Path) SHACLPaths.getJenaPath(pathString, path.getModel());
		eval = new PathEvaluator(jenaPath, expr.getModel());
	}
	else {
		pathString = FmtUtils.stringForRDFNode(path);
		eval = new PathEvaluator(JenaUtil.asProperty(path));
	}
	eval.setInput(input);
}
 
Example #11
Source File: PathNormalizer.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
/**
 * @return latest normalized path
 */
public Path getResult() {
    return result;
}
 
Example #12
Source File: SPARQLExtPathWriter.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
public static void write(IndentedWriter out, Path path, SerializationContext context) {
    PathWriterWorker w = new PathWriterWorker(out, context);
    path.visit(w);
    out.flush();
}
 
Example #13
Source File: SPARQLExtPathWriter.java    From sparql-generate with Apache License 2.0 4 votes vote down vote up
private void visitPath(Path path) {
    visitPath(path, true);
}
 
Example #14
Source File: Shape.java    From shacl with Apache License 2.0 4 votes vote down vote up
public Path getJenaPath() {
	return jenaPath;
}
 
Example #15
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");
}
 
Example #16
Source File: ShapePath.java    From RDFUnit with Apache License 2.0 votes vote down vote up
Path getJenaPath();