use of org.eclipse.rdf4j.query.algebra.StatementPattern in project rdf4j by eclipse.
the class ConstructorBuilder method getConstructVars.
/**
* Gets the set of variables that are relevant for the constructor. This method accumulates all subject,
* predicate and object variables from the supplied statement patterns, but ignores any context variables.
*/
private Set<Var> getConstructVars(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());
}
return vars;
}
use of org.eclipse.rdf4j.query.algebra.StatementPattern 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.StatementPattern in project rdf4j by eclipse.
the class SPARQLParserTest method testSES1922PathSequenceWithValueConstant.
@Test
public void testSES1922PathSequenceWithValueConstant() throws Exception {
StringBuilder qb = new StringBuilder();
qb.append("ASK {?A (<foo:bar>)/<foo:foo> <foo:objValue>} ");
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();
assertTrue(leftArg.getObjectVar().equals(rightArg.getSubjectVar()));
assertEquals(leftArg.getObjectVar().getName(), rightArg.getSubjectVar().getName());
}
use of org.eclipse.rdf4j.query.algebra.StatementPattern in project rdf4j by eclipse.
the class SPARQLParserTest method testSES1927UnequalLiteralValueConstants1.
@Test
public void testSES1927UnequalLiteralValueConstants1() throws Exception {
StringBuilder qb = new StringBuilder();
qb.append("ASK {?a <foo:bar> \"test\". ?a <foo:foo> \"test\"@en .} ");
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());
}
use of org.eclipse.rdf4j.query.algebra.StatementPattern 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;
}
Aggregations