use of org.eclipse.rdf4j.query.algebra.TupleExpr in project rdf4j by eclipse.
the class TupleExprBuilder method visit.
@Override
public Object visit(ASTPathAlternative pathAltNode, Object data) throws VisitorException {
int altCount = pathAltNode.jjtGetNumChildren();
if (altCount > 1) {
GraphPattern parentGP = graphPattern;
Union union = new Union();
Union currentUnion = union;
for (int i = 0; i < altCount - 1; i++) {
graphPattern = new GraphPattern(parentGP);
pathAltNode.jjtGetChild(i).jjtAccept(this, data);
TupleExpr arg = graphPattern.buildTupleExpr();
currentUnion.setLeftArg(arg);
if (i == altCount - 2) {
// second-to-last item
graphPattern = new GraphPattern(parentGP);
pathAltNode.jjtGetChild(i + 1).jjtAccept(this, data);
arg = graphPattern.buildTupleExpr();
currentUnion.setRightArg(arg);
} else {
Union newUnion = new Union();
currentUnion.setRightArg(newUnion);
currentUnion = newUnion;
}
}
parentGP.addRequiredTE(union);
graphPattern = parentGP;
} else {
pathAltNode.jjtGetChild(0).jjtAccept(this, data);
}
return null;
}
use of org.eclipse.rdf4j.query.algebra.TupleExpr in project rdf4j by eclipse.
the class TupleExprBuilder method visit.
@Override
public TupleExpr visit(ASTDescribe node, Object data) throws VisitorException {
TupleExpr tupleExpr = (TupleExpr) data;
if (tupleExpr == null) {
tupleExpr = new SingletonSet();
}
Extension e = new Extension();
ProjectionElemList projectionElements = new ProjectionElemList();
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
ValueExpr resource = (ValueExpr) node.jjtGetChild(i).jjtAccept(this, null);
if (resource instanceof Var) {
projectionElements.addElement(new ProjectionElem(((Var) resource).getName()));
} else {
String alias = "_describe_" + UUID.randomUUID().toString().replaceAll("-", "_");
ExtensionElem elem = new ExtensionElem(resource, alias);
e.addElement(elem);
projectionElements.addElement(new ProjectionElem(alias));
}
}
if (!e.getElements().isEmpty()) {
e.setArg(tupleExpr);
tupleExpr = e;
}
Projection p = new Projection(tupleExpr, projectionElements);
return new DescribeOperator(p);
}
use of org.eclipse.rdf4j.query.algebra.TupleExpr in project rdf4j by eclipse.
the class TupleExprBuilder method createPath.
private TupleExpr createPath(Scope scope, Var subjVar, TupleExpr pathExpression, Var endVar, Var contextVar, long length) throws VisitorException {
if (pathExpression instanceof StatementPattern) {
Var predVar = ((StatementPattern) pathExpression).getPredicateVar();
if (length == 0L) {
return new ZeroLengthPath(scope, subjVar, endVar, contextVar);
} else {
GraphPattern gp = new GraphPattern();
gp.setContextVar(contextVar);
gp.setStatementPatternScope(scope);
Var nextVar = null;
for (long i = 0L; i < length; i++) {
if (i < length - 1) {
nextVar = createAnonVar();
} else {
nextVar = endVar;
}
gp.addRequiredSP(subjVar, predVar, nextVar);
subjVar = nextVar;
}
return gp.buildTupleExpr();
}
} else {
if (length == 0L) {
return new ZeroLengthPath(scope, subjVar, endVar, contextVar);
} else {
GraphPattern gp = new GraphPattern();
gp.setContextVar(contextVar);
gp.setStatementPatternScope(scope);
Var nextVar = null;
for (long i = 0L; i < length; i++) {
if (i < length - 1L) {
nextVar = createAnonVar();
} else {
nextVar = endVar;
}
// create a clone of the path expression.
TupleExpr clone = pathExpression.clone();
VarReplacer replacer = new VarReplacer(endVar, nextVar);
clone.visit(replacer);
gp.addRequiredTE(clone);
subjVar = nextVar;
}
return gp.buildTupleExpr();
}
}
}
use of org.eclipse.rdf4j.query.algebra.TupleExpr in project rdf4j by eclipse.
the class TupleExprBuilder method visit.
@Override
public Object visit(ASTGraphPatternGroup node, Object data) throws VisitorException {
GraphPattern parentGP = graphPattern;
graphPattern = new GraphPattern(parentGP);
boolean optionalPatternInGroup = false;
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
if (optionalPatternInGroup) {
// building the LeftJoin and resetting the graphPattern.
TupleExpr te = graphPattern.buildTupleExpr();
graphPattern = new GraphPattern(parentGP);
graphPattern.addRequiredTE(te);
optionalPatternInGroup = false;
}
Node childNode = node.jjtGetChild(i);
data = childNode.jjtAccept(this, data);
if (childNode instanceof ASTOptionalGraphPattern) {
optionalPatternInGroup = true;
}
}
// Filters are scoped to the graph pattern group and do not affect
// bindings external to the group
TupleExpr te = graphPattern.buildTupleExpr();
parentGP.addRequiredTE(te);
graphPattern = parentGP;
return te;
}
use of org.eclipse.rdf4j.query.algebra.TupleExpr in project rdf4j by eclipse.
the class TupleExprBuilder method visit.
@Override
public TupleExpr visit(ASTAskQuery node, Object data) throws VisitorException {
graphPattern = new GraphPattern();
super.visit(node, null);
TupleExpr tupleExpr = graphPattern.buildTupleExpr();
tupleExpr = new Slice(tupleExpr, 0, 1);
// Apply grouping
Group group = null;
ASTGroupClause groupNode = node.getGroupClause();
if (groupNode != null) {
tupleExpr = (TupleExpr) groupNode.jjtAccept(this, tupleExpr);
group = (Group) tupleExpr;
}
final ASTHavingClause havingClause = node.getHavingClause();
if (havingClause != null) {
if (group == null) {
// create implicit group
group = new Group(tupleExpr);
}
// Apply HAVING group filter condition
tupleExpr = processHavingClause(havingClause, tupleExpr, group);
}
// process bindings clause
final ASTBindingsClause bindingsClause = node.getBindingsClause();
if (bindingsClause != null) {
tupleExpr = new Join((BindingSetAssignment) bindingsClause.jjtAccept(this, null), tupleExpr);
}
final ASTOrderClause orderClause = node.getOrderClause();
if (orderClause != null) {
if (group == null) {
// create implicit group
group = new Group(tupleExpr);
}
// Apply result ordering
tupleExpr = processOrderClause(node.getOrderClause(), tupleExpr, group);
}
return tupleExpr;
}
Aggregations