MolecularFormulaChecker { public IMolecularFormula isValid(IMolecularFormula formula) { logger.info("Generating the validity of the molecular formula"); if (formula.getIsotopeCount() == 0) { logger.error("Proposed molecular formula has not elements"); return formula; } Iterator<IRule> iterRules = rules.iterator(); try { while (iterRules.hasNext()) { IRule rule = iterRules.next(); double result = rule.validate(formula); formula.setProperty(rule.getClass(), result); } } catch (CDKException e) { e.printStackTrace(); } return formula; } MolecularFormulaChecker(List<IRule> rules); List<IRule> getRules(); Double isValidSum(IMolecularFormula formula); IMolecularFormula isValid(IMolecularFormula formula);  }
@Test public void testIsValid_NOT() { IMolecularFormula formula = new MolecularFormula(); formula.addIsotope(ifac.getMajorIsotope("C"), 1); formula.addIsotope(ifac.getMajorIsotope("H"), 100); List<IRule> rules = new ArrayList<IRule>(); rules.add(new MMElementRule()); MolecularFormulaChecker MFChecker = new MolecularFormulaChecker(rules); IMolecularFormula formulaWith = MFChecker.isValid(formula); Assert.assertEquals(0.0d, (double)formulaWith.getProperty((new MMElementRule()).getClass()), 0.01); }
@Test public void testIsValid_NOT_2Rules() { IMolecularFormula formula = new MolecularFormula(); formula.addIsotope(ifac.getMajorIsotope("C"), 1); formula.addIsotope(ifac.getMajorIsotope("H"), 100); formula.setCharge(0); List<IRule> rules = new ArrayList<IRule>(); rules.add(new MMElementRule()); rules.add(new ChargeRule()); MolecularFormulaChecker MFChecker = new MolecularFormulaChecker(rules); IMolecularFormula formulaWith = MFChecker.isValid(formula); Assert.assertEquals(0.0, formulaWith.getProperty((new MMElementRule()).getClass()), 0.01); Assert.assertEquals(1.0, formulaWith.getProperty((new ChargeRule()).getClass()), 0.01); }
@Test public void testIsValid_True_2Rules() { IMolecularFormula formula = new MolecularFormula(); formula.addIsotope(ifac.getMajorIsotope("C"), 1); formula.addIsotope(ifac.getMajorIsotope("H"), 4); formula.setCharge(0); List<IRule> rules = new ArrayList<IRule>(); rules.add(new MMElementRule()); rules.add(new ChargeRule()); MolecularFormulaChecker MFChecker = new MolecularFormulaChecker(rules); IMolecularFormula formulaWith = MFChecker.isValid(formula); Assert.assertEquals(1.0, formulaWith.getProperty((new MMElementRule()).getClass()), 0.01); Assert.assertEquals(1.0, formulaWith.getProperty((new ChargeRule()).getClass()), 0.01); }