Skip to content

Commit

Permalink
Adding descriptor initialisation for those which require it.
Browse files Browse the repository at this point in the history
Signed-off-by: Egon Willighagen <egonw@users.sourceforge.net>
  • Loading branch information
johnmay authored and egonw committed Jul 31, 2013
1 parent a097d37 commit 284f1ef
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,14 @@ public class AcidicGroupCountDescriptor extends AbstractMolecularDescriptor impl
* Creates a new {@link AcidicGroupCountDescriptor}.
*/
@TestMethod("testConstructor")
public AcidicGroupCountDescriptor(IChemObjectBuilder builder) throws CDKException {
public AcidicGroupCountDescriptor() {
this.checkAromaticity = true;
}

@Override public void initialise(IChemObjectBuilder builder) {
for (String smarts : SMARTS_STRINGS) {
tools.add(new SMARTSQueryTool(smarts, builder));
}
this.checkAromaticity = true;
}

/** {@inheritDoc} */
Expand Down Expand Up @@ -117,6 +120,11 @@ public String[] getDescriptorNames() {
/** {@inheritDoc} */
@TestMethod("testCalculate_IAtomContainer")
public DescriptorValue calculate(IAtomContainer atomContainer) {

if(tools.isEmpty()) {
throw new IllegalStateException("descriptor is not initalised, invoke 'initalise' first");
}

// do aromaticity detection
if (this.checkAromaticity) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ public class BasicGroupCountDescriptor extends AbstractMolecularDescriptor imple
* Creates a new {@link BasicGroupCountDescriptor}.
*/
@TestMethod("testConstructor")
public BasicGroupCountDescriptor(IChemObjectBuilder builder) throws CDKException {
public BasicGroupCountDescriptor() {
}

@Override public void initialise(IChemObjectBuilder builder) {
for (String smarts : SMARTS_STRINGS) {
tools.add(new SMARTSQueryTool(smarts, builder));
}
Expand Down Expand Up @@ -101,6 +104,11 @@ public String[] getDescriptorNames() {
/** {@inheritDoc} */
@TestMethod("testCalculate_IAtomContainer")
public DescriptorValue calculate(IAtomContainer atomContainer) {

if(tools.isEmpty()) {
throw new IllegalStateException("descriptor is not initalised, invoke 'initalise' first");
}

try {
int count = 0;
for (SMARTSQueryTool tool : tools) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,9 @@ public DescriptorTest() {}

public void setDescriptor(Class<? extends T> descriptorClass) throws Exception {
if (descriptor == null) {
try {
Constructor<? extends T> defaultConstructor = descriptorClass.getConstructor();
this.descriptor = defaultConstructor.newInstance();
} catch (NoSuchMethodException ex) {
// no default constructor, try with an IChemObjectBuilder...
Constructor<? extends T> builderConstructor = descriptorClass.getConstructor(IChemObjectBuilder.class);
this.descriptor = builderConstructor.newInstance(DefaultChemObjectBuilder.getInstance());
}
Constructor<? extends T> defaultConstructor = descriptorClass.getConstructor();
this.descriptor = defaultConstructor.newInstance();
this.descriptor.initialise(DefaultChemObjectBuilder.getInstance());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ public void setUp() throws Exception {
}

@Test public void testConstructor() throws Exception {
Assert.assertNotNull(new AcidicGroupCountDescriptor(DefaultChemObjectBuilder.getInstance()));
Assert.assertNotNull(new AcidicGroupCountDescriptor());
}

@Test(expected = IllegalStateException.class)
public void uninitalisedError() {
new BasicGroupCountDescriptor().calculate(new AtomContainer());
}

@Test public void testOneAcidGroup() throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openscience.cdk.AtomContainer;
import org.openscience.cdk.DefaultChemObjectBuilder;
import org.openscience.cdk.interfaces.IAtom;
import org.openscience.cdk.interfaces.IAtomContainer;
Expand All @@ -41,7 +42,7 @@ public void setUp() throws Exception {
}

@Test public void testConstructor() throws Exception {
Assert.assertNotNull(new BasicGroupCountDescriptor(DefaultChemObjectBuilder.getInstance()));
Assert.assertNotNull(new BasicGroupCountDescriptor());
}

@Test public void testAmine() throws Exception {
Expand All @@ -54,6 +55,11 @@ public void setUp() throws Exception {
Assert.assertEquals(1, result.intValue());
}

@Test(expected = IllegalStateException.class)
public void uninitalisedError() {
new BasicGroupCountDescriptor().calculate(new AtomContainer());
}

/**
* @cdk.inchi InChI=1S/C2H4N2/c1-4-2-3/h2-3H,1H2
*/
Expand Down

0 comments on commit 284f1ef

Please sign in to comment.