コード例 #1
0
ファイル: Smarts2MQLVisitor.cs プロジェクト: ch-hristov/NCDK
        public object Visit(ASTSmarts node, object data)
        {
            string local = "";

            for (int i = 0; i < node.JjtGetNumChildren(); i++)
            {
                INode child = node.JjtGetChild(i);
                if (child is ASTAtom)
                {
                    local = (string)child.JjtAccept(this, local);
                }
                else if (child is ASTLowAndBond)
                {
                    i++;
                    INode nextChild = node.JjtGetChild(i); // the next child should
                                                           // be another smarts
                    string bond = (string)child.JjtAccept(this, local);
                    local = local + bond;
                    local = (string)nextChild.JjtAccept(this, local);
                }
                else if (child is ASTSmarts)
                { // implicit single bond
                    if (local.Length != 0)
                    {
                        local = local + "-";
                    }
                    local = (string)child.JjtAccept(this, local);
                }
                else if (child is ASTExplicitAtom)
                {
                    if (local.Length != 0)
                    {
                        local = local + "-";
                    }
                    local = (string)child.JjtAccept(this, local);
                }
            }
            return(data + local);
        }
コード例 #2
0
        public object Visit(ASTSmarts node, object data)
        {
            SMARTSAtom atom = null;
            SMARTSBond bond = null;

            ASTAtom first = (ASTAtom)node.JjtGetChild(0);

            atom = (SMARTSAtom)first.JjtAccept(this, null);
            if (data != null)
            { // this is a sub smarts
                bond = (SMARTSBond)((object[])data)[1];
                IAtom prev = (SMARTSAtom)((object[])data)[0];
                if (bond == null)
                { // since no bond was specified it could be aromatic or single
                    bond = new AromaticOrSingleQueryBond(builder);
                    bond.SetAtoms(new[] { prev, atom });
                }
                else
                {
                    bond.SetAtoms(new[] { prev, atom });
                }
                if (neighbors.ContainsKey(prev))
                {
                    neighbors[prev].Add(atom);
                }
                query.Bonds.Add(bond);
                bond = null;
            }
            // first ATOM in expression
            query.Atoms.Add(atom);

            if (BitArrays.GetValue(tetrahedral, query.Atoms.Count - 1))
            {
                List <IAtom> localNeighbors = new List <IAtom>(query.GetConnectedAtoms(atom))
                {
                    atom
                };
                neighbors[atom] = localNeighbors;
            }

            // now process the rest of the bonds/atoms
            for (int i = 1; i < node.JjtGetNumChildren(); i++)
            {
                INode child = node.JjtGetChild(i);
                if (child is ASTLowAndBond)
                {
                    bond = (SMARTSBond)child.JjtAccept(this, data);
                }
                else if (child is ASTAtom)
                {
                    SMARTSAtom newAtom = (SMARTSAtom)child.JjtAccept(this, null);
                    if (bond == null)
                    { // since no bond was specified it could be aromatic or single
                        bond = new AromaticOrSingleQueryBond(builder);
                    }
                    bond.SetAtoms(new[] { atom, newAtom });
                    query.Bonds.Add(bond);
                    query.Atoms.Add(newAtom);

                    if (neighbors.ContainsKey(atom))
                    {
                        neighbors[atom].Add(newAtom);
                    }
                    if (BitArrays.GetValue(tetrahedral, query.Atoms.Count - 1))
                    {
                        List <IAtom> localNeighbors = new List <IAtom>(query.GetConnectedAtoms(newAtom))
                        {
                            newAtom
                        };
                        neighbors[newAtom] = localNeighbors;
                    }

                    atom = newAtom;
                    bond = null;
                }
                else if (child is ASTSmarts)
                { // another smarts
                    child.JjtAccept(this, new object[] { atom, bond });
                    bond = null;
                }
                else if (child is ASTRingIdentifier)
                {
                    HandleRingClosure(atom, (ASTRingIdentifier)child);
                }
                else
                {
                    throw new InvalidOperationException("Unhandled node type: " + child.GetType());
                }
            }

            return(query);
        }