use of org.eclipse.rdf4j.query.algebra.SameTerm 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.SameTerm in project rdf4j by eclipse.
the class QueryModelBuilder method visit.
@Override
public SameTerm visit(ASTSameTerm node, Object data) throws VisitorException {
ValueExpr leftArg = (ValueExpr) node.getLeftOperand().jjtAccept(this, null);
ValueExpr rightArg = (ValueExpr) node.getRightOperand().jjtAccept(this, null);
return new SameTerm(leftArg, rightArg);
}
use of org.eclipse.rdf4j.query.algebra.SameTerm in project rdf4j by eclipse.
the class QueryModelBuilder method visit.
@Override
public ValueExpr visit(ASTInList node, Object data) throws VisitorException {
ValueExpr leftArg = (ValueExpr) node.getValueExpr().jjtAccept(this, null);
ValueExpr result = null;
for (ASTValueExpr argExpr : node.getArgList().getElements()) {
ValueExpr rightArg = (ValueExpr) argExpr.jjtAccept(this, null);
if (result == null) {
// First argument
result = new SameTerm(leftArg, rightArg);
} else {
SameTerm sameTerm = new SameTerm(leftArg.clone(), rightArg);
result = new Or(result, sameTerm);
}
}
assert result != null;
return result;
}
use of org.eclipse.rdf4j.query.algebra.SameTerm in project rdf4j by eclipse.
the class QueryModelBuilder method visit.
@Override
public List<Var> visit(ASTNode node, Object data) throws VisitorException {
List<Var> nodeVars = new ArrayList<Var>();
for (ASTNodeElem nodeElem : node.getNodeElemList()) {
Var nodeVar = (Var) nodeElem.jjtAccept(this, null);
nodeVars.add(nodeVar);
}
// Create any implicit unequalities
for (int i = 0; i < nodeVars.size() - 1; i++) {
Var var1 = nodeVars.get(i);
for (int j = i + 1; j < nodeVars.size(); j++) {
Var var2 = nodeVars.get(j);
// for the unequality to make any sense:
if (!var1.hasValue() || !var2.hasValue()) {
graphPattern.addConstraint(new Not(new SameTerm(var1, var2)));
}
}
}
return nodeVars;
}
use of org.eclipse.rdf4j.query.algebra.SameTerm in project rdf4j by eclipse.
the class TupleExprBuilder method replaceSameTermVars.
private List<StatementPattern> replaceSameTermVars(List<StatementPattern> statementPatterns, Set<SameTerm> sameTermConstraints) {
if (sameTermConstraints != null) {
for (SameTerm st : sameTermConstraints) {
Var left = (Var) st.getLeftArg();
Var right = (Var) st.getRightArg();
for (StatementPattern sp : statementPatterns) {
Var subj = sp.getSubjectVar();
Var obj = sp.getObjectVar();
if (subj.equals(left) || subj.equals(right)) {
if (obj.equals(left) || obj.equals(right)) {
sp.setObjectVar(subj);
}
}
}
}
}
return statementPatterns;
}
Aggregations