Hi Guillermo,
Sorry I don't have Scala installed so this is from looking at the code
below.
On 09/03/2021 21:59, Guillermo Coscarelli wrote:
Hi,
I am trying to build the following query using ARQ
PREFIX ldp: <http://www.w3.org/ns/ldp#>
CONSTRUCT
{
?a ldp:member ?b .
}
WHERE {
SELECT ?b WHERE {
GRAPH ?d {
?b ldp:memberOf ?a
}
GRAPH ?b {
?r ldp:memberOf ?s
}
}
}
I was able to create the patterns and Ops necessary for the building
But when it comes to building the construct operation, which I created with
OpAsQuery and then setting the construct type, I was not able to add the
inner select since there is no OpSelect for that purpose.
My code in Scala
val resourceVar = Var.alloc("b")
val provenanceVar = Var.alloc("d")
val containerVar = Var.alloc("a")
val memberOf = NodeFactory.createLiteral("ldp:memberOf")
val member = NodeFactory.createLiteral("ldp:member")
val provenanceGraphPattern = new BasicPattern()
provenanceGraphPattern.add(new Triple(resourceVar, memberOf, containerVar))
val provenanceBGP = new OpBGP(provenanceGraphPattern)
val resourceGraphPattern = new BasicPattern()
resourceGraphPattern.add(new Triple(Var.alloc("r"),
NodeFactory.createLiteral("ldp:memberOf"), Var.alloc("s")))
val resourceBGP = new OpBGP(resourceGraphPattern)
val provenanceGraph = new OpGraph(provenanceVar, provenanceBGP)
val resourceGraph = new OpGraph(resourceVar, resourceBGP)
val join = OpJoin.create(provenanceGraph, resourceGraph)
val innerSelect = OpAsQuery.asQuery(join)
innerSelect.addResultVar(resourceVar)
val subQuery = new ElementSubQuery(innerSelect)
You can create the project as an Op:
Op whereClause = new OpProject(op1, Arrays.asList(var));
val constructPattern = new BasicPattern()
constructPattern.add(new Triple(containerVar,
member,
resourceVar
))
val op = Algebra.compile(subQuery)
val result = OpAsQuery.asQuery(op)
All Op provide a "toString" so you can print this out.
If it says "SELECT" the changing the query into a construct is going to
replace that SELECT with CONSTRUCT, loose the projection and not nest it.
Put the subquery as the WHERE clause of the fresh Query object which is
setQueryConstructType.
myConstructQuery.setQueryPattern(element)
where element is a subquery.
Andy
result.setConstructTemplate(new Template(constructPattern))
result.setQueryConstructType()
result.getPrefixMapping
.setNsPrefix("ldp", "http://www.w3.org/ns/ldp#")
result
*This creates the following query, which lacks of the inner SELECT clause*
PREFIX ldp: <http://www.w3.org/ns/ldp#>
CONSTRUCT
{
?a "ldp:member" ?b .
}
WHERE
{ GRAPH ?d
{ ?b "ldp:memberOf" ?a }
GRAPH ?resource
{ ?r "doc:encodes" ?s }
}
Any help is appreciated
*PS*: Also, which is the way to create a prefix reference Node in the
Triple. NodeFactory.createLiteral seems to add "" always and
NodeFactory.createURI always adds <>
Regards,
Guillermo.