Search in sources :

Example 46 with Grammar

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

Example 47 with Grammar

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

the class IteratorFactoryTest method testDescendantAndDescendantOrSelfIterators.

@Test
public void testDescendantAndDescendantOrSelfIterators() {
    Grammar g = new TestXmlGrammar().create();
    IEntityIterator<IEntity> dsi = IteratorFactory.<IEntity>descendantOrSelfIterator();
    dsi.reset(g);
    IEntityIterator<IEntity> di = IteratorFactory.<IEntity>descendantIterator();
    di.reset(g);
    assertSame(g, dsi.next());
    assertSame(dsi.hasNext(), di.hasNext());
    boolean hasNext;
    do {
        assertSame(dsi.lookahead(), di.lookahead());
        assertSame(dsi.next(), di.next());
        assertSame(hasNext = dsi.hasNext(), di.hasNext());
    } while (hasNext);
}
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 48 with Grammar

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

the class IteratorFactoryTest method testChildByNameAndByIndexIterators.

@Test
public void testChildByNameAndByIndexIterators() {
    Grammar g = new TestXmlGrammar().create();
    IEntityIterator<IEntity> i1 = IteratorFactory.featureByNameIterator("phraseStructure");
    i1.reset(g);
    assertTrue(i1.hasNext());
    assertSame(g.getPhraseStructure(), i1.next());
    assertFalse(i1.hasNext());
    assertNull(i1.lookahead());
    IEntityIterator<IEntity> i2 = IteratorFactory.featureByIndexIterator(0);
    i2.reset(g);
    assertTrue(i2.hasNext());
    assertSame(g.wGet(0), i2.next());
    assertFalse(i2.hasNext());
    assertNull(i2.lookahead());
}
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 49 with Grammar

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

the class IteratorFactoryTest method testSelfIteratorMarkReset.

@Test
public void testSelfIteratorMarkReset() {
    Grammar g = new TestXmlGrammar().create();
    IEntityIterator<Grammar> si = IteratorFactory.<Grammar>selfIterator();
    si.reset(g);
    IEntityIterator<Grammar> si2 = si.clone();
    assertSame(g, si.next());
    assertFalse(si.hasNext());
    assertTrue(si2.hasNext());
    assertSame(g, si2.next());
}
Also used : 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 50 with Grammar

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

the class IteratorFactoryTest method testTopDownIteratorAdd.

@Test
public void testTopDownIteratorAdd() {
    Grammar g = new TestXmlGrammar().create();
    Production production = (Production) g.getPhraseStructure().wGet(3);
    Choose choose = (Choose) production.getRule();
    int size = choose.wSize();
    int count = 0;
    Rule rAdded1 = null;
    Rule rAdded2 = null;
    IEntityIterator<IEntity> i = IteratorFactory.<IEntity>descendantOrSelfIterator();
    i.reset(production);
    i.next();
    i.next();
    while (i.hasNext()) {
        IEntity r = i.next();
        assertNotSame(rAdded1, r);
        assertNotSame(rAdded2, r);
        if (count == 0)
            assertSame(choose, r);
        else if (count == 1)
            i.add(rAdded1 = GrammarsEntityFactory.instance.createProduction());
        else if (count == 10) {
            i.add(rAdded1 = GrammarsEntityFactory.instance.createProduction());
            i.add(rAdded2 = GrammarsEntityFactory.instance.createProduction());
        }
        count++;
    }
    assertEquals(size + 3, choose.wSize());
}
Also used : Choose(org.whole.lang.grammars.model.Choose) IEntity(org.whole.lang.model.IEntity) 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) Rule(org.whole.lang.grammars.model.Rule) 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