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;
}
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));
}
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;
}
Aggregations