Hi Dan,

Glad you've make progress. Processing aggregations is harder than most other forms because one piece of syntax has multiple effects on the resulting algebra. LIMIT only causes a (slice ...), COUNT is grouping and aggregation function. In ARQ, the aggreagion is always assigned to a hidden variable then assigned to the named variable (The aggregation can be mentioned multiple times but it is calculated once per query level).

> import com.hp.hpl.jena.sparql.algebra.Op;

I do notice you are using a quite old version of Jena: the packages are all "org.apache.jena" now. That's also why you had to declare the syntax and suggests it is really quite an old version as SPARQL 1.1 has been the default for a while now.

The package:
org.apache.jena.sparql.syntax.syntaxtransform

work on the syntax, rather than the algebra, which might be an alternative for some transformation for you. It mikght be newer than code you have.

On 24/03/2019 02:48, Dan Davis wrote:
Long story short - I had two problems, but the primary is that Jena
requires parenthesis and named var:

Yes, that is part of the SPARQL standard.

The non-standard "ARQ" syntax (a few extensions) is a little more forgiving.

    Andy


SELECT (COUNT(?resource) AS ?count)
FROM <http://id.nlm.nih.gov/mesh>
WHERE {
   ?resource a meshv:Descriptor .
   ?resource rdfs:label ?label .
   ?label bif:contains "Pyrin" .
}

The second problem was that ORDER BY screws up the results, because it
prevented an automated grouping.

On Sat, Mar 23, 2019 at 10:28 PM Dan Davis <dansm...@gmail.com> wrote:

I've now designed a unit test for the code above, and it works mostly as
intended.  I still don't see how to get Count queries work as a
transformation.   The documentation is clear that this should be supported,
as is the EBNF of the Sparql 1.1 specification.

The next step was to make sure that I was telling qparse to treat my query
as sparql11:

        ~/tools/apache-jena/bin/qparse --query countquery --syntax=sparql11
--print=op

Finding the values of Syntax were not that easy, because the --help
doesn't provide the valid values of the syntax.  Jshell did crack it
eventually: Syntax.querySyntaxNames.keys()

Sigh - I'd rather be programming Python, but that's OK.

On Fri, Mar 22, 2019 at 5:55 PM Dan Davis <dansm...@gmail.com> wrote:


Hi guys, I'm writing my first Transform, and I'm having trouble with a
count query.
arq.qparse doesn't parse and print my count query, so  I cannot get much
feedback whether I'm doing the right thing or not.

A limit query seems simple enough, however:


package gov.nih.nlm.lode.service;

import com.hp.hpl.jena.sparql.algebra.Op;
import com.hp.hpl.jena.sparql.algebra.TransformBase;
import com.hp.hpl.jena.sparql.algebra.op.OpProject;
import com.hp.hpl.jena.sparql.algebra.op.OpSlice;

public class LimitTransform extends TransformBase {
     private long offset;
     private long limit;

     public LimitTransform(long limit, long offset) {
         this.limit = limit;
         this.offset = offset;
     }
     public LimitTransform(long limit) {
         this(limit, 0);
     }

     @Override
     public Op transform(OpProject opProject, Op subOp) {
         return new OpSlice(opProject, offset, limit);
     }
}






Reply via email to