DerivationTree extends NimbleTree<Symbol> { String getGECodonValue(String s, int codon) throws MalformedGrammarException { String c = Constants.GE_CODON_VALUE_PARSING; if (s.indexOf("{") != -1) { if (s.indexOf("}") == -1) { throw new MalformedGrammarException("Bad GECodonValue specification: " + s); } String GECodonValueRE = c + "\\{(.*)\\}" + ">"; Pattern pattern = Pattern.compile(GECodonValueRE); Matcher matcher = pattern.matcher(s); if (matcher.find()) { if (matcher.groupCount() == 1) { String content = matcher.group(1); String [] setValues = content.split(","); return setValues[codon % setValues.length].trim(); } else { throw new MalformedGrammarException("Bad GECodonValue specification (found too many matches): " + s); } } else { throw new MalformedGrammarException("Bad GECodonValue specification (didn't find a match): " + s); } } else if (s.indexOf("(") != -1 || s.indexOf("[") != -1) { if (s.indexOf(")") == -1 && s.indexOf("]") == -1) { throw new MalformedGrammarException("Bad GECodonValue specification (didn't find closing brackets): " + s); } String GECodonValueRE = c + "([\\(\\[])(.*)([\\)\\]])" + ">"; Pattern pattern = Pattern.compile(GECodonValueRE); Matcher matcher = pattern.matcher(s); String content; String [] boundaryValues; if (matcher.find()) { if (matcher.groupCount() != 3) { throw new MalformedGrammarException("Bad GECodonValue specification (didn't find opening and closing brackets and boundaries): " + s); } content = matcher.group(2); boundaryValues = content.split(","); if (boundaryValues.length != 2) { throw new MalformedGrammarException("Bad GECodonValue specification (didn't find upper and lower boundaries): " + s); } } else { throw new MalformedGrammarException("Bad GECodonValue specification (didn't find a match): " + s); } int low, high; double lowd, highd; boolean intMode; try { intMode = true; low = Integer.parseInt(boundaryValues[0].trim()); high = Integer.parseInt(boundaryValues[1].trim()); lowd = 0.0; highd = 0.0; } catch (Exception e) { intMode = false; lowd = Double.parseDouble(boundaryValues[0].trim()); highd = Double.parseDouble(boundaryValues[1].trim()); low = 0; high = genny.getMaxCodonValue(); } if (matcher.group(1).equals("(")) { low += 1; lowd += 0.0001; } if (matcher.group(3).equals(")")) { high -= 1; highd -= 0.0001; } if (intMode == true) { if (low > high) { throw new MalformedGrammarException("Bad GECodonValue specification (bad boundaries): " + s); } return String.valueOf(low + (codon % (high - low + 1))); } else { if (lowd > highd) { throw new MalformedGrammarException("Bad GECodonValue specification (bad boundaries): " + s); } return String.valueOf(lowd + (highd - lowd) * (codon) / Double.valueOf((genny.getMaxCodonValue() - 0))); } } else { logger.warn("Warning: DerivationTree attempting to use GECodonValue legacy format"); return getGECodonValueLegacyFormat(s, codon); } } DerivationTree(GEGrammar gram, GEChromosome gen);  DerivationTree(DerivationTree copy); boolean buildDerivationTree(); int getWrapCount(); int getGeneCnt(); boolean derive();  }
@Test public void testGetGECodonValue() throws Exception { System.out.println("* DerivationTreeTest: getGECodonValue"); String[] geCodonSpecs = { "<GECodonValue-3+5>", "<GECodonValue{x,y,z}>", "<GECodonValue[-10,-5]>", "<GECodonValue[-10,-5)>", "<GECodonValue(-10,-5]>", "<GECodonValue(-10,-5)>", "<GECodonValue[6.0,7.0]>", "<GECodonValue[6e2, 7e2]>", "<GECodonValue(10.0,11.0]>", "<GECodonValue[13.0, 14.0)>" }; String[][] results = { {"3", "4", "5", "3", "4", "5", "3", "4", "5", "3", "4"}, {"x", "y", "z", "x", "y", "z", "x", "y", "z", "x", "y"}, {"-10", "-9", "-8", "-7", "-6", "-5", "-10", "-9", "-8", "-7", "-6"}, {"-10", "-9", "-8", "-7", "-6", "-10", "-9", "-8", "-7", "-6", "-10"}, {"-9", "-8", "-7", "-6", "-5", "-9", "-8", "-7", "-6", "-5", "-9"}, {"-9", "-8", "-7", "-6", "-9", "-8", "-7", "-6", "-9", "-8", "-7"}, {"6.0", "6.1", "6.2", "6.3", "6.4", "6.5", "6.6", "6.7", "6.8", "6.9", "7.0"}, {"600.0", "610.0", "620.0", "630.0", "640.0", "650.0", "660.0", "670.0", "680.0", "690.0", "700.0"}, {"10.0001", "10.10009", "10.20008", "10.30007", "10.40006", "10.50005", "10.60004", "10.70003", "10.80002", "10.90001", "11.0"}, {"13.0", "13.09999", "13.19998", "13.29997", "13.39996", "13.49995", "13.59994", "13.69993", "13.79992", "13.89991", "13.9999"} }; chrom.setMaxCodonValue(10); int cnt = 0; for (String spec : geCodonSpecs) { for (int j = 0; j <= 10; j++) { String s = dt.getGECodonValue(spec, j); assertEquals(true, s.equals(results[cnt][j])); } cnt++; } String[] geCodonSpecs2 = { "<GECodonValue{x,y,z>" }; String s = ""; for (String spec : geCodonSpecs2) { try { s = dt.getGECodonValue(spec, 0); } catch (MalformedGrammarException e) { assertTrue(true); } String msg = "spec:" + spec + " s:" + s; assertEquals(msg, true, s.equals("")); } }