use of org.eclipse.rdf4j.query.algebra.Var in project rdf4j by eclipse.
the class ConstructorBuilder method buildConstructor.
private TupleExpr buildConstructor(TupleExpr bodyExpr, TupleExpr constructExpr, boolean explicitConstructor, boolean distinct, boolean reduced) {
TupleExpr result = bodyExpr;
// Retrieve all StatementPattern's from the construct expression
List<StatementPattern> statementPatterns = StatementPatternCollector.process(constructExpr);
Set<Var> constructVars = getConstructVars(statementPatterns);
// Finally, the spo-bindings are again filtered for duplicates.
if (distinct || reduced) {
// Create projection that removes all bindings that are not used in the
// constructor
ProjectionElemList projElemList = new ProjectionElemList();
for (Var var : constructVars) {
// the distinct
if (!var.isAnonymous() && !var.hasValue()) {
projElemList.addElement(new ProjectionElem(var.getName()));
}
}
result = new Projection(result, projElemList);
// Filter the duplicates from these projected bindings
if (distinct) {
result = new Distinct(result);
} else {
result = new Reduced(result);
}
}
// Create BNodeGenerator's for all anonymous variables
Map<Var, ExtensionElem> extElemMap = new HashMap<Var, ExtensionElem>();
for (Var var : constructVars) {
if (var.isAnonymous() && !extElemMap.containsKey(var)) {
ValueExpr valueExpr = null;
if (var.hasValue()) {
valueExpr = new ValueConstant(var.getValue());
} else if (explicitConstructor) {
// only generate bnodes in case of an explicit constructor
valueExpr = new BNodeGenerator();
}
if (valueExpr != null) {
extElemMap.put(var, new ExtensionElem(valueExpr, var.getName()));
}
}
}
if (!extElemMap.isEmpty()) {
result = new Extension(result, extElemMap.values());
}
// Create a Projection for each StatementPattern in the constructor
List<ProjectionElemList> projections = 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"));
projections.add(projElemList);
}
if (projections.size() == 1) {
result = new Projection(result, projections.get(0));
// Note: no need to apply the second duplicate elimination step if
// there's just one projection
} else if (projections.size() > 1) {
result = new MultiProjection(result, projections);
if (distinct) {
// Add another distinct to filter duplicate statements
result = new Distinct(result);
} else if (reduced) {
result = new Reduced(result);
}
} else {
// Empty constructor
result = new EmptySet();
}
return result;
}
use of org.eclipse.rdf4j.query.algebra.Var in project rdf4j by eclipse.
the class QueryModelBuilder method visit.
@Override
public Object visit(ASTFrom node, Object data) throws VisitorException {
StatementPattern.Scope scope = StatementPattern.Scope.DEFAULT_CONTEXTS;
Var contextVar = null;
if (node.hasContextID()) {
scope = StatementPattern.Scope.NAMED_CONTEXTS;
ValueExpr contextID = (ValueExpr) node.getContextID().jjtAccept(this, null);
if (contextID instanceof Var) {
contextVar = (Var) contextID;
} else if (contextID instanceof ValueConstant) {
ValueConstant vc = (ValueConstant) contextID;
contextVar = createConstantVar(vc.getValue());
} else {
throw new IllegalArgumentException("Unexpected contextID result type: " + contextID.getClass());
}
}
graphPattern.setStatementPatternScope(scope);
graphPattern.setContextVar(contextVar);
node.getPathExpr().jjtAccept(this, null);
return null;
}
use of org.eclipse.rdf4j.query.algebra.Var in project rdf4j by eclipse.
the class QueryModelBuilder method createConstantVar.
/*---------*
* Methods *
*---------*/
private Var createConstantVar(Value value) {
Var var = new Var("-const-" + constantVarID++);
var.setAnonymous(true);
var.setValue(value);
return var;
}
use of org.eclipse.rdf4j.query.algebra.Var in project rdf4j by eclipse.
the class QueryModelBuilder method visit.
@Override
public TupleExpr visit(ASTSelect node, Object data) throws VisitorException {
TupleExpr result = (TupleExpr) data;
Extension extension = new Extension();
ProjectionElemList projElemList = new ProjectionElemList();
for (ASTProjectionElem projElemNode : node.getProjectionElemList()) {
ValueExpr valueExpr = (ValueExpr) projElemNode.getValueExpr().jjtAccept(this, null);
String alias = projElemNode.getAlias();
if (alias != null) {
// aliased projection element
extension.addElement(new ExtensionElem(valueExpr, alias));
projElemList.addElement(new ProjectionElem(alias));
} else if (valueExpr instanceof Var) {
// unaliased variable
Var projVar = (Var) valueExpr;
projElemList.addElement(new ProjectionElem(projVar.getName()));
} else {
throw new IllegalStateException("required alias for non-Var projection elements not found");
}
}
if (!extension.getElements().isEmpty()) {
extension.setArg(result);
result = extension;
}
result = new Projection(result, projElemList);
if (node.isDistinct()) {
result = new Distinct(result);
} else if (node.isReduced()) {
result = new Reduced(result);
}
return result;
}
use of org.eclipse.rdf4j.query.algebra.Var 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;
}
Aggregations