use of org.apache.jena.sparql.core.Var in project jena by apache.
the class TestOpVars method check.
private static void check(String[] varsExpected, Collection<Var> varsFound) {
Var[] vars = new Var[varsExpected.length];
for (int i = 0; i < varsExpected.length; i++) {
Var v = Var.alloc(varsExpected[i]);
vars[i] = v;
}
List<Var> varList = Arrays.asList(vars);
HashSet<Var> varSet = new HashSet<>();
varSet.addAll(varList);
assertEquals(varSet, varsFound);
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class TestSyntaxTransform method testUpdate.
private void testUpdate(String input, String output, String varStr, String valStr) {
UpdateRequest req1 = UpdateFactory.create(PREFIX + input);
UpdateRequest reqExpected = UpdateFactory.create(PREFIX + output);
Map<Var, Node> map = new HashMap<>();
map.put(Var.alloc(varStr), SSE.parseNode(valStr));
UpdateRequest reqTrans = UpdateTransformOps.transform(req1, map);
// Crude.
String x1 = reqExpected.toString().replaceAll("[ \n\t]", "");
String x2 = reqTrans.toString().replaceAll("[ \n\t]", "");
//assertEquals(reqExpected, reqTrans) ;
assertEquals(x1, x2);
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class TestSyntaxTransform method testQuery.
private void testQuery(String input, String output, String varStr, String valStr) {
Query q1 = QueryFactory.create(PREFIX + input);
Query qExpected = QueryFactory.create(PREFIX + output);
Map<Var, Node> map = new HashMap<>();
map.put(Var.alloc(varStr), SSE.parseNode(valStr));
Query qTrans = QueryTransformOps.transform(q1, map);
assertEquals(qExpected, qTrans);
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class TestQuery method query_recursive_01.
@Test
public void query_recursive_01() {
String query = "SELECT * WHERE { SERVICE <" + serviceQuery() + "> { ?s ?p ?o . BIND(?o AS ?x) } }";
try (QueryExecution qExec = QueryExecutionFactory.sparqlService(serviceQuery(), query)) {
ResultSet rs = qExec.execSelect();
Var x = Var.alloc("x");
while (rs.hasNext()) {
Binding b = rs.nextBinding();
Assert.assertNotNull(b.get(x));
}
}
}
use of org.apache.jena.sparql.core.Var in project jena by apache.
the class TransformFilterEquality method safeToTransform.
private static boolean safeToTransform(Collection<Var> varsEquality, Op op) {
// Structure as a visitor?
if (op instanceof OpBGP || op instanceof OpQuadPattern)
return true;
if (op instanceof OpPath)
return true;
if (op instanceof OpFilter) {
OpFilter opf = (OpFilter) op;
// Expressions are always safe transform by substitution.
return safeToTransform(varsEquality, opf.getSubOp());
}
// are very rarely so deep that it matters.
if (op instanceof OpSequence) {
OpN opN = (OpN) op;
for (Op subOp : opN.getElements()) {
if (!safeToTransform(varsEquality, subOp))
return false;
}
return true;
}
if (op instanceof OpJoin || op instanceof OpUnion) {
Op2 op2 = (Op2) op;
return safeToTransform(varsEquality, op2.getLeft()) && safeToTransform(varsEquality, op2.getRight());
}
// Not safe unless filter variables are mentioned on the LHS.
if (op instanceof OpConditional || op instanceof OpLeftJoin) {
Op2 opleftjoin = (Op2) op;
if (!safeToTransform(varsEquality, opleftjoin.getLeft()) || !safeToTransform(varsEquality, opleftjoin.getRight()))
return false;
// Not only must the left and right be safe to transform,
// but the equality variable must be known to be always set.
// If the varsLeft are disjoint from assigned vars,
// we may be able to push assign down right
// (this generalises the unit table case specialcase1)
// Needs more investigation.
Op opLeft = opleftjoin.getLeft();
Set<Var> varsLeft = OpVars.visibleVars(opLeft);
if (varsLeft.containsAll(varsEquality))
return true;
return false;
}
if (op instanceof OpGraph) {
OpGraph opg = (OpGraph) op;
return safeToTransform(varsEquality, opg.getSubOp());
}
// Subquery - assume scope rewriting has already been applied.
if (op instanceof OpModifier) {
// ORDER BY?
OpModifier opMod = (OpModifier) op;
if (opMod instanceof OpProject) {
OpProject opProject = (OpProject) op;
// AS-ification.
for (Var v : opProject.getVars()) {
if (varsEquality.contains(v))
return false;
}
}
return safeToTransform(varsEquality, opMod.getSubOp());
}
if (op instanceof OpGroup) {
OpGroup opGroup = (OpGroup) op;
VarExprList varExprList = opGroup.getGroupVars();
return safeToTransform(varsEquality, varExprList) && safeToTransform(varsEquality, opGroup.getSubOp());
}
if (op instanceof OpTable) {
OpTable opTable = (OpTable) op;
if (opTable.isJoinIdentity())
return true;
}
return false;
}
Aggregations