use of org.eclipse.rdf4j.query.algebra.StatementPattern in project rdf4j by eclipse.
the class QueryModelBuilder method visit.
@Override
public Object visit(ASTBasicPathExprTail tailNode, Object data) throws VisitorException {
List<Var> subjVars = (List<Var>) data;
Var predVar = (Var) tailNode.getEdge().jjtAccept(this, null);
List<Var> objVars = (List<Var>) tailNode.getNode().jjtAccept(this, null);
Var contextVar = graphPattern.getContextVar();
StatementPattern.Scope spScope = graphPattern.getStatementPatternScope();
for (Var subjVar : subjVars) {
for (Var objVar : objVars) {
StatementPattern sp = new StatementPattern(spScope, subjVar, predVar, objVar, contextVar);
graphPattern.addRequiredTE(sp);
}
}
// Process next tail segment
ASTPathExprTail nextTailNode = tailNode.getNextTail();
if (nextTailNode != null) {
List<Var> joinVars = nextTailNode.isBranch() ? subjVars : objVars;
nextTailNode.jjtAccept(this, joinVars);
}
return null;
}
use of org.eclipse.rdf4j.query.algebra.StatementPattern in project rdf4j by eclipse.
the class QueryModelBuilder method visit.
@Override
public Var visit(ASTReifiedStat node, Object data) throws VisitorException {
assert node.getID() != null : "ID variable not set";
Var subjVar = (Var) node.getSubject().jjtAccept(this, null);
Var predVar = (Var) node.getPredicate().jjtAccept(this, null);
Var objVar = (Var) node.getObject().jjtAccept(this, null);
Var idVar = (Var) node.getID().jjtAccept(this, null);
Var contextVar = graphPattern.getContextVar();
StatementPattern.Scope spScope = graphPattern.getStatementPatternScope();
Var rdfType = new Var("_rdfType", RDF.TYPE);
Var rdfStatement = new Var("_rdfStatement", RDF.STATEMENT);
Var rdfSubject = new Var("_rdfSubject", RDF.SUBJECT);
Var rdfPredicate = new Var("_rdfPredicate", RDF.PREDICATE);
Var rdfObject = new Var("_rdfObject", RDF.OBJECT);
graphPattern.addRequiredTE(new StatementPattern(spScope, idVar, rdfType, rdfStatement, contextVar));
graphPattern.addRequiredTE(new StatementPattern(spScope, idVar, rdfSubject, subjVar, contextVar));
graphPattern.addRequiredTE(new StatementPattern(spScope, idVar, rdfPredicate, predVar, contextVar));
graphPattern.addRequiredTE(new StatementPattern(spScope, idVar, rdfObject, objVar, contextVar));
return idVar;
}
use of org.eclipse.rdf4j.query.algebra.StatementPattern 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.StatementPattern in project rdf4j by eclipse.
the class UpdateExprBuilder method getProjectionVars.
private Set<Var> getProjectionVars(Collection<StatementPattern> statementPatterns) {
Set<Var> vars = new LinkedHashSet<Var>(statementPatterns.size() * 2);
for (StatementPattern sp : statementPatterns) {
vars.add(sp.getSubjectVar());
vars.add(sp.getPredicateVar());
vars.add(sp.getObjectVar());
if (sp.getContextVar() != null) {
vars.add(sp.getContextVar());
}
}
return vars;
}
use of org.eclipse.rdf4j.query.algebra.StatementPattern in project rdf4j by eclipse.
the class SPARQLParserTest method testSES1927UnequalLiteralValueConstants2.
@Test
public void testSES1927UnequalLiteralValueConstants2() throws Exception {
StringBuilder qb = new StringBuilder();
qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"^^<foo:bar> .} ");
ParsedQuery q = parser.parseQuery(qb.toString(), null);
TupleExpr te = q.getTupleExpr();
assertNotNull(te);
assertTrue(te instanceof Slice);
Slice s = (Slice) te;
assertTrue(s.getArg() instanceof Join);
Join j = (Join) s.getArg();
assertTrue(j.getLeftArg() instanceof StatementPattern);
assertTrue(j.getRightArg() instanceof StatementPattern);
StatementPattern leftArg = (StatementPattern) j.getLeftArg();
StatementPattern rightArg = (StatementPattern) j.getRightArg();
assertFalse(leftArg.getObjectVar().equals(rightArg.getObjectVar()));
assertNotEquals(leftArg.getObjectVar().getName(), rightArg.getObjectVar().getName());
}
Aggregations