use of org.eclipse.rdf4j.query.algebra.TupleExpr 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.TupleExpr in project rdf4j by eclipse.
the class UpdateExprBuilder method visit.
@Override
public TupleExpr visit(ASTQuadsNotTriples node, Object data) throws VisitorException {
GraphPattern parentGP = graphPattern;
graphPattern = new GraphPattern();
ValueExpr contextNode = (ValueExpr) node.jjtGetChild(0).jjtAccept(this, data);
Var contextVar = mapValueExprToVar(contextNode);
graphPattern.setContextVar(contextVar);
graphPattern.setStatementPatternScope(Scope.NAMED_CONTEXTS);
for (int i = 1; i < node.jjtGetNumChildren(); i++) {
node.jjtGetChild(i).jjtAccept(this, data);
}
TupleExpr result = graphPattern.buildTupleExpr();
parentGP.addRequiredTE(result);
graphPattern = parentGP;
return result;
}
use of org.eclipse.rdf4j.query.algebra.TupleExpr in project rdf4j by eclipse.
the class UpdateExprBuilder method visit.
@Override
public TupleExpr visit(ASTInsertClause node, Object data) throws VisitorException {
TupleExpr result = (TupleExpr) data;
// Collect insert clause triples
GraphPattern parentGP = graphPattern;
graphPattern = new GraphPattern();
// inherit scope & context
graphPattern.setStatementPatternScope(parentGP.getStatementPatternScope());
graphPattern.setContextVar(parentGP.getContextVar());
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
node.jjtGetChild(i).jjtAccept(this, data);
}
TupleExpr insertExpr = graphPattern.buildTupleExpr();
graphPattern = parentGP;
return insertExpr;
}
use of org.eclipse.rdf4j.query.algebra.TupleExpr in project rdf4j by eclipse.
the class UpdateExprBuilder method visit.
@Override
public TupleExpr visit(ASTDeleteClause node, Object data) throws VisitorException {
TupleExpr result = (TupleExpr) data;
// Collect construct triples
GraphPattern parentGP = graphPattern;
graphPattern = new GraphPattern();
// inherit scope & context
graphPattern.setStatementPatternScope(parentGP.getStatementPatternScope());
graphPattern.setContextVar(parentGP.getContextVar());
for (int i = 0; i < node.jjtGetNumChildren(); i++) {
node.jjtGetChild(i).jjtAccept(this, data);
}
TupleExpr deleteExpr = graphPattern.buildTupleExpr();
// FIXME we should adapt the grammar so we can avoid doing this in
// post-processing.
VarCollector collector = new VarCollector();
deleteExpr.visit(collector);
for (Var var : collector.getCollectedVars()) {
if (var.isAnonymous() && !var.hasValue()) {
// blank node in delete pattern, not allowed by SPARQL spec.
throw new VisitorException("DELETE clause may not contain blank nodes");
}
}
graphPattern = parentGP;
return deleteExpr;
}
use of org.eclipse.rdf4j.query.algebra.TupleExpr in project rdf4j by eclipse.
the class UpdateExprBuilder method visit.
@Override
public Modify visit(ASTModify node, Object data) throws VisitorException {
ASTWhereClause whereClause = node.getWhereClause();
TupleExpr where = null;
if (whereClause != null) {
where = (TupleExpr) whereClause.jjtAccept(this, data);
}
TupleExpr delete = null;
ASTDeleteClause deleteNode = node.getDeleteClause();
if (deleteNode != null) {
delete = (TupleExpr) deleteNode.jjtAccept(this, data);
}
TupleExpr insert = null;
ASTInsertClause insertNode = node.getInsertClause();
if (insertNode != null) {
insert = (TupleExpr) insertNode.jjtAccept(this, data);
}
Modify modifyExpr = new Modify(delete, insert, where);
return modifyExpr;
}
Aggregations