Thanks Holger. I ended up using SPINFactory.isVariable() and
SPINFactory.asVariable().
I also realized afterwards that I need a recursive check, so the final
version of my method was this:
protected boolean isWhereVariable(Variable var)
{
return ResourceUtils.reachableClosure(getWhere()).
listSubjectsWithProperty(SP.varName, var.getName()).
hasNext();
}
Will check SPINUtil.
Martynas
graphity.org
2013 m. gegužė 23 d., ketvirtadienis 02:21:28 UTC+3, Holger Knublauch rašė:
>
> On 5/23/2013 0:53, [email protected] <javascript:> wrote:
> > Hey,
> >
> > I'm working on a SPIN-based query builder. One of the last things I
> > added is a possibility to specify a Variable or expression to be used
> > in ORDER BY.
> > This is working fine, except that the supplied Variable might not be
> > used in the WHERE pattern, which produces an invalid SPARQL query.
> >
> > I want to fix this by checking if the supplied Variable is present in
> > the WHERE pattern, and throw an exception if it's not. The method I'm
> > using:
> >
> > protected boolean isWhereVariable(Variable var)
> > {
> > Iterator<Element> it = getWhere().getElements().iterator();
> >
> > while (it.hasNext())
> > {
> > Element element = it.next();
> >
> > RDFNode subject =
> > element.getRequiredProperty(SP.subject).getObject();
> > if (subject.canAs(Variable.class) &&
> > subject.as(Variable.class).getName().equals(var.getName()))
> > return true;
> >
> > RDFNode predicate =
> > element.getRequiredProperty(SP.predicate).getObject();
> > if (predicate.canAs(Variable.class) &&
> > predicate.as(Variable.class).getName().equals(var.getName()))
> > return true;
> >
> > RDFNode object =
> > element.getRequiredProperty(SP.object).getObject();
> > if (object.canAs(Variable.class) &&
> > object.as(Variable.class).getName().equals(var.getName()))
> > return true;
> > }
> >
> > return false;
> > }
> >
> > The problem is that subject.canAs(Variable.class) returns false, even
> > if stepping through the code I can see, that
> > subject.as(Variable.class) is "?instance" for example (the variable
> > I'm expecting).
> > Any ideas on why the canAs() check doesn't work, or how this could be
> > implemented in a better way?
>
> canAs() probably relies on an rdf:type triple, but Variables usually
> don't have those. Instead use SPINFactory.isVariable(node).
>
> The loop above looks rather fragile though because it only handles BGPs,
> i.e. nested blocks would be ignored. This may work in your specific
> case, but instead you may want to do a recursive traversal. I believe
> the following does something close to what you need (from SPINUtil):
>
> public static Integer containsThis(CommandWithWhere command) {
> return new ContainsVarChecker(SPIN._this).checkDepth(command);
> }
>
> HTH
> Holger
>
>
--
-- You received this message because you are subscribed to the Google
Group "TopBraid Suite Users", the topics of which include Enterprise Vocabulary
Network (EVN), TopBraid Composer, TopBraid Live,
TopBraid Ensemble, SPARQLMotion, SPARQL Web Pages and SPIN.
To post to this group, send email to
[email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/topbraid-users?hl=en
---
You received this message because you are subscribed to the Google Groups
"TopBraid Suite Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.