You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@jena.apache.org by ajaz shaik <aj...@yahoo.com> on 2011/11/18 13:50:11 UTC

ARQ Building query incrementally help

Dear all, 

I need some help in changing my static SPARQL query to build Incrementally using ARQ. 
below is my query 

 1 PREFIX  dc:   <http://purl.org/dc/terms/>
  2 PREFIX  db:   <http://data.linkedmdb.org/resource/>
  3 PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
  4 PREFIX  movie: <http://data.linkedmdb.org/resource/movie/>
  5 PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  6 PREFIX  oddlinker: <http://data.linkedmdb.org/resource/oddlinker/>
  7 PREFIX  d2r:  <http://sites.wiwiss.fu-berlin.de/suhl/bizer/d2r-server/config.rdf#>
  8 PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
  9 PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
 10 PREFIX  map:  <file:///C:/d2r-server-0.4/mapping.n3#>
 11 PREFIX  dbpedia: <http://dbpedia.org/property/>
 12 PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 13 PREFIX  skos: <http://www.w3.org/2004/02/skos/core#>
 14 PREFIX  fn:   <java:facet.>
 15 
 16 SELECT  ?fpredicate (count(distinct ?instance) AS ?ficount) (count(distinct ?object) AS ?focount) (fn:weights3(count(distinct ?instance), count(distinct ?object), 85620) AS ?coverage) ?otype
 17 WHERE
 18   { ?instance rdf:type movie:film .
 19     ?instance ?fpredicate ?object
 20     FILTER regex(str(?fpredicate), "http://data.linkedmdb.org/resource/", "i")
 21   }
 22 GROUP BY ?fpredicate (fn:ifuri(?object) AS ?otype)
  1 (project (?fpredicate ?ficount ?focount ?coverage ?otype)
  2   (extend ((?ficount ?.0) (?focount ?.1) (?coverage (<java:facet.weights3> ?.0 ?.1 85620)))
  3     (group (?fpredicate (?otype (<java:facet.ifuri> ?object))) ((?.0 (count distinct ?instance)) (?.1 (count distinct ?object)))
  4       (filter (regex (str ?fpredicate) "http://data.linkedmdb.org/resource/" "i")
  5         (bgp
  6           (triple ?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://data.linkedmdb.org/resource/movie/film>)
  7           (triple ?instance ?fpredicate ?object)
  8         ))))) 

                                        </code></pre>

I want to build the above query incrementally because, I dynamically add Triples (as constraints) inside bgp. 
I really appreciate if any of experts here can at least give me rough code. I have seen all the documentation 
went through the ARQ code of AlgebraGenerator but it did not help me. I am still confused in adding 
functions to projections grouping by functions. Kindly pardon me it was trivial.  

I appreciate your help. 

Regards 
aj 

Re: ARQ Building query incrementally help

Posted by ajaz shaik <aj...@yahoo.com>.
Thanks andy for the Information , sorry I wasnt able to come up with simpler example, 

as I was little stuck in understanding why my query was not working.   

 
Regards
aj










________________________________
 From: Andy Seaborne <an...@apache.org>
To: jena-users@incubator.apache.org 
Sent: Tuesday, 22 November 2011 9:57 AM
Subject: Re: ARQ Building query incrementally help
 
Somewhat simpler example:

SELECT (sample(?a) + 1 AS ?c) {} GROUP BY ?x

Recorded as JENA-166
https://issues.apache.org/jira/browse/JENA-166

    Andy

On 20/11/11 17:22, Andy Seaborne wrote:
> On 20/11/11 16:16, ajaz shaik wrote:
>> Problem 1:
>>
>>
>> Difference between 1st and 2nd Query is adding of function weight in
>> the projection.
>
> But it is not a minimal example. Do you know if it requires the other
> parts of the query to be there or not?
>
> It takes time to reduce a problem to something that focuses on the key
> essential part. Removing everything unrelated makes the test case easier
> to work with. If you aren't going to take the time to do it, why should
> anyone else? It's just good debugging practice to produce a manageable
> example.
>
> As things stand, I'll look at when I get time, but as it needs time to
> pin down exactly what's going on, it's not going to be immediately as it
> is wil take longer than necessary.
>
> Andy

Re: ARQ Building query incrementally help

Posted by Andy Seaborne <an...@apache.org>.
Somewhat simpler example:

SELECT (sample(?a) + 1 AS ?c) {} GROUP BY ?x

Recorded as JENA-166
https://issues.apache.org/jira/browse/JENA-166

	Andy

On 20/11/11 17:22, Andy Seaborne wrote:
> On 20/11/11 16:16, ajaz shaik wrote:
>> Problem 1:
>>
>>
>> Difference between 1st and 2nd Query is adding of function weight in
>> the projection.
>
> But it is not a minimal example. Do you know if it requires the other
> parts of the query to be there or not?
>
> It takes time to reduce a problem to something that focuses on the key
> essential part. Removing everything unrelated makes the test case easier
> to work with. If you aren't going to take the time to do it, why should
> anyone else? It's just good debugging practice to produce a manageable
> example.
>
> As things stand, I'll look at when I get time, but as it needs time to
> pin down exactly what's going on, it's not going to be immediately as it
> is wil take longer than necessary.
>
> Andy


Re: ARQ Building query incrementally help

Posted by Andy Seaborne <an...@apache.org>.
On 20/11/11 16:16, ajaz shaik wrote:
> Problem 1:
>
>
> Difference between 1st and 2nd Query is adding of function weight in the projection.

But it is not a minimal example.  Do you know if it requires the other 
parts of the query to be there or not?

It takes time to reduce a problem to something that focuses on the key 
essential part.  Removing everything unrelated makes the test case 
easier to work with.  If you aren't going to take the time to do it, why 
should anyone else?  It's just good debugging practice to produce a 
manageable example.

As things stand, I'll look at when I get time, but as it needs time to 
pin down exactly what's going on, it's not going to be immediately as it 
is wil take longer than necessary.

	Andy

Re: ARQ Building query incrementally help

Posted by ajaz shaik <aj...@yahoo.com>.
Problem 1:


Difference between 1st and 2nd Query is adding of function weight in the projection. 


>> SELECT  ?fpredicate (count(distinct ?instance) AS ?ficount) (count(distinct ?object) AS ?focount) 

(fn:weights3(count(distinct ?instance), count(distinct ?object), 85620) AS ?coverage)


>>WHERE
>>    { ?instance rdf:type movie:film .
>>      ?instance ?fpredicate ?object
>>      FILTER regex(str(?fpredicate), "http://data.linkedmdb.org/resource/";, "i")
>>   }
>> GROUP BY ?fpredicate

In algebra. the additional part 

>>(project (?fpredicate ?ficount ?focount ?coverage) 
 
(extend ((?ficount ?.0) (?focount ?.1) (?coverage (<java:facet.weights3> ?.0 ?.1 85620))) 

>>(group (?fpredicate) ((?.0 (count distinct ?instance)) (?.1 (count distinct ?object)))



Problem 4:

First I have loaded my data using 

tdbloader2 --loc /home/MyDatabases/LinkedMDB/ /home/Downloads/linkedmdb.nt


String directory = "/home/MyDatabases/LinkedMDB" ;

      
>>Dataset dataset = TDBFactory.createDataset(directory) ;
>>PrefixMapping premap = dataset.asDatasetGraph().getDefaultGraph().getPrefixMapping();


System.out.println(premap.getNsPrefixMap().size()); 
is 0



Problem 5:


>>"StageGenerator"s are used by ARQ.

>>You can register your own - although TDB installs it's own query engine 
>>factory and executor (OpExecutorTDB).  StageGeneratorDirectTDB is only 
>>used if a TDB-backed model is placed in a generate dataset, not one 
>>obtained from TDBFactory.

I am sorry, I could not follow what 

" if a TDB-backed model is placed in a generate dataset, not one 
 obtained from TDBFactory "

mean, Should not I extract dataset using TDBFactory ? 
What should I do In order to make my Stategenerator in place of 
StageGeneratorDirectTDB .. and also to get prefixes ?


Thank you
aj

Re: ARQ Building query incrementally help

Posted by Andy Seaborne <an...@apache.org>.
On 19/11/11 06:10, ajaz shaik wrote:
> Hi, I dont know whats going wrong I am still having problems in converting/manipulating  :( ..
>
> It all started with just aliasing problem and it still exists in my brain.
>
> well here is what I found.... hope any one can help me....
>
> Problem 1 :
>
> Test Original Query 1 --------------------------------------------------

...

>   Test Original Query 2 --------------------------------------------------
>
> PREFIX  movie:<http://data.linkedmdb.org/resource/movie/>
>
> PREFIX  rdf:<http://www.w3.org/1999/02/22-rdf-syntax-ns#>
>
> PREFIX  fn:<java:facet.>
>
>
> SELECT  ?fpredicate (count(distinct ?instance) AS ?ficount) (count(distinct ?object) AS ?focount) (fn:weights3(count(distinct ?instance), count(distinct ?object), 85620) AS ?coverage)
> WHERE
>    { ?instance rdf:type movie:film .
>      ?instance ?fpredicate ?object
>      FILTER regex(str(?fpredicate), "http://data.linkedmdb.org/resource/", "i")
>    }
> GROUP BY ?fpredicate
>
>
>   Test Original Query 2 Algebra ---------------------------------------------
>
> (project (?fpredicate ?ficount ?focount ?coverage)
>    (extend ((?ficount ?.0) (?focount ?.1) (?coverage (<java:facet.weights3>  ?.0 ?.1 85620)))
>      (group (?fpredicate) ((?.0 (count distinct ?instance)) (?.1 (count distinct ?object)))
>        (filter (regex (str ?fpredicate) "http://data.linkedmdb.org/resource/" "i")
>          (bgp
>            (triple ?instance<http://www.w3.org/1999/02/22-rdf-syntax-ns#type>  <http://data.linkedmdb.org/resource/movie/film>)
>            (triple ?instance ?fpredicate ?object)
>          )))))
>
>
> Test OpAsQuery Query 2  --------------------------------------------------
> Exception in thread "main" com.hp.hpl.jena.sparql.ARQNotImplemented: Expected simple assignment for group
> at com.hp.hpl.jena.sparql.algebra.OpAsQuery$Converter.visit(OpAsQuery.java:385)
> at com.hp.hpl.jena.sparql.algebra.op.OpExtend.visit(OpExtend.java:107)
> at com.hp.hpl.jena.sparql.algebra.OpAsQuery$Converter.visit(OpAsQuery.java:417)
> at com.hp.hpl.jena.sparql.algebra.op.OpProject.visit(OpProject.java:34)
> at com.hp.hpl.jena.sparql.algebra.OpAsQuery.asQuery(OpAsQuery.java:45)
> at test.Temp.main(Temp.java:145)
>
>
> Can any body help me with this exception ...

Could you please reduce this to a simple query that goes wrong.  It's 
very hard to see what's happening with all the query features.  Extract 
the difference between query 1 and query 2, make it as simple as possible.

> Problem 3:
>
>
>
>   Aggregator agg1 = new AggCountVarDistinct(ev1);
>
>
>   VarAlloc va = new VarAlloc(ARQConstants.allocVarMarker);
>   Var temp1 = va.allocVar();
>   Expr extemp1 = new ExprVar(temp1);
>
>
>    ExprAggregator eagg1 = new ExprAggregator(temp1, agg1);
>    aggrelist.add(eagg1);
>
>
> When there are count expressions in my projection, Is It Mandatory to have Temporary
>   Variables
> to define implicit grouping of variables when I am building Algebra of query?

Probably not but it might make it impossible to run OpAsQuery.

You can build queries in the algebra that have no exact SPARQL syntax form.

>
>
> Problem 4:
>
>
> Dataset dataset = TDBFactory.createDataset(directory) ;
>
> PrefixMapping premap = dataset.asDatasetGraph().getDefaultGraph().getPrefixMapping();
>        It does not return any prefix entries ,...... I asked this before but when I tried The result is 0;

Maybe there are no prefixes in the data.

Complete, minimal example please.

>
> Pardon me I could not follow properly  .. :/
>
>
> Question General :
>
> Can I make TDB use customize StageGenerator than its native StageGeneratorDirectTDB ?

"StageGenerator"s are used by ARQ.

You can register your own - although TDB installs it's own query engine 
factory and executor (OpExecutorTDB).  StageGeneratorDirectTDB is only 
used if a TDB-backed model is placed in a generate dataset, not one 
obtained from TDBFactory.

>
> I debugged a bit and found out that its not using  customized one.
>
>
> Thanks,
> Regards,
> aj

Re: ARQ Building query incrementally help

Posted by ajaz shaik <aj...@yahoo.com>.
Hi, I dont know whats going wrong I am still having problems in converting/manipulating  :( .. 

It all started with just aliasing problem and it still exists in my brain. 

well here is what I found.... hope any one can help me....  

Problem 1 :

Test Original Query 1 --------------------------------------------------

PREFIX  movie: <http://data.linkedmdb.org/resource/movie/>

PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

PREFIX  fn:   <java:facet.>


SELECT  ?fpredicate (count(distinct ?instance) AS ?ficount) (count(distinct ?object) AS ?focount)
WHERE
  { ?instance rdf:type movie:film .
    ?instance ?fpredicate ?object
    FILTER regex(str(?fpredicate), "http://data.linkedmdb.org/resource/", "i")
  }
GROUP BY ?fpredicate


 Test Original Query 1 Algebra--------------------------------------------

(project (?fpredicate ?ficount ?focount)
  (extend ((?ficount ?.0) (?focount ?.1))
    (group (?fpredicate) ((?.0 (count distinct ?instance)) (?.1 (count distinct ?object)))
      (filter (regex (str ?fpredicate) "http://data.linkedmdb.org/resource/" "i")
        (bgp
          (triple ?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://data.linkedmdb.org/resource/movie/film>)
          (triple ?instance ?fpredicate ?object)
        )))))


Test OpAsQuery Query 1 --------------------------------------------------

SELECT  ?fpredicate (count(distinct ?instance) AS ?ficount) (count(distinct ?object) AS ?focount)
WHERE
  { ?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://data.linkedmdb.org/resource/movie/film> .
    ?instance ?fpredicate ?object
    FILTER regex(str(?fpredicate), "http://data.linkedmdb.org/resource/", "i")
  }
GROUP BY ?fpredicate


 Test Original Query 2 --------------------------------------------------

PREFIX  movie: <http://data.linkedmdb.org/resource/movie/>

PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>

PREFIX  fn:   <java:facet.>


SELECT  ?fpredicate (count(distinct ?instance) AS ?ficount) (count(distinct ?object) AS ?focount) (fn:weights3(count(distinct ?instance), count(distinct ?object), 85620) AS ?coverage)
WHERE
  { ?instance rdf:type movie:film .
    ?instance ?fpredicate ?object
    FILTER regex(str(?fpredicate), "http://data.linkedmdb.org/resource/", "i")
  }
GROUP BY ?fpredicate


 Test Original Query 2 Algebra ---------------------------------------------

(project (?fpredicate ?ficount ?focount ?coverage)
  (extend ((?ficount ?.0) (?focount ?.1) (?coverage (<java:facet.weights3> ?.0 ?.1 85620)))
    (group (?fpredicate) ((?.0 (count distinct ?instance)) (?.1 (count distinct ?object)))
      (filter (regex (str ?fpredicate) "http://data.linkedmdb.org/resource/" "i")
        (bgp
          (triple ?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://data.linkedmdb.org/resource/movie/film>)
          (triple ?instance ?fpredicate ?object)
        )))))


Test OpAsQuery Query 2  --------------------------------------------------
Exception in thread "main" com.hp.hpl.jena.sparql.ARQNotImplemented: Expected simple assignment for group
at com.hp.hpl.jena.sparql.algebra.OpAsQuery$Converter.visit(OpAsQuery.java:385)
at com.hp.hpl.jena.sparql.algebra.op.OpExtend.visit(OpExtend.java:107)
at com.hp.hpl.jena.sparql.algebra.OpAsQuery$Converter.visit(OpAsQuery.java:417)
at com.hp.hpl.jena.sparql.algebra.op.OpProject.visit(OpProject.java:34)
at com.hp.hpl.jena.sparql.algebra.OpAsQuery.asQuery(OpAsQuery.java:45)
at test.Temp.main(Temp.java:145)
 
 
Can any body help me with this exception ... 

What is did was 

 Query query1 = QueryFactory.create(queryString);

 System.out.println("\n\n Test Original Query 1 --------------------------------------------------");
   
 query1.serialize(new IndentedWriter(System.out));
   
 System.out.println("\n\n Test Original Query 1 Algebra--------------------------------------------");
   
 Op o1 = Algebra.compile(query1);
 o1.output(new IndentedWriter(System.out));
   
 System.out.println("\n\n Test OpAsQuery Query 1 --------------------------------------------------");
       
 Query queryRe1 = OpAsQuery.asQuery(o1); 
 queryRe1.serialize(new IndentedWriter(System.out));
      

///ry for 2nd 

I could not follow in code what does 

 /**     * Special case: group involves and internal assignment
             * e.g.  (assign ((?.1 ?.0)) (group () ((?.0 (count)))
             * We attempt to intercept that here.
             */

mean and how should I handle this case in building query , If problem above was Obvious . 
Problem 2:

op =new OpGroup(op,exl, aggrelist);

What if I dont want to have aggrelist ? 

if My Query is some thing like 

SELECT  DISTINCT ?fpredicate 
WHERE
  {
    ?instance rdf:type movie:film .
    ?instance ?fpredicate ?object
    FILTER regex(str(?fpredicate), "http://data.linkedmdb.org/resource/", "i")
  }
GROUP BY ?fpredicate
Can I leave my  List<ExprAggregator> aggrelist = new ArrayList<ExprAggregator>(); 
Empty in here or pass NULL value to aggrelist ? Could not try this out myself as was stuck in above.
  

Problem 3:



 Aggregator agg1 = new AggCountVarDistinct(ev1);
  

 VarAlloc va = new VarAlloc(ARQConstants.allocVarMarker);
 Var temp1 = va.allocVar();
 Expr extemp1 = new ExprVar(temp1);
  

  ExprAggregator eagg1 = new ExprAggregator(temp1, agg1);
  aggrelist.add(eagg1);


When there are count expressions in my projection, Is It Mandatory to have Temporary
 Variables
to define implicit grouping of variables when I am building Algebra of query?  


Problem 4:


Dataset dataset = TDBFactory.createDataset(directory) ;
        
PrefixMapping premap = dataset.asDatasetGraph().getDefaultGraph().getPrefixMapping();
      It does not return any prefix entries ,...... I asked this before but when I tried The result is 0; 

Pardon me I could not follow properly  .. :/


Question General :

Can I make TDB use customize StageGenerator than its native StageGeneratorDirectTDB ?

I debugged a bit and found out that its not using  customized one. 


Thanks,
Regards, 
aj 



________________________________
 From: Andy Seaborne <an...@apache.org>
To: jena-users@incubator.apache.org 
Sent: Friday, 18 November 2011 7:56 PM
Subject: Re: ARQ Building query incrementally help
 
On 18/11/11 18:26, ajaz shaik wrote:
> Hi again,
>
> I solved the problem myself with help of andy's previous reply.
> I just needed some patients. Sorry for inconvenience.
>
>
> In case if it helps for some one around this group here is the code

Thanks for letting us know what you've discovered.

    Andy

Re: ARQ Building query incrementally help

Posted by Andy Seaborne <an...@apache.org>.
On 18/11/11 18:26, ajaz shaik wrote:
> Hi again,
>
> I solved the problem myself with help of andy's previous reply.
> I just needed some patients. Sorry for inconvenience.
>
>
> In case if it helps for some one around this group here is the code

Thanks for letting us know what you've discovered.

	Andy



Re: ARQ Building query incrementally help

Posted by ajaz shaik <aj...@yahoo.com>.
Hi again, 

I solved the problem myself with help of andy's previous reply. 
I just needed some patients. Sorry for inconvenience.


In case if it helps for some one around this group here is the code

----------------------------------------------------------------------------------------------------------
 Var v1 = Var.alloc("instance");
  Expr ev1 = new ExprVar(v1);
  
  Var v2 = Var.alloc("fpredicate");
  Expr pre = new ExprVar(v2);
  
  Var v3 = Var.alloc("object");
  Expr ev3 = new ExprVar(v3);
  
  Var v4 = Var.alloc("otype");
  
  Var v5 = Var.alloc("coverage");
  
  Var v6 = Var.alloc("ficount");
  
  Var v7 = Var.alloc("focount");
  
  Triple t1 = new Triple(v1.asNode(),RDF.type.asNode(),NodeValue.makeNodeString("<http://data.linkedmdb.org/resource/movie/film>").asNode());
  Triple t2 = new Triple(v1.asNode(), v2.asNode(), v3.asNode());
  
  BasicPattern bp1 = new BasicPattern();
  bp1.add(t1);
  bp1.add(t2);
  
  Op op = new OpBGP(bp1);
  
  
  
  Expr str = new E_Str(pre);
  
  E_Regex reex = new E_Regex(str, "http://data.linkedmdb.org/resource/", "i");
  
  op = OpFilter.filter(reex, op);
  
  VarExprList exl = new VarExprList();
  exl.add(v2);
  
  ExprList el1 = new ExprList();
  el1.add(ev3);
  
  Expr exf1 = new E_Function("java:facet.ifuri", el1);
  
    
  exl.add(v4, exf1);
  
  Aggregator agg1 = new AggCountVarDistinct(ev1);
  Aggregator agg2 = new AggCountVarDistinct(ev3);
  
  
  ExprAggregator eagg1 = new ExprAggregator(v6, agg1);
  ExprAggregator eagg2 = new ExprAggregator(v7, agg2);
  
  List<ExprAggregator> aggrelist = new ArrayList<ExprAggregator>();
  
  aggrelist.add(eagg1);
  aggrelist.add(eagg2);
  
  op =new OpGroup(op,exl, aggrelist);
  
  ExprList el2 = new ExprList();
  el2.add(eagg1);
  el2.add(eagg2);
  Expr v = NodeValue.makeNodeString("85620");
  el2.add(v);
  
  Expr exf2 = new E_Function("java:facet.weights3", el2);
  
  VarExprList var = new VarExprList();
  var.add(v2);
  var.add(v5, exf2);
  var.add(v6,agg1.getExpr());
  var.add(v7, agg2.getExpr());
  var.add(v4);
  
  op = new OpProject(op, var.getVars());
  op.output(new IndentedWriter(System.out));
  
  System.out.println("\n\n\n");
  
  Query query = OpAsQuery.asQuery(op);
  
   
  query.serialize(new IndentedWriter(System.out,true)) ; 
--------------------------------------------------------------------------------------------------------------------------------


Regards, 
ajaz 











________________________________
 From: ajaz shaik <aj...@yahoo.com>
To: Jena incubator <je...@incubator.apache.org> 
Sent: Friday, 18 November 2011 1:50 PM
Subject: ARQ Building query incrementally help 
 
Dear all, 

I need some help in changing my static SPARQL query to build Incrementally using ARQ. 
below is my query 

 1 PREFIX  dc:   <http://purl.org/dc/terms/>
  2 PREFIX  db:   <http://data.linkedmdb.org/resource/>
  3 PREFIX  foaf: <http://xmlns.com/foaf/0.1/>
  4 PREFIX  movie: <http://data.linkedmdb.org/resource/movie/>
  5 PREFIX  rdfs: <http://www.w3.org/2000/01/rdf-schema#>
  6 PREFIX  oddlinker: <http://data.linkedmdb.org/resource/oddlinker/>
  7 PREFIX  d2r:  <http://sites.wiwiss.fu-berlin.de/suhl/bizer/d2r-server/config.rdf#>
  8 PREFIX  xsd:  <http://www.w3.org/2001/XMLSchema#>
  9 PREFIX  owl:  <http://www.w3.org/2002/07/owl#>
 10 PREFIX  map:  <file:///C:/d2r-server-0.4/mapping.n3#>
 11 PREFIX  dbpedia: <http://dbpedia.org/property/>
 12 PREFIX  rdf:  <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
 13 PREFIX  skos: <http://www.w3.org/2004/02/skos/core#>
 14 PREFIX  fn:   <java:facet.>
 15 
 16 SELECT  ?fpredicate (count(distinct ?instance) AS ?ficount) (count(distinct ?object) AS ?focount) (fn:weights3(count(distinct ?instance), count(distinct ?object), 85620) AS ?coverage) ?otype
 17 WHERE
 18   { ?instance rdf:type movie:film .
 19     ?instance ?fpredicate ?object
 20     FILTER regex(str(?fpredicate), "http://data.linkedmdb.org/resource/", "i")
 21   }
 22 GROUP BY ?fpredicate (fn:ifuri(?object) AS ?otype)
  1 (project (?fpredicate ?ficount ?focount ?coverage ?otype)
  2   (extend ((?ficount ?.0) (?focount ?.1) (?coverage (<java:facet.weights3> ?.0 ?.1 85620)))
  3     (group (?fpredicate (?otype (<java:facet.ifuri> ?object))) ((?.0 (count distinct ?instance)) (?.1 (count distinct ?object)))
  4       (filter (regex (str ?fpredicate) "http://data.linkedmdb.org/resource/" "i")
  5         (bgp
  6           (triple ?instance <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://data.linkedmdb.org/resource/movie/film>)
  7           (triple ?instance ?fpredicate ?object)
  8         ))))) 

                                        </code></pre>

I want to build the above query incrementally because, I dynamically add Triples (as constraints) inside bgp. 
I really appreciate if any of experts here can at least give me rough code. I have seen all the documentation 
went through the ARQ code of AlgebraGenerator but it did not help me. I am still confused in adding 
functions to projections grouping by functions. Kindly pardon me it was trivial.  

I appreciate your help. 

Regards 
aj