Search in sources :

Example 1 with IfWithTypeTest

use of org.whole.lang.queries.codebase.IfWithTypeTest in project whole by wholeplatform.

the class QueriesDynamicCompilerVisitor method visit.

@Override
public void visit(Choose entity) {
    int size = entity.wSize();
    if (size == 1)
        entity.get(0).accept(this);
    else {
        boolean canOptimize = true;
        ILanguageKit languageKit = null;
        Map<EntityDescriptor<?>, Expression> typeMap = new HashMap<EntityDescriptor<?>, Expression>();
        If ifWithTemplate = new IfWithTemplate().create();
        If ifWithTypeTest = new IfWithTypeTest().create();
        ITransactionScope ts = BindingManagerFactory.instance.createTransactionScope();
        getBindings().wEnterScope(ts);
        for (int i = 0; i < size; i++) {
            Expression child = entity.get(i);
            try {
                if (!Matcher.match(ifWithTemplate, child, getBindings()) && !Matcher.match(ifWithTypeTest, child, getBindings())) {
                    canOptimize = false;
                    break;
                }
                EntityDescriptor<?> ed = getBindings().wIsSet("typeTest") && getBindings().wGet("typeTest").wGetEntityDescriptor().getDataKind().isString() ? CommonsDataTypePersistenceParser.getEntityDescriptor(getBindings().wStringValue("typeTest")) : getBindings().wIsSet("pattern") ? getBindings().wGet("pattern").wGetEntityDescriptor() : null;
                if (ed == null) {
                    canOptimize = false;
                    break;
                }
                if (typeMap.containsKey(ed)) {
                    Expression behavior = typeMap.get(ed);
                    boolean isPattern = behavior.wGetParent() == entity;
                    if (isPattern) {
                        canOptimize = false;
                        break;
                    }
                } else {
                    if (languageKit == null)
                        languageKit = ed.getLanguageKit();
                    else if (!languageKit.equals(ed.getLanguageKit())) {
                        // ||
                        // !languageKit.getURI().equals("whole:org.whole.lang.javascript:JavaScript"))
                        // {
                        canOptimize = false;
                        break;
                    }
                    typeMap.put(ed, getBindings().wIsSet("pattern") ? child : getBindings().wGet("expression").wGetAdapter(QueriesEntityDescriptorEnum.Expression));
                }
            } finally {
                ts.rollback();
            }
        }
        getBindings().wExitScope();
        if (canOptimize && languageKit != null) {
            ChooseByTypeIterator<IEntity> chooseIterator = IteratorFactory.chooseIterator(languageKit);
            for (Entry<EntityDescriptor<?>, Expression> entry : typeMap.entrySet()) {
                Set<String> oldDeclaredNames = declaredNames;
                entry.getValue().accept(this);
                chooseIterator.setCase(entry.getKey(), getResultIterator());
                declaredNames = oldDeclaredNames;
            }
            setResultIterator(chooseIterator.withSourceEntity(entity));
        } else {
            IEntityIterator<? extends IEntity>[] iteratorChain = new IEntityIterator<?>[size];
            for (int i = 0; i < size; i++) {
                entity.get(i).accept(this);
                iteratorChain[i] = getResultIterator();
            }
            setResultIterator(IteratorFactory.chooseIterator(iteratorChain).withSourceEntity(entity));
        }
    }
}
Also used : ITransactionScope(org.whole.lang.bindings.ITransactionScope) HashMap(java.util.HashMap) IEntity(org.whole.lang.model.IEntity) ILanguageKit(org.whole.lang.reflect.ILanguageKit) EntityDescriptor(org.whole.lang.reflect.EntityDescriptor) IfWithTypeTest(org.whole.lang.queries.codebase.IfWithTypeTest) IfWithTemplate(org.whole.lang.queries.codebase.IfWithTemplate) IEntityIterator(org.whole.lang.iterators.IEntityIterator)

Aggregations

HashMap (java.util.HashMap)1 ITransactionScope (org.whole.lang.bindings.ITransactionScope)1 IEntityIterator (org.whole.lang.iterators.IEntityIterator)1 IEntity (org.whole.lang.model.IEntity)1 IfWithTemplate (org.whole.lang.queries.codebase.IfWithTemplate)1 IfWithTypeTest (org.whole.lang.queries.codebase.IfWithTypeTest)1 EntityDescriptor (org.whole.lang.reflect.EntityDescriptor)1 ILanguageKit (org.whole.lang.reflect.ILanguageKit)1