gestione macchina a stati

This commit is contained in:
zaccaria.majid
2023-07-27 15:29:10 +02:00
parent a1926bc4f1
commit 2f13e828c3
2 changed files with 52 additions and 25 deletions
+50 -24
View File
@@ -52,9 +52,16 @@ namespace SMGen.Pages
{
get => conf.GetValue<bool>("SetupOptions:DoCalcItself");
}
protected bool calcEmptyState
{
get => conf.GetValue<bool>("SetupOptions:DoCalcEmptyState");
}
protected List<RuleClass> Rules { get; set; } = new List<RuleClass>();
protected Dictionary<string, string> States { get; set; } = new Dictionary<string, string>();
protected Dictionary<string, int> StatesAll { get; set; } = new Dictionary<string, int>();
protected Dictionary<string, int> StatesAll2 { get; set; } = new Dictionary<string, int>();
protected List<string> States2Rules { get; set; } = new List<string>();
//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;
}
+2 -1
View File
@@ -15,6 +15,7 @@
},
"AllowedHosts": "*",
"SetupOptions": {
"DoCalcItself": false
"DoCalcItself": true,
"DoCalcEmptyState": false
}
}