Search in sources :

Example 1 with FilterRule

use of org.whole.lang.tests.model.FilterRule in project whole by wholeplatform.

the class TestsHelpers method createFilterRule.

public static FilterRule createFilterRule(Collection<IEntity> entities) {
    // create the filter logic
    QueriesEntityFactory qef = QueriesEntityFactory.instance;
    Sequence sequence = qef.createSequence(0);
    Iterator<IEntity> iterator = entities.iterator();
    IEntity baseEntity = iterator.next();
    while (iterator.hasNext()) updateFilterBody(baseEntity, iterator.next(), sequence);
    if (sequence.wIsEmpty())
        return createResolver(FilterRule);
    // create the filter rule
    TestsEntityFactory tef = TestsEntityFactory.instance;
    FilterRule filterRule = tef.createFilterRule(tef.createDescription("Auto-generated filter rule"), createVariable(Name, "filterName"), sequence.wGetAdapter(Statement));
    return filterRule;
}
Also used : IEntity(org.whole.lang.model.IEntity) QueriesEntityFactory(org.whole.lang.queries.factories.QueriesEntityFactory) TestsEntityFactory(org.whole.lang.tests.factories.TestsEntityFactory) Sequence(org.whole.lang.queries.model.Sequence) FilterRule(org.whole.lang.tests.reflect.TestsEntityDescriptorEnum.FilterRule) FilterRule(org.whole.lang.tests.model.FilterRule)

Example 2 with FilterRule

use of org.whole.lang.tests.model.FilterRule in project whole by wholeplatform.

the class TestsHelpers method createFilterRulesMap.

public static Map<String, FilterRule> createFilterRulesMap(ITemplateManager templateManager) {
    Map<String, FilterRule> filterRulesMap = new HashMap<String, FilterRule>();
    TestsEntityFactory ef = TestsEntityFactory.instance;
    Class<? extends ITemplateManager> templateManagerClass = templateManager.getClass();
    for (String name : templateManager.names()) {
        FilterRule filterRule = ef.createFilterRule();
        filterRule.setName(ef.createName(name));
        filterRule.setBody(templateManager.create(name).wGetAdapter(Statement));
        filterRulesMap.put(templateManagerClass.getSimpleName() + "." + name, filterRule);
        filterRulesMap.put(templateManagerClass.getName() + "." + name, filterRule);
    }
    return filterRulesMap;
}
Also used : HashMap(java.util.HashMap) TestsEntityFactory(org.whole.lang.tests.factories.TestsEntityFactory) FilterRule(org.whole.lang.tests.reflect.TestsEntityDescriptorEnum.FilterRule) FilterRule(org.whole.lang.tests.model.FilterRule)

Example 3 with FilterRule

use of org.whole.lang.tests.model.FilterRule 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 4 with FilterRule

use of org.whole.lang.tests.model.FilterRule in project whole by wholeplatform.

the class FilterRulePart method getModelSpecificChildren.

protected List<IEntity> getModelSpecificChildren() {
    FilterRule entity = getModelEntity();
    List<IEntity> list = new ArrayList<IEntity>(3);
    list.add(entity.getDescription());
    list.add(entity.getName());
    list.add(entity.getBody());
    return list;
}
Also used : IEntity(org.whole.lang.model.IEntity) ArrayList(java.util.ArrayList) FilterRule(org.whole.lang.tests.model.FilterRule)

Example 5 with FilterRule

use of org.whole.lang.tests.model.FilterRule in project whole by wholeplatform.

the class TestsInterpreterVisitor method getFilterRule.

@SuppressWarnings("unchecked")
protected FilterRule getFilterRule(UsingFilter usingFilter) {
    Filter filter = usingFilter.getFilter();
    Map<String, FilterRule> filterRulesMap = (Map<String, FilterRule>) getBindings().wGetValue("filterRulesMap");
    return filterRulesMap.get(filter.getName().getValue());
}
Also used : UsingFilter(org.whole.lang.tests.model.UsingFilter) Filter(org.whole.lang.tests.model.Filter) FilterRule(org.whole.lang.tests.model.FilterRule) Map(java.util.Map)

Aggregations

FilterRule (org.whole.lang.tests.model.FilterRule)8 IEntity (org.whole.lang.model.IEntity)4 ArrayList (java.util.ArrayList)3 FilterRule (org.whole.lang.tests.reflect.TestsEntityDescriptorEnum.FilterRule)3 HashMap (java.util.HashMap)2 TestsEntityFactory (org.whole.lang.tests.factories.TestsEntityFactory)2 FilterFamily (org.whole.lang.tests.model.FilterFamily)2 UsingFilter (org.whole.lang.tests.model.UsingFilter)2 List (java.util.List)1 Map (java.util.Map)1 ITransactionScope (org.whole.lang.bindings.ITransactionScope)1 QueriesEntityFactory (org.whole.lang.queries.factories.QueriesEntityFactory)1 Sequence (org.whole.lang.queries.model.Sequence)1 Filter (org.whole.lang.tests.model.Filter)1 FilterRules (org.whole.lang.tests.model.FilterRules)1 Name (org.whole.lang.tests.model.Name)1 PackageName (org.whole.lang.tests.model.PackageName)1 SubjectStatement (org.whole.lang.tests.model.SubjectStatement)1 TestSuite (org.whole.lang.tests.model.TestSuite)1 FreshNameGenerator (org.whole.lang.util.FreshNameGenerator)1