Search in sources :

Example 11 with ITransactionScope

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

the class TypedModelTransactionHandler method canExecute.

@CanExecute
public boolean canExecute(@Named(ED_URI_PARAMETER_ID) String edUri, @Optional @Named(FD_URI_PARAMETER_ID) String fdUri, @Optional @Named(IServiceConstants.ACTIVE_SELECTION) IBindingManager bm) {
    ITransactionScope ts = BindingManagerFactory.instance.createTransactionScope();
    try {
        bm.wEnterScope(ts);
        defineBindings(edUri, fdUri, bm);
        return isEnabled(bm);
    } catch (Exception e) {
        return false;
    } finally {
        ts.rollback();
        bm.wExitScope();
    }
}
Also used : ITransactionScope(org.whole.lang.bindings.ITransactionScope) RollbackException(org.whole.lang.lifecycle.RollbackException) CanExecute(org.eclipse.e4.core.di.annotations.CanExecute)

Example 12 with ITransactionScope

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

the class OperationHandler method canExecute.

@CanExecute
public boolean canExecute(@Optional @Named(IServiceConstants.ACTIVE_SELECTION) IBindingManager bm) throws Exception {
    ITransactionScope ts = BindingManagerFactory.instance.createTransactionScope();
    try {
        bm.wEnterScope(ts);
        IEntityPartViewer viewer = (IEntityPartViewer) bm.wGetValue("viewer");
        return !viewer.getEditDomain().isDisabled() && isEnabled(bm);
    } catch (Exception e) {
        return false;
    } finally {
        ts.rollback();
        bm.wExitScope();
    }
}
Also used : ITransactionScope(org.whole.lang.bindings.ITransactionScope) IEntityPartViewer(org.whole.lang.ui.viewers.IEntityPartViewer) CanExecute(org.eclipse.e4.core.di.annotations.CanExecute)

Example 13 with ITransactionScope

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

the class ExecuteSampleModelRunnable method run.

@Override
public void run(IOperationProgressMonitor pm) throws InvocationTargetException, InterruptedException {
    IEntity selfEntity = EntityUtils.mapEntity(selfModel, EntityUtils.clone(EntityUtils.getCompoundRoot(selfModel)));
    Set<String> initialNames = bm.wNames();
    pm.beginTask("Executing sample...", IOperationProgressMonitor.TOTAL_WORK);
    behaviorModel = BehaviorUtils.apply("whole:org.whole.lang.ui.views:SamplePerspectiveSemantics#SampleViewBehavior", behaviorModel, bm);
    IEntity derivedModel = null;
    try {
        IEntityIterator<?> iterator = BehaviorUtils.lazyEvaluate(behaviorModel, 0, bm);
        iterator.setBindings(selfBindings);
        iterator.reset(selfEntity);
        if (iterator.getClass().equals(ConstantIterator.class)) {
            IEntity result = iterator.next();
            if (result == null || !EntityUtils.isData(result))
                derivedModel = result;
            else {
                Object resultValue = result.wGetValue();
                derivedModel = IVisitor.class.isInstance(resultValue) ? BindingManagerFactory.instance.createValue(Matcher.match((IVisitor) resultValue, selfEntity)) : result;
            }
        } else if (iterator.hasNext()) {
            derivedModel = MiscEntityFactory.instance.createMisc(0);
            ITransactionScope transactionScope = BindingManagerFactory.instance.createTransactionScope();
            bm.wEnterScope(transactionScope);
            try {
                for (IEntity result : iterator) {
                    transactionScope.commit();
                    derivedModel.wAdd(GenericEntityFactory.instance.create(CommonsEntityDescriptorEnum.SameStageFragment, // CommonsEntityFactory.instance.createSameStageFragment(
                    EntityUtils.clone(// TODO substitute with a no containment fragment
                    result)));
                }
            } finally {
                transactionScope.rollback();
                bm.wExitScope();
            }
        }
    } catch (MissingVariableException e) {
        addMissingVariables(contextModel, e);
    } catch (OperationCanceledException e) {
    // gracefully terminate execution
    } catch (Exception e) {
        if (e.getCause() instanceof MissingVariableException)
            addMissingVariables(contextModel, (MissingVariableException) e.getCause());
    } finally {
        pm.endTask();
    }
    IEntity variablesModel = null;
    if (derivedModel != null) {
        EnvironmentEntityFactory ef = EnvironmentEntityFactory.instance;
        variablesModel = ef.createBindings(0);
        for (String name : new TreeSet<String>(bm.wLocalNames())) if (!initialNames.contains(name))
            variablesModel.wAdd(ef.createBinding(ef.createName(name), ef.createValue(BindingUtils.wGet(bm, name))));
        final IEntity contents = derivedModel;
        final IEntity variables = variablesModel;
        context.get(UISynchronize.class).asyncExec(new Runnable() {

            public void run() {
                context.get(IEntityPartViewer.class).setContents(null, contents);
                context.get(IEventBroker.class).post(IE4UIConstants.TOPIC_UPDATE_VARIABLES, variables);
            }
        });
    }
}
Also used : ITransactionScope(org.whole.lang.bindings.ITransactionScope) IEntity(org.whole.lang.model.IEntity) IVisitor(org.whole.lang.visitors.IVisitor) OperationCanceledException(org.whole.lang.operations.OperationCanceledException) EnvironmentEntityFactory(org.whole.lang.environment.factories.EnvironmentEntityFactory) OperationCanceledException(org.whole.lang.operations.OperationCanceledException) InvocationTargetException(java.lang.reflect.InvocationTargetException) MissingVariableException(org.whole.lang.visitors.MissingVariableException) MissingVariableException(org.whole.lang.visitors.MissingVariableException) TreeSet(java.util.TreeSet) UISynchronize(org.eclipse.e4.ui.di.UISynchronize)

Example 14 with ITransactionScope

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

the class WorkflowsInterpreterVisitor method visit.

@Override
public void visit(CreateEntity entity) {
    try {
        entity.getEntityName().accept(this);
        String typeName = getResultString();
        EntityDescriptor<?> ed = CommonsDataTypePersistenceParser.parseEntityDescriptor(typeName);
        if (ed == null)
            throw new WholeIllegalArgumentException("The requested entity does not exist: " + typeName).withSourceEntity(entity).withBindings(getBindings());
        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;
        }
        IEntityFactory ef = GenericEntityFactory.instance(provider);
        IEntity model;
        Arguments arguments = entity.getArguments();
        if (Matcher.matchImpl(WorkflowsEntityDescriptorEnum.Assignments, arguments)) {
            ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
            getBindings().wEnterScope(resettableScope);
            arguments.accept(this);
            for (int i = 0; i < arguments.wSize(); i++) {
                String name = ((Assignments) arguments).get(i).getName().getValue();
                FeatureDescriptor fd = ed.getFeatureDescriptorEnum().valueOf(name);
                if (fd != null)
                    getBindings().wDef(name, EntityUtils.convertCloneIfReparenting(getBindings().wGet(name), ed.getEntityFeatureDescriptor(fd)));
            }
            model = ef.create(ed, getBindings());
            resettableScope.rollback();
            getBindings().wExitScope();
        } else if (Matcher.matchImpl(WorkflowsEntityDescriptorEnum.Expressions, arguments)) {
            IEntity selfEntity = getBindings().wGet("self");
            if (ed.getEntityKind().isData()) {
                ((Expressions) arguments).get(0).accept(this);
                model = DataTypeUtils.convertCloneIfParented(getResult(), ed);
                resetSelfEntity(selfEntity);
            } else {
                IEntity[] values = new IEntity[arguments.wSize()];
                for (int i = 0; i < values.length; i++) {
                    ((Expressions) arguments).get(i).accept(this);
                    values[i] = EntityUtils.convertCloneIfReparenting(getResult(), ed.getEntityFeatureDescriptor(i));
                    resetSelfEntity(selfEntity);
                }
                model = ef.create(ed, values);
            }
        } else
            model = ef.create(ed);
        setResult(entity.getModel(), model);
    } catch (Exception e) {
        throw IWholeRuntimeException.asWholeException(e, entity, getBindings());
    }
}
Also used : ITransactionScope(org.whole.lang.bindings.ITransactionScope) IEntity(org.whole.lang.model.IEntity) IEntityFactory(org.whole.lang.factories.IEntityFactory) Arguments(org.whole.lang.workflows.model.Arguments) Assignments(org.whole.lang.workflows.model.Assignments) WholeIllegalArgumentException(org.whole.lang.exceptions.WholeIllegalArgumentException) 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) FeatureDescriptor(org.whole.lang.reflect.FeatureDescriptor) IEntityRegistryProvider(org.whole.lang.factories.IEntityRegistryProvider) Expressions(org.whole.lang.workflows.model.Expressions)

Example 15 with ITransactionScope

use of org.whole.lang.bindings.ITransactionScope 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

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