Search in sources :

Example 6 with Variable

use of org.whole.lang.workflows.model.Variable in project whole by wholeplatform.

the class WorkflowsInterpreterVisitor method visit.

@Override
public void visit(ForeachLoop entity) {
    evaluate(entity.getCompositeVariable());
    IEntityIterator<?> iterator = null;
    if (isResultIterator()) {
        iterator = getResultIterator();
        setResultIterator(null);
        resetIterator(iterator);
    } else {
        IEntity result = getResult();
        if (result == null)
            return;
        iterator = IteratorFactory.childIterator();
        iterator.setBindings(getBindings());
        iterator.reset(result);
    }
    Variable elementVariable = entity.getElementVariable();
    String elementVar = DataTypeUtils.getDataKind(elementVariable).isString() ? elementVariable.wStringValue() : null;
    Variable indexVariable = entity.getIndexVariable();
    String indexVar = DataTypeUtils.getDataKind(indexVariable).isString() ? indexVariable.wStringValue() : null;
    int index = 0;
    while (iterator.hasNext()) {
        handleCancelRequest();
        if (indexVar != null)
            getBindings().wDefValue(indexVar, index++);
        if (elementVar != null)
            getBindings().wDef(elementVar, iterator.next());
        entity.getFlowObject().accept(this);
    }
}
Also used : Variable(org.whole.lang.workflows.model.Variable) IEntity(org.whole.lang.model.IEntity)

Example 7 with Variable

use of org.whole.lang.workflows.model.Variable in project whole by wholeplatform.

the class WorkflowsInterpreterVisitor method visit.

@Override
public void visit(LoadArtifacts entity) {
    try {
        IArtifactsEntity model = null;
        Variable variable = entity.getModel();
        String variableName = variable.getValue();
        boolean isDefined = getBindings().wIsSet(variableName);
        if (isDefined) {
            variable.accept(this);
            model = (IArtifactsEntity) getResult();
        }
        entity.getRootResource().accept(this);
        String resource = getResultString();
        ResourceKind resourceKind = entity.getRootResourceKind();
        Traverse traverse = Traverse.valueOf(DataTypeUtils.getAsPersistenceString(entity.getTraversalStrategy()));
        Synchronize synchronize = Synchronize.valueOf(DataTypeUtils.getAsPersistenceString(entity.getSynchronizeStrategy()));
        IPersistenceKit defaultPersistenceKit = getPersistenceKit(entity.getDefaultPersistence());
        IArtifactsEntity result = ArtifactsSynchronizerVisitor.synchronize(model, traverse, synchronize, createArtifactsBindings(resource, resourceKind), defaultPersistenceKit, true);
        if (isDefined) {
            if (EntityUtils.hasParent(model))
                model.wGetParent().wSet(model, result);
            getBindings().wSet(variableName, result);
        } else
            getBindings().wDef(variableName, result);
        setResult(result);
    } catch (Exception e) {
        throw IWholeRuntimeException.asWholeException(e, entity, getBindings());
    }
}
Also used : Variable(org.whole.lang.workflows.model.Variable) IArtifactsEntity(org.whole.lang.artifacts.model.IArtifactsEntity) Synchronize(org.whole.lang.artifacts.visitors.ArtifactsSynchronizerVisitor.Synchronize) ResourceKind(org.whole.lang.workflows.model.ResourceKind) IPersistenceKit(org.whole.lang.codebase.IPersistenceKit) MissingVariableException(org.whole.lang.visitors.MissingVariableException) IOException(java.io.IOException) IWholeRuntimeException(org.whole.lang.exceptions.IWholeRuntimeException) VisitException(org.whole.lang.visitors.VisitException) ParseException(org.whole.lang.parsers.ParseException) WholeIllegalArgumentException(org.whole.lang.exceptions.WholeIllegalArgumentException) Traverse(org.whole.lang.artifacts.visitors.ArtifactsSynchronizerVisitor.Traverse)

Example 8 with Variable

use of org.whole.lang.workflows.model.Variable in project whole by wholeplatform.

the class WorkflowsInterpreterVisitor method visit.

@Override
public void visit(InvokeJavaInstanceMethod entity) {
    try {
        entity.getClassName().accept(this);
        String className = getResultString();
        entity.getMethod().accept(this);
        IEntity methodData = getResult();
        Method method;
        if (DataTypeUtils.getDataKind(methodData).isString())
            method = JavaReflectUtils.getMethod(className, methodData.wStringValue(), ReflectionFactory.getClassLoader(getBindings()));
        else
            method = (Method) methodData.wGetValue();
        Object[] arguments = toArguments(method.getParameterTypes(), method.isVarArgs(), entity.getArguments());
        Variable object = entity.getObject();
        Object instance = unbox(method.getDeclaringClass(), object, false);
        Object resultValue = JavaReflectUtils.invokeMethod(instance, method, arguments);
        setResult(entity.getResult(), resultValue, method.getReturnType());
    } catch (Exception e) {
        throw IWholeRuntimeException.asWholeException(e, entity, getBindings());
    }
}
Also used : Variable(org.whole.lang.workflows.model.Variable) IEntity(org.whole.lang.model.IEntity) InvokeJavaClassMethod(org.whole.lang.workflows.model.InvokeJavaClassMethod) InvokeJavaInstanceMethod(org.whole.lang.workflows.model.InvokeJavaInstanceMethod) Method(java.lang.reflect.Method) MissingVariableException(org.whole.lang.visitors.MissingVariableException) IOException(java.io.IOException) IWholeRuntimeException(org.whole.lang.exceptions.IWholeRuntimeException) VisitException(org.whole.lang.visitors.VisitException) ParseException(org.whole.lang.parsers.ParseException) WholeIllegalArgumentException(org.whole.lang.exceptions.WholeIllegalArgumentException)

Example 9 with Variable

use of org.whole.lang.workflows.model.Variable 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)

Example 10 with Variable

use of org.whole.lang.workflows.model.Variable in project whole by wholeplatform.

the class WorkflowsDynamicCompilerVisitor method visit.

@Override
public void visit(InvokeJavaInstanceMethod entity) {
    final Variable resultVariable = entity.getResult();
    final Variable instanceVariable = entity.getObject();
    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();
            IEntity instanceEntity = bm.wGet(instanceVariable.getValue());
            if (instanceEntity == null)
                throw new MissingVariableException(instanceVariable.getValue()).withSourceEntity(getSourceEntity()).withBindings(getBindings());
            Object instance = DataTypeUtils.unbox(instanceEntity, method.getDeclaringClass(), false);
            Object[] arguments = toArguments(method.getParameterTypes(), method.isVarArgs(), argsEntities);
            Object resultValue = JavaReflectUtils.invokeMethod(instance, 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) MissingVariableException(org.whole.lang.visitors.MissingVariableException)

Aggregations

Variable (org.whole.lang.workflows.model.Variable)11 IEntity (org.whole.lang.model.IEntity)7 IBindingManager (org.whole.lang.bindings.IBindingManager)5 IEntityIterator (org.whole.lang.iterators.IEntityIterator)4 Expressions (org.whole.lang.workflows.model.Expressions)4 Method (java.lang.reflect.Method)3 MissingVariableException (org.whole.lang.visitors.MissingVariableException)3 VisitException (org.whole.lang.visitors.VisitException)3 InvokeJavaClassMethod (org.whole.lang.workflows.model.InvokeJavaClassMethod)3 InvokeJavaInstanceMethod (org.whole.lang.workflows.model.InvokeJavaInstanceMethod)3 IOException (java.io.IOException)2 IWholeRuntimeException (org.whole.lang.exceptions.IWholeRuntimeException)2 WholeIllegalArgumentException (org.whole.lang.exceptions.WholeIllegalArgumentException)2 ParseException (org.whole.lang.parsers.ParseException)2 Constructor (java.lang.reflect.Constructor)1 TreeSet (java.util.TreeSet)1 IArtifactsEntity (org.whole.lang.artifacts.model.IArtifactsEntity)1 Synchronize (org.whole.lang.artifacts.visitors.ArtifactsSynchronizerVisitor.Synchronize)1 Traverse (org.whole.lang.artifacts.visitors.ArtifactsSynchronizerVisitor.Traverse)1 IFilePersistenceProvider (org.whole.lang.codebase.IFilePersistenceProvider)1