Search in sources :

Example 1 with EnumEntity

use of org.whole.lang.models.model.EnumEntity in project whole by wholeplatform.

the class ControlQueriesTest method testChoose4.

@Test
public void testChoose4() {
    Model model = new ModelsModel().create();
    ModelDeclarations decls = model.getDeclarations();
    IBindingManager bm = BindingManagerFactory.instance.createBindingManager();
    ITemplateManager tm = ControlQueriesTemplateManager.instance();
    PathExpression query = (PathExpression) tm.create("choose4");
    int i = 0;
    IEntityIterator<IEntity> iterator = BehaviorUtils.compileAndLazyEvaluate(query, model, bm);
    while (iterator.hasNext()) {
        IEntity result = iterator.next();
        assertSame(result, bm.wGet("type"));
        assertTrue(result instanceof ClassDeclaration);
        ClassDeclaration classDecl = (ClassDeclaration) result;
        IEntity decl = decls.wGet(i++);
        String name = "", superClassName = "";
        switch(decl.wGetEntityOrd()) {
            case ModelsEntityDescriptorEnum.SimpleEntity_ord:
                SimpleEntity simpleEntity = (SimpleEntity) decl;
                name = simpleEntity.getName().wStringValue();
                superClassName = "AbstractSimpleEntity";
                break;
            case ModelsEntityDescriptorEnum.CompositeEntity_ord:
                CompositeEntity compositeEntity = (CompositeEntity) decl;
                name = compositeEntity.getName().wStringValue();
                superClassName = "AbstractCompositeEntity";
                break;
            case ModelsEntityDescriptorEnum.DataEntity_ord:
                DataEntity dataEntity = (DataEntity) decl;
                name = dataEntity.getName().wStringValue();
                superClassName = "AbstractDataEntity";
                break;
            case ModelsEntityDescriptorEnum.EnumEntity_ord:
                EnumEntity enumEntity = (EnumEntity) decl;
                name = enumEntity.getName().wStringValue();
                superClassName = "AbstractEnumEntity";
                break;
        }
        assertEquals(name, classDecl.getName().wStringValue());
        assertEquals(superClassName, classDecl.getSuperclassType().wStringValue());
    }
}
Also used : EnumEntity(org.whole.lang.models.model.EnumEntity) IEntity(org.whole.lang.model.IEntity) SimpleEntity(org.whole.lang.models.model.SimpleEntity) ITemplateManager(org.whole.lang.templates.ITemplateManager) ClassDeclaration(org.whole.lang.java.model.ClassDeclaration) ModelDeclarations(org.whole.lang.models.model.ModelDeclarations) PathExpression(org.whole.lang.queries.model.PathExpression) XmlModel(org.whole.lang.models.codebase.XmlModel) ModelsModel(org.whole.lang.models.codebase.ModelsModel) Model(org.whole.lang.models.model.Model) IBindingManager(org.whole.lang.bindings.IBindingManager) CompositeEntity(org.whole.lang.models.model.CompositeEntity) DataEntity(org.whole.lang.models.model.DataEntity) ModelsModel(org.whole.lang.models.codebase.ModelsModel) Test(org.junit.Test)

Example 2 with EnumEntity

use of org.whole.lang.models.model.EnumEntity in project whole by wholeplatform.

the class ControlQueriesTest method testChoose3.

@Test
public void testChoose3() {
    Model model = new ModelsModel().create();
    ModelDeclarations decls = model.getDeclarations();
    ITemplateManager tm = ControlQueriesTemplateManager.instance();
    PathExpression query = (PathExpression) tm.create("choose3");
    int i = 0;
    IEntityIterator<IEntity> iterator = BehaviorUtils.compileAndLazyEvaluate(query, model);
    while (iterator.hasNext()) {
        IEntity result = iterator.next();
        IEntity decl = decls.wGet(i++);
        switch(decl.wGetEntityOrd()) {
            case ModelsEntityDescriptorEnum.SimpleEntity_ord:
                Features features = ((SimpleEntity) decl).getFeatures();
                if (!features.isEmpty())
                    assertEquals(features.get(0).getName().wStringValue(), result.wStringValue());
                break;
            case ModelsEntityDescriptorEnum.CompositeEntity_ord:
                CompositeEntity compositeEntity = (CompositeEntity) decl;
                assertEquals(compositeEntity.getComponentType().wStringValue(), result.wStringValue());
                break;
            case ModelsEntityDescriptorEnum.DataEntity_ord:
                DataEntity dataEntity = (DataEntity) decl;
                assertEquals(dataEntity.getDataType().wStringValue(), result.wStringValue());
                break;
            case ModelsEntityDescriptorEnum.EnumEntity_ord:
                EnumEntity enumEntity = (EnumEntity) decl;
                assertEquals(enumEntity.getValues().wGet(0).wStringValue(), result.wStringValue());
                break;
        }
    }
}
Also used : EnumEntity(org.whole.lang.models.model.EnumEntity) IEntity(org.whole.lang.model.IEntity) SimpleEntity(org.whole.lang.models.model.SimpleEntity) ITemplateManager(org.whole.lang.templates.ITemplateManager) ModelDeclarations(org.whole.lang.models.model.ModelDeclarations) PathExpression(org.whole.lang.queries.model.PathExpression) XmlModel(org.whole.lang.models.codebase.XmlModel) ModelsModel(org.whole.lang.models.codebase.ModelsModel) Model(org.whole.lang.models.model.Model) CompositeEntity(org.whole.lang.models.model.CompositeEntity) Features(org.whole.lang.models.model.Features) DataEntity(org.whole.lang.models.model.DataEntity) ModelsModel(org.whole.lang.models.codebase.ModelsModel) Test(org.junit.Test)

Example 3 with EnumEntity

use of org.whole.lang.models.model.EnumEntity in project whole by wholeplatform.

the class ControlQueriesTest method testChoose2.

@Test
public void testChoose2() {
    Model model = new ModelsModel().create();
    ModelDeclarations decls = model.getDeclarations();
    ITemplateManager tm = ControlQueriesTemplateManager.instance();
    PathExpression query = (PathExpression) tm.create("choose2");
    int i = 0;
    IEntityIterator<IEntity> iterator = BehaviorUtils.compileAndLazyEvaluate(query, model);
    while (iterator.hasNext()) {
        IEntity result;
        IEntity decl = decls.wGet(i++);
        switch(decl.wGetEntityOrd()) {
            case ModelsEntityDescriptorEnum.SimpleEntity_ord:
                IEntity features = ((SimpleEntity) decl).getFeatures();
                for (int j = 0; j < features.wSize(); j++) {
                    result = iterator.next();
                    assertEquals(((Feature) features.wGet(j)).getName().wStringValue(), result.wStringValue());
                }
                break;
            case ModelsEntityDescriptorEnum.CompositeEntity_ord:
                result = iterator.next();
                CompositeEntity compositeEntity = (CompositeEntity) decl;
                assertEquals(compositeEntity.getComponentType().wStringValue(), result.wStringValue());
                break;
            case ModelsEntityDescriptorEnum.DataEntity_ord:
                result = iterator.next();
                DataEntity dataEntity = (DataEntity) decl;
                assertEquals(dataEntity.getDataType().wStringValue(), result.wStringValue());
                break;
            case ModelsEntityDescriptorEnum.EnumEntity_ord:
                EnumValues enumValues = ((EnumEntity) decl).getValues();
                if (enumValues.wIsEmpty())
                    iterator.next();
                else
                    for (int j = 0; j < enumValues.wSize(); j++) {
                        result = iterator.next();
                        assertEquals(enumValues.wGet(j).wStringValue(), result.wStringValue());
                    }
                break;
        }
    }
}
Also used : EnumEntity(org.whole.lang.models.model.EnumEntity) IEntity(org.whole.lang.model.IEntity) SimpleEntity(org.whole.lang.models.model.SimpleEntity) ITemplateManager(org.whole.lang.templates.ITemplateManager) Feature(org.whole.lang.models.model.Feature) EnumValues(org.whole.lang.models.model.EnumValues) ModelDeclarations(org.whole.lang.models.model.ModelDeclarations) PathExpression(org.whole.lang.queries.model.PathExpression) XmlModel(org.whole.lang.models.codebase.XmlModel) ModelsModel(org.whole.lang.models.codebase.ModelsModel) Model(org.whole.lang.models.model.Model) CompositeEntity(org.whole.lang.models.model.CompositeEntity) DataEntity(org.whole.lang.models.model.DataEntity) ModelsModel(org.whole.lang.models.codebase.ModelsModel) Test(org.junit.Test)

Example 4 with EnumEntity

use of org.whole.lang.models.model.EnumEntity in project whole by wholeplatform.

the class Grammars2ModelsVisitor method visit.

@Override
public void visit(Production entity) {
    ModelsEntityFactory mf = ModelsEntityFactory.instance;
    String eName = getMappedEntityName(entity);
    Rule rule = entity.getRule();
    AbstractPatternFilterIterator<Rule> ruleIterator = IteratorFactory.<Rule>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Rule);
    ruleIterator.reset(rule);
    while (ruleIterator.hasNext()) {
        rule = ruleIterator.next();
        switch(rule.wGetEntityOrd()) {
            // map productions with a single lexical non-terminal as data entities
            case GrammarsEntityDescriptorEnum.NonTerminal_ord:
                if (!isLexicalNonTerminal((NonTerminal) rule) || !EntityUtils.hasParent(rule) || !Matcher.match(GrammarsEntityDescriptorEnum.Production, rule.wGetParent()) || !Matcher.match(GrammarsEntityDescriptorEnum.DataTerminal, lexiconMap.get(((NonTerminal) rule).getValue()).getRule()))
                    break;
            case GrammarsEntityDescriptorEnum.DataTerminal_ord:
                DataEntity de = getModelDeclaration(eName, ModelsEntityDescriptorEnum.DataEntity);
                Template template = entity.getTemplate();
                if (Matcher.matchImpl(GrammarsEntityDescriptorEnum.DataType, template))
                    de.getDataType().wSetValue(GrammarsUtils.getDataType((DataType) template));
                else if (Matcher.matchImpl(GrammarsEntityDescriptorEnum.CustomDataType, template))
                    de.getDataType().wSetValue(template.wStringValue());
                return;
            case GrammarsEntityDescriptorEnum.Choose_ord:
                boolean isPolymorphic = false;
                AbstractPatternFilterIterator<NonTerminal> ruleIterator2 = IteratorFactory.<NonTerminal>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.NonTerminal);
                ruleIterator2.reset(rule);
                for (NonTerminal nt2 : ruleIterator2) if (!isLexicalNonTerminal(nt2)) {
                    ensureType(getModelDeclaration(getMappedEntityName(nt2)).getTypes(), eName);
                    isPolymorphic = true;
                }
                if (isPolymorphic) {
                    SimpleEntity se = getModelDeclaration(eName, ModelsEntityDescriptorEnum.SimpleEntity);
                    se.getModifiers().wAdd(mf.createEntityModifier(EntityModifierEnum._abstract));
                    return;
                } else if (!rule.wIsEmpty() && Matcher.match(GrammarsEntityDescriptorEnum.As, rule.wGet(0))) {
                    EnumEntity ee = getModelDeclaration(eName, ModelsEntityDescriptorEnum.EnumEntity);
                    EnumValues enumValues = ModelsEntityFactory.instance.createEnumValues(0);
                    AbstractPatternFilterIterator<As> ruleIterator3 = IteratorFactory.<As>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.As);
                    ruleIterator3.reset(rule);
                    for (As as : ruleIterator3) enumValues.wAdd(ModelsEntityFactory.instance.createEnumValue(as.getName().getValue()));
                    ee.setValues(enumValues);
                    return;
                }
                // }
                break;
            case GrammarsEntityDescriptorEnum.Repeat_ord:
                if (ruleIterator.skipTo(GrammarsEntityDescriptorEnum.NonTerminal)) {
                    // FIXME wrongly assumes that the separator, if present, is a lexical token
                    if (!EntityUtils.isResolver(((Repeat) rule).getSeparator())) {
                        // if (Matcher.matchImpl(GrammarsEntityDescriptorEnum.NonTerminal, ((Repeat) rule).getSeparator())) {
                        ruleIterator.next();
                        ruleIterator.skipTo(GrammarsEntityDescriptorEnum.NonTerminal);
                    }
                    NonTerminal nt = null;
                    AbstractPatternFilterIterator<NonTerminal> ruleIterator4 = IteratorFactory.<NonTerminal>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.NonTerminal);
                    ruleIterator4.reset(rule);
                    for (NonTerminal nt2 : ruleIterator4) if (!isLexicalNonTerminal(nt2)) {
                        nt = nt2;
                        break;
                    }
                    // by now we assume that the separator, if present, is a lexical token
                    if (nt == null)
                        break;
                    // the above statements replace this code, allowing lexical Repeat rule
                    // NonTerminal nt = ruleIterator.next(GrammarsEntityDescriptorEnum.NonTerminal);
                    // while (isLexicalNonTerminal(nt)) {
                    // nt = ruleIterator.next(GrammarsEntityDescriptorEnum.NonTerminal);
                    // }
                    CompositeEntity ce = getModelDeclaration(eName, ModelsEntityDescriptorEnum.CompositeEntity);
                    ce.setComponentType(mf.createSimpleName(getMappedEntityName(nt)));
                    ce.getComponentModifiers().wAdd(mf.createComponentModifier(ComponentModifierEnum.ordered));
                }
                return;
            case GrammarsEntityDescriptorEnum.As_ord:
                As asRule = (As) rule;
                // normalization invariants assure that this branch is unreachable
                // if (Matcher.match(GrammarsEntityDescriptorEnum.LiteralTerminal, asRule.getRule())) {
                // EnumEntity ee = getModelDeclaration(eName, ModelsEntityDescriptorEnum.EnumEntity);
                // 
                // ee.getValues().wAdd(mf.createEnumValue(getMappedName(asRule)));
                // 
                // while (ruleIterator.skipTo(GrammarsEntityDescriptorEnum.As)) {
                // rule = ruleIterator.next(GrammarsEntityDescriptorEnum.As);
                // asRule = (As) rule;
                // 
                // ee.getValues().wAdd(mf.createEnumValue(getMappedName(asRule)));
                // }
                // } else {
                SimpleEntity se = getModelDeclaration(eName, ModelsEntityDescriptorEnum.SimpleEntity);
                Features features = se.getFeatures();
                ensureFeature(features, ruleIterator, asRule);
                while (ruleIterator.hasNext()) {
                    rule = ruleIterator.next();
                    switch(rule.wGetEntityOrd()) {
                        case GrammarsEntityDescriptorEnum.As_ord:
                            asRule = (As) rule;
                            ensureFeature(features, ruleIterator, asRule);
                            break;
                    }
                }
                // }
                return;
            case GrammarsEntityDescriptorEnum.Concatenate_ord:
                break;
        }
    }
    getModelDeclaration(eName, ModelsEntityDescriptorEnum.SimpleEntity);
}
Also used : EnumEntity(org.whole.lang.models.model.EnumEntity) SimpleEntity(org.whole.lang.models.model.SimpleEntity) ModelsEntityFactory(org.whole.lang.models.factories.ModelsEntityFactory) NonTerminal(org.whole.lang.grammars.model.NonTerminal) Repeat(org.whole.lang.grammars.model.Repeat) EnumValues(org.whole.lang.models.model.EnumValues) Template(org.whole.lang.grammars.model.Template) As(org.whole.lang.grammars.model.As) AbstractPatternFilterIterator(org.whole.lang.iterators.AbstractPatternFilterIterator) CompositeEntity(org.whole.lang.models.model.CompositeEntity) DataEntity(org.whole.lang.models.model.DataEntity) Features(org.whole.lang.models.model.Features) Rule(org.whole.lang.grammars.model.Rule)

Example 5 with EnumEntity

use of org.whole.lang.models.model.EnumEntity in project whole by wholeplatform.

the class ControlQueriesTest method testChoose1.

@Test
public void testChoose1() {
    Model model = new ModelsModel().create();
    ModelDeclarations decls = model.getDeclarations();
    ITemplateManager tm = ControlQueriesTemplateManager.instance();
    PathExpression query = (PathExpression) tm.create("choose1");
    assertFalse(BehaviorUtils.compileAndLazyEvaluate(query, model).hasNext());
    int i = 0;
    while (!Matcher.match(ModelsEntityDescriptorEnum.SimpleEntity, decls.wGet(i))) i++;
    SimpleEntity simpleEntity = (SimpleEntity) decls.wGet(i);
    int j = 0;
    for (IEntity name : BehaviorUtils.compileAndLazyEvaluate(query, simpleEntity)) {
        assertEquals(((Feature) simpleEntity.getFeatures().wGet(j++)).getName().wStringValue(), name.wStringValue());
    }
    assertEquals(simpleEntity.getFeatures().wSize(), j);
    while (!Matcher.match(ModelsEntityDescriptorEnum.CompositeEntity, decls.wGet(i))) i++;
    CompositeEntity compositeEntity = (CompositeEntity) decls.wGet(i);
    IEntityIterator<IEntity> iterator = BehaviorUtils.compileAndLazyEvaluate(query, compositeEntity);
    assertTrue(iterator.hasNext());
    assertEquals(compositeEntity.getComponentType().wStringValue(), iterator.next().wStringValue());
    assertFalse(iterator.hasNext());
    while (!Matcher.match(ModelsEntityDescriptorEnum.EnumEntity, decls.wGet(i))) i++;
    EnumEntity enumEntity = (EnumEntity) decls.wGet(i);
    iterator = BehaviorUtils.compileAndLazyEvaluate(query, enumEntity);
    assertTrue(iterator.hasNext());
    assertTrue(iterator.next() instanceof EnumValues);
    assertFalse(iterator.hasNext());
}
Also used : EnumEntity(org.whole.lang.models.model.EnumEntity) IEntity(org.whole.lang.model.IEntity) SimpleEntity(org.whole.lang.models.model.SimpleEntity) ITemplateManager(org.whole.lang.templates.ITemplateManager) Feature(org.whole.lang.models.model.Feature) EnumValues(org.whole.lang.models.model.EnumValues) ModelDeclarations(org.whole.lang.models.model.ModelDeclarations) PathExpression(org.whole.lang.queries.model.PathExpression) XmlModel(org.whole.lang.models.codebase.XmlModel) ModelsModel(org.whole.lang.models.codebase.ModelsModel) Model(org.whole.lang.models.model.Model) CompositeEntity(org.whole.lang.models.model.CompositeEntity) ModelsModel(org.whole.lang.models.codebase.ModelsModel) Test(org.junit.Test)

Aggregations

EnumEntity (org.whole.lang.models.model.EnumEntity)6 IEntity (org.whole.lang.model.IEntity)5 CompositeEntity (org.whole.lang.models.model.CompositeEntity)5 SimpleEntity (org.whole.lang.models.model.SimpleEntity)5 Test (org.junit.Test)4 ModelsModel (org.whole.lang.models.codebase.ModelsModel)4 XmlModel (org.whole.lang.models.codebase.XmlModel)4 DataEntity (org.whole.lang.models.model.DataEntity)4 Model (org.whole.lang.models.model.Model)4 ModelDeclarations (org.whole.lang.models.model.ModelDeclarations)4 PathExpression (org.whole.lang.queries.model.PathExpression)4 ITemplateManager (org.whole.lang.templates.ITemplateManager)4 EnumValues (org.whole.lang.models.model.EnumValues)3 Feature (org.whole.lang.models.model.Feature)2 Features (org.whole.lang.models.model.Features)2 ArrayList (java.util.ArrayList)1 IBindingManager (org.whole.lang.bindings.IBindingManager)1 As (org.whole.lang.grammars.model.As)1 NonTerminal (org.whole.lang.grammars.model.NonTerminal)1 Repeat (org.whole.lang.grammars.model.Repeat)1