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