Search in sources :

Example 1 with IEntityIterator

use of org.whole.lang.iterators.IEntityIterator in project whole by wholeplatform.

the class WorkflowsDynamicCompilerVisitor method visit.

@Override
public void visit(InvokeJavaClassMethod entity) {
    final Variable resultVariable = entity.getResult();
    Expressions arguments = entity.getArguments();
    int size = arguments.wSize();
    IEntityIterator<?>[] runnableIterators = new IEntityIterator<?>[2 + size];
    entity.getClassName().accept(this);
    runnableIterators[0] = getResultIterator();
    entity.getMethod().accept(this);
    runnableIterators[1] = getResultIterator();
    for (int i = 0; i < size; i++) {
        arguments.get(i).accept(this);
        runnableIterators[2 + i] = getResultIterator();
    }
    setResultIterator(IteratorFactory.multiValuedRunnableIterator(new AbstractWorkflowsRunnable() {

        public void run(IEntity selfEntity, IBindingManager bm, IEntity... argsEntities) {
            String className = argsEntities[0].wStringValue();
            IEntity methodData = argsEntities[1];
            Method method;
            if (DataTypeUtils.getDataKind(methodData).isString())
                method = JavaReflectUtils.getMethod(className, methodData.wStringValue(), ReflectionFactory.getClassLoader(bm));
            else
                method = (Method) methodData.wGetValue();
            Object[] arguments = toArguments(method.getParameterTypes(), method.isVarArgs(), argsEntities);
            Object resultValue = JavaReflectUtils.invokeMethod(null, method, arguments);
            setResult(bm, resultVariable, resultValue, method.getReturnType());
        }
    }, runnableIterators).withSourceEntity(entity));
}
Also used : Variable(org.whole.lang.workflows.model.Variable) IEntity(org.whole.lang.model.IEntity) IBindingManager(org.whole.lang.bindings.IBindingManager) Expressions(org.whole.lang.workflows.model.Expressions) InvokeJavaClassMethod(org.whole.lang.workflows.model.InvokeJavaClassMethod) InvokeJavaInstanceMethod(org.whole.lang.workflows.model.InvokeJavaInstanceMethod) Method(java.lang.reflect.Method) IEntityIterator(org.whole.lang.iterators.IEntityIterator)

Example 2 with IEntityIterator

use of org.whole.lang.iterators.IEntityIterator in project whole by wholeplatform.

the class ActionsDynamicCompilerVisitor method visit.

@Override
public void visit(ActionCall entity) {
    SelectedEntities selectedEntitiesFeature = entity.getSelectedEntities();
    IEntityIterator<?>[] argumentsIterators = null;
    if (EntityUtils.isNotResolver(selectedEntitiesFeature)) {
        selectedEntitiesFeature.accept(this);
        IEntityIterator<?> resultIterator = getResultIterator();
        argumentsIterators = new IEntityIterator<?>[1];
        argumentsIterators[0] = resultIterator;
    }
    setResultIterator(new ActionCallIterator(entity.getName().getValue(), argumentsIterators).withSourceEntity(entity));
}
Also used : ActionCallIterator(org.whole.lang.actions.iterators.ActionCallIterator) SelectedEntities(org.whole.lang.actions.model.SelectedEntities) IEntityIterator(org.whole.lang.iterators.IEntityIterator)

Example 3 with IEntityIterator

use of org.whole.lang.iterators.IEntityIterator in project whole by wholeplatform.

the class WorkflowsInterpreterVisitor method visit.

@Override
public void visit(InvokeQuery entity) {
    Variable queryName = entity.getQueryName();
    Arguments arguments = entity.getArguments();
    IEntityIterator<? extends IEntity>[] argsIterators = new IEntityIterator<?>[0];
    Set<String> filterNames = getOperation().getResultsScope().wNames();
    IBindingManager args = BindingManagerFactory.instance.createBindingManager(BindingManagerFactory.instance.createExcludeFilterScope(filterNames).wWithEnclosingScope(getBindings()), getBindings().wGetEnvironmentManager());
    if (!EntityUtils.isNotResolver(arguments)) {
        setResultValue(argsIterators);
        arguments.accept(this);
        argsIterators = (IEntityIterator<?>[]) getResultValue();
    } else if (Matcher.match(WorkflowsEntityDescriptorEnum.Expressions, arguments)) {
        IEntity selfEntity = getBindings().wGet("self");
        argsIterators = new IEntityIterator<?>[arguments.wSize()];
        for (int i = 0; i < argsIterators.length; i++) {
            ((Expressions) arguments).get(i).accept(this);
            argsIterators[i] = getResultIterator();
            setResultIterator(null);
            resetSelfEntity(selfEntity);
        }
    } else
        define(args, (Assignments) arguments);
    IEntityIterator<?> iterator = QueriesIteratorFactory.callIterator(queryName.getValue(), argsIterators);
    iterator.setBindings(args);
    resetIterator(iterator);
    while (iterator.hasNext()) iterator.next();
}
Also used : Variable(org.whole.lang.workflows.model.Variable) IEntity(org.whole.lang.model.IEntity) Arguments(org.whole.lang.workflows.model.Arguments) IBindingManager(org.whole.lang.bindings.IBindingManager) Expressions(org.whole.lang.workflows.model.Expressions) IEntityIterator(org.whole.lang.iterators.IEntityIterator)

Example 4 with IEntityIterator

use of org.whole.lang.iterators.IEntityIterator 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)

Example 5 with IEntityIterator

use of org.whole.lang.iterators.IEntityIterator in project whole by wholeplatform.

the class WorkflowsDynamicCompilerVisitor method visit.

// FIXME add compilation of Assignments before fixing
// @Override
// public void visit(CreateEntity entity) {
// final Variable resultVariable = entity.getModel();
// 
// entity.getEntityName().accept(this);
// final String typeName = getResult().wStringValue();
// 
// IEntityRegistryProvider provider = null;
// switch (entity.getRegistry().getValue().getOrdinal()) {
// case RegistryEnum.DEFAULT_ord:
// provider = RegistryConfigurations.DEFAULT;
// break;
// case RegistryEnum.RESOLVER_ord:
// provider = RegistryConfigurations.RESOLVER;
// break;
// case RegistryEnum.ADAPTER_ord:
// provider = RegistryConfigurations.ADAPTER;
// break;
// case RegistryEnum.STRICT_ord:
// provider = RegistryConfigurations.STRICT;
// break;
// case RegistryEnum.CUSTOM_ord:
// provider = RegistryConfigurations.CUSTOM;
// break;
// }
// final IEntityFactory ef = GenericEntityFactory.instance(provider);
// 
// final Arguments arguments = entity.getArguments();
// int size = arguments.wSize();
// IEntityIterator<?>[] runnableIterators = new IEntityIterator<?>[size];
// 
// if (Matcher.matchImpl(WorkflowsEntityDescriptorEnum.Assignments, arguments)) {
// for (int i=0; i<size; i++) {
// Assign argument = ((Assignments) arguments).get(i);
// String name = argument.getName().getValue();
// argument.getExpression().accept(this);
// runnableIterators[i] = getResultIterator();//FIXME
// }
// } else if (Matcher.matchImpl(WorkflowsEntityDescriptorEnum.Expressions, arguments)) {
// for (int i=0; i<size; i++) {
// ((Expressions) arguments).get(i).accept(this);
// runnableIterators[i] = getResultIterator();
// }
// }
// 
// setResultIterator(IteratorFactory.multiValuedRunnableIterator(new AbstractWorkflowsRunnable() {
// public void run(IEntity selfEntity, IBindingManager bm, IEntity... argsEntities) {
// EntityDescriptor<?> ed = CommonsDataTypePersistenceParser.parseEntityDescriptor(typeName);
// if (ed == null)
// throw new IllegalArgumentException("The requested entity does not exist: "+typeName);
// 
// IEntity resultValue;
// if (Matcher.matchImpl(WorkflowsEntityDescriptorEnum.Assignments, arguments)) {
// ResettableScope resettableScope = BindingManagerFactory.instance.createResettableScope();
// bm.wEnterScope(resettableScope);
// 
// //FIXME
// for (int i = 0; i < arguments.wSize(); i++) {
// String name = ((Assignments) arguments).get(i).getName().getValue();
// bm.wSet(name, EntityUtils.convert(bm.wGet(name), ed.getEntityDescriptor(i)));
// }
// resultValue = ef.create(ed, bm);
// 
// resettableScope.reset();
// bm.wExitScope();
// } else if (Matcher.matchImpl(WorkflowsEntityDescriptorEnum.Expressions, arguments)) {
// if (ed.getEntityKind().isData())
// resultValue = DataTypeUtils.convert(argsEntities[0], ed);
// else {
// IEntity[] values = new IEntity[argsEntities.length];
// for (int i = 0; i < values.length; i++)
// values[i] = EntityUtils.convert(argsEntities[i], ed.getEntityDescriptor(i));
// resultValue = ef.create(ed, values);
// }
// } else
// resultValue = ef.create(ed);
// 
// setResult(bm, resultVariable, resultValue);
// }
// }, runnableIterators).withDomainEntity(entity));
// }
@Override
public void visit(CreateJavaClassInstance entity) {
    final Variable resultVariable = entity.getResult();
    Expressions arguments = entity.getArguments();
    int size = arguments.wSize();
    IEntityIterator<?>[] runnableIterators = new IEntityIterator<?>[2 + size];
    entity.getClassName().accept(this);
    runnableIterators[0] = getResultIterator();
    entity.getConstructor().accept(this);
    runnableIterators[1] = getResultIterator();
    for (int i = 0; i < size; i++) {
        arguments.get(i).accept(this);
        runnableIterators[2 + i] = getResultIterator();
    }
    setResultIterator(IteratorFactory.singleValuedRunnableIterator(new AbstractWorkflowsRunnable() {

        public void run(IEntity selfEntity, IBindingManager bm, IEntity... argsEntities) {
            String className = argsEntities[0].wStringValue();
            IEntity constructorData = argsEntities[1];
            Constructor<?> constructor;
            if (DataTypeUtils.getDataKind(constructorData).isString())
                constructor = JavaReflectUtils.getConstructor(className, constructorData.wStringValue(), ReflectionFactory.getClassLoader(bm));
            else
                constructor = (Constructor<?>) constructorData.wGetValue();
            Object[] arguments = toArguments(constructor.getParameterTypes(), constructor.isVarArgs(), argsEntities);
            Object resultValue = JavaReflectUtils.invokeConstructor(constructor, arguments);
            setResult(bm, resultVariable, resultValue, constructor.getDeclaringClass());
        }
    }, runnableIterators).withSourceEntity(entity));
}
Also used : Variable(org.whole.lang.workflows.model.Variable) IEntity(org.whole.lang.model.IEntity) Constructor(java.lang.reflect.Constructor) IBindingManager(org.whole.lang.bindings.IBindingManager) Expressions(org.whole.lang.workflows.model.Expressions) IEntityIterator(org.whole.lang.iterators.IEntityIterator)

Aggregations

IEntityIterator (org.whole.lang.iterators.IEntityIterator)8 IEntity (org.whole.lang.model.IEntity)7 IBindingManager (org.whole.lang.bindings.IBindingManager)5 Expressions (org.whole.lang.workflows.model.Expressions)4 Variable (org.whole.lang.workflows.model.Variable)4 Method (java.lang.reflect.Method)2 InvokeJavaClassMethod (org.whole.lang.workflows.model.InvokeJavaClassMethod)2 InvokeJavaInstanceMethod (org.whole.lang.workflows.model.InvokeJavaInstanceMethod)2 Constructor (java.lang.reflect.Constructor)1 HashMap (java.util.HashMap)1 ActionCallIterator (org.whole.lang.actions.iterators.ActionCallIterator)1 SelectedEntities (org.whole.lang.actions.model.SelectedEntities)1 ITransactionScope (org.whole.lang.bindings.ITransactionScope)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 MissingVariableException (org.whole.lang.visitors.MissingVariableException)1 Arguments (org.whole.lang.workflows.model.Arguments)1