Olivier Corby, INRIA, Novembre 2008.
See also:
Corese
Select the result of an expression
select fun(?x) as ?y
where {
?x ex:prop ?z
}
Select distinct results of an expression
select distinct fun(?x) as ?y
where {
?x ex:prop ?z
}
Group results according to variable values, count occurrences of other values
select * count(?z) as ?count
where {
?x ex:prop ?z
}
group by ?x
Group results according to expression values
select fun(?x) as ?y
where {
?x ex:prop ?z
}
group by ?y
Group results according to any argument (January 2009)
select *
where {
?x ex:prop ?z
}
group by any
Group by can have more than one argument, it be used with construct.
Construct with Graph Graph Pattern
construct {
graph ?g { ?x ex:prop ?y }
}
where {
graph ?g { ?y ex:prop ?x }
}
Construct with Select expression
construct {
?x ex:relat ?z
}
select fun(?y) as ?z
where {
?x ex:prop ?y
}
Construct with aggregate
construct {
?x ex:total ?sum
}
select sum(?y) as ?sum
where {
?x ex:prop ?y
}
group by ?x
Construct/Add within the source graph (Update)
add {
?x ex:total ?sum
}
select sum(?y) as ?sum
where {
?x ex:prop ?y
}
group by ?x
It is possible to post process results of a SPARQL query using an external function. The external function is executed after the completion of SPARQL processing, just before returning the results. It is possible to process such treatment as ordering the results or eliminating results according to new criteria. The principle consists in using an external user defined function within the postProcess() pseudo function. The result of the query is given to the postProcess function by means of a pseudo function results().
prefix my: <function://my.cool.package>
select * where {
?x ?p ?y
filter(postProcess(my:process(results()))
}
The external user defined function retrieves the results by means of a getObject() accessor.
package my.cool.package;
public IDatatype process(IDatatype dt){
IResults res = (IResults) dt.getObject();
for (IResult rr : res){
rr.getResultValue("?x");
}
return dt;
}
It is possible to add or remove a result from IResults result list.
interface IResults {
public int size();
public IResult get(int i);
public void remove(IResult r);
public void add(IResult r);
public void add(int n, IResult r);
}