I am converting user-defined-queries into SPARQL. For example, when user says, "abc", it means give me all nodes of a given type which have some attribute named "abc". As an extension of this, if user says, "abc or (pqr and lmn)", I need to find all nodes of a given type for which some attribute is "abc or (pqr and lmn)". Following is the query I have come up with:
SELECT DISTINCT ?node, ?type
WHERE
{
{
?node a ?type .
FILTER ( ?type != <sometype>)
}
{
{
?node ?reln0 ?obj0 .
FILTER ( regex(str(?obj0), 'abc', "i") )
}
UNION
{
{
?node ?reln1 ?obj1 .
FILTER ( regex(str(?obj1), 'pqr', "i") )
}
{
?node ?reln2 ?obj2 .
FILTER ( regex(str(?obj2), 'lmn', "i") )
}
}
}
}
ORDER BY ?node
But it doesn't return proper results. Is there something wrong with the above given query? I don't want to use the following because I need to generate the conditions dynamically and each clause needs to be separate.
FILTER (regex(str(?obj2), 'abc', "i") || regex(str(?obj2), 'pqr', "i") && regex(str(?obj2), 'lmn', "i"))
I made your query concrete as follows (putting in a definite type for the first filter):
I then generated the following data:
Only
:n1
and:n3
should be kept. I can run this with Jena's command line ARQ, or the Redland basedroqet
, and I get these rules in both cases.With ARQ:
With
roqet
: