use of org.whole.lang.queries.codebase.IfWithTemplate 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));
}
}
}
Aggregations