Search in sources :

Example 1 with WholeIllegalStateException

use of org.whole.lang.exceptions.WholeIllegalStateException in project whole by wholeplatform.

the class SelectIterator method lookahead.

public E lookahead() {
    if (nextEntity != null)
        return nextEntity;
    IEntity selfEntityOld = getBindings().wGet("self");
    lookaheadScope().setFilterEnabled(false);
    clearLookaheadScope();
    IEntity selfEntity = getFromIterator().lookahead();
    if (selfEntity == null) {
        lookaheadScope().setFilterEnabled(true);
        return null;
    }
    getBindings().wEnterScope(lookaheadScope(), true);
    getSelectIterator().reset(selfEntity);
    lookaheadScope().wAddAll(getFromIterator().lookaheadScope());
    Set<String> filterNames = lookaheadScope().getFilterNames();
    for (String name : getFromIterator().lookaheadScope().wLocalNames()) filterNames.add(name);
    lookaheadScope().wDef("self", selfEntity);
    filterNames.add("self");
    try {
        nextEntity = getSelectIterator().next();
    } catch (NoSuchElementException e) {
        throw new WholeIllegalStateException("Select clause must return a value", e).withSourceEntity(getSourceEntity()).withBindings(getBindings());
    }
    lookaheadScope().wAddAll(getSelectIterator().lookaheadScope());
    // enable select names hidden by from iterator
    filterNames.removeAll(getSelectIterator().lookaheadScope().wLocalNames());
    applyWhereClause(nextEntity, selfEntity, getBindings());
    filterNames.addAll(getWhereIterator().lookaheadScope().wLocalNames());
    lookaheadScope().setFilterEnabled(true);
    getBindings().wExitScope();
    if (!Matcher.removeVars(nextEntity, namesToBind(), withNamesComplement())) {
        Set<String> unboundedNames = Matcher.vars(nextEntity, false);
        if (withNamesComplement())
            unboundedNames.removeAll(namesToBind());
        else
            unboundedNames.retainAll(namesToBind());
        throw new MissingVariableException("Unbounded names in select clause: ", null, unboundedNames.toArray(new String[unboundedNames.size()])).withSourceEntity(getSourceEntity()).withBindings(getBindings());
    }
    if (selfEntityOld != null)
        getBindings().wDef("self", selfEntityOld);
    return nextEntity;
}
Also used : WholeIllegalStateException(org.whole.lang.exceptions.WholeIllegalStateException) IEntity(org.whole.lang.model.IEntity) NoSuchElementException(java.util.NoSuchElementException) MissingVariableException(org.whole.lang.visitors.MissingVariableException)

Aggregations

NoSuchElementException (java.util.NoSuchElementException)1 WholeIllegalStateException (org.whole.lang.exceptions.WholeIllegalStateException)1 IEntity (org.whole.lang.model.IEntity)1 MissingVariableException (org.whole.lang.visitors.MissingVariableException)1