ContextFreeGrammar extends Grammar { void calculateMinimumDepthRecursive(Rule startRule, ArrayList<Rule> visitedRules) { Symbol tempSymbol; Production tempProd; Rule currentRule; Iterator<Symbol> symbIt; Iterator<Production> prodIt; if(!visitedRules.contains(startRule)) { prodIt = startRule.iterator(); while(prodIt.hasNext()) { tempProd = prodIt.next(); tempProd.setMinimumDepth(0); symbIt = tempProd.iterator(); while(symbIt.hasNext()) { tempSymbol = symbIt.next(); if(tempSymbol.getType() == Enums.SymbolType.NTSymbol) { currentRule = findRule(tempSymbol); if(currentRule != null) { visitedRules.add(startRule); calculateMinimumDepthRecursive(currentRule, visitedRules); if(tempProd.getMinimumDepth() < (currentRule.getMinimumDepth() + 1)) { tempProd.setMinimumDepth(currentRule.getMinimumDepth() + 1); } } } else { if(tempProd.getMinimumDepth()<1) { tempProd.setMinimumDepth(1); } } } if(startRule.getMinimumDepth() > tempProd.getMinimumDepth()) { startRule.setMinimumDepth(tempProd.getMinimumDepth()); } } } } ContextFreeGrammar();   ContextFreeGrammar(ContextFreeGrammar g); abstract boolean genotype2Phenotype(); abstract boolean phenotype2Genotype(); abstract int getUsedCodons(); abstract int getUsedWraps(); @SuppressWarnings({"UnusedReturnValue", "IOResourceOpenedButNotSafelyClosed"}) boolean readBNFFile(String file_name); Rule findRule(Symbol s); Rule findRule(String s); ArrayList getTerminalRules(); ArrayList getNonTerminalRules();  }
@Test public void testCalculateMinimumDepthRecursive() { System.out.println("* ContextFreeGrammarTest: calculateMinimumDepthRecursive"); Rule startRule = null; ArrayList<Rule> visitedRules = null; ContextFreeGrammar instance = new ContextFreeGrammarMock(); try { instance.calculateMinimumDepthRecursive(startRule, visitedRules); } catch (NullPointerException e) { assertTrue(true); } visitedRules = new ArrayList<Rule>(); instance = new ContextFreeGrammarMock(); try { instance.calculateMinimumDepthRecursive(startRule, visitedRules); } catch (NullPointerException e) { assertTrue(true); } startRule = g.getStartRule(); visitedRules = new ArrayList<Rule>(); g.calculateMinimumDepthRecursive(startRule, visitedRules); assertEquals(3, startRule.getMinimumDepth()); startRule = g.getRules().get(1); visitedRules = new ArrayList<Rule>(); g.calculateMinimumDepthRecursive(startRule, visitedRules); assertEquals(2, startRule.getMinimumDepth()); }