Exemple #1
0
            private void initTables()
            {
                NSSTable = new Dictionary<uint, uint>();
                IsDupTable = new List<int>((int)MAXVID);

                string[] accNames = {"VID", "Bal", "Time"};
                Type[] accTypes = { typeof(Int32), typeof(Int32), typeof(Int32) };
                int[] accIndexes = { 0 };
                accountBalances = new RelationTable(accNames, accTypes, accIndexes);

                DataRow drow;
                for (uint i = 0; i < MAXVID; i++)
                {
                    drow = accountBalances.GetRow();
                    drow["VID"] = i;
                    drow["Bal"] = 0;
                    drow["Time"] = 0;
                    accountBalances.Add(drow);
                }

                string[] acdntNames = { "Xway", "Dir", "Seg" };
                Type[] acdntTypes = { typeof(Int32), typeof(Int32), typeof(Int32) };
                int[] acdntIndexes = { 0, 1, 2};
                accidents = new RelationTable(acdntNames, acdntTypes, acdntIndexes);

                string[] tqNames = { "VID", "Seg", "LAV", "Toll" };
                Type[] tqTypes = { typeof(Int32), typeof(Int32), typeof(Int32), typeof(Int32) };
                int[] tqIndexes = { 0, 1 };
                tollQuotes = new RelationTable(tqNames, tqTypes, tqIndexes);

                string[] ssNames = { "Seg", "Xway", "Dir", "LAV", "Count", "LAVWID", "CountWID" };
                Type[] ssTypes = { typeof(Int32), typeof(Int32), typeof(Int32), typeof(Int32), typeof(Int32), typeof(UInt64), typeof(UInt64) };
                int[] ssIndexes = { 0, 1, 2 };
                segStats = new RelationTable(ssNames, ssTypes, ssIndexes);
            }
Exemple #2
0
 public DataItem Type2Relation(DataItem di, RelationTable rt)
 {
     //<Time, VID, QID>
     if (!(di is Punctuation))
     {
         object[] keys = { di[1] }; //VID
         int bal, time;
         di.AddCapacity(2);
         lock (rt)
         {
             DataRow drow = rt.Find(keys);
             if (drow == null)
             {
                 drow = rt.GetRow();
                 drow["VID"] = Convert.ToInt32(di[1]);
                 drow["Bal"] = 0;
                 drow["Time"] = DateTime.Now.Subtract(di.TimeStamp).TotalSeconds;
                 rt.Add(drow);
                 //Console.WriteLine("We had to make a new balance for a type 2 query");
             }
             bal = drow["Bal"];
             time = drow["Time"];
         }
         di.AddValue(bal);
         di.AddValue(time);
         return di;
     }
     else
         return di;
     //<Time, VID, QID, Bal, UTime>
 }
Exemple #3
0
 public DataItem Type3Relation(DataItem di, RelationTable rt)
 {
     //<Time, VID, Xway, QID, Day, InTime(DateTime)>
     di.AddCapacity(1);
     object[] keys = { di[1] };
     lock (rt)
     {
         DataRow drow = rt.Find(keys);
         if (drow == null)
         {
             drow = rt.GetRow();
             drow["VID"] = Convert.ToInt32(di[1]);
             drow["Bal"] = 0;
             drow["Time"] = DateTime.Now.Subtract(di.TimeStamp).TotalSeconds;
             rt.Add(drow);
             //Console.WriteLine("We had to make a new balance for a type 3 query");
         }
         di.AddValue(drow["Bal"]);
     }
     return di;
     //<Time, VID, Xway, QID, Day, Bal>
 }
Exemple #4
0
            public DataItem QuoteTolls(DataItem di, RelationTable rt)
            {
                //<Time, VID, Xway, Lane, Dir, Seg, ?isNSS, ?isAIR, LAV, Count>
                if (!(di is Punctuation))
                {
                    uint toll,
                        LAV = Convert.ToUInt32(di[8]),
                        Count = Convert.ToUInt32(di[9]);
                    int isAccident = Convert.ToInt32(di[7]);

                    if (isAccident != -1 || Count <= 50 || LAV >= 40)
                        toll = 0;
                    else
                    {
                        toll = 2 * (uint)Math.Pow((Count - 50), 2.0);
                        //Console.WriteLine("Toll Quote: {0}, Count: {1}, LAV: {2}", toll, Count, LAV);
                        lock (rt)
                        {
                            DataRow drow = rt.GetRow();
                            drow["VID"] = di[1];
                            drow["Seg"] = di[5];
                            drow["LAV"] = LAV;
                            drow["Toll"] = toll;
                            rt.Add(drow);
                        }
                    }
                }
                //<Time, VID, Xway, Lane, Dir, Seg, ?isNSS, ?isAIR, LAV, Count>
                return di;
            }
Exemple #5
0
 public DataItem SStatsLAVInsertion(DataItem di, RelationTable rt)
 {
     if (!(di is Punctuation))
     {
         object[] keys = { di[2], di[0], di[1] }; //Seg, Xway, Dir
         lock (rt)
         {
             DataRow drow = rt.Find(keys);
             if (drow != null)
             {
                 drow["LAV"] = di[4];
                 drow["LAVWID"] = di[3];
             }
             else
             {
                 drow = rt.GetRow();
                 drow["Seg"] = di[2];
                 drow["Xway"] = di[0];
                 drow["Dir"] = di[1];
                 drow["LAV"] = di[4];
                 drow["LAVWID"] = di[3];
                 drow["Count"] = -1; //we have no idea what it might be
                 drow["CountWID"] = di[3];
                 rt.Add(drow);
             }
         }
     }
     else //we need to check every segment
     {
         Punctuation p = di as Punctuation;
         ulong WID = (ulong)((Punctuation.LiteralPattern)p[3]).Value;
         for (int s = 0; s < SEGMENTS; s++)
         {
             for (int x = 0; x < EXPRESSWAYS; x++)
             {
                 for (int d = 0; d < DIRECTIONS; d++)
                 {
                     object[] keys = { s, x, d };
                     lock (rt)
                     {
                         DataRow drow = rt.Find(keys);
                         if (drow != null)
                         {
                             if ((ulong)drow["LAVWID"] < WID)
                             {
                                 drow["LAV"] = -1;
                                 drow["LAVWID"] = WID;
                             }
                         }
                         else
                         {
                             drow = rt.GetRow();
                             drow["Seg"] = s;
                             drow["Xway"] = x;
                             drow["Dir"] = d;
                             drow["LAV"] = -1;
                             drow["LAVWID"] = 0;
                             drow["Count"] = -1;
                             drow["CountWID"] = 0;
                             rt.Add(drow);
                         }
                     }
                 }
             }
         }
     }
     return di;
 }
Exemple #6
0
 public DataItem FindToll(DataItem di, RelationTable rt)
 {
     //<Time, VID, Xway, Lane, Dir, Seg, ?isNSS>
     di.AddCapacity(2);
     if (di is Punctuation)
     {
         Punctuation p = di as Punctuation;
         p.AddValue(new Punctuation.WildcardPattern());
         p.AddValue(new Punctuation.WildcardPattern());
         return p;
     }
     else
     {
         uint seg = Convert.ToUInt32(di[5]);
         if (Convert.ToUInt32(di[4]) == 0) //find the seg before this one
             seg--;
         else
             seg++;
         object[] keys = { di[1], seg }; //VID
         lock (rt)
         {
             DataRow drow = rt.Find(keys);
             if (drow == null) //there's no toll, we can drop it
             {
                 di.AddValue((int)0);
                 di.AddValue((int)0);
             }
             else
             {
                 di.AddValue(drow["Toll"]);
                 di.AddValue(drow["LAV"]);
                 rt.Drop(drow);
                 //Console.WriteLine("We found a toll");
             }
         }
         return di;
     }
     //<Time, VID, Xway, Lane, Dir, Seg, ?isNSS, Toll, LAV>
 }
Exemple #7
0
 public DataItem ChargeToll(DataItem di, RelationTable rt)
 {
     //<Time, VID, Xway, Lane, Dir, Seg, ?isNSS, Toll, LAV>
     if (!(di is Punctuation))
     {
         object[] keys = { di[1] }; //VID
         lock (rt)
         {
             DataRow drow = rt.Find(keys);
             if (drow == null)
             {
                 drow = rt.GetRow();
                 drow["Vid"] = di[1];
                 drow["Bal"] = di[7];
                 drow["Time"] = di[0];
                 rt.Add(drow);
             }
             else
             {
                 drow["Bal"] = Convert.ToUInt32(di[7]) + (uint)drow["Bal"];
                 drow["Time"] = di[0];
             }
         }
     }
     return di;
     //<Time, VID, Xway, Lane, Dir, Seg, ?isNSS, Toll, LAV>
 }
Exemple #8
0
 public DataItem AttachStats(DataItem di, RelationTable rt)
 {
     //<Time, VID, Xway, Lane, Dir, Seg, InTime(DateTime), ?isNSS, isAIR>
     di.AddCapacity(2);
     if (!(di is Punctuation))
     {
         try
         {
             object[] keys = { di[5], di[2], di[4] }; //Seg, Xway, Dir
             lock (rt)
             {
                 DataRow drow = rt.Find(keys);
                 if (drow == null)
                 {
                     di.AddValue((uint)0);
                     di.AddValue((uint)0);
                 }
                 else
                 {
                     di.AddValue(drow["LAV"]);
                     di.AddValue(drow["Count"]);
                 }
             }
         }
         catch (Exception ex)
         {
             Console.WriteLine(ex.Message);
         }
     }
     return di;
     //<Time, VID, Xway, Lane, Dir, Seg, InTime(DateTime), ?isNSS, ?isAIR, LAV, Count>
 }
Exemple #9
0
            public DataItem AccidentInRange(DataItem di, RelationTable rt)
            {
                //<Time, VID, Xway, Lane, Dir, Seg, ?isNSS>
                di.AddCapacity(1);
                if (di is Punctuation)
                {
                    Punctuation p = di as Punctuation;
                    p.AddValue(new Punctuation.WildcardPattern());
                    return p;
                }
                else
                {
                    int Xway = (int)di[2],
                        dir = (int)di[4],
                        seg = (int)di[5];
                    lock (rt)
                    {
                        DataRow[] drows = rt.Find("[Xway] = " + Xway + " AND [Dir] = " + dir);
                        if (drows.Length > 0)
                        {
                            int accseg = (int)drows[0]["Seg"],
                                lbound, hbound;
                            if (dir == 0)//segs are increasing
                            {
                                lbound = accseg - 4;
                                hbound = accseg;
                            }
                            else
                            {
                                lbound = accseg;
                                hbound = accseg + 4;
                            }

                            if (seg <= hbound && seg >= lbound) //between the bounds
                                di.AddValue(accseg);
                            else
                                di.AddValue((int)-1);
                        }
                        else
                            di.AddValue((int)-1);
                    }
                    return di;
                }
                //<Time, VID, Xway, Lane, Dir, Seg, ?isNSS, isAIR>
            }
Exemple #10
0
            public DataItem AccidentDetection(DataItem di, RelationTable rt)
            {
                if (!(di is Punctuation))
                {
                    uint count = Convert.ToUInt32(di[6]);

                    if (count == 4)
                    {
                        object[] keys = { di[1], di[2], di[3] }; //Xway, Dir, Seg
                        lock (rt)
                        {
                            DataRow drow = rt.Find(keys);
                            if (drow == null)
                            {
                                drow = rt.GetRow();
                                drow["Xway"] = di[1];
                                drow["Dir"] = di[2];
                                drow["Seg"] = di[3];
                                rt.Add(drow);
                            }
                            //Console.WriteLine("Accidents: {0} - From Insertion", rt.Rows);
                        }
                    }
                    else //4 > count > 1
                    {
                        object[] keys = { di[1], di[2], di[3] }; //Xway, Dir, Seg
                        lock (rt)
                        {
                            DataRow drow = rt.Find(keys); //assumes that there will only be one accident per Xway/dir
                            if (drow != null)
                                rt.Drop(drow);
                            //Console.WriteLine("Accidents: {0} - From Deletion", rt.Rows);
                        }
                    }
                }
                return di;
            }