From 2f13e828c3bf6d522802cf7608735f99f4a9e261 Mon Sep 17 00:00:00 2001 From: "zaccaria.majid" Date: Thu, 27 Jul 2023 15:29:10 +0200 Subject: [PATCH] gestione macchina a stati --- SMGen/Pages/SMEvent2State.razor.cs | 74 ++++++++++++++++++++---------- SMGen/appsettings.json | 3 +- 2 files changed, 52 insertions(+), 25 deletions(-) diff --git a/SMGen/Pages/SMEvent2State.razor.cs b/SMGen/Pages/SMEvent2State.razor.cs index 3c3ccff..1feabae 100644 --- a/SMGen/Pages/SMEvent2State.razor.cs +++ b/SMGen/Pages/SMEvent2State.razor.cs @@ -52,9 +52,16 @@ namespace SMGen.Pages { get => conf.GetValue("SetupOptions:DoCalcItself"); } + protected bool calcEmptyState + { + get => conf.GetValue("SetupOptions:DoCalcEmptyState"); + } protected List Rules { get; set; } = new List(); - protected Dictionary States { get; set; } = new Dictionary(); + protected Dictionary StatesAll { get; set; } = new Dictionary(); + protected Dictionary StatesAll2 { get; set; } = new Dictionary(); + protected List States2Rules { get; set; } = new List(); + //protected string sz_file2Read { get; set; } = "D:\\Repo\\Mapo-IOB\\StateMachine\\_DEFAULT\\60_STD_MACHINE.rul"; protected string sz_file2Read { get; set; } = ""; @@ -92,7 +99,7 @@ namespace SMGen.Pages using (StreamReader sr = new StreamReader(sz_file2Read)) { - States.Clear(); + StatesAll.Clear(); Events_to_send.Clear(); Rules.Clear(); string line = ""; @@ -116,13 +123,15 @@ namespace SMGen.Pages case "$IDX": n_state_machine_index = sz_tokens[1].Trim(); break; +#if false case "$N_STATES": n_states = int.Parse(sz_tokens[1].Trim()); - break; + break; +#endif case "$STATE": - States.Add(sz_tokens[2].Trim().ToUpper(), sz_tokens[1].Trim().ToUpper()); + StatesAll.Add(sz_tokens[2].Trim().ToUpper(), int.Parse(sz_tokens[1].Trim().ToUpper())); break; case "$EVENT": @@ -152,14 +161,19 @@ namespace SMGen.Pages default: if (b_rules_definition) { + var state = sz_temp.Trim().ToUpper(); + var event_to_send = sz_tokens[1].Trim().ToUpper(); + var next_state = sz_tokens[2].Trim().ToUpper(); + var temp_rule = new RuleClass() { - state = sz_temp.Trim().ToUpper(), - event_to_send = sz_tokens[1].Trim().ToUpper(), - next_state = sz_tokens[2].Trim().ToUpper() + state = state, + event_to_send = event_to_send, + next_state = next_state }; - Rules.Add(temp_rule); + States2Rules.Add(next_state); + } break; } @@ -177,55 +191,67 @@ namespace SMGen.Pages using (StreamWriter sw = new StreamWriter(sz_file2Write)) { sw.WriteLine("IdxFamiglia;IdxStato;IdxTipo;next_IdxStato"); - string sz_actual_state = ""; + int sz_actual_state = 0; string sz_line = ""; + foreach (var item in States2Rules) + { + StatesAll2.Add(item, StatesAll[item]); + StatesAll2 = StatesAll2.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value); + } + + //ciclo negli stati try { - foreach(var item in States) + foreach (var item in StatesAll2) { sz_actual_state = item.Value; foreach (var act_rule in Rules) { var exitFor = false; - if ((act_rule.state == "ALL_STATES") || (act_rule.state == sz_actual_state)) + if ((act_rule.state == "ALL_STATES") || (act_rule.state == StatesAll.Where(x => x.Value == sz_actual_state).FirstOrDefault().Key)) { //DA RESETTARE A FALSE (errore da principianti...) - int nextState = int.Parse(States[$"{act_rule.next_state}"]); + int nextState = StatesAll2[act_rule.next_state]; if (!exitFor) { sz_line = ""; //eseguo solo se diverso dallo stato corrente - if (nextState != int.Parse(item.Value) || calcItself) + // || calcItself + if (nextState != item.Value || calcItself) { if (nextState < 0) { nextState = 0; } - sz_line = $"{n_state_machine_index}" + + if ((nextState == 0 && calcEmptyState) || (nextState > 0)) + { + sz_line = $"{n_state_machine_index}" + $";" + - $"{int.Parse(item.Value)}" + + $"{item.Value}" + $";" + $"{Events_to_send.IndexOf(act_rule.event_to_send)}" + $";" + $"{nextState}"; - sw.WriteLine(sz_line); + sw.WriteLine(sz_line); - var newTranIn = new TransizioneIngressiModelTemp() - { - IdxFamigliaIngresso = int.Parse(n_state_machine_index), - IdxMicroStato = int.Parse(item.Value), - IdxTipoEvento = Events_to_send.IndexOf(act_rule.event_to_send), - next_IdxMicroStato = nextState - }; + var newTranIn = new TransizioneIngressiModelTemp() + { + IdxFamigliaIngresso = int.Parse(n_state_machine_index), + IdxMicroStato = item.Value, + IdxTipoEvento = Events_to_send.IndexOf(act_rule.event_to_send), + next_IdxMicroStato = nextState + }; + + TranInList2add.Add(newTranIn); + } - TranInList2add.Add(newTranIn); } exitFor = true; } diff --git a/SMGen/appsettings.json b/SMGen/appsettings.json index 80cb620..f7ed63a 100644 --- a/SMGen/appsettings.json +++ b/SMGen/appsettings.json @@ -15,6 +15,7 @@ }, "AllowedHosts": "*", "SetupOptions": { - "DoCalcItself": false + "DoCalcItself": true, + "DoCalcEmptyState": false } }