DerivationTree extends NimbleTree<Symbol> { @SuppressWarnings({"LoopStatementThatDoesntLoop"}) boolean growNode(DerivationNode t) { Symbol s = t.getData(); if(this.getDepth() > grammy.getMaxDerivationTreeDepth()) { logger.warn("maxDerivationTreeDepth exceeded:" + this.getDepth()+">"+grammy.maxDerivationTreeDepth); return false; } if(this.geneCnt > this.genny.getMaxChromosomeLength()) { logger.warn("maxGEChromosomeLength exceeded:" + this.geneCnt+">"+genny.getMaxChromosomeLength()); return false; } if(s.getType()==Enums.SymbolType.NTSymbol){ Rule r = this.grammy.findRule(s); int numProd = r.size(); if(!this.genIter.hasNext()){ this.wrapCount++; this.genIter = genny.iterator(); } while(this.wrapCount <= this.grammy.getMaxWraps()) { Production p; int currentCodonValue; if(numProd>1) { currentCodonValue = this.genIter.next(); t.setCodonIndex ( geneCnt, currentCodonValue % numProd ); this.geneCnt++; p= r.get(currentCodonValue % numProd); } else { p = r.get(0); } Iterator<Symbol> symIt = p.iterator(); DerivationNode newTree; Symbol newSym; while(symIt.hasNext()) { newSym = symIt.next(); if(newSym.getType() == Enums.SymbolType.NTSymbol && newSym.getSymbolString().startsWith(Constants.GE_CODON_VALUE_PARSING)) { String value; if(!this.genIter.hasNext()){ this.genIter = genny.iterator(); this.wrapCount++; } this.geneCnt++; currentCodonValue = this.genIter.next(); try { value = getGECodonValue(newSym.getSymbolString(), currentCodonValue); } catch (MalformedGrammarException e) { logger.error("Malformed GECodonValue specification in grammar"); return false; } newSym = new Symbol(value, Enums.SymbolType.TSymbol); } newTree = new DerivationNode(t, newSym); t.add(newTree); this.setCurrentLevel(this.getCurrentLevel()+1); if(this.getDepth()<this.getCurrentLevel()) { this.setDepth(this.getCurrentLevel()); } if(!growNode(newTree)) { return false; } this.setCurrentLevel(this.getCurrentLevel()-1); } return true; } return false; } else { this.grammy.getPhenotype().add(s); return true; } } DerivationTree(GEGrammar gram, GEChromosome gen);  DerivationTree(DerivationTree copy); boolean buildDerivationTree(); int getWrapCount(); int getGeneCnt(); boolean derive();  }
@Test public void testGrowNode() throws MalformedGrammarException { System.out.println("* DerivationTreeTest: growNode"); geg.setMaxDerivationTreeDepth(0); assertEquals(false, dt.growNode((DerivationNode) dt.getRoot())); tc.growTree(); chrom.setMaxChromosomeLength(1); assertEquals(false, dt.growNode((DerivationNode) dt.getRoot())); int[] ia = {0}; tc.growTree(); geg.getGenotype().setAll(ia); geg.setMaxWraps(1); dt = new DerivationTree(geg, chrom); assertEquals(false, dt.growNode((DerivationNode) dt.getRoot())); tc.growTree(); String grammar = "<A>::=<GECodonValue(1,2>|<B>\n<B>::=a|b\n"; geg.clear(); geg.readBNFString(grammar); dt = new DerivationTree(geg, chrom); assertEquals(false, dt.growNode((DerivationNode) dt.getRoot())); tc.growTree(); grammar = "<A>::=<GECodonValue(1,3)>|<B>\n<B>::=a|b\n"; geg.clear(); geg.readBNFString(grammar); dt = new DerivationTree(geg, chrom); assertEquals(true, dt.growNode((DerivationNode) dt.getRoot())); tc.growTree(); grammar = "<A>::=<B>|b\n<B>::=a\n"; geg.clear(); geg.readBNFString(grammar); dt = new DerivationTree(geg, chrom); dt.growNode((DerivationNode) dt.getRoot()); assertEquals(1, dt.getGeneCnt()); int[] ia2 = {0, 0}; tc.growTree(); geg.getGenotype().setAll(ia2); dt = new DerivationTree(geg, chrom); dt.growNode((DerivationNode) dt.getRoot()); assertEquals(2, dt.getWrapCount()); assertEquals(3, dt.getGeneCnt()); assertEquals(3, dt.getDepth()); }