ContextFreeGrammar extends Grammar { boolean isRecursive(ArrayList<Rule> visitedRules, Rule currentRule){ Production tempProd; Iterator<Symbol> symbIt; Iterator<Production> prodIt; Symbol tempSymbol; Rule definingRule ; if(visitedRules.size() == 0) { prodIt = currentRule.iterator(); } else { prodIt = visitedRules.get(visitedRules.size()-1).iterator(); } if(visitedRules.contains(findRule(currentRule.getLHS()))){ currentRule.setRecursive(true); return true; } while(prodIt.hasNext()){ tempProd = prodIt.next(); symbIt = tempProd.iterator(); while(symbIt.hasNext()) { tempSymbol = symbIt.next(); if(tempSymbol.getType() == Enums.SymbolType.NTSymbol) { definingRule = findRule(tempSymbol); if(definingRule != null) { if(!visitedRules.contains(definingRule)) { visitedRules.add(definingRule); if(isRecursive(visitedRules, currentRule)) { tempProd.setRecursive(true); return true; } } } } } } return false; } 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 testIsRecursive() { System.out.println("* ContextFreeGrammarTest: isRecursive"); ArrayList<Rule> visitedRules = null; Rule startRule = null; ContextFreeGrammar instance = new ContextFreeGrammarMock(); boolean result = false; try { result = instance.isRecursive(visitedRules, startRule); } catch (NullPointerException e) { assertTrue(true); } assertEquals(false, result); visitedRules = new ArrayList<Rule>(); instance = new ContextFreeGrammarMock(); result = false; try { result = instance.isRecursive(visitedRules, startRule); } catch (NullPointerException e) { assertTrue(true); } assertEquals(false, result); startRule = g.getStartRule(); visitedRules = new ArrayList<Rule>(); result = g.isRecursive(visitedRules, startRule); assertEquals(true, result); startRule = g.getRules().get(1); visitedRules = new ArrayList<Rule>(); result = g.isRecursive(visitedRules, startRule); assertEquals(false, result); }