2

我正在用 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
4

1 回答 1

0

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

于 2013-03-26T08:38:08.500 回答