Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Implemented SymbolMatcher for use in UniversalIsomorphismTester (to f…

…ollow)

The symbol matcher allows matching of atom symbols without query atoms.
 A new interface (IAtomMatcher) was defined that provides decoupled
matching of atom attributes. The AbstractAtomMatcher provides
simplified implementation of other matchers (including charge).
Several tests were implemented to test the SymbolMatcher is functioning
as expected when matching and handling null attributes.
The new test class was added to the MstandardTests
  • Loading branch information...
commit 45e979c3739867b79383bd1bdcef058806f3ee30 1 parent b5caeba
@johnmay authored
View
53 src/main/org/openscience/cdk/isomorphism/matchers/IAtomMatcher.java
@@ -0,0 +1,53 @@
+/*
+ * Copyright (C) 2012 John May <jwmay@sf.net>
+ *
+ * Contact: cdk-devel@lists.sourceforge.net
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+package org.openscience.cdk.isomorphism.matchers;
+
+import org.openscience.cdk.interfaces.IAtom;
+
+/**
+ * A decoupled atom matcher that allows separation of atom matching from structural isomorphism
+ * algorithms. The atom matchers can be used for container wide variable matching of non-query
+ * molecules (i.e. determine if two non-query containers are the identical when atom symbol
+ * and charge is considered). The default behaviour of the {@see UniversalIsomorphismTester}
+ * is to match only the atom symbols (or use provided {@link IQueryAtom}'s).
+ * If a per-atom variation is needed then {@link IQueryAtom} or SMARTS should be used.
+ *
+ * @author John May
+ * @cdk.module interfaces
+ * @see org.openscience.cdk.isomorphism.UniversalIsomorphismTester
+ * @see IQueryAtom
+ * @see org.openscience.cdk.isomorphism.matchers.smarts.SMARTSAtom
+ * @see org.openscience.cdk.smiles.smarts.parser.SMARTSParser
+ */
+public interface IAtomMatcher {
+
+ /**
+ * Determines if the query and subject atoms match. This method should follow
+ * default java behaviour where if both properties of the atoms being matched
+ * are null the match is considered true.
+ *
+ * @param query a query atom
+ * @param subject a subject atom
+ *
+ * @return whether the atoms match on this matcher criteria
+ */
+ public boolean matches(IAtom query, IAtom subject);
+
+}
View
64 src/main/org/openscience/cdk/isomorphism/matchers/atom/AbstractAtomMatcher.java
@@ -0,0 +1,64 @@
+/*
+ * Copyright (C) 2012 John May <jwmay@sf.net>
+ *
+ * Contact: cdk-devel@lists.sourceforge.net
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+package org.openscience.cdk.isomorphism.matchers.atom;
+
+import org.openscience.cdk.interfaces.IAtom;
+import org.openscience.cdk.isomorphism.matchers.IAtomMatcher;
+
+/**
+ * An abstract atom matcher that allows simplified implementation of new atom
+ * matchers requiring only the specification of a {@link #getAttribute(IAtom)}
+ * method. Each matcher should match a different attribute (e.g. symbol or charge)
+ * and combined using logical connectives (TODO: links to be added - not yet implemented).
+ *
+ * @author John May
+ * @cdk.module standard
+ * @see org.openscience.cdk.isomorphism.UniversalIsomorphismTester
+ * @see SymbolMatcher
+ */
+public abstract class AbstractAtomMatcher implements IAtomMatcher {
+
+ /**
+ * @inheritDoc
+ */
+ @Override
+ public final boolean matches(IAtom query, IAtom subject) {
+
+ Object queryAttribute = getAttribute(query);
+ Object subjectAttribute = getAttribute(subject);
+
+ return queryAttribute != null
+ ? queryAttribute.equals(subjectAttribute)
+ : subjectAttribute == null;
+
+ }
+
+ /**
+ * Used to simplify match implementation. This method should return
+ * the value of the desired metric for the given atom. This method
+ * can safely return null.
+ *
+ * @param atom The atom for which to get the attribute
+ *
+ * @return value of the metric
+ */
+ public abstract Object getAttribute(IAtom atom);
+
+}
View
67 src/main/org/openscience/cdk/isomorphism/matchers/atom/SymbolMatcher.java
@@ -0,0 +1,67 @@
+/*
+ * Copyright (C) 2012 John May <jwmay@sf.net>
+ *
+ * Contact: cdk-devel@lists.sourceforge.net
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+package org.openscience.cdk.isomorphism.matchers.atom;
+
+import org.openscience.cdk.annotations.TestClass;
+import org.openscience.cdk.annotations.TestMethod;
+import org.openscience.cdk.interfaces.IAtom;
+
+/**
+ * An {@link org.openscience.cdk.isomorphism.matchers.IAtomMatcher} for atom symbols. This method
+ * determines whether the symbols of an atom ({@link org.openscience.cdk.interfaces.IAtom#getSymbol()})
+ * are equal.
+ * <p/>
+ * This matcher is is the default matcher for the {@link org.openscience.cdk.isomorphism.UniversalIsomorphismTester}.
+ *
+ * @author John May
+ * @cdk.module standard
+ * @see org.openscience.cdk.isomorphism.UniversalIsomorphismTester
+ * @see org.openscience.cdk.interfaces.IAtom#getSymbol()
+ */
+@TestClass("org.openscience.cdk.isomorphism.matchers.SymbolMatcherTest")
+public class SymbolMatcher extends AbstractAtomMatcher {
+
+
+ /**
+ * Provides the symbol of the atom as an attribute to match.
+ *
+ * @param atom The atom for which to get the symbol
+ *
+ * @return the symbol of the atom or null if the symbol is unset
+ *
+ * @see org.openscience.cdk.interfaces.IAtom#getSymbol()
+ */
+ @Override
+ @TestMethod("testMatches,testMatches_Mismatch,"
+ + "testMatches_QueryNull,testMatches_SubjectNull,testMatches_BothNull,"
+ + "testGetAttribute_Null,testGetAttribute")
+ public Object getAttribute(IAtom atom) {
+ return atom.getSymbol();
+ }
+
+ /**
+ * @inheritDoc
+ */
+ @Override
+ @TestMethod("testToString")
+ public String toString() {
+ return "query.getSymbol() == subject.getSymbol()";
+ }
+}
View
150 src/test/org/openscience/cdk/isomorphism/matchers/atom/SymbolMatcherTest.java
@@ -0,0 +1,150 @@
+/*
+ * Copyright (C) 2012 John May <jwmay@sf.net>
+ *
+ * Contact: cdk-devel@lists.sourceforge.net
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public License
+ * as published by the Free Software Foundation; either version 2.1
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+package org.openscience.cdk.isomorphism.matchers.atom;
+
+import junit.framework.Assert;
+import org.junit.Test;
+import org.openscience.cdk.DefaultChemObjectBuilder;
+import org.openscience.cdk.interfaces.IAtom;
+import org.openscience.cdk.interfaces.IChemObjectBuilder;
+
+/**
+ * Tests the atom symbol matching.
+ * @cdk.module test-standard
+ * @author John May
+ */
+public class SymbolMatcherTest {
+
+ private IChemObjectBuilder builder = DefaultChemObjectBuilder.getInstance();
+ private AbstractAtomMatcher matcher = new SymbolMatcher();
+
+ @Test
+ public void testGetAttribute_Null() throws Exception {
+
+ IAtom atom = builder.newInstance(IAtom.class);
+
+ Assert.assertNotNull("Built atom was null", atom);
+
+ Assert.assertNull("Expected null symbol on atom",
+ matcher.getAttribute(atom));
+ }
+
+ @Test
+ public void testGetAttribute() throws Exception {
+
+ IAtom atom = builder.newInstance(IAtom.class);
+
+ Assert.assertNotNull("Built atom was null", atom);
+
+ atom.setSymbol("C");
+
+ Assert.assertEquals("Expected 'C' symbol on atom",
+ "C", matcher.getAttribute(atom));
+
+ }
+
+ @Test
+ public void testMatches_QueryNull() throws Exception {
+
+ IAtom query = builder.newInstance(IAtom.class);
+ IAtom subject = builder.newInstance(IAtom.class);
+
+ Assert.assertNotNull("Built query was null", query);
+ Assert.assertNotNull("Built subject was null", subject);
+
+ subject.setSymbol("C");
+
+ Assert.assertFalse("Null symbol on query atom should not match 'C' on subject",
+ matcher.matches(query, subject));
+
+ }
+
+ @Test
+ public void testMatches_SubjectNull() throws Exception {
+
+ IAtom query = builder.newInstance(IAtom.class);
+ IAtom subject = builder.newInstance(IAtom.class);
+
+ Assert.assertNotNull("Built query was null", query);
+ Assert.assertNotNull("Built subject was null", subject);
+
+ query.setSymbol("C");
+
+ Assert.assertFalse("Null symbol on subject atom should not match 'C' on query",
+ matcher.matches(query, subject));
+
+ }
+
+ @Test
+ public void testMatches_BothNull() throws Exception {
+
+ IAtom query = builder.newInstance(IAtom.class);
+ IAtom subject = builder.newInstance(IAtom.class);
+
+ Assert.assertNotNull("Built query was null", query);
+ Assert.assertNotNull("Built subject was null", subject);
+
+ Assert.assertTrue("Atoms without any symbols (null) should match",
+ matcher.matches(query, subject));
+
+ }
+
+ @Test
+ public void testMatches() throws Exception {
+
+ IAtom query = builder.newInstance(IAtom.class);
+ IAtom subject = builder.newInstance(IAtom.class);
+
+ Assert.assertNotNull("Built query was null", query);
+ Assert.assertNotNull("Built subject was null", subject);
+
+ query.setSymbol("C");
+ subject.setSymbol("C");
+
+ Assert.assertTrue("Atoms with the same symbol should match",
+ matcher.matches(query, subject));
+
+ }
+
+
+ @Test
+ public void testMatches_Mismatch() throws Exception {
+
+ IAtom query = builder.newInstance(IAtom.class);
+ IAtom subject = builder.newInstance(IAtom.class);
+
+ Assert.assertNotNull("Built query was null", query);
+ Assert.assertNotNull("Built subject was null", subject);
+
+ query.setSymbol("C");
+ subject.setSymbol("N");
+
+ Assert.assertFalse("Atoms with different symbols ('C' and 'N') should not match",
+ matcher.matches(query, subject));
+
+ }
+
+ @Test
+ public void testToString() throws Exception {
+ Assert.assertEquals("query.getSymbol() == subject.getSymbol()", matcher.toString());
+ }
+
+
+}
View
4 src/test/org/openscience/cdk/modulesuites/MstandardTests.java
@@ -50,6 +50,7 @@
import org.openscience.cdk.graph.rebond.RebondToolTest;
import org.openscience.cdk.isomorphism.IsomorphismTesterTest;
import org.openscience.cdk.isomorphism.UniversalIsomorphismTesterTest;
+import org.openscience.cdk.isomorphism.matchers.atom.SymbolMatcherTest;
import org.openscience.cdk.isomorphism.mcss.RGraphTest;
import org.openscience.cdk.isomorphism.mcss.RMapTest;
import org.openscience.cdk.isomorphism.mcss.RNodeTest;
@@ -188,6 +189,7 @@
HOSECodeGeneratorTest.class,
LonePairElectronCheckerTest.class ,
StereoToolTest.class,
- VABCVolumeTest.class
+ VABCVolumeTest.class,
+ SymbolMatcherTest.class
})
public class MstandardTests {}
Please sign in to comment.
Something went wrong with that request. Please try again.