用c#处理事件列表
本文关键字:列表 处理事件 | 更新日期: 2023-09-27 18:01:36
我正在c#中实现一个状态机,我遇到了一个问题。我有一个事件日志,包含事件和时间戳。当处理这个时,我得到以下格式的输出:转换时间戳,State transitions to, Time in State
我在这里遇到的麻烦是,我不知道状态的时间,直到我运行eventlog,它找到一个有效的过渡。在我的实现中,我利用一些函数来处理事件的触发器,我返回一个包含一些数据(过渡时间和新状态)的字符串的元组,但因为我不知道这个状态的时间是什么,我还不能正确地写它。在处理完事件集之后(有多种方法来启动它,步进函数,运行到断点函数),元组中的字符串被写入一个列表,当整个事件日志被处理时,该列表被写入一个文件。
不要看元组的扩展使用太多,这是因为我正在测试一些东西,那部分仍然可以优化,我只是在寻找一些好主意来解决我的问题与状态部分的时间。
触发一个事件的函数:
/// <summary>
/// Trigger an event in the event log on the state machine
/// </summary>
/// <param name="ev">Event to be triggered</param>
/// <param name="oldDate">Initial date</param>
/// <param name="oldMs">Initial milliseconds</param>
/// <returns>Tuple containing output string for event log, new date and new milliseconds belonging to the date</returns>
public Tuple<Tuple<string, string, string>, DateTime, int> TriggerEvent(Event ev, DateTime oldDate, int oldMs)
{
string outputString;
int newMs = oldMs;
DateTime newDate = oldDate;
Tuple<string, string, string> outPut;
if (this.Fire(ev.Trigger))
{
int outputMs = ev.Milliseconds - oldMs;
TimeSpan outputTs = ev.Timestamp - oldDate;
if (outputMs < 0)
{
outputMs = Math.Abs(outputMs);
outputTs = outputTs.Add(TimeSpan.FromSeconds(-1));
}
string firstPart = ev.Timestamp + "." + ev.Milliseconds.ToString();
string secondPart = this.fsm.State;
string thirdPart = outputTs + "." + outputMs.ToString().PadRight(6, '0');
outputString = firstPart + "," + secondPart;// +"," + thirdPart;
newDate = ev.Timestamp;
newMs = ev.Milliseconds;
this.activeState = this.GetStateByTag(this.fsm.State);
outPut = new Tuple<string, string, string>(firstPart, secondPart, thirdPart);
}
else
{
outputString = string.Empty;
outPut = new Tuple<string, string, string>(string.Empty, string.Empty, string.Empty);
}
this.lastEvent = ev;
return new Tuple<Tuple<string, string, string>, DateTime, int>(outPut, newDate, newMs);
}
处理事件列表的函数:
/// <summary>
/// Process an event log
/// </summary>
/// <param name="list">Event list to process</param>
/// <returns>List containing the output for storage</returns>
public Tuple<List<string>, Event> ProcessEventSet(List<Event> list)
{
List<string> tmpObj = new List<string>();
Tuple<string, string, string> outPut;
if (this.initialDate == DateTime.MinValue)
{
this.initialDate = list.First().Timestamp;
this.initialMs = list.First().Milliseconds;
}
foreach (Event ev in list)
{
Tuple<Tuple<string, string, string>, DateTime, int> newData = this.TriggerEvent(ev, this.initialDate, this.initialMs);
outPut = newData.Item1;
if (!string.IsNullOrEmpty(newData.Item1.Item1))
{
string output;
if (outPut.Item3 == "00:00:00.000000")
{
output = outPut.Item1 + "," + outPut.Item2;
}
else
{
output = outPut.Item1 + "," + outPut.Item2 + "," + outPut.Item3;
}
tmpObj.Add(output);
this.initialDate = newData.Item2;
this.initialMs = newData.Item3;
}
}
return new Tuple<List<string>, Event>(tmpObj, list.Last());
}
输出:
7-1-2013 0:00:06.193133,State B,00:00:00.000000
7-1-2013 0:00:06.227664,State C,00:00:00.345310
7-1-2013 0:00:07.391359,State D,00:00:01.163695
7-1-2013 0:06:22.693034,State A,00:06:15.301675
7-1-2013 1:10:43.770820,State B,01:04:21.777860
7-1-2013 1:10:43.808832,State A,00:00:00.380120
7-1-2013 4:59:16.704133,State B,03:48:32.104699
7-1-2013 4:59:16.742639,State C,00:00:00.385060
7-1-2013 5:01:57.975030,State A,00:02:41.232391
7-1-2013 6:50:03.577993,State B,01:48:05.397037
7-1-2013 6:50:03.613139,State C,00:00:00.351460
7-1-2013 6:50:03.680799,State D,00:00:00.676600
7-1-2013 6:51:10.399170,State A,00:01:06.281629
7-1-2013 8:51:02.344653,State B,01:59:51.545170
7-1-2013 8:51:02.383053,State A,00:00:00.384000
7-1-2013 10:11:46.822542,State B,01:20:44.439489
7-1-2013 10:11:46.871144,State A,00:00:00.486020
7-1-2013 10:22:29.117037,State B,00:10:42.754107
7-1-2013 10:22:29.153400,State A,00:00:00.363630
7-1-2013 10:39:08.495431,State B,00:16:39.342031
7-1-2013 10:39:08.537063,State A,00:00:00.416320
7-1-2013 11:29:54.932916,State B,00:50:46.395853
7-1-2013 11:29:54.971428,State A,00:00:00.385120
输出:
7-1-2013 0:00:06.193133,State B,00:00:00.345310
7-1-2013 0:00:06.227664,State C,00:00:01.163695
7-1-2013 0:00:07.391359,State D,00:06:15.301675
7-1-2013 0:06:22.693034,State A,01:04:21.777860
7-1-2013 1:10:43.770820,State B,00:00:00.380120
7-1-2013 1:10:43.808832,State A,03:48:32.104699
7-1-2013 4:59:16.704133,State B,00:00:00.385060
7-1-2013 4:59:16.742639,State C,00:02:41.232391
7-1-2013 5:01:57.975030,State A,01:48:05.397037
7-1-2013 6:50:03.577993,State B,00:00:00.351460
7-1-2013 6:50:03.613139,State C,00:00:00.676600
7-1-2013 6:50:03.680799,State D,00:01:06.281629
7-1-2013 6:51:10.399170,State A,01:59:51.545170
7-1-2013 8:51:02.344653,State B,00:00:00.384000
7-1-2013 8:51:02.383053,State A,01:20:44.439489
7-1-2013 10:11:46.822542,State B,00:00:00.486020
7-1-2013 10:11:46.871144,State A,00:10:42.754107
7-1-2013 10:22:29.117037,State B,00:00:00.363630
7-1-2013 10:22:29.153400,State A,00:16:39.342031
7-1-2013 10:39:08.495431,State B,00:00:00.416320
7-1-2013 10:39:08.537063,State A,00:50:46.395853
7-1-2013 11:29:54.932916,State B,00:00:00.385120
7-1-2013 11:29:54.971428,State A
我通过后处理输出来解决这个问题,而不是每次迭代都这样做。在每次迭代中这样做的逻辑似乎很难实现。