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?
Thanks,
Martynas
graphity.org