private long ShortestPath( )
        {
            long distance = long.MaxValue;

            foreach (var node in ForwardProcss)
            {
                var reverse = RGraph.ElementAt((int)node.Key - 1);

                if (node.Distance != long.MaxValue && reverse.Distance != long.MaxValue &&
                    distance > node.Distance + reverse.Distance)
                {
                    distance = node.Distance + reverse.Distance;
                }
            }

            foreach (var node in ReverseProcess)
            {
                var forward = Graph.ElementAt((int)node.Key - 1);

                if (node.Distance != long.MaxValue && forward.Distance != long.MaxValue &&
                    distance > node.Distance + forward.Distance)
                {
                    distance = node.Distance + forward.Distance;
                }
            }

            return((distance == long.MaxValue) ? -1 : distance);
        }
Beispiel #2
0
        //////////////////////////////////////////////////
        //          Internal methods

        /// <summary>  Builds the RGraph ( resolution graph ), from two atomContainer
        /// (description of the two molecules to compare)
        /// This is the interface point between the CDK model and
        /// the generic MCSS algorithm based on the RGRaph.
        ///
        /// </summary>
        /// <param name="g1"> Description of the first molecule
        /// </param>
        /// <param name="g2"> Description of the second molecule
        /// </param>
        /// <returns>     the rGraph
        /// </returns>
        public static RGraph buildRGraph(IAtomContainer g1, IAtomContainer g2)
        {
            RGraph rGraph = new RGraph();

            nodeConstructor(rGraph, g1, g2);
            arcConstructor(rGraph, g1, g2);
            return(rGraph);
        }
Beispiel #3
0
 public override void Unload()
 {
     base.Unload();
     if (UIEditor.UIEditor.Instance != null)
     {
         UIEditor.UIEditor.Instance.UIStateMachine.Remove(RecipeGraphUI);
     }
     Instance          = null;
     Graph             = null;
     QuickRecipeHotkey = null;
     RecipeGraphUI     = null;
 }
        private void RemovalExploration(Node v)
        {
            RGraph.Remove(RGraph.Find(n => n.Key == v.Key));
            Graph.Remove(v);

            foreach (var vv in v.AdjacentNodes)
            {
                if (Graph.Contains(vv.Item1))
                {
                    RemovalExploration(vv.Item1);
                }
            }
        }
        private void Setup(long nodeCount, long source, long target)
        {
            Graph.ForEach(n => n.Distance  = long.MaxValue);
            RGraph.ForEach(n => n.Distance = long.MaxValue);

            ForwardHeap = new PriorityQueue(nodeCount);
            ReverseHeap = new PriorityQueue(nodeCount);

            ForwardProcss  = new List <Node>();
            ReverseProcess = new List <Node>();

            Graph[(int)source - 1].Distance  = 0;
            RGraph[(int)target - 1].Distance = 0;

            ForwardHeap.Insert(Graph[(int)source - 1]);
            ReverseHeap.Insert(RGraph[(int)target - 1]);
        }
Beispiel #6
0
        /// <summary>  Builds  the nodes of the RGraph ( resolution graph ), from
        /// two atom containers (description of the two molecules to compare)
        ///
        /// </summary>
        /// <param name="gr">  the target RGraph
        /// </param>
        /// <param name="ac1"> description of the first molecule
        /// </param>
        /// <param name="ac2"> description of the second molecule
        /// </param>
        private static void nodeConstructor(RGraph gr, IAtomContainer ac1, IAtomContainer ac2)
        {
            // resets the target graph.
            gr.clear();
            IBond[] bondsA1 = ac1.Bonds;
            IBond[] bondsA2 = ac2.Bonds;

            // compares each bond of G1 to each bond of G2
            for (int i = 0; i < bondsA1.Length; i++)
            {
                for (int j = 0; j < bondsA2.Length; j++)
                {
                    if (timeout > -1 && ((System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start) > timeout)
                    {
                        throw new CDKException("Timeout exceeded in getOverlaps");
                    }
                    IBond bondA2 = bondsA2[j];
                    if (bondA2 is IQueryBond)
                    {
                        IQueryBond queryBond = (IQueryBond)bondA2;
                        IQueryAtom atom1     = (IQueryAtom)(bondA2.getAtomAt(0));
                        IQueryAtom atom2     = (IQueryAtom)(bondA2.getAtomAt(1));
                        IBond      bond      = bondsA1[i];
                        if (queryBond.matches(bond))
                        {
                            // ok, bonds match
                            if (atom1.matches(bond.getAtomAt(0)) && atom2.matches(bond.getAtomAt(1)) || atom1.matches(bond.getAtomAt(1)) && atom2.matches(bond.getAtomAt(0)))
                            {
                                // ok, atoms match in either order
                                gr.addNode(new RNode(i, j));
                            }
                        }
                    }
                    else
                    {
                        // if both bonds are compatible then create an association node
                        // in the resolution graph
                        if (((bondsA1[i].Order == bondsA2[j].Order && bondsA1[i].getFlag(CDKConstants.ISAROMATIC) == bondsA2[j].getFlag(CDKConstants.ISAROMATIC)) || (bondsA1[i].getFlag(CDKConstants.ISAROMATIC) && bondsA2[j].getFlag(CDKConstants.ISAROMATIC))) && ((bondsA1[i].getAtomAt(0).Symbol.Equals(bondsA2[j].getAtomAt(0).Symbol) && bondsA1[i].getAtomAt(1).Symbol.Equals(bondsA2[j].getAtomAt(1).Symbol)) || (bondsA1[i].getAtomAt(0).Symbol.Equals(bondsA2[j].getAtomAt(1).Symbol) && bondsA1[i].getAtomAt(1).Symbol.Equals(bondsA2[j].getAtomAt(0).Symbol))))
                        {
                            gr.addNode(new RNode(i, j));
                        }
                    }
                }
            }
        }
        private List <int> GetSCC(long nodeCount)
        {
            List <int> markedNodes = new List <int>();

            DFSOnRGraph(nodeCount);

            RGraph = RGraph.OrderBy(v => v.PostVisit).ToList();
            while (Graph.Count > 0)
            {
                var maxPost = RGraph.Last();

                markedNodes.Add((int)maxPost.Key);

                RemovalExploration(Graph.Find(v => v.Key == maxPost.Key));
            }

            return(markedNodes);
        }
Beispiel #8
0
        /// <summary>  General Rgraph parsing method (usually not used directly)
        /// This method is the entry point for the recursive search
        /// adapted to the atom container input.
        ///
        /// </summary>
        /// <param name="g1">               first molecule
        /// </param>
        /// <param name="g2">               second molecule
        /// </param>
        /// <param name="c1">               initial condition ( bonds from g1 that
        /// must be contains in the solution )
        /// </param>
        /// <param name="c2">               initial condition ( bonds from g2 that
        /// must be contains in the solution )
        /// </param>
        /// <param name="findAllStructure"> if false stop at the first structure found
        /// </param>
        /// <param name="findAllMap">       if true search all the 'mappings' for one same
        /// structure
        /// </param>
        /// <returns>                   a List of Lists of RMap objects that represent the search solutions
        /// </returns>
        public static System.Collections.IList search(IAtomContainer g1, IAtomContainer g2, System.Collections.BitArray c1, System.Collections.BitArray c2, bool findAllStructure, bool findAllMap)
        {
            // reset result
            System.Collections.ArrayList rMapsList = new System.Collections.ArrayList();

            // build the RGraph corresponding to this problem
            RGraph rGraph = buildRGraph(g1, g2);

            // parse the RGraph with the given constrains and options
            rGraph.parse(c1, c2, findAllStructure, findAllMap);
            System.Collections.IList solutionList = rGraph.Solutions;

            // convertions of RGraph's internal solutions to G1/G2 mappings
            //UPGRADE_TODO: Method 'java.util.Iterator.hasNext' was converted to 'System.Collections.IEnumerator.MoveNext' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratorhasNext'"
            for (System.Collections.IEnumerator i = solutionList.GetEnumerator(); i.MoveNext();)
            {
                //UPGRADE_TODO: Method 'java.util.Iterator.next' was converted to 'System.Collections.IEnumerator.Current' which has a different behavior. "ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?index='!DefaultContextWindowIndex'&keyword='jlca1073_javautilIteratornext'"
                System.Collections.BitArray set_Renamed = (System.Collections.BitArray)i.Current;
                rMapsList.Add(rGraph.bitSetToRMap(set_Renamed));
            }

            return(rMapsList);
        }
Beispiel #9
0
 public override void PostAddRecipes()
 {
     base.PostAddRecipes();
     Graph = new RGraph();
 }
Beispiel #10
0
        /// <summary>  Build edges of the RGraphs
        /// This method create the edge of the RGraph and
        /// calculates the incompatibility and neighbourhood
        /// relationships between RGraph nodes.
        ///
        /// </summary>
        /// <param name="gr">  the rGraph
        /// </param>
        /// <param name="ac1"> Description of the first molecule
        /// </param>
        /// <param name="ac2"> Description of the second molecule
        /// </param>
        private static void arcConstructor(RGraph gr, IAtomContainer ac1, IAtomContainer ac2)
        {
            // each node is incompatible with himself
            for (int i = 0; i < gr.Graph.Count; i++)
            {
                RNode x = (RNode)gr.Graph[i];
                SupportClass.BitArraySupport.Set(x.Forbidden, i);
            }

            IBond a1;
            IBond a2;
            IBond b1;
            IBond b2;

            IBond[] bondsA1 = ac1.Bonds;
            IBond[] bondsA2 = ac2.Bonds;

            gr.FirstGraphSize  = ac1.getBondCount();
            gr.SecondGraphSize = ac2.getBondCount();

            for (int i = 0; i < gr.Graph.Count; i++)
            {
                RNode x = (RNode)gr.Graph[i];

                // two nodes are neighbours if their adjacency
                // relationship in are equivalent in G1 and G2
                // else they are incompatible.
                for (int j = i + 1; j < gr.Graph.Count; j++)
                {
                    if (timeout > -1 && ((System.DateTime.Now.Ticks - 621355968000000000) / 10000 - start) > timeout)
                    {
                        throw new CDKException("Timeout exceeded in getOverlaps");
                    }
                    RNode y = (RNode)gr.Graph[j];

                    a1 = bondsA1[((RNode)gr.Graph[i]).RMap.Id1];
                    a2 = bondsA2[((RNode)gr.Graph[i]).RMap.Id2];
                    b1 = bondsA1[((RNode)gr.Graph[j]).RMap.Id1];
                    b2 = bondsA2[((RNode)gr.Graph[j]).RMap.Id2];

                    if (a2 is IQueryBond)
                    {
                        if (a1.Equals(b1) || a2.Equals(b2) || !queryAdjacency(a1, b1, a2, b2))
                        {
                            SupportClass.BitArraySupport.Set(x.Forbidden, j);
                            SupportClass.BitArraySupport.Set(y.Forbidden, i);
                        }
                        else if (hasCommonAtom(a1, b1))
                        {
                            SupportClass.BitArraySupport.Set(x.Extension, j);
                            SupportClass.BitArraySupport.Set(y.Extension, i);
                        }
                    }
                    else
                    {
                        if (a1.Equals(b1) || a2.Equals(b2) || (!getCommonSymbol(a1, b1).Equals(getCommonSymbol(a2, b2))))
                        {
                            SupportClass.BitArraySupport.Set(x.Forbidden, j);
                            SupportClass.BitArraySupport.Set(y.Forbidden, i);
                        }
                        else if (hasCommonAtom(a1, b1))
                        {
                            SupportClass.BitArraySupport.Set(x.Extension, j);
                            SupportClass.BitArraySupport.Set(y.Extension, i);
                        }
                    }
                }
            }
        }