Search in sources :

Example 31 with TriplePath

use of org.apache.jena.sparql.core.TriplePath in project jena by apache.

the class UpdateBuilder method list.

/**
	 * Create a list node from a list of objects as per RDF Collections.
	 * 
	 * http://www.w3.org/TR/2013/REC-sparql11-query-20130321/#collections
	 * 
	 * See {@link AbstractQueryBuilder#makeNode} for conversion of the param
	 * values.
	 * <p>
	 * usage:
	 * <ul>
	 * <li>list( param1, param2, param3, ... )</li>
	 * <li>addWhere( list( param1, param2, param3, ... ), p, o )</li>
	 * <li>addOptional( list( param1, param2, param3, ... ), p, o )</li>
	 * </ul>
	 * </p>
	 * 
	 * @param objs
	 *            the list of objects for the list.
	 * 
	 * @param objs
	 *            the list of objects for the list.
	 * @return the first blank node in the list.
	 */
public Node list(Object... objs) {
    Node retval = NodeFactory.createBlankNode();
    Node lastObject = retval;
    for (int i = 0; i < objs.length; i++) {
        Node n = makeNode(objs[i]);
        addWhere(new TriplePath(new Triple(lastObject, RDF.first.asNode(), n)));
        if (i + 1 < objs.length) {
            Node nextObject = NodeFactory.createBlankNode();
            addWhere(new TriplePath(new Triple(lastObject, RDF.rest.asNode(), nextObject)));
            lastObject = nextObject;
        } else {
            addWhere(new TriplePath(new Triple(lastObject, RDF.rest.asNode(), RDF.nil.asNode())));
        }
    }
    return retval;
}
Also used : FrontsTriple(org.apache.jena.graph.FrontsTriple) Triple(org.apache.jena.graph.Triple) Node(org.apache.jena.graph.Node) TriplePath(org.apache.jena.sparql.core.TriplePath)

Example 32 with TriplePath

use of org.apache.jena.sparql.core.TriplePath in project jena by apache.

the class PathCompiler method reduce.

private static void reduce(PathBlock x, VarAlloc varAlloc, Node startNode, Path path, Node endNode) {
    if (path instanceof P_Link) {
        Node pred = ((P_Link) path).getNode();
        Triple t = new Triple(startNode, pred, endNode);
        x.add(new TriplePath(t));
        return;
    }
    if (path instanceof P_Seq) {
        P_Seq ps = (P_Seq) path;
        Node v = varAlloc.allocVar();
        reduce(x, varAlloc, startNode, ps.getLeft(), v);
        reduce(x, varAlloc, v, ps.getRight(), endNode);
        return;
    }
    if (path instanceof P_Inverse) {
        reduce(x, varAlloc, endNode, ((P_Inverse) path).getSubPath(), startNode);
        return;
    }
    if (path instanceof P_FixedLength) {
        P_FixedLength pFixed = (P_FixedLength) path;
        long N = pFixed.getCount();
        if (N > 0) {
            // Don't do {0}
            Node stepStart = startNode;
            for (long i = 0; i < N - 1; i++) {
                Node v = varAlloc.allocVar();
                reduce(x, varAlloc, stepStart, pFixed.getSubPath(), v);
                stepStart = v;
            }
            reduce(x, varAlloc, stepStart, pFixed.getSubPath(), endNode);
            return;
        }
    }
    if (path instanceof P_Mod) {
        P_Mod pMod = (P_Mod) path;
        if (pMod.isFixedLength() && pMod.getFixedLength() > 0) {
            long N = pMod.getFixedLength();
            if (N > 0) {
                Node stepStart = startNode;
                for (long i = 0; i < N - 1; i++) {
                    Node v = varAlloc.allocVar();
                    reduce(x, varAlloc, stepStart, pMod.getSubPath(), v);
                    stepStart = v;
                }
                reduce(x, varAlloc, stepStart, pMod.getSubPath(), endNode);
                return;
            }
        }
        if (pMod.getMin() > 0) {
            Path p1 = PathFactory.pathFixedLength(pMod.getSubPath(), pMod.getMin());
            Path p2;
            if (pMod.getMax() < 0)
                p2 = PathFactory.pathZeroOrMoreN(pMod.getSubPath());
            else {
                long len2 = pMod.getMax() - pMod.getMin();
                if (len2 < 0)
                    len2 = 0;
                p2 = PathFactory.pathMod(pMod.getSubPath(), 0, len2);
            }
            Node v = varAlloc.allocVar();
            // Start at the fixed end.
            if (!startNode.isVariable() || endNode.isVariable()) {
                reduce(x, varAlloc, startNode, p1, v);
                reduce(x, varAlloc, v, p2, endNode);
            } else {
                // endNode fixed, start node not.
                reduce(x, varAlloc, v, p2, endNode);
                reduce(x, varAlloc, startNode, p1, v);
            }
            return;
        }
    // Else drop through
    }
    // Nothing can be done.
    x.add(new TriplePath(startNode, path, endNode));
}
Also used : Triple(org.apache.jena.graph.Triple) TriplePath(org.apache.jena.sparql.core.TriplePath) Node(org.apache.jena.graph.Node) TriplePath(org.apache.jena.sparql.core.TriplePath)

Example 33 with TriplePath

use of org.apache.jena.sparql.core.TriplePath in project jena by apache.

the class PathLib method pathToTriples.

/** Convert any paths of exactly one predicate to a triple pattern */
public static Op pathToTriples(PathBlock pattern) {
    BasicPattern bp = null;
    Op op = null;
    for (TriplePath tp : pattern) {
        if (tp.isTriple()) {
            if (bp == null)
                bp = new BasicPattern();
            bp.add(tp.asTriple());
            continue;
        }
        // Path form.
        op = flush(bp, op);
        bp = null;
        OpPath opPath2 = new OpPath(tp);
        op = OpSequence.create(op, opPath2);
        continue;
    }
    // End. Finish off any outstanding BGP.
    op = flush(bp, op);
    return op;
}
Also used : Op(org.apache.jena.sparql.algebra.Op) OpPath(org.apache.jena.sparql.algebra.op.OpPath) BasicPattern(org.apache.jena.sparql.core.BasicPattern) TriplePath(org.apache.jena.sparql.core.TriplePath)

Aggregations

TriplePath (org.apache.jena.sparql.core.TriplePath)33 Triple (org.apache.jena.graph.Triple)22 Test (org.junit.Test)18 Node (org.apache.jena.graph.Node)10 WhereHandler (org.apache.jena.arq.querybuilder.handlers.WhereHandler)4 Query (org.apache.jena.query.Query)4 Path (org.apache.jena.sparql.path.Path)4 SelectBuilder (org.apache.jena.arq.querybuilder.SelectBuilder)3 PrefixMapping (org.apache.jena.shared.PrefixMapping)3 PrefixMappingImpl (org.apache.jena.shared.impl.PrefixMappingImpl)3 Var (org.apache.jena.sparql.core.Var)3 HashMap (java.util.HashMap)2 Op (org.apache.jena.sparql.algebra.Op)2 OpPath (org.apache.jena.sparql.algebra.op.OpPath)2 BasicPattern (org.apache.jena.sparql.core.BasicPattern)2 ContractTest (org.xenei.junit.contract.ContractTest)2 FrontsTriple (org.apache.jena.graph.FrontsTriple)1 PathBlock (org.apache.jena.sparql.core.PathBlock)1 ElementPathBlock (org.apache.jena.sparql.syntax.ElementPathBlock)1