for (Triple triple : query.getWhereClause().getClauses()) {
if (triple==pivot) {
continue;
}
URI uri = ((TermRef)triple.getPredicate()).getTerm();
Term term = db.findTerm(uri);
Ref object = triple.getObject();
if (object instanceof VariableRef) {
if (term!=null) {
bindings.put(((VariableRef)object).getName(),term);
}
} else if (object instanceof LiteralRef) {
if (term==null) {
skip = true;
} else {
comparisons.add(new Value(term,((LiteralRef)object).getValue()));
}
} else if (object instanceof NilRef) {
if (term==null) {
skip = true;
} else {
comparisons.add(new Value(term,Nil.getInstance()));
}
}
}
listener.onStart();
if (!skip) {
Term term = db.findTerm(pivotTerm);
if (term!=null) {
if (feedPivot) {
Iterator<TermInstance<Feed>> feeds = db.getFeedsByTerm(term,pivotValue);
while (feeds.hasNext()) {
TermInstance<Feed> termValue = feeds.next();
// need to compare if nil
if (pivotValue==Nil.getInstance() && termValue.getValue()!=Nil.getInstance()) {
continue;
}
Feed feed = termValue.getTarget();
Map<String,Object> variables = new TreeMap<String,Object>();
boolean ok = true;
// match values
for (Triple triple : query.getWhereClause().getClauses()) {
if (triple==pivot) {
continue;
}
for (Value value : comparisons) {
TermInstance<Feed> boundValue = feed.getTerm(value.term);
if (boundValue==null) {
ok = false;
break;
} else if (value.value==null && boundValue.getValue()!=null) {
ok = false;
break;
} else if (value.value!=null && !value.value.equals(boundValue.getValue())) {
ok = false;
break;
}
}
if (!ok) {
break;
}
for (String name : bindings.keySet()) {
Term t = bindings.get(name);
TermInstance<Feed> boundValue = feed.getTerm(t);
if (boundValue!=null && boundValue.getValue()!=null) {
variables.put(name,boundValue.getValue());
}
}
}
// do filter
ok = ok ? filter(variables,query.getWhereClause().getFilterExpressions()) : false;
if (ok) {
listener.onFeed(feed);
}
}
}
if (entryPivot) {
// TODO: code is almost the same as feed
Iterator<TermInstance<Entry>> entries = db.getEntriesByTerm(term,pivotValue);
while (entries.hasNext()) {
TermInstance<Entry> termValue = entries.next();
// need to compare if nil
if (pivotValue==Nil.getInstance() && termValue.getValue()!=Nil.getInstance()) {
continue;
}
Entry entry = termValue.getTarget();
Map<String,Object> variables = new TreeMap<String,Object>();
boolean ok = true;
// match values
for (Triple triple : query.getWhereClause().getClauses()) {
if (triple==pivot) {
continue;
}
for (Value value : comparisons) {
TermInstance<Entry> boundValue = entry.getTerm(value.term);
if (boundValue==null) {
ok = false;
break;
} else if (value.value==null && boundValue.getValue()!=null) {
ok = false;
break;
} else if (value.value!=null && !value.value.equals(boundValue.getValue())) {
ok = false;
break;
}
}
if (!ok) {
break;
}
for (String name : bindings.keySet()) {
Term t = bindings.get(name);
TermInstance<Entry> boundValue = entry.getTerm(t);
if (boundValue!=null && boundValue.getValue()!=null) {
variables.put(name,boundValue.getValue());
}
}