Beispiel #1
0
        /// <summary>
        /// predicts how many seconds are likely to elapse before the next event takes place
        /// </summary>
        /// <returns>number of seconds before the next event</returns>
        public float PredictNextEventArrivalSec()
        {
            float next_event_arrival_sec = 0;

            if (prev_node != null)
            {
                ulong max_hits = 0;
                int   winner   = -1;
                for (int i = 0; i < prev_node.Links.Count; i++)
                {
                    UsageLink lnk = (UsageLink)prev_node.Links[i];
                    UsageNode n   = (UsageNode)lnk.From;
                    if (n.Hits > max_hits)
                    {
                        max_hits = n.Hits;
                        winner   = i;
                    }
                }
                if (winner > -1)
                {
                    UsageLink lnk            = (UsageLink)prev_node.Links[winner];
                    double    av_duration_mS = lnk.GetAverageDuration();
                    TimeSpan  diff           = DateTime.Now.Subtract(prev_node_time);
                    next_event_arrival_sec = (float)(diff.TotalMilliseconds - av_duration_mS) / 1000.0f;
                    if (next_event_arrival_sec < 0)
                    {
                        next_event_arrival_sec = 0;
                    }
                }
            }
            return(next_event_arrival_sec);
        }
Beispiel #2
0
        /// <summary>
        /// returns a list of the next likely events
        /// </summary>
        /// <param name="Description">description of each event</param>
        /// <param name="Probability">probability of each event</param>
        /// <param name="TimeToEventSec">number of seconds before each event is likely to occur</param>
        public void PredictNextEvents(
            ref List <string> Description,
            ref List <float> Probability,
            ref List <float> TimeToEventSec)
        {
            if (Description == null)
            {
                Description = new List <string>();
            }
            if (Probability == null)
            {
                Probability = new List <float>();
            }
            if (TimeToEventSec == null)
            {
                TimeToEventSec = new List <float>();
            }
            Description.Clear();
            Probability.Clear();
            TimeToEventSec.Clear();

            if (prev_node != null)
            {
                ulong total_hits = 0;
                for (int i = 0; i < prev_node.Links.Count; i++)
                {
                    UsageLink lnk = (UsageLink)prev_node.Links[i];
                    UsageNode n   = (UsageNode)lnk.From;
                    total_hits += n.Hits;
                }
                for (int i = 0; i < prev_node.Links.Count; i++)
                {
                    UsageLink lnk = (UsageLink)prev_node.Links[i];
                    UsageNode n   = (UsageNode)lnk.From;
                    Description.Add(n.Name);
                    Probability.Add((float)((double)n.Hits / (double)total_hits));

                    double   av_duration_mS = lnk.GetAverageDuration();
                    TimeSpan diff           = DateTime.Now.Subtract(prev_node_time);
                    float    diff_sec       = (float)(diff.TotalMilliseconds - av_duration_mS) / 1000.0f;
                    if (diff_sec < 0)
                    {
                        diff_sec = 0;
                    }
                    TimeToEventSec.Add(diff_sec);
                }
            }
        }
Beispiel #3
0
        /// <summary>
        /// returns a graph containing events separated by a time less than the given interval
        /// </summary>
        /// <param name="maximum_temoral_separation_sec">maximum time between events</param>
        /// <returns>graph object</returns>
        public UsageGraph GetCommonSequences(float maximum_temoral_separation_sec)
        {
            double maximum_temoral_separation_mS = maximum_temoral_separation_sec * 1000;

            UsageGraph graph = new UsageGraph();

            for (int i = 0; i < Nodes.Count; i++)
            {
                UsageNode n = (UsageNode)Nodes[i];
                for (int j = 0; j < n.Links.Count; j++)
                {
                    UsageLink lnk         = (UsageLink)n.Links[j];
                    double    duration_mS = lnk.GetAverageDuration();
                    if (duration_mS < maximum_temoral_separation_mS)
                    {
                        graph.Update(n.Name);
                        graph.Update(lnk.From.Name);
                        graph.Reset();
                    }
                }
            }

            return(graph);
        }
Beispiel #4
0
        /// <summary>
        /// export the graph in dot format
        /// </summary>
        /// <param name="filename">filename to save as</param>
        /// <param name="invert">inverts the direction of links</param>
        /// <param name="show_weight">show durations between events in seconds</param>
        public override void ExportAsDot(
            string filename,
            bool invert,
            bool show_weight)
        {
            StreamWriter oWrite          = null;
            bool         allowWrite      = true;
            int          max_edge_weight = 10;

            try
            {
                oWrite = File.CreateText(filename);
            }
            catch
            {
                allowWrite = false;
            }

            if (allowWrite)
            {
                ulong total_hits = 0;
                for (int i = 0; i < Nodes.Count; i++)
                {
                    UsageNode n = (UsageNode)Nodes[i];
                    total_hits += n.Hits;
                }

                // compute durations, and find the longest duration
                double        max_duration_mS = 0;
                List <double> durations       = new List <double>();
                for (int i = 0; i < Nodes.Count; i++)
                {
                    for (int j = 0; j < Nodes[i].Links.Count; j++)
                    {
                        UsageLink lnk         = (UsageLink)Nodes[i].Links[j];
                        double    duration_mS = lnk.GetAverageDuration();
                        durations.Add(duration_mS);
                        if (duration_mS > max_duration_mS)
                        {
                            max_duration_mS = duration_mS;
                        }
                    }
                }

                int    ctr = 0;
                string str;
                string duration_str = "";
                oWrite.WriteLine("digraph G {");
                for (int i = 0; i < Nodes.Count; i++)
                {
                    UsageNode node = (UsageNode)Nodes[i];
                    if (node.Symbol != SYMBOL_ELLIPSE)
                    {
                        str  = "    " + ReplaceSpaces(node.Name);
                        str += GetHitsSuffix(node.Hits, total_hits);
                        str += " [shape=";
                        int symbol = (int)(node.Symbol);
                        switch (symbol)
                        {
                        case SYMBOL_SQUARE:
                        {
                            str += "square";
                            break;
                        }
                        }
                        str += "];";
                        oWrite.WriteLine(str);
                    }

                    for (int j = 0; j < Nodes[i].Links.Count; j++, ctr++)
                    {
                        UsageLink lnk       = (UsageLink)Nodes[i].Links[j];
                        UsageNode from_node = (UsageNode)(lnk.From);
                        UsageNode to_node   = (UsageNode)Nodes[i];

                        if (invert)
                        {
                            to_node   = (UsageNode)(Nodes[i].Links[j].From);
                            from_node = (UsageNode)Nodes[i];
                        }

                        double duration_mS = 0;
                        if (show_weight)
                        {
                            if (ctr < durations.Count)
                            {
                                duration_mS = durations[ctr];
                                double duration_sec = duration_mS / 1000.0;
                                if (show_milliseconds)
                                {
                                    duration_str = ((int)duration_mS).ToString();
                                }
                                else
                                {
                                    duration_str = ((int)(duration_sec * 10) / 10.0f).ToString();
                                }
                            }
                        }

                        str = "    " +
                              ReplaceSpaces(from_node.Name) + GetHitsSuffix(from_node.Hits, total_hits) +
                              " -> " +
                              ReplaceSpaces(to_node.Name) + GetHitsSuffix(to_node.Hits, total_hits);

                        if (show_weight)
                        {
                            if (duration_str != "0")  // don't bother showing zero values
                            {
                                str += " [label=" + '"' + duration_str + '"';
                                str += ",weight=" + ((int)(duration_mS * max_edge_weight / max_duration_mS)).ToString() + "]";
                            }
                        }

                        str += ";";

                        oWrite.WriteLine(str);
                    }
                }
                oWrite.WriteLine("}");
                oWrite.Close();
            }
        }