ElementRule implements IRule { @Override public double validate(IMolecularFormula formula) throws CDKException { logger.info("Start validation of ", formula); ensureDefaultOccurElements(formula.getBuilder()); double isValid = 1.0; Iterator<IElement> itElem = MolecularFormulaManipulator.elements(formula).iterator(); while (itElem.hasNext()) { IElement element = itElem.next(); int occur = MolecularFormulaManipulator.getElementCount(formula, element); IIsotope elemIsotope = formula.getBuilder().newInstance(IIsotope.class, element.getSymbol()); if ((occur < mfRange.getIsotopeCountMin(elemIsotope)) || (occur > mfRange.getIsotopeCountMax(elemIsotope))) { isValid = 0.0; break; } } return isValid; } ElementRule(); @Override void setParameters(Object[] params); @Override Object[] getParameters(); @Override double validate(IMolecularFormula formula);  }
@Test public void testDefaultValidFalse() throws Exception { IRule rule = new ElementRule(); IMolecularFormula formula = new MolecularFormula(); formula.addIsotope(builder.newInstance(IIsotope.class, "C"), 2); formula.addIsotope(builder.newInstance(IIsotope.class, "H"), 200); Assert.assertEquals(0.0, rule.validate(formula), 0.0001); }
@Test public void testDefaultValidTrue() throws Exception { IRule rule = new ElementRule(); IMolecularFormula formula = new MolecularFormula(); formula.addIsotope(builder.newInstance(IIsotope.class, "C"), 2); formula.addIsotope(builder.newInstance(IIsotope.class, "H"), 6); Assert.assertEquals(1.0, rule.validate(formula), 0.0001); }