Search in sources :

Example 16 with ITransactionScope

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

the class TestsLearningInterpreterVisitor method visit.

@Override
public void visit(TestSuite entity) {
    if (isLearning()) {
        Map<IEntity, List<IEntity>> learntMap = getLearntMap();
        IEntity result = null;
        for (int cycle = 1; cycle <= learnCycles(); cycle++) {
            printWriter().printf("*** Learning cycle %d ***\n\n", cycle);
            ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
            getBindings().wEnterScope(resettableScope);
            try {
                getBindings().wDefValue("learnCycle", cycle);
                super.visit(entity);
                result = getBindings().getResult();
            } finally {
                resettableScope.rollback();
                getBindings().wExitScope();
            }
        }
        getBindings().setResult(result);
        FilterFamily filterFamily = getFilterFamily(entity);
        FilterRules filterRules = filterFamily.getFilterRules();
        FreshNameGenerator fnGen = new FreshNameGenerator();
        for (IEntity name : BehaviorUtils.compileAndLazyEvaluate(createFindAllFilterRuleNamesQuery(), filterFamily)) fnGen.addBoundName(name.wStringValue());
        for (IEntity adapter : learntMap.keySet()) {
            List<IEntity> learntEntities = learntMap.get(adapter);
            IEntity value = learntEntities.get(0);
            if (learntEntities.size() > 1 && EntityUtils.isData(value)) {
                for (IEntity learntEntity : learntEntities) if (!learntEntity.wEquals(value))
                    continue;
            } else if (learntEntities.size() > 1) {
                for (IEntity learntEntity2 : learntEntities) if (EntityUtils.isData(learntEntity2))
                    continue;
                // generate filter rule
                FilterRule filterRule = TestsHelpers.createFilterRule(learntEntities);
                if (EntityUtils.isNotResolver(filterRule)) {
                    String filterName;
                    IEntity filterBody;
                    if ((filterBody = Matcher.find(filterRule.getBody(), filterRules, false)) != null) {
                        // try to reuse a generated filter
                        filterName = ((FilterRule) filterBody.wGetParent()).getName().getValue();
                    } else {
                        // add the filter rule to the filter family
                        filterName = fnGen.nextFreshName(GENERATED_FILTER_NAME);
                        ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
                        getBindings().wEnterScope(resettableScope);
                        try {
                            getBindings().wDefValue("filterName", filterName);
                            Matcher.substitute(filterRule, getBindings(), false);
                            filterRules.wAdd(filterRule);
                        } finally {
                            resettableScope.rollback();
                            getBindings().wExitScope();
                        }
                    }
                    // wrap SubjectStatement with a UsingFilter
                    ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
                    getBindings().wEnterScope(resettableScope);
                    try {
                        SubjectStatement statement = BehaviorUtils.evaluateFirstResult(createFindAncestorSubjectStatement(), adapter, getBindings());
                        UsingFilter usingFilter = createUsingFilter(filterName);
                        statement.wGetParent().wSet(statement, usingFilter);
                        usingFilter.setSubjectStatement(statement);
                    } finally {
                        resettableScope.rollback();
                        getBindings().wExitScope();
                    }
                }
            }
            TestsHelpers.replace(adapter, value);
        }
        // add the newly generated family
        if (!EntityUtils.hasParent(filterFamily) && !filterRules.wIsEmpty())
            entity.getFilterFamilies().wAdd(filterFamily);
    } else
        super.visit(entity);
}
Also used : ITransactionScope(org.whole.lang.bindings.ITransactionScope) FilterFamily(org.whole.lang.tests.model.FilterFamily) IEntity(org.whole.lang.model.IEntity) FilterRule(org.whole.lang.tests.model.FilterRule) SubjectStatement(org.whole.lang.tests.model.SubjectStatement) UsingFilter(org.whole.lang.tests.model.UsingFilter) FilterRules(org.whole.lang.tests.model.FilterRules) ArrayList(java.util.ArrayList) List(java.util.List) FreshNameGenerator(org.whole.lang.util.FreshNameGenerator)

Example 17 with ITransactionScope

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

the class IEntityIterator method evaluateSingleton.

public default E evaluateSingleton() {
    E result = null;
    IBindingManager bm = getBindings();
    ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
    bm.wEnterScope(resettableScope);
    try {
        if (hasNext()) {
            bm.setResult(result = next());
            resettableScope.commit();
        }
        if (result == null || hasNext())
            throw new IllegalArgumentException("The result is not a singleton");
    } finally {
        resettableScope.rollback();
        bm.wExitScope();
    }
    return result;
}
Also used : ITransactionScope(org.whole.lang.bindings.ITransactionScope) IBindingManager(org.whole.lang.bindings.IBindingManager)

Example 18 with ITransactionScope

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

the class DynamicInterpreterVisitor method apply.

protected void apply(IEntityIterator<?> iterator) {
    IBindingManager bm = getBindings();
    ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
    bm.wEnterScope(resettableScope);
    for (IEntity e : iterator) {
        bm.setResult(e);
        resettableScope.commit();
    }
    resettableScope.rollback();
    bm.wExitScope();
}
Also used : ITransactionScope(org.whole.lang.bindings.ITransactionScope) IEntity(org.whole.lang.model.IEntity) IBindingManager(org.whole.lang.bindings.IBindingManager)

Example 19 with ITransactionScope

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

the class GenericCloneVisitor method setChildren.

protected void setChildren(IEntity entity, IEntity entityClone) {
    IEntity oldSelfEntity2 = getBindings().wGet("self");
    for (int index = 0; index < entity.wSize(); index++) {
        int resultSize = entityClone.wSize();
        stagedVisitIfNeeded(entityClone.wGet(index));
        // was			applyIfNeeded(entity.wGet(index));
        int nextResultSize = entityClone.wSize();
        index += (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 = entityClone.wGetFeatureDescriptor(index);
            if (EntityUtils.isComposite(entityClone)) {
                entityClone.wRemove(index--);
                if (iterator.hasNext()) {
                    ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
                    getBindings().wEnterScope(resettableScope);
                    resultSize = entityClone.wSize();
                    for (IEntity value : iterator) {
                        nextResultSize = entityClone.wSize();
                        index += (nextResultSize - resultSize);
                        if (BindingManagerFactory.instance.isVoid(value))
                            resultSize = nextResultSize;
                        else {
                            entityClone.wAdd(++index, EntityUtils.convertCloneIfReparenting(value, resultChildDescriptor));
                            resultSize = entityClone.wSize();
                        }
                        resettableScope.commit();
                    }
                    resettableScope.rollback();
                    getBindings().wExitScope();
                }
            } else {
                IEntity value = null;
                if (iterator.hasNext()) {
                    ITransactionScope resettableScope = BindingManagerFactory.instance.createTransactionScope();
                    getBindings().wEnterScope(resettableScope);
                    for (IEntity r : iterator) {
                        if (!BindingManagerFactory.instance.isVoid(r))
                            value = r;
                        resettableScope.commit();
                    }
                    resettableScope.rollback();
                    getBindings().wExitScope();
                }
                if (value != null)
                    entityClone.wSet(index, EntityUtils.convertCloneIfReparenting(value, resultChildDescriptor));
                else
                    entityClone.wRemove(index);
            }
        } else {
            IEntity value = getResult();
            if (value != null && !BindingManagerFactory.instance.isVoid(value)) {
                FeatureDescriptor resultChildDescriptor = entityClone.wGetFeatureDescriptor(index);
                entityClone.wSet(index, EntityUtils.convertCloneIfReparenting(value, resultChildDescriptor));
            } else
                entityClone.wRemove(index);
        }
    }
}
Also used : ITransactionScope(org.whole.lang.bindings.ITransactionScope) IEntity(org.whole.lang.model.IEntity) InternalIEntity(org.whole.lang.model.InternalIEntity) FeatureDescriptor(org.whole.lang.reflect.FeatureDescriptor)

Example 20 with ITransactionScope

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

the class ActionCallHandler method canExecute.

@CanExecute
public boolean canExecute(@Named(FUNCTION_URI_PARAMETER_ID) String functionUri, @Named(PREDICATE_XWL_PARAMETER_ID) String predicateXwl, @Optional @Named(ANALYSING_PARAMETER_ID) String analyzing, @Optional @Named(IServiceConstants.ACTIVE_SELECTION) IBindingManager bm) throws Exception {
    ITransactionScope ts = BindingManagerFactory.instance.createTransactionScope();
    try {
        bm.wEnterScope(ts);
        defineBindings(functionUri, predicateXwl, analyzing, bm);
        return HandlersBehavior.canCallAction(bm);
    } catch (Exception e) {
        return false;
    } finally {
        ts.rollback();
        bm.wExitScope();
    }
}
Also used : ITransactionScope(org.whole.lang.bindings.ITransactionScope) CanExecute(org.eclipse.e4.core.di.annotations.CanExecute)

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