Search in sources :

Example 21 with Production

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

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

Example 23 with Production

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

the class IteratorFactoryTest method testTopDownMatcherIteratorRemove.

@Test
public void testTopDownMatcherIteratorRemove() {
    Grammar g = new TestXmlGrammar().create();
    g.wRemove(GrammarsFeatureDescriptorEnum.lexicalStructure);
    Productions productions = g.getPhraseStructure();
    IEntityIterator<Production> ci = IteratorFactory.<Production>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Production);
    ci.reset(g);
    while (ci.hasNext()) {
        Production p = ci.next();
        assertSame(productions.wGet(0), p);
        ci.remove();
    }
    assertEquals(0, productions.wSize());
    g = new TestXmlGrammar().create();
    IEntityIterator<NonTerminal> ci2 = IteratorFactory.<NonTerminal>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.NonTerminal);
    ci2.reset(g);
    while (ci2.hasNext()) {
        NonTerminal nt = ci2.next();
        ci2.remove();
    }
    assertNull(Matcher.find(GenericTraversalFactory.instance.sequence(GenericMatcherFactory.instance.hasTypeMatcher(GrammarsEntityDescriptorEnum.NonTerminal), GenericMatcherFactory.instance.isImplMatcher()), g, false));
}
Also used : Productions(org.whole.lang.grammars.model.Productions) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Production(org.whole.lang.grammars.model.Production) NonTerminal(org.whole.lang.grammars.model.NonTerminal) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) Test(org.junit.Test)

Example 24 with Production

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

the class IteratorFactoryTest method testChildIteratorAdd.

@Test
public void testChildIteratorAdd() {
    Grammar g = new TestXmlGrammar().create();
    Productions productions = g.getPhraseStructure();
    int size = productions.wSize();
    int count = 0;
    Production pAdded1 = null;
    Production pAdded2 = null;
    IEntityIterator<Production> i = IteratorFactory.<Production>childIterator();
    i.reset(productions);
    while (i.hasNext()) {
        Production p = i.next();
        assertNotSame(pAdded1, p);
        assertNotSame(pAdded2, p);
        count++;
        if (count == 1)
            i.add(pAdded1 = GrammarsEntityFactory.instance.createProduction());
        if (count == 3) {
            i.add(pAdded1 = GrammarsEntityFactory.instance.createProduction());
            i.add(pAdded2 = GrammarsEntityFactory.instance.createProduction());
        }
    }
    assertEquals(size, count);
    assertEquals(size + 3, 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 25 with Production

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

the class IteratorFactoryTest method testComposeIterator.

@Test
public void testComposeIterator() {
    Grammar g = new TestXmlGrammar().create();
    // query: g/phraseStructure/child()[ED = Production]/rule//[ED = Production]
    IEntityIterator<Production> pi = IteratorFactory.composeIterator(IteratorFactory.<Production>descendantOrSelfMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Production), IteratorFactory.featureByNameIterator("rule"), IteratorFactory.<Production>childMatcherIterator().withPattern(GrammarsEntityDescriptorEnum.Production), IteratorFactory.featureByNameIterator("phraseStructure"));
    pi.reset(g);
    Set<String> l = new HashSet<String>();
    for (Production p : pi) l.add(p.getName().getValue());
    assertEquals(16, l.size());
    NonTerminal prologNt = ((Production) ((Production) g.getPhraseStructure().wGet(0)).getRule().wGet(0)).getName();
    // query: g/phraseStructure/0/rule/0/name
    IEntityIterator<? extends IEntity> nti = IteratorFactory.composeIterator(IteratorFactory.featureByNameIterator("name"), IteratorFactory.featureByIndexIterator(0), IteratorFactory.featureByNameIterator("rule"), IteratorFactory.featureByIndexIterator(0), IteratorFactory.featureByNameIterator("phraseStructure"));
    nti.reset(g);
    assertTrue(nti.hasNext());
    assertSame(prologNt, nti.next());
    nti.reset(g.getPhraseStructure());
    assertFalse(nti.hasNext());
    nti.reset(g);
    assertTrue(nti.hasNext());
    assertSame(prologNt, nti.next());
}
Also used : TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Production(org.whole.lang.grammars.model.Production) NonTerminal(org.whole.lang.grammars.model.NonTerminal) TestXmlGrammar(org.whole.lang.grammars.util.TestXmlGrammar) Grammar(org.whole.lang.grammars.model.Grammar) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

Production (org.whole.lang.grammars.model.Production)55 Grammar (org.whole.lang.grammars.model.Grammar)43 Test (org.junit.Test)41 TestXmlGrammar (org.whole.lang.grammars.util.TestXmlGrammar)41 ITemplateManager (org.whole.lang.templates.ITemplateManager)26 PathExpression (org.whole.lang.queries.model.PathExpression)20 QueriesGrammar (org.whole.lang.grammars.codebase.QueriesGrammar)18 IEntity (org.whole.lang.model.IEntity)18 IBindingManager (org.whole.lang.bindings.IBindingManager)14 Productions (org.whole.lang.grammars.model.Productions)13 NonTerminal (org.whole.lang.grammars.model.NonTerminal)8 HashSet (java.util.HashSet)5 ArrayList (java.util.ArrayList)3 Rule (org.whole.lang.grammars.model.Rule)3 PrettyPrinterOperation.toPrettyPrintString (org.whole.lang.operations.PrettyPrinterOperation.toPrettyPrintString)3 ModelsEntityFactory (org.whole.lang.models.factories.ModelsEntityFactory)2 ILanguageKit (org.whole.lang.reflect.ILanguageKit)2 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 Category (org.junit.experimental.categories.Category)1