Stardog similarity search query through jena


After testing and playing around with the similarity search, we are trying to integrate it with one of our software tools. At this moment we are able to provide the user with a way to select object attibutes to construct a similarity search model and we are able to create a insert model and a select model. However, we are not able to get the queries to stardog via jena, because Jena does not accept the queries.

For example we run the model insert query as a update query:

UpdateRequest request = UpdateFactory.create(query);
UpdateProcessor updateExec = UpdateExecutionFactory.createRemote(request , endpoint);

And we get back the following error:

Non-group key variable in SELECT: ?bouwbest in expression (urn:aggregatespa:set ?bouwbest)

The query used generated is:

prefix spa: <tag:stardog:api:analytics:>
 prefix : <>
 PREFIX agg: <urn:aggregate>

 INSERT { graph spa:model { :building_model a spa:SimilarityModel; spa:arguments (?bouwbest2 ?functie2 ?status2 ?bag_oppvlk2 ?bouwjaar2 );
 spa:predict ?object .}}WHERE {
(agg:spa:set(?bouwbest) as ?bouwbest2) (agg:spa:set(?functie) as ?functie2) (agg:spa:set(?status) as ?status2) (agg:spa:set(?bag_oppvlk) as ?bag_oppvlk2) (agg:spa:set(?bouwjaar) as ?bouwjaar2) ?object { GRAPH <> {?object <> ?hasWOZ.
?hasWOZ <> ?bouwbest.
?object <> ?hasBuilding.
?hasBuilding <> ?functie.
?hasBuilding <> ?status.
?hasBuilding <> ?bag_oppvlk.
?hasBuilding <> ?bouwjaar.
}} GROUP BY ?object}

The query works fine via Stardog Studio, I am curious whether there is some experience with such specific stardog query constructions and jena (or other ways to get the query to Stardog)

The Jena parser doesn’t know about the custom aggregate functions.

That being said, there is the possibility to register custom aggregate functions via ARQ, e.g.

/* Registration */
AggregateRegistry.register(aggUri, myAccumulatorFactory, NodeConst.nodeMinusOne);

shown in this example code

I don’t know whether this already exists for Stardog somewhere. The devs should know it better indeed.

So only registering would be enough (without having the aggregation code which is used by Stardog), since the query executed by Stardog and not by Jena?

We do offer Jena bindings, which should allow you to run your query on Stardog without having to send it through Jena, but it will work with the rest of your application, as everything implements the Jena interface.

Thanx Stephen,

We tried with a Jena Binding:

public void executeUpdate(String arg0) {
UpdateProcessor updateExec = UpdateExecutionFactory.create(UpdateFactory.create(arg0), getDataset());

In which getDataset() returns a SD dataset:


But we get the same result:

Non-group key variable in SELECT: ?bouwbest in expression (urn:aggregatespa:set ?bouwbest)

Is this the right way using the Jena Binding to get the result we would like to have?

It appears that using the Jena bindings in Stardog still sends the query through Jena, which is the problem in the first place. Have you tried Lorenz's suggestion regarding registering the aggregate function via ARQ (Stardog similarity search query through jena)?

Lorenz's suggestion should work but you also need to tweak your query.

First, to make SPARQL parsing in Jena work you can register a dummy aggregate function with the following one-liner:

AggregateRegistry.register("tag:stardog:api:analytics:set", (agg, distinct) -> AggNull.createAccNull(), NodeConst.nodeNil);

Second, you should not use the agg: prefix in your query at all. The set aggregate function is defined in the spa namespace and agg: prefix was a workaround in old versions of Stardog that is not needed anymore (we will update the examples in our documentation). So your aggregate expressions can look like (spa:set(?bouwbest) as ?bouwbest2) with the spa namespace declaration you already have.


1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.