C#でステートマシンを実装していますが、問題が発生しています。イベントとタイムスタンプを含むイベントログがあります。これを処理すると、次の形式で出力が得られます:遷移のタイムスタンプ、遷移した状態、状態の時間
ここでの問題は、イベントログを実行して有効な遷移が見つかるまで、状態の時刻がまだわからないことです。私の実装では、イベントのトリガーを処理するためにいくつかの関数を使用し、いくつかのデータ(遷移時間と新しい状態)を含む文字列を含むタプルを返しますが、この状態の時間がまだわからないためですまだちゃんと書けません。イベントセットの処理後(これを開始する複数の方法、ステップ関数、ブレークポイント関数まで実行)、タプル内の文字列がリストに書き込まれ、イベントログ全体が処理されるときにファイルに書き込まれます。
タプルの拡張使用をあまり見ないでください。これは、いくつかのものをテストしていたためです。その部分はまだ最適化できます。状態部分の時間に関する問題を解決するための良いアイデアを探しています。
1つのイベントをトリガーする関数:
/// <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