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());
}
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);
}
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());
}
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());
}
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());
}
Aggregations