gpt4 book ai didi

c# - 在 C# 中处理事件列表

转载 作者:太空宇宙 更新时间:2023-11-03 16:17:56 25 4
gpt4 key购买 nike

我正在用 C# 实现状态机,但遇到了问题。我有一个事件日志,其中包含事件和时间戳。处理这个时,我得到以下格式的输出:转换的时间戳,状态转换到,状态时间

我在这里遇到的问题是,在我运行事件日志并找到有效转换之前,我还不知道状态时间。在我的实现中,我使用一些函数来处理事件的触发,并返回一个包含带有一些数据(转换时间和新状态)的字符串的元组,但因为我还不知道处于这种状态的时间是多少我还不能正确地写它。事件集处理后(多种启动方式、步进函数、运行到断点函数)元组中的字符串被写入列表,当整个事件日志被处理时,该列表被写入文件。

不要过多关注元组的扩展使用,这是因为我正在测试一些东西,那部分仍然可以优化,我只是在寻找一些好主意来解决状态部分的时间问题。

触发一个事件的函数:

/// <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

最佳答案

我已经通过对输出进行后处理而不是每次迭代都进行处理来解决这个问题。在每次迭代中执行此操作的逻辑似乎很难实现。

关于c# - 在 C# 中处理事件列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15132917/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com