Hello all,

At the beginning I would like to say that having SMARTS implementation in CDK is really a great step forward, congratulations to all contributors.

I've started trying CDK smarts,  with the intention to replace Joelib Smarts I am currently using in Toxtree. 

Unfortunately, I've experienced StackOverflow error, and after some debugging it turns out that if IAtomContainer clone method is executed after matching a smarts it goes into an infinite cycle.
Most probably this is due to atoms containing RingSet of the same molecule as a property and clone() method tries recursively to do deep copies of atom->ringset as a property of atom->atoms of the ringset->ringset as a property of atom, etc.

Not sure what is the best workaround (perhaps shallow copies of Atom properties?).

Here is the test, the smarts and smiles are purely for example, the result will be the same with any molecule that have rings.

    public int CDKSmarts(String smarts, String smiles) throws Exception  {

        SMARTSQueryTool sqt = new SMARTSQueryTool(smarts, true);
       
        IAtomContainer atomContainer = FunctionalGroups.createAtomContainer(smiles,true);
        CDKHueckelAromaticityDetector.detectAromaticity(atomContainer);
       
        int n = 0;
       
        if (sqt.matches(atomContainer)) {
            n = sqt.countMatches();
        }
        atomContainer.clone();
        return n;
    }   
    public void testCDKSmarts890() throws Exception  {
        String smiles="CCCN(CCC)C1=CC=C(C=C1)N=NC2=CC=[N+]([O-])C=C2";
        assertTrue(CDKSmarts("a[N]=[N]a", smiles)>0);
    }

and the stack trace
java.lang.StackOverflowError
    at org.openscience.cdk.ChemObject.clone(ChemObject.java:304)
    at org.openscience.cdk.AtomContainer.clone(AtomContainer.java:1544)
    at org.openscience.cdk.AtomContainerSet.clone(AtomContainerSet.java:363)
    at org.openscience.cdk.RingSet.clone(RingSet.java:190)
    at org.openscience.cdk.ChemObject.clone(ChemObject.java:319)
    at org.openscience.cdk.Element.clone(Element.java:179)
    at org.openscience.cdk.Isotope.clone(Isotope.java:291)
    at org.openscience.cdk.AtomType.clone(AtomType.java:397)
    at org.openscience.cdk.Atom.clone(Atom.java:390)
    at org.openscience.cdk.AtomContainer.clone(AtomContainer.java:1549)
    at org.openscience.cdk.AtomContainerSet.clone(AtomContainerSet.java:363)
    at org.openscience.cdk.RingSet.clone(RingSet.java:190)
    at org.openscience.cdk.ChemObject.clone(ChemObject.java:319)
    at org.openscience.cdk.Element.clone(Element.java:179)
    at org.openscience.cdk.Isotope.clone(Isotope.java:291)
    at org.openscience.cdk.AtomType.clone(AtomType.java:397)
    at org.openscience.cdk.Atom.clone(Atom.java:390)
    at org.openscience.cdk.AtomContainer.clone(AtomContainer.java:1549)
    at org.openscience.cdk.AtomContainerSet.clone(AtomContainerSet.java:363)
    at org.openscience.cdk.RingSet.clone(RingSet.java:190)
    at org.openscience.cdk.ChemObject.clone(ChemObject.java:319)
    at org.openscience.cdk.Element.clone(Element.java:179)
    at org.openscience.cdk.Isotope.clone(Isotope.java:291)
    at org.openscience.cdk.AtomType.clone(AtomType.java:397)
    at org.openscience.cdk.Atom.clone(Atom.java:390)
    at org.openscience.cdk.AtomContainer.clone(AtomContainer.java:1549)
    at org.openscience.cdk.AtomContainerSet.clone(AtomContainerSet.java:363)
    at org.openscience.cdk.RingSet.clone(RingSet.java:190)
    at org.openscience.cdk.ChemObject.clone(ChemObject.java:319)
    at org.openscience.cdk.Element.clone(Element.java:179)
    at org.openscience.cdk.Isotope.clone(Isotope.java:291)
    at org.openscience.cdk.AtomType.clone(AtomType.java:397)
    at org.openscience.cdk.Atom.clone(Atom.java:390)
    at org.openscience.cdk.AtomContainer.clone(AtomContainer.java:1549)
    at org.openscience.cdk.AtomContainerSet.clone(AtomContainerSet.java:363)
    at org.openscience.cdk.RingSet.clone(RingSet.java:190)
    at org.openscience.cdk.ChemObject.clone(ChemObject.java:319)
    at org.openscience.cdk.Element.clone(Element.java:179)
    at org.openscience.cdk.Isotope.clone(Isotope.java:291)
    at org.openscience.cdk.AtomType.clone(AtomType.java:397)
    at org.openscience.cdk.Atom.clone(Atom.java:390)
    at org.openscience.cdk.AtomContainer.clone(AtomContainer.java:1549)
    at org.openscience.cdk.AtomContainerSet.clone(AtomContainerSet.java:363)
    at org.openscience.cdk.RingSet.clone(RingSet.java:190)
    at org.openscience.cdk.ChemObject.clone(ChemObject.java:319)
    at org.openscience.cdk.Element.clone(Element.java:179)
    at org.openscience.cdk.Isotope.clone(Isotope.java:291)
    at org.openscience.cdk.AtomType.clone(AtomType.java:397)
    at org.openscience.cdk.Atom.clone(Atom.java:390)
    at org.openscience.cdk.AtomContainer.clone(AtomContainer.java:1549)
    at org.openscience.cdk.AtomContainerSet.clone(AtomContainerSet.java:363)
    at org.openscience.cdk.RingSet.clone(RingSet.java:190)
    at org.openscience.cdk.ChemObject.clone(ChemObject.java:319)
    at org.openscience.cdk.Element.clone(Element.java:179)
    at org.openscience.cdk.Isotope.clone(Isotope.java:291)
    at org.openscience.cdk.AtomType.clone(AtomType.java:397)
    at org.openscience.cdk.Atom.clone(Atom.java:390)
    at org.openscience.cdk.AtomContainer.clone(AtomContainer.java:1549)
    at org.openscience.cdk.AtomContainerSet.clone(AtomContainerSet.java:363)
    at org.openscience.cdk.RingSet.clone(RingSet.java:190)
    at org.openscience.cdk.ChemObject.clone(ChemObject.java:319)
    at org.openscience.cdk.Element.clone(Element.java:179)
    at org.openscience.cdk.Isotope.clone(Isotope.java:291)
    at org.openscience.cdk.AtomType.clone(AtomType.java:397)
    ....


Regards,
Nina