Corese / KGRAM AST

This document describes SPARQL Abstract Syntax Tree API. It can be used to create a query by program. The resulting AST can be executed right away by KGRAM. TBD: SPARQL Update.

SPARQL Abstract Syntax Tree with namespace manager:

ASTQuery ast  = ASTQuery.create();
ast.getNSM().definePrefix("ns", "http://ns.inria.fr/schema/");

Variable:

Variable.create("?x");

URI Constant, Datatype Constant, Literal with lang

ast.createConstant("ns:John");	
ast.createConstant("12", "xsd:integer", null);
ast.createConstant("engineer", null, "en");

Triple:

Triple t1 = Triple.create(
  Variable.create("?x"), ast.createConstant("rdf:type"), Variable.create("?y"));

Body:

Exp body = BasicGraphPattern.create();
body.add(t1);
ast.setBody(body);	

SPARQL Statements:

BasicGraphPattern bgp = BasicGraphPattern.create();
Option.create(bgp);
Source.create(graph, bgp);
Minus.create(bgp);
Or.create(bgp1, bgp2);

bgp.add(e1);
bgp.add(e2);

Filter:

Term.create("<", Variable.create("?x"), ast.createConstant("100", "xsd:integer", null)); 

Term.create("&&", t1, t2);

Term.create("||", t1, t2);

Term.create("!", t1, t2);

Term.function("isBlank", Variable("?t"));

Term list = Term.list();
list.add(t1);
list.add(t2);
Term.create("in", Variable.create("?x"), list);

Add a filter:

Term t = Term.create("&&", t1, t2);
Triple triple = Triple.create(t);
BasicGraphPattern bgp = BasicGraphPattern.create();
bgp.add(triple);

Select:

ast.setSelect(Variable.create("?x"));

ast.setSelect(Variable.create("?sum"), 
  Term.create("+", Variable.create("?x"), Variable.create("?y)));

ast.setSelectAll(true);

ast.setDistinct(true);

From:

ast.setFrom (ast.createConstant("http://www.example.org/graph"));
ast.setNamed(ast.createConstant("http://www.example.org/graph"));

Construct:

ast.setConstruct(bgp);

Describe:

ast.setDescribe(Variable.create("?z"));

Ask:

ast.setAsk(true);

Order by, group by:

ast.setSort(var);
ast.setSort(var, true);

ast.setSort(exp);
ast.setSort(exp, true);

ast.setGroup(var);
ast.setGroup(exp, var);

Limit, offset:

ast.setLimit(10);

ast.setOffset(10);

Subquery:

ASTQuery sub = ast.subCreate();

q = Query.create(sub); 
body.add(q);

Property Path Regex:

ast.createConstant("rdf:type");

ast.createOperator("^", regex);
ast.createOperator("!", regex);
ast.createOperator("+", regex);
ast.createOperator("*", regex);
ast.createOperator("?", regex);

ast.createOperator ("|", regex1, regex2);
ast.createOperator ("/", regex1, regex2);

Property Path:

ast.createTriple(subject, ast.createProperty(regex), object);

Aggregates:

Term agg = Term.function("group_concat", Variable.create("?x"));
agg.setDistinct(true);
agg.setModality("; ");
ast.setSelect(Variable.create("?conc"), agg);

Having:

Term exp = Term.create(">=", Variable.create("?x"), ast.createConstant("12", "xsd:integer"));
ast.setHaving(exp);

Bindings:

Values values = Values.create();
	
ArrayList<Variable> list  = new ArrayList<Variable>();
ArrayList<Constant> value = new ArrayList<Constant>();
	
values.setVariables(list);
values.addValues(value);

bgp.add(values);
	
ast.setValues(values);