use of org.apache.jena.sparql.syntax.Element in project jena by apache.
the class ParserARQ method parseElement.
public static Element parseElement(String string) {
final Query query = new Query();
Action action = new Action() {
@Override
public void exec(ARQParser parser) throws Exception {
Element el = parser.GroupGraphPattern();
query.setQueryPattern(el);
}
};
perform(query, string, action);
return query.getQueryPattern();
}
use of org.apache.jena.sparql.syntax.Element in project jena by apache.
the class WhereClauseTest method testBindStringVar.
@ContractTest
public void testBindStringVar() throws ParseException {
Var v = Var.alloc("foo");
WhereClause<?> whereClause = getProducer().newInstance();
AbstractQueryBuilder<?> builder = whereClause.addBind("rand()", v);
assertContainsRegex(OPEN_CURLY + BIND + OPEN_PAREN + "rand\\(\\)" + SPACE + "AS" + SPACE + var("foo") + CLOSE_PAREN + CLOSE_CURLY, builder.buildString());
builder.setVar(v, NodeFactory.createURI("three"));
Query q = builder.build();
ElementGroup eg = (ElementGroup) q.getQueryPattern();
List<Element> lst = eg.getElements();
assertEquals("Should only be one element", 1, lst.size());
assertTrue("Should have an ElementTriplesBlock", lst.get(0) instanceof ElementTriplesBlock);
ElementTriplesBlock etb = (ElementTriplesBlock) lst.get(0);
assertTrue("ElementGroup should be empty", etb.isEmpty());
}
use of org.apache.jena.sparql.syntax.Element in project jena by apache.
the class TestParameterizedSparqlString method test_param_string_positional_injection_05.
@Test
public void test_param_string_positional_injection_05() {
// This injection attempt results in a valid query but a failed
// injection
String str = "PREFIX : <http://example/>\nSELECT * WHERE { <s> <p> ? . }";
ParameterizedSparqlString pss = new ParameterizedSparqlString(str);
pss.setLiteral(0, "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.Element in project jena by apache.
the class AbstractTestUpdateGraph method testModifyInitialBindings.
@Test
public void testModifyInitialBindings() {
DatasetGraph gStore = getEmptyDatasetGraph();
defaultGraphData(gStore, data12());
namedGraphData(gStore, graphIRI, Factory.createDefaultGraph());
Binding initialBinding = BindingFactory.binding(Var.alloc("o"), o1);
UpdateModify modify = new UpdateModify();
Element element = QueryFactory.createElement("{ ?s <http://example/p> ?o }");
modify.setElement(element);
modify.getInsertAcc().addQuad(new Quad(graphIRI, triple1));
modify.getDeleteAcc().addTriple(SSE.parseTriple("(?s <http://example/p> ?o)"));
modify.getDeleteAcc().addQuad(SSE.parseQuad("(<http://example/graph> ?s <http://example/p> ?o)"));
UpdateAction.execute(modify, gStore, initialBinding);
assertFalse(graphEmpty(gStore.getGraph(graphIRI)));
assertFalse(graphEmpty(gStore.getDefaultGraph()));
assertTrue(graphContains(gStore.getGraph(graphIRI), triple1));
assertTrue(graphContains(gStore.getDefaultGraph(), triple2));
assertFalse(graphContains(gStore.getDefaultGraph(), triple1));
}
use of org.apache.jena.sparql.syntax.Element in project jena by apache.
the class AbstractTestUpdateGraph method testDelete5.
@Test
public void testDelete5() {
DatasetGraph gStore = getEmptyDatasetGraph();
defaultGraphData(gStore, data2());
namedGraphData(gStore, graphIRI, data1());
UpdateModify modify = new UpdateModify();
Element element = QueryFactory.createElement("{ ?s <http://example/p> ?o }");
modify.setElement(element);
modify.getDeleteAcc().addQuad(SSE.parseQuad("(<http://example/graph> ?s <http://example/p> 2007 )"));
UpdateAction.execute(modify, gStore);
assertTrue("Not empty", graphEmpty(gStore.getGraph(graphIRI)));
assertFalse(graphEmpty(gStore.getDefaultGraph()));
}
Aggregations