Exemplo n.º 1
0
        public void Update(string Description, int symbol)
        {
            if (enabled)
            {
                if (log_file != "")
                {
                    LogToFile(Description);
                }

                string ClassName  = "";
                string MethodName = "";
                ParseDescription(
                    ref Description,
                    ref ClassName,
                    ref MethodName);

                UsageNode n = null;
                int       ID;
                int       pos = EventDescription.IndexOf(Description);
                if (pos > -1)
                {
                    ID     = EventID[pos];
                    n      = (UsageNode)GetNode(ID);
                    symbol = EventSymbol[pos];
                }
                else
                {
                    ID = EventID.Count;
                    EventDescription.Add(Description);
                    EventID.Add(ID);
                    EventSymbol.Add(symbol);
                    n = new UsageNode(Description, ID, symbol, ClassName, MethodName);
                    Add(n);
                }

                // increment hit score for this node
                n.Hits++;
                if (n.Hits > 5000)
                {
                    RenumberHits();
                }

                // link nodes together
                if (prev_node != null)
                {
                    LinkByID(n.ID, prev_node.ID);

                    // set the duration between events
                    UsageLink lnk = (UsageLink)prev_node.GetLink(n.ID);
                    if (lnk != null)
                    {
                        TimeSpan time_elapsed = DateTime.Now.Subtract(prev_node_time);
                        lnk.SetDuration(time_elapsed.TotalMilliseconds);
                    }
                }

                prev_node      = n;
                prev_node_time = DateTime.Now;
            }
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        public static UsageNode Load(BinaryReader br)
        {
            // read the node data
            int       ID          = br.ReadInt32();
            string    Description = br.ReadString();
            ulong     Hits        = br.ReadUInt64();
            int       Symbol      = br.ReadByte();
            string    ClassName   = br.ReadString();
            string    MethodName  = br.ReadString();
            UsageNode n           = new UsageNode(Description, ID, Symbol, ClassName, MethodName);

            n.Hits = Hits;

            // read the link IDs
            n.temp_link_IDs = new List <int>();
            n.temp_duration = new List <List <double> >();
            int no_of_links = br.ReadInt32();

            for (int i = 0; i < no_of_links; i++)
            {
                n.temp_link_IDs.Add(br.ReadInt32());

                List <double> duration = new List <double>();
                int           no_of_duration_values = br.ReadInt32();
                for (int j = 0; j < no_of_duration_values; j++)
                {
                    duration.Add(br.ReadDouble());
                }
                n.temp_duration.Add(duration);
            }

            return(n);
        }
Exemplo n.º 4
0
 /// <summary>
 /// renumbers hit scores to prevent overflows
 /// </summary>
 protected void RenumberHits()
 {
     for (int i = 0; i < Nodes.Count; i++)
     {
         UsageNode n = (UsageNode)Nodes[i];
         n.Hits /= 2;
     }
 }
Exemplo n.º 5
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);
                }
            }
        }
Exemplo n.º 6
0
        public static UsageGraph Load(string filename)
        {
            UsageGraph graph = new UsageGraph();

            if (File.Exists(filename))
            {
                FileStream fs = File.Open(filename, FileMode.Open);

                BinaryReader br = new BinaryReader(fs);

                // read event types
                int no_of_events = br.ReadInt32();
                for (int i = 0; i < no_of_events; i++)
                {
                    graph.EventID.Add(br.ReadInt32());
                    graph.EventDescription.Add(br.ReadString());
                    graph.EventSymbol.Add(br.ReadInt32());
                }

                // load nodes
                int no_of_nodes = br.ReadInt32();
                for (int i = 0; i < no_of_nodes; i++)
                {
                    graph.Add(UsageNode.Load(br));
                }

                br.Close();
                fs.Close();

                // link nodes together
                for (int i = 0; i < graph.Nodes.Count; i++)
                {
                    UsageNode n = (UsageNode)graph.Nodes[i];
                    if (n.temp_link_IDs != null)
                    {
                        for (int j = 0; j < n.temp_link_IDs.Count; j++)
                        {
                            graph.LinkByID(n.temp_link_IDs[j], n.ID);
                            UsageLink lnk = (UsageLink)(n.Links[n.Links.Count - 1]);
                            lnk.Load(n.temp_duration[j]);
                        }
                    }
                }
            }

            return(graph);
        }
Exemplo n.º 7
0
        /// <summary>
        /// predicts the even most likely to happen next
        /// </summary>
        /// <returns></returns>
        public string PredictNextEvent()
        {
            string next_event_description = "";

            if (prev_node != null)
            {
                ulong max_hits = 0;
                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;
                        next_event_description = n.Name;
                    }
                }
            }
            return(next_event_description);
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
0
     public void Update(string Description, int symbol)
     {
         if (enabled)
         {
             if (log_file != "") LogToFile(Description);
             
             string ClassName = "";
             string MethodName = "";
             ParseDescription(
                 ref Description, 
                 ref ClassName, 
                 ref MethodName);
         
             UsageNode n = null;
             int ID;
             int pos = EventDescription.IndexOf(Description);
             if (pos > -1)
             {
                 ID = EventID[pos];
                 n = (UsageNode)GetNode(ID);
                 symbol = EventSymbol[pos];
             }
             else
             {
                 ID = EventID.Count;
                 EventDescription.Add(Description);
                 EventID.Add(ID);
                 EventSymbol.Add(symbol);
                 n = new UsageNode(Description, ID, symbol, ClassName, MethodName);
                 Add(n);
             }
 
             // increment hit score for this node
             n.Hits++;
             if (n.Hits > 5000)
                 RenumberHits();
 
             // link nodes together
             if (prev_node != null)
             {
                 LinkByID(n.ID, prev_node.ID);
 
                 // set the duration between events
                 UsageLink lnk = (UsageLink)prev_node.GetLink(n.ID);
                 if (lnk != null)
                 {
                     TimeSpan time_elapsed = DateTime.Now.Subtract(prev_node_time);
                     lnk.SetDuration(time_elapsed.TotalMilliseconds);
                 }
             }
 
             prev_node = n;
             prev_node_time = DateTime.Now;
         }
     }
Exemplo n.º 10
0
 public void Reset()
 {
     prev_node = null;
 }
Exemplo n.º 11
0
        public static UsageNode Load(BinaryReader br)
        {
            // read the node data
            int ID = br.ReadInt32();
            string Description = br.ReadString();
            ulong Hits = br.ReadUInt64();
            int Symbol = br.ReadByte();
            string ClassName = br.ReadString();
            string MethodName = br.ReadString();
            UsageNode n = new UsageNode(Description, ID, Symbol, ClassName, MethodName);
            n.Hits = Hits;

            // read the link IDs
            n.temp_link_IDs = new List<int>();
            n.temp_duration = new List<List<double>>();
            int no_of_links = br.ReadInt32();
            for (int i = 0; i < no_of_links; i++)
            {
                n.temp_link_IDs.Add(br.ReadInt32());

                List<double> duration = new List<double>();
                int no_of_duration_values = br.ReadInt32();
                for (int j = 0; j < no_of_duration_values; j++)
                    duration.Add(br.ReadDouble());
                n.temp_duration.Add(duration);
            }

            return (n);
        }
Exemplo n.º 12
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();
            }
        }
Exemplo n.º 13
0
 public void Reset()
 {
     prev_node = null;
 }