use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class UpdateBuilderTest method testInsertAndDeleteWithVar.
@Test
public void testInsertAndDeleteWithVar() {
UpdateBuilder builder = new UpdateBuilder();
Var v = Var.alloc("v");
builder.addInsert(new Quad(g, s, v, o));
builder.addDelete(new Triple(s, v, o));
builder.addWhere(null, v, "foo");
builder.setVar(v, p);
Update update = builder.build();
assertTrue(update instanceof UpdateModify);
UpdateModify um = (UpdateModify) update;
List<Quad> quads = um.getInsertQuads();
assertEquals(1, quads.size());
Quad q = quads.get(0);
assertEquals(g, q.getGraph());
assertEquals(s, q.getSubject());
assertEquals(p, q.getPredicate());
assertEquals(o, q.getObject());
quads = um.getDeleteQuads();
assertEquals(1, quads.size());
q = quads.get(0);
assertEquals(Quad.defaultGraphNodeGenerated, q.getGraph());
assertEquals(s, q.getSubject());
assertEquals(p, q.getPredicate());
assertEquals(o, q.getObject());
Element e = um.getWherePattern();
assertTrue(e instanceof ElementGroup);
ElementGroup eg = (ElementGroup) e;
assertEquals(1, eg.getElements().size());
ElementPathBlock epb = (ElementPathBlock) eg.getElements().get(0);
Triple t = epb.getPattern().get(0).asTriple();
assertEquals(Node.ANY, t.getSubject());
assertEquals(p, t.getPredicate());
assertEquals(builder.makeNode("foo"), t.getObject());
}
use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class QueryExecDataset method describe.
// -- Describe
@Override
public Graph describe(Graph graph) {
checkNotClosed();
Model model = ModelFactory.createModelForGraph(graph);
if (!query.isDescribeType())
throw new QueryExecException("Attempt to get a DESCRIBE result from a " + labelForQuery(query) + " query");
query.setResultVars();
// columns).
if (query.getQueryPattern() == null)
query.setQueryPattern(new ElementGroup());
Set<Node> set = new HashSet<>();
RowSet rows = execute();
// Prefixes for result (after initialization)
insertPrefixesInto(graph);
// Variables in DESCRIBE
if (rows != null) {
// Single pass over rows.
rows.forEachRemaining(row -> {
for (Var var : rows.getResultVars()) {
Node n = row.get(var);
if (n != null)
set.add(n);
}
});
}
// Any URIs in the DESCRIBE
if (query.getResultURIs() != null) {
query.getResultURIs().forEach(set::add);
}
// DescribeHandlers work on models.
// Create new handlers for this process.
List<DescribeHandler> dhList = DescribeHandlerRegistry.get().newHandlerList();
getContext().put(ARQConstants.sysCurrentDataset, getDataset());
// Notify start of describe phase
for (DescribeHandler dh : dhList) dh.start(model, getContext());
// Do describe for each resource found.
for (Node n : set) {
RDFNode rdfNode = ModelUtils.convertGraphNodeToRDFNode(n, model);
if (rdfNode instanceof Resource) {
for (DescribeHandler dh : dhList) {
dh.describe((Resource) rdfNode);
}
} else {
// Can't describe literals
continue;
}
}
for (DescribeHandler dh : dhList) dh.finish();
this.close();
return graph;
}
use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class TestParameterizedSparqlString method test_param_string_injection_05.
@Test
public void test_param_string_injection_05() {
// This injection attempt results in a valid query but a failed
// injection
String str = "PREFIX : <http://example/>\nSELECT * WHERE { <s> <p> ?var2 . }";
ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
pss.setLiteral("var2", "hello\" . ?s ?p ?o");
Query q = pss.asQuery();
Element el = q.getQueryPattern();
if (el instanceof ElementTriplesBlock) {
Assert.assertEquals(1, ((ElementTriplesBlock) q.getQueryPattern()).getPattern().size());
} else if (el instanceof ElementGroup) {
Assert.assertEquals(1, ((ElementGroup) el).getElements().size());
el = ((ElementGroup) el).getElements().get(0);
if (el instanceof ElementTriplesBlock) {
Assert.assertEquals(1, ((ElementTriplesBlock) el).getPattern().size());
}
}
}
use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class BuildElementVisitor method visit.
@Override
public void visit(ElementUnion el) {
List<Element> lst = el.getElements();
if (lst.size() <= 1) {
ElementGroup eg = new ElementGroup();
if (lst.size() == 1) {
el.getElements().get(0).visit(this);
eg.addElement(result);
}
result = eg;
} else {
updateList(lst);
result = el;
}
}
use of org.apache.jena.sparql.syntax.ElementGroup in project jena by apache.
the class ElementRewriter method visit.
@Override
public void visit(ElementGroup el) {
ElementGroup retval = new ElementGroup();
for (Element e : el.getElements()) {
e.visit(this);
retval.addElement(getResult());
}
push(retval);
}
Aggregations