Search in sources :

Example 31 with Grammar

use of org.whole.lang.grammars.model.Grammar in project whole by wholeplatform.

the class SelectQueriesTest method testSelectTemplate1.

@Test
public void testSelectTemplate1() {
    ITemplateManager tm = SelectQueriesTemplateManager.instance();
    Grammar model = new TestXmlGrammar().create();
    IBindingManager bm = BindingManagerFactory.instance.createArguments();
    PathExpression query = (PathExpression) tm.create("selectTemplate1");
    for (IEntity tuple : BehaviorUtils.compileAndLazyEvaluate(query, model, bm)) {
        Feature f = (Feature) tuple.wGet(0);
        As e = (As) tuple.wGet(1);
        assertEquals(e.getName().getValue(), f.getName().wStringValue());
        assertEquals(e.getRule().wStringValue(), f.getType().wStringValue());
    }
}
Also used : As(org.whole.lang.grammars.model.As) PathExpression(org.whole.lang.queries.model.PathExpression) IEntity(org.whole.lang.model.IEntity) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) IBindingManager(org.whole.lang.bindings.IBindingManager) ITemplateManager(org.whole.lang.templates.ITemplateManager) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) Feature(org.whole.lang.models.model.Feature) Test(org.junit.Test)

Example 32 with Grammar

use of org.whole.lang.grammars.model.Grammar in project whole by wholeplatform.

the class SelectQueriesTest method testSelectTuple1.

@Test
public void testSelectTuple1() {
    ITemplateManager tm = SelectQueriesTemplateManager.instance();
    Grammar g = new TestXmlGrammar().create();
    PathExpression pe1 = (PathExpression) tm.create("selectTuple1");
    int count = 0;
    for (IEntity t : BehaviorUtils.compileAndLazyEvaluate(pe1, g)) {
        NonTerminal name = (NonTerminal) t.wGet(0);
        Rule rule = (Rule) t.wGet(1);
        Production prod = (Production) t.wGet(2);
        assertEquals(name, prod.getName());
        assertSame(rule, prod.getRule());
        assertEquals(g.getName(), t.wGet(3));
        count++;
    }
    assertEquals(g.getPhraseStructure().wSize(), count);
}
Also used : PathExpression(org.whole.lang.queries.model.PathExpression) IEntity(org.whole.lang.model.IEntity) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Production(org.whole.lang.grammars.model.Production) ITemplateManager(org.whole.lang.templates.ITemplateManager) NonTerminal(org.whole.lang.grammars.model.NonTerminal) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) Rule(org.whole.lang.grammars.model.Rule) Test(org.junit.Test)

Example 33 with Grammar

use of org.whole.lang.grammars.model.Grammar in project whole by wholeplatform.

the class IteratorFactoryTest method testPrune.

@Test
public void testPrune() {
    Grammar g = new TestXmlGrammar().create();
    IEntityIterator<IEntity> i1 = IteratorFactory.<IEntity>descendantOrSelfIterator();
    i1.reset(g);
    IEntityIterator<IEntity> i2 = IteratorFactory.<IEntity>descendantOrSelfIterator();
    i2.reset(g);
    for (IEntity e : i1) {
        assertSame(e, i2.next());
        IEntityIterator<IEntity> i12 = i1.clone();
        i12.prune();
    }
    assertSame(i1.hasNext(), i2.hasNext());
    i1 = IteratorFactory.<IEntity>descendantOrSelfIterator();
    i1.reset(g);
    for (IEntity e : i1) {
        IEntityIterator<IEntity> i3 = IteratorFactory.<IEntity>followingSiblingIterator();
        i3.reset(e);
        if (i3.hasNext()) {
            IEntityIterator<IEntity> i12 = i1.clone();
            i12.prune();
            IEntity e1 = i12.next();
            IEntity e2 = i3.next();
            assertSame(e1, e2);
        }
    }
}
Also used : IEntity(org.whole.lang.model.IEntity) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) Test(org.junit.Test)

Example 34 with Grammar

use of org.whole.lang.grammars.model.Grammar in project whole by wholeplatform.

the class IteratorFactoryTest method testTopDownIterator.

@Test
public void testTopDownIterator() throws Exception {
    Grammar g = new TestXmlGrammar().create();
    final Productions productions = g.getPhraseStructure();
    final IEntityIterator<IEntity> ci = IteratorFactory.<IEntity>descendantOrSelfIterator();
    ci.reset(productions);
    IVisitor v = GenericTraversalFactory.instance.topDown(new GenericIdentityVisitor() {

        public void visit(IEntity entity) {
            if (ci.hasNext())
                assertSame(entity, ci.next());
            else
                fail();
        }
    }, false);
    v.visit(productions);
    final IEntityIterator<IEntity> ci2 = IteratorFactory.<IEntity>descendantOrSelfScannerIterator();
    ci2.reset(productions);
    v = GenericTraversalFactory.instance.topDown(new GenericIdentityVisitor() {

        public void visit(IEntity entity) {
            if (EntityUtils.isResolver(entity))
                return;
            if (ci2.hasNext())
                assertSame(entity, ci2.next());
            else
                fail();
        }
    }, false);
    v.visit(productions);
    final IEntityIterator<IEntity> ci3 = IteratorFactory.<IEntity>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Production);
    ci3.reset(productions);
    v = GenericTraversalFactory.instance.topDown(new GenericIdentityVisitor() {

        public void visit(IEntity entity) {
            if (!Matcher.match(GrammarsEntityDescriptorEnum.Production, entity))
                return;
            if (ci3.hasNext())
                assertSame(entity, ci3.next());
            else
                fail();
        }
    }, false);
    v.visit(productions);
    IEntity artifactsModel = XmlBuilderPersistenceKit.instance().readModel(new ClasspathPersistenceProvider("org/whole/lang/artifacts/ArtifactsModel.xwl"));
    Set<Type> typeSet = new HashSet<Type>();
    IEntityIterator<Type> ci4 = IteratorFactory.<Type>descendantOrSelfMatcherIterator().withPattern(ModelsEntityFactory.instance.createSimpleName("Atifacts"));
    ci4.reset(artifactsModel);
    while (ci4.hasNext()) assertTrue(typeSet.add(ci4.next()));
}
Also used : Productions(org.whole.lang.grammars.model.Productions) Type(org.whole.lang.models.model.Type) IEntity(org.whole.lang.model.IEntity) IVisitor(org.whole.lang.visitors.IVisitor) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) ClasspathPersistenceProvider(org.whole.lang.codebase.ClasspathPersistenceProvider) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) GenericIdentityVisitor(org.whole.lang.visitors.GenericIdentityVisitor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 35 with Grammar

use of org.whole.lang.grammars.model.Grammar in project whole by wholeplatform.

the class IteratorFactoryTest method testChildReverseIteratorRemove.

@Test
public void testChildReverseIteratorRemove() {
    Grammar g = new TestXmlGrammar().create();
    Productions productions = EntityUtils.clone(g.getPhraseStructure());
    IEntityIterator<Production> ci = IteratorFactory.<Production>childReverseIterator();
    ci.reset(productions);
    while (ci.hasNext()) {
        Production p = ci.next();
        assertSame(productions.wGet(productions.wSize() - 1), p);
        ci.remove();
    }
    assertEquals(0, productions.wSize());
    productions = EntityUtils.clone(g.getPhraseStructure());
    ci = IteratorFactory.<Production>childReverseScannerIterator();
    ci.reset(productions);
    while (ci.hasNext()) {
        Production p = ci.next();
        assertSame(productions.wGet(productions.wSize() - 1), p);
        ci.remove();
    }
    assertEquals(0, productions.wSize());
    productions = EntityUtils.clone(g.getPhraseStructure());
    ci = IteratorFactory.<Production>childReverseMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Production);
    ci.reset(productions);
    while (ci.hasNext()) {
        Production p = ci.next();
        assertSame(productions.wGet(productions.wSize() - 1), p);
        ci.remove();
    }
    assertEquals(0, productions.wSize());
}
Also used : Productions(org.whole.lang.grammars.model.Productions) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Production(org.whole.lang.grammars.model.Production) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) Test(org.junit.Test)

Aggregations

Grammar (org.whole.lang.grammars.model.Grammar)96 Test (org.junit.Test)82 TestXmlGrammar (org.whole.lang.grammars.util.TestXmlGrammar)80 IEntity (org.whole.lang.model.IEntity)50 ITemplateManager (org.whole.lang.templates.ITemplateManager)46 Production (org.whole.lang.grammars.model.Production)43 PathExpression (org.whole.lang.queries.model.PathExpression)40 IBindingManager (org.whole.lang.bindings.IBindingManager)28 QueriesGrammar (org.whole.lang.grammars.codebase.QueriesGrammar)25 NonTerminal (org.whole.lang.grammars.model.NonTerminal)14 Productions (org.whole.lang.grammars.model.Productions)14 HashSet (java.util.HashSet)8 PrettyPrinterOperation.toPrettyPrintString (org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString)7 Rule (org.whole.lang.grammars.model.Rule)6 XmlGrammar (org.whole.lang.grammars.codebase.XmlGrammar)3 As (org.whole.lang.grammars.model.As)3 Feature (org.whole.lang.models.model.Feature)3 ILanguageKit (org.whole.lang.reflect.ILanguageKit)3 ArrayList (java.util.ArrayList)2 IGrammarProvider (org.whole.lang.grammars.codebase.IGrammarProvider)2