Search in sources :

Example 6 with ITransactionScope

use of org.whole.lang.bindings.ITransactionScope in project whole by wholeplatform.

the class WorkflowsInterpreterVisitor method visit.

@Override
public void visit(CreateModel entity) {
    ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
    getBindings().wEnterScope(resettableScope);
    entity.getBindings().accept(this);
    entity.getTemplate().accept(this);
    IEntity model = getResult();
    // TODO remove ?
    if (Matcher.matchImpl(WorkflowsEntityDescriptorEnum.Name, entity.getTemplate())) {
        IEntityIterator<IEntity> tii = QueriesIteratorFactory.templateInterpreterIterator(getResult());
        tii.setBindings(getBindings());
        tii.reset(entity);
        model = tii.next();
    }
    resettableScope.rollback();
    getBindings().wExitScope();
    setResult(entity.getModel(), model);
}
Also used : ITransactionScope(org.whole.lang.bindings.ITransactionScope) IEntity(org.whole.lang.model.IEntity)

Example 7 with ITransactionScope

use of org.whole.lang.bindings.ITransactionScope in project whole by wholeplatform.

the class GenericTemplateInterpreterVisitor method visit.

public void visit(IEntity entity) {
    IEntity adaptee = entity.wGetAdaptee(false);
    EntityDescriptor<?> ed = adaptee.wGetEntityDescriptor();
    // TODO workaround move into a separate visitor
    if (ed.getLanguageKit().getURI().equals(CommonsLanguageKit.URI)) {
        switch(ed.getOrdinal()) {
            case CommonsEntityDescriptorEnum.Resolver_ord:
                break;
            case CommonsEntityDescriptorEnum.SameStageFragment_ord:
                stagedVisit(adaptee.wGetRoot());
                if (getStage() > 0)
                    setResult(GenericEntityFactory.instance.create(CommonsEntityDescriptorEnum.SameStageFragment, // CommonsEntityFactory.instance.createSameStageFragment(
                    EntityUtils.cloneIfParented(getResult())));
                return;
            case CommonsEntityDescriptorEnum.RootFragment_ord:
            case CommonsEntityDescriptorEnum.StageDownFragment_ord:
                setResult(null);
                IEntity oldSelfEntity = getBindings().wGet("self");
                final int stageShift0 = -1;
                stagedVisit(adaptee.wGetRoot(), stageShift0);
                if (getBindings().wGet("self") != oldSelfEntity)
                    getBindings().wDef("self", oldSelfEntity);
                if (getStage() + stageShift0 > 0 && !isResultIterator())
                    setResult(GenericEntityFactory.instance.create(CommonsEntityDescriptorEnum.StageDownFragment, // CommonsEntityFactory.instance.createStageDownFragment(
                    EntityUtils.cloneIfParented(getResult())));
                return;
            case CommonsEntityDescriptorEnum.StageUpFragment_ord:
                final int stageShift = +1;
                stagedVisit(adaptee.wGetRoot(), stageShift);
                if (getStage() > 0) {
                    setResult(GenericEntityFactory.instance.create(CommonsEntityDescriptorEnum.StageUpFragment, // CommonsEntityFactory.instance.createStageUpFragment(
                    EntityUtils.cloneIfParented(BehaviorUtils.evaluateResult(getBindings()))));
                }
                return;
            case CommonsEntityDescriptorEnum.Variable_ord:
            case CommonsEntityDescriptorEnum.InlineVariable_ord:
                Variable variable = (Variable) adaptee;
                String varName = variable.getVarName().getValue();
                IEntity value = getBindings().wGet(varName);
                if (value != null) {
                    if (BindingManagerFactory.instance.isVoid(value))
                        setResult(value);
                    else {
                        QuantifierEnum.Value quantifierValue = variable.getQuantifier().getValue();
                        if (quantifierValue.isComposite()) {
                            Variable newVariable = EntityUtils.clone(variable);
                            newVariable.getQuantifier().setValue(quantifierValue.toOptional());
                            if (EntityUtils.isInlineVariable(variable)) {
                                // TODO constantChildIterator should call EntityUtils.convert(childValue, varType)
                                setResultIterator(IteratorFactory.sequenceIterator(IteratorFactory.constantChildIterator(value), IteratorFactory.constantIterator(newVariable, true)));
                            } else {
                                EntityDescriptor<?> varType = variable.getVarType().getValue();
                                try {
                                    setResultIterator(IteratorFactory.sequenceIterator(IteratorFactory.constantIterator(EntityUtils.convertCloneIfParented(value, varType), true), IteratorFactory.constantIterator(newVariable, true)));
                                } catch (IllegalArgumentException e) {
                                    throw new SubstituteException(variable, value.wGetEntityDescriptor());
                                }
                            }
                        } else {
                            if (EntityUtils.isInlineVariable(variable)) {
                                // TODO constantChildIterator should call EntityUtils.convert(childValue, varType)
                                setResultIterator(IteratorFactory.constantChildIterator(value));
                            } else {
                                EntityDescriptor<?> varType = variable.getVarType().getValue();
                                try {
                                    setResult(EntityUtils.convertCloneIfParented(value, varType));
                                } catch (IllegalArgumentException e) {
                                    throw new SubstituteException(variable, value.wGetEntityDescriptor());
                                }
                            }
                        }
                    }
                    return;
                }
        }
    }
    IEntity result = ((InternalIEntity) adaptee).wShallowClone();
    IEntity oldSelfEntity2 = getBindings().wGet("self");
    for (int i = 0; i < result.wSize(); i++) {
        int resultSize = result.wSize();
        visit(result.wGet(i));
        int nextResultSize = result.wSize();
        i += (nextResultSize - resultSize);
        if (isResultIterator()) {
            IEntityIterator<?> iterator = getResultIterator();
            setResultIterator(null);
            IEntity selfEntity = getBindings().wGet("self");
            if (selfEntity != oldSelfEntity2)
                getBindings().wDef("self", selfEntity = oldSelfEntity2);
            iterator.reset(selfEntity);
            FeatureDescriptor resultChildDescriptor = result.wGetFeatureDescriptor(i);
            if (EntityUtils.isComposite(result)) {
                result.wRemove(i--);
                if (iterator.hasNext()) {
                    ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
                    getBindings().wEnterScope(resettableScope);
                    resultSize = result.wSize();
                    for (IEntity e : iterator) {
                        nextResultSize = result.wSize();
                        i += (nextResultSize - resultSize);
                        if (BindingManagerFactory.instance.isVoid(e))
                            resultSize = nextResultSize;
                        else {
                            result.wAdd(++i, EntityUtils.convertCloneIfReparenting(e, resultChildDescriptor));
                            resultSize = result.wSize();
                        }
                        resettableScope.commit();
                    }
                    resettableScope.rollback();
                    getBindings().wExitScope();
                }
            } else {
                IEntity e = null;
                if (iterator.hasNext()) {
                    ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
                    getBindings().wEnterScope(resettableScope);
                    for (IEntity r : iterator) {
                        if (!BindingManagerFactory.instance.isVoid(r))
                            e = r;
                        resettableScope.commit();
                    }
                    resettableScope.rollback();
                    getBindings().wExitScope();
                }
                if (e != null)
                    result.wSet(i, EntityUtils.convertCloneIfReparenting(e, resultChildDescriptor));
                else
                    result.wRemove(i);
            }
        } else {
            IEntity child = getResult();
            if (child != null && !BindingManagerFactory.instance.isVoid(child)) {
                FeatureDescriptor resultChildDescriptor = result.wGetAdaptee(false).wGetFeatureDescriptor(i);
                result.wSet(i, EntityUtils.convertCloneIfReparenting(child, resultChildDescriptor));
            } else
                result.wRemove(i);
        }
        IEntity selfEntity = getBindings().wGet("self");
        if (selfEntity != oldSelfEntity2)
            getBindings().wDef("self", selfEntity = oldSelfEntity2);
    }
    setResult(result);
}
Also used : Variable(org.whole.lang.commons.model.Variable) ITransactionScope(org.whole.lang.bindings.ITransactionScope) IEntity(org.whole.lang.model.IEntity) InternalIEntity(org.whole.lang.model.InternalIEntity) SubstituteException(org.whole.lang.matchers.SubstituteException) QuantifierEnum(org.whole.lang.commons.model.QuantifierEnum) FeatureDescriptor(org.whole.lang.reflect.FeatureDescriptor) InternalIEntity(org.whole.lang.model.InternalIEntity)

Example 8 with ITransactionScope

use of org.whole.lang.bindings.ITransactionScope in project whole by wholeplatform.

the class HandlersBehavior method generateJava.

public static void generateJava(IBindingManager bm) {
    ClassLoader cl = ReflectionFactory.getClassLoader(bm);
    ITransactionScope ts = BindingManagerFactory.instance.createTransactionScope();
    try {
        bm.wEnterScope(ts);
        Class<?> generatorClass = Class.forName("org.whole.lang.ui.actions.JavaModelGeneratorAction", true, cl);
        Method generateMethod = generatorClass.getMethod("generate", IProgressMonitor.class, IEntity.class, IBindingManager.class);
        final IOperationProgressMonitor operationProgressMonitor = (IOperationProgressMonitor) bm.wGetValue("progressMonitor");
        generateMethod.invoke(null, operationProgressMonitor.getAdapter(IProgressMonitor.class), bm.wGet("self"), bm);
    } catch (OperationCanceledException e) {
        throw e;
    } catch (Exception e) {
        throw new IllegalStateException(e);
    } finally {
        ts.rollback();
        bm.wExitScope();
    }
}
Also used : IProgressMonitor(org.eclipse.core.runtime.IProgressMonitor) ITransactionScope(org.whole.lang.bindings.ITransactionScope) OperationCanceledException(org.whole.lang.operations.OperationCanceledException) Method(java.lang.reflect.Method) IOperationProgressMonitor(org.whole.lang.operations.IOperationProgressMonitor) OperationCanceledException(org.whole.lang.operations.OperationCanceledException)

Example 9 with ITransactionScope

use of org.whole.lang.bindings.ITransactionScope in project whole by wholeplatform.

the class PerformHandler method execute.

@Execute
public void execute(@Named(BEHAVIOR_XWL_PARAMETER_ID) String behaviorXwl, @Named(PREDICATE_XWL_PARAMETER_ID) String predicateXwl, @Optional @Named(DESCRIPTION_PARAMETER_ID) String label, @Optional @Named(IServiceConstants.ACTIVE_SELECTION) IBindingManager bm, IEntityPartViewer viewer) throws Exception {
    ModelTransactionCommand mtc = new ModelTransactionCommand(bm.wGet("focusEntity"), label);
    CommandStack commandStack = viewer.getEditDomain().getCommandStack();
    ITransactionScope ts = BindingManagerFactory.instance.createTransactionScope();
    try {
        bm.wEnterScope(ts);
        defineBindings(behaviorXwl, predicateXwl, bm);
        mtc.begin();
        HandlersBehavior.perform(bm);
        mtc.commit();
        if (mtc.canUndo())
            commandStack.execute(mtc);
    } catch (RuntimeException e) {
        mtc.rollbackIfNeeded();
        throw e;
    } finally {
        ts.rollback();
        bm.wExitScope();
    }
}
Also used : ModelTransactionCommand(org.whole.lang.ui.commands.ModelTransactionCommand) CommandStack(org.eclipse.gef.commands.CommandStack) ITransactionScope(org.whole.lang.bindings.ITransactionScope) CanExecute(org.eclipse.e4.core.di.annotations.CanExecute) Execute(org.eclipse.e4.core.di.annotations.Execute)

Example 10 with ITransactionScope

use of org.whole.lang.bindings.ITransactionScope in project whole by wholeplatform.

the class TypedModelTransactionHandler method execute.

@Execute
public void execute(@Named(ED_URI_PARAMETER_ID) String edUri, @Optional @Named(FD_URI_PARAMETER_ID) String fdUri, @Optional @Named(IServiceConstants.ACTIVE_SELECTION) IBindingManager bm, IEntityPartViewer viewer) throws Exception {
    EntityEditDomainJob.asyncExec("replacing entity...", viewer.getEditDomain(), (monitor) -> {
        CommandStack commandStack = viewer.getEditDomain().getCommandStack();
        ModelTransactionCommand mtc = new ModelTransactionCommand(bm.wGet("focusEntity"));
        ITransactionScope ts = BindingManagerFactory.instance.createTransactionScope();
        try {
            bm.wEnterScope(ts);
            defineBindings(edUri, fdUri, bm);
            mtc.setLabel(getLabel(bm));
            mtc.begin();
            TypedModelTransactionHandler.this.run(bm);
            mtc.commit();
            if (mtc.canUndo())
                commandStack.execute(mtc);
        } catch (RollbackException e) {
        // rollback done
        } catch (RuntimeException e) {
            mtc.rollbackIfNeeded();
            throw e;
        } finally {
            ts.rollback();
            bm.wExitScope();
        }
    });
}
Also used : CommandStack(org.eclipse.gef.commands.CommandStack) ModelTransactionCommand(org.whole.lang.ui.commands.ModelTransactionCommand) ITransactionScope(org.whole.lang.bindings.ITransactionScope) RollbackException(org.whole.lang.lifecycle.RollbackException) CanExecute(org.eclipse.e4.core.di.annotations.CanExecute) Execute(org.eclipse.e4.core.di.annotations.Execute)

Aggregations

ITransactionScope (org.whole.lang.bindings.ITransactionScope)27 IEntity (org.whole.lang.model.IEntity)14 CanExecute (org.eclipse.e4.core.di.annotations.CanExecute)10 Execute (org.eclipse.e4.core.di.annotations.Execute)4 CommandStack (org.eclipse.gef.commands.CommandStack)4 IBindingManager (org.whole.lang.bindings.IBindingManager)4 RollbackException (org.whole.lang.lifecycle.RollbackException)4 ModelTransactionCommand (org.whole.lang.ui.commands.ModelTransactionCommand)4 OperationCanceledException (org.whole.lang.operations.OperationCanceledException)3 FeatureDescriptor (org.whole.lang.reflect.FeatureDescriptor)3 IEntityPartViewer (org.whole.lang.ui.viewers.IEntityPartViewer)3 IWholeRuntimeException (org.whole.lang.exceptions.IWholeRuntimeException)2 InternalIEntity (org.whole.lang.model.InternalIEntity)2 IVisitor (org.whole.lang.visitors.IVisitor)2 MissingVariableException (org.whole.lang.visitors.MissingVariableException)2 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 Method (java.lang.reflect.Method)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1