use of org.eclipse.rdf4j.query.algebra.ValueExpr in project rdf4j by eclipse.
the class TupleExprBuilder method replaceVarOccurrence.
private TupleExpr replaceVarOccurrence(TupleExpr te, List<ValueExpr> objectList, Var replacementVar) throws VisitorException {
for (ValueExpr objExpr : objectList) {
Var objVar = mapValueExprToVar(objExpr);
VarReplacer replacer = new VarReplacer(objVar, replacementVar);
te.visit(replacer);
}
return te;
}
use of org.eclipse.rdf4j.query.algebra.ValueExpr in project rdf4j by eclipse.
the class TupleExprBuilder method visit.
@Override
public TupleExpr visit(ASTConstruct node, Object data) throws VisitorException {
TupleExpr result = (TupleExpr) data;
// Collect construct triples
graphPattern = new GraphPattern();
super.visit(node, null);
TupleExpr constructExpr = graphPattern.buildTupleExpr();
// Retrieve all StatementPatterns from the construct expression
List<StatementPattern> statementPatterns = StatementPatternCollector.process(constructExpr);
if (constructExpr instanceof Filter) {
// sameTerm filters in construct (this can happen when there's a
// cyclic
// path defined, see SES-1685 and SES-2104)
// we remove the sameTerm filters by simply replacing all mapped
// variable occurrences
Set<SameTerm> sameTermConstraints = getSameTermConstraints((Filter) constructExpr);
statementPatterns = replaceSameTermVars(statementPatterns, sameTermConstraints);
}
Set<Var> constructVars = getConstructVars(statementPatterns);
VarCollector whereClauseVarCollector = new VarCollector();
result.visit(whereClauseVarCollector);
// Create BNodeGenerators for all anonymous variables
// NB: preserve order for a deterministic output
Map<Var, ExtensionElem> extElemMap = new LinkedHashMap<Var, ExtensionElem>();
for (Var var : constructVars) {
if (var.isAnonymous() && !extElemMap.containsKey(var)) {
ValueExpr valueExpr;
if (var.hasValue()) {
valueExpr = new ValueConstant(var.getValue());
} else {
valueExpr = new BNodeGenerator();
}
extElemMap.put(var, new ExtensionElem(valueExpr, var.getName()));
} else if (!whereClauseVarCollector.collectedVars.contains(var)) {
// non-anon var in construct clause not present in where clause
if (!extElemMap.containsKey(var)) {
// assign non-anonymous vars not present in where clause as
// extension elements. This is necessary to make external
// binding
// assingnment possible (see SES-996)
extElemMap.put(var, new ExtensionElem(var, var.getName()));
}
}
}
if (!extElemMap.isEmpty()) {
result = new Extension(result, extElemMap.values());
}
// Create a Projection for each StatementPattern in the constructor
List<ProjectionElemList> projList = new ArrayList<ProjectionElemList>();
for (StatementPattern sp : statementPatterns) {
ProjectionElemList projElemList = new ProjectionElemList();
projElemList.addElement(new ProjectionElem(sp.getSubjectVar().getName(), "subject"));
projElemList.addElement(new ProjectionElem(sp.getPredicateVar().getName(), "predicate"));
projElemList.addElement(new ProjectionElem(sp.getObjectVar().getName(), "object"));
if (sp.getContextVar() != null) {
projElemList.addElement(new ProjectionElem(sp.getContextVar().getName(), "context"));
}
projList.add(projElemList);
}
if (projList.size() == 1) {
result = new Projection(result, projList.get(0));
} else if (projList.size() > 1) {
result = new MultiProjection(result, projList);
} else {
// Empty constructor
result = new EmptySet();
}
return new Reduced(result);
}
use of org.eclipse.rdf4j.query.algebra.ValueExpr in project rdf4j by eclipse.
the class TupleExprBuilder method visit.
@Override
public Object visit(ASTRegexExpression node, Object data) throws VisitorException {
ValueExpr arg = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, null);
ValueExpr pattern = (ValueExpr) node.jjtGetChild(1).jjtAccept(this, null);
ValueExpr flags = null;
if (node.jjtGetNumChildren() > 2) {
flags = (ValueExpr) node.jjtGetChild(2).jjtAccept(this, null);
}
return new Regex(arg, pattern, flags);
}
use of org.eclipse.rdf4j.query.algebra.ValueExpr in project rdf4j by eclipse.
the class TupleExprBuilder method visit.
@Override
public Object visit(ASTGraphGraphPattern node, Object data) throws VisitorException {
Var oldContext = graphPattern.getContextVar();
Scope oldScope = graphPattern.getStatementPatternScope();
ValueExpr newContext = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, null);
graphPattern.setContextVar(mapValueExprToVar(newContext));
graphPattern.setStatementPatternScope(Scope.NAMED_CONTEXTS);
node.jjtGetChild(1).jjtAccept(this, null);
graphPattern.setContextVar(oldContext);
graphPattern.setStatementPatternScope(oldScope);
return null;
}
use of org.eclipse.rdf4j.query.algebra.ValueExpr in project rdf4j by eclipse.
the class TupleExprBuilder method visit.
@Override
public Object visit(ASTOptionalGraphPattern node, Object data) throws VisitorException {
GraphPattern parentGP = graphPattern;
graphPattern = new GraphPattern(parentGP);
super.visit(node, null);
// remove filter conditions from graph pattern for inclusion as
// conditions
// in the OptionalTE
List<ValueExpr> optionalConstraints = graphPattern.removeAllConstraints();
TupleExpr optional = graphPattern.buildTupleExpr();
graphPattern = parentGP;
graphPattern.addOptionalTE(optional, optionalConstraints);
return null;
}
Aggregations