Search in sources :

Example 1 with FreshNameGenerator

use of org.whole.lang.util.FreshNameGenerator 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 2 with FreshNameGenerator

use of org.whole.lang.util.FreshNameGenerator in project whole by wholeplatform.

the class ModelsJavaModelGeneratorVisitor method visit.

public void visit(EnumEntity entity) {
    entityModifiers.clear();
    isConcrete = true;
    entity.getModifiers().accept(this);
    metaName = entity.getName().wStringValue();
    // was entity.getName().wStringValue();
    entityName = modelInfo.entityImplName(metaName);
    qEntityName = modelsGen.entityInterfaceQName(entityName);
    String enumTypeName = entityName + "Enum";
    String valueDataType = enumTypeName + ".Value";
    String valueQDataType = modelsGen.modelPackage() + "." + valueDataType;
    org.whole.gen.lang.model.EnumTypeBuilder enumBuilder = modelsGen.enumTypeBuilder(enumTypeName, valueQDataType);
    EnumValues values = entity.getValues();
    FreshNameGenerator enumValueGen = new FreshNameGenerator();
    for (int i = 0, size = values.wSize(); i < size; i++) {
        String value = values.wGet(i).wStringValue();
        if (StringUtils.isValidEnumLiteralName(value))
            enumValueGen.putBoundName(value);
    }
    for (int i = 0, size = values.wSize(); i < size; i++) {
        if (EntityUtils.isData(values.wGet(i))) {
            String value = values.wGet(i).wStringValue();
            enumBuilder.addValue(value, enumValueGen.isBoundOnlyName(value) ? value : enumValueGen.nextFreshName(StringUtils.toEnumLiteralName(value)));
        }
    }
    entityAdapterBuilder = modelsGen.entityAdapterBuilder(entityName);
    entityAdapterBuilder.addDataFeature(valueQDataType, "value");
    modelsGen.adapterRegistryBuilder().addFactoryProduct(entityName);
    modelsGen.implRegistryBuilder().addFactoryProduct(entityName);
    buildEnumEntity(entityName, metaName, valueQDataType, valueDataType);
    entity.getTypes().accept(this);
}
Also used : FreshNameGenerator(org.whole.lang.util.FreshNameGenerator) EnumValues(org.whole.lang.models.model.EnumValues)

Example 3 with FreshNameGenerator

use of org.whole.lang.util.FreshNameGenerator in project whole by wholeplatform.

the class PojoNormalizerVisitor method visit.

@Override
public void visit(Library entity) {
    IBindingManager bindings = BindingManagerFactory.instance.createBindingManager();
    FreshNameGenerator entityNameGenerator = new FreshNameGenerator();
    resolveTemplateNameCollisionsInDeclarations(entity, bindings, entityNameGenerator);
    createDefaultTemplateInDeclarations(entity, bindings, entityNameGenerator);
    createDefaultTemplateInProperties(entity, bindings);
    createDefaultTypeInParameters(entity, bindings);
    createDefaultTemplateInParameters(entity, bindings);
    addMappingDataTypes(entity, bindings, entityNameGenerator);
}
Also used : IBindingManager(org.whole.lang.bindings.IBindingManager) FreshNameGenerator(org.whole.lang.util.FreshNameGenerator)

Example 4 with FreshNameGenerator

use of org.whole.lang.util.FreshNameGenerator in project whole by wholeplatform.

the class Matcher method forceMatchUsingVariables.

public static boolean forceMatchUsingVariables(IEntity pattern, IEntity model) {
    try {
        GenericMatcher gm = new GenericMatcher();
        final Set<String> boundNames = new HashSet<String>();
        gm.withMatchStrategy(IMatchStrategy.CollectVariableNames(boundNames), CommonsEntityDescriptorEnum.Variable, CommonsEntityDescriptorEnum.InlineVariable).withMismatchStrategy(IMatchStrategy.IgnoreSubtree).match(model, pattern);
        final FreshNameGenerator fng = new FreshNameGenerator(boundNames);
        gm.withAsIsMatching().withMismatchStrategy(IMatchStrategy.ReplaceWithVariable(fng)).match(pattern, model);
        return true;
    } catch (MatchException | VisitException e) {
        return false;
    }
}
Also used : VisitException(org.whole.lang.visitors.VisitException) FreshNameGenerator(org.whole.lang.util.FreshNameGenerator) HashSet(java.util.HashSet)

Aggregations

FreshNameGenerator (org.whole.lang.util.FreshNameGenerator)4 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1 IBindingManager (org.whole.lang.bindings.IBindingManager)1 ITransactionScope (org.whole.lang.bindings.ITransactionScope)1 IEntity (org.whole.lang.model.IEntity)1 EnumValues (org.whole.lang.models.model.EnumValues)1 FilterFamily (org.whole.lang.tests.model.FilterFamily)1 FilterRule (org.whole.lang.tests.model.FilterRule)1 FilterRules (org.whole.lang.tests.model.FilterRules)1 SubjectStatement (org.whole.lang.tests.model.SubjectStatement)1 UsingFilter (org.whole.lang.tests.model.UsingFilter)1 VisitException (org.whole.lang.visitors.VisitException)1