Search in sources :

Example 1 with Productions

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

the class PathExpressionsQueriesTest method testProduct4.

@Test
public void testProduct4() {
    ITemplateManager tm = PathExpressionsQueriesTemplateManager.instance();
    Grammar g = new TestXmlGrammar().create();
    Productions productions = g.getPhraseStructure();
    PathExpression pe1 = (PathExpression) tm.create("testProduct4");
    IBindingManager bm = BindingManagerFactory.instance.createArguments();
    int count = 0;
    for (IEntity tuple : BehaviorUtils.compileAndLazyEvaluate(pe1, g, bm)) {
        Production p = (Production) productions.wGet(count++);
        Assert.assertSame(p.getName(), bm.wGet("name"));
        Assert.assertSame(p.getRule(), bm.wGet("rule"));
        Assert.assertSame(p.getName(), tuple.wGet(0));
        Assert.assertSame(p.getRule(), tuple.wGet(1));
    }
    Assert.assertEquals(g.getPhraseStructure().wSize(), count);
}
Also used : Productions(org.whole.lang.grammars.model.Productions) 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) 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) QueriesGrammar(org.whole.lang.grammars.codebase.QueriesGrammar) Test(org.junit.Test)

Example 2 with Productions

use of org.whole.lang.grammars.model.Productions 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 3 with Productions

use of org.whole.lang.grammars.model.Productions 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)

Example 4 with Productions

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

the class IteratorFactoryTest method testChildIteratorRemove.

@Test
public void testChildIteratorRemove() {
    Grammar g = new TestXmlGrammar().create();
    Productions productions = EntityUtils.clone(g.getPhraseStructure());
    IEntityIterator<Production> ci = IteratorFactory.<Production>childIterator();
    ci.reset(productions);
    while (ci.hasNext()) {
        Production p = ci.next();
        assertSame(productions.wGet(0), p);
        ci.remove();
    }
    assertEquals(0, productions.wSize());
    productions = EntityUtils.clone(g.getPhraseStructure());
    ci = IteratorFactory.<Production>childScannerIterator();
    ci.reset(productions);
    while (ci.hasNext()) {
        Production p = ci.next();
        assertSame(productions.wGet(0), p);
        ci.remove();
    }
    assertEquals(0, productions.wSize());
    productions = EntityUtils.clone(g.getPhraseStructure());
    ci = IteratorFactory.<Production>childMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Production);
    ci.reset(productions);
    while (ci.hasNext()) {
        Production p = ci.next();
        assertSame(productions.wGet(0), 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)

Example 5 with Productions

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

the class IteratorFactoryTest method testFilterByIndexIterator.

@Test
public void testFilterByIndexIterator() {
    Grammar g = new TestXmlGrammar().create();
    Productions productions = g.getPhraseStructure();
    Production p = (Production) productions.wGet(1);
    IEntityIterator<?> i = IteratorFactory.filterByIndexIterator(IteratorFactory.followingSiblingIterator(), 0);
    i.reset(p);
    assertSingleton(i, productions.wGet(2));
    i = IteratorFactory.filterByIndexIterator(IteratorFactory.followingSiblingIterator(), 1);
    i.reset(p);
    assertSingleton(i, productions.wGet(3));
    i = IteratorFactory.filterByIndexIterator(IteratorFactory.ancestorOrSelfIterator(), 2);
    i.reset(p);
    assertSingleton(i, productions.wGetParent());
    i = IteratorFactory.filterByIndexIterator(IteratorFactory.descendantOrSelfIterator(), 1);
    i.reset(p);
    assertSingleton(i, p.getName());
}
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

Test (org.junit.Test)14 Grammar (org.whole.lang.grammars.model.Grammar)14 Productions (org.whole.lang.grammars.model.Productions)14 TestXmlGrammar (org.whole.lang.grammars.util.TestXmlGrammar)14 Production (org.whole.lang.grammars.model.Production)13 IEntity (org.whole.lang.model.IEntity)4 NonTerminal (org.whole.lang.grammars.model.NonTerminal)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 IBindingManager (org.whole.lang.bindings.IBindingManager)1 ClasspathPersistenceProvider (org.whole.lang.codebase.ClasspathPersistenceProvider)1 QueriesGrammar (org.whole.lang.grammars.codebase.QueriesGrammar)1 Type (org.whole.lang.models.model.Type)1 PathExpression (org.whole.lang.queries.model.PathExpression)1 ITemplateManager (org.whole.lang.templates.ITemplateManager)1 GenericIdentityVisitor (org.whole.lang.visitors.GenericIdentityVisitor)1 IVisitor (org.whole.lang.visitors.IVisitor)1