Files
Mapo-IOB/StateMachine/src/M_transitions.vb
T

242 lines
8.9 KiB
VB.net

Imports System.IO
Module M_transitions
#Region "variabili locali"
Private sz_tokens As String()
Private objWriter As StreamWriter
Private sz_file_name As String
#End Region
#Region "Strutture"
#End Region
#Region "Lettura file regole "
Sub Read_rule_file_transitions(ByVal sz_filename As String)
Dim sz_TextLine As String, sz_temp As String
Dim temp_rule As Rule, b_rules_definition As Boolean
Dim n_line As Int16
n_line = 0
b_rules_definition = False
sz_file_name = sz_filename
If System.IO.File.Exists(sz_filename) = True Then
Dim objfile As New System.IO.StreamReader(sz_filename)
Do While objfile.Peek() <> -1 ' finche c'è vita
sz_TextLine = Trim(objfile.ReadLine())
n_line = n_line + 1
If (Not String.IsNullOrEmpty(sz_TextLine)) Then ' linea vuota o commento ?
If (Trim(sz_TextLine).Chars(0) <> "#") Then
If InStr(sz_TextLine, ControlChars.Tab) > 0 Then ' sostituzione dei tab !!!!
sz_TextLine = sz_TextLine.Replace(ControlChars.Tab, String.Empty)
End If
sz_tokens = sz_TextLine.Split(New Char() {":"c}) ' splitta sui ":"
sz_temp = Trim(UCase$(Trim(sz_tokens(0)))) ' primo campo
Select Case sz_temp
Case "$DEFINITIONS"
b_rules_definition = False
Case "$NAME"
sz_state_machine_name = UCase$(Trim(sz_tokens(1)))
Case "$IDX"
n_state_machine_index = my_CInt(Trim(sz_tokens(1)))
Case "$N_STATES"
n_states = my_CInt(Trim(sz_tokens(1)))
Case "$N_BITS"
n_bits = my_CInt(Trim(sz_tokens(1)))
Case "$BIT"
Bits.Add(UCase$(Trim(sz_tokens(2))))
Case "$STATE"
States.Add(UCase$(Trim(sz_tokens(2))))
Case "$EVENT"
Events_to_send.Add(UCase$(Trim(sz_tokens(2))))
Case "$RULES"
b_rules_definition = True
Case "$DO"
b_rules_definition = False
Call evaluate_transitions()
Case Else
If (b_rules_definition) Then
temp_rule.state = sz_temp
temp_rule.expression = (UCase$(Trim(sz_tokens(1))))
temp_rule.next_state = (UCase$(Trim(sz_tokens(2))))
temp_rule.event_to_send = (UCase$(Trim(sz_tokens(3))))
Rules.Add(temp_rule)
Else
MsgBox("bad keyword or no rule on line : " & sz_TextLine, MsgBoxStyle.Critical, "ERROR on line : " & n_line.ToString)
End If
End Select
End If ' commento
End If ' linea vuota
Loop
objfile.Close()
Else
MsgBox(Message.Msg(4), MsgBoxStyle.Critical, sz_filename) ' file non esiste
End If ' file exists
End Sub
#End Region
#Region "Evaluate"
Private Sub evaluate_transitions()
Dim sz_actual_state As String, sz_actual_bit As String, sz_line
Dim i As Int16, n_input As Int16, n As Int16, n_mask As Int16, n_bit As Int16
Dim b_bit(20) As Boolean, b_invert As Boolean
FrmMain.TextBox1.Text = ""
Call open_mac_file(Path.GetDirectoryName(sz_file_name) & "\" & Path.GetFileNameWithoutExtension(sz_file_name) & ".csv", IniRead.sz_file_init_transitions)
' ciclo per ogni stato
For i = 0 To n_states - 1
sz_actual_state = States(i)
' ciclo per ogni ingresso
For n_input = 0 To ((2 ^ n_bits) - 1)
' calcolo true false per ogni bit dell' ingresso
n_mask = 1
For n = 0 To (n_bits - 1)
b_bit(n) = n_input And n_mask
n_mask = n_mask << 1
Next n
' FrmMain.TextBox1.Text = FrmMain.TextBox1.Text & " state " & i.ToString & " - " & sz_actual_state & " --- input : " & n_input.ToString
FrmMain.TextBox1.Text = " state " & i.ToString & " - " & sz_actual_state & " --- input : " & n_input.ToString
' ciclo per ogni regola
For Each act_rule As Rule In Rules
n_bit = -1
' controllo se la regola attuale vale in questo stato
If ((act_rule.state = "ALL_STATES") Or (act_rule.state = sz_actual_state)) Then
If (act_rule.state = sz_actual_state) Then
FrmMain.TextBox1.Text = FrmMain.TextBox1.Text & " state " & i.ToString & " - " & sz_actual_state & " --- input : " & n_input.ToString
FrmMain.TextBox1.Text = FrmMain.TextBox1.Text & " parliamo della regola " & Rules.IndexOf(act_rule) & vbCrLf
End If
'recupero il bit in questione
b_invert = False
sz_actual_bit = act_rule.expression
If InStr(sz_actual_bit, "NOT") Then ' bit negato ???
b_invert = True
sz_actual_bit = Trim(sz_actual_bit.Replace("NOT ", ""))
End If ' bit negato
' cerca il nome del bit e ne trova l' indice da 0
n_bit = Bits.FindIndex(Function(bittolo) (sz_actual_bit.Equals(bittolo)))
If n_bit = -1 Then
MsgBox("Bit name error - " & sz_actual_bit & vbCrLf & act_rule.state & " - " & act_rule.expression & " next " & act_rule.next_state,
MsgBoxStyle.Critical,
"ERROR - bit " & n_bit.ToString & " -- " & " state " & i.ToString & " - " & sz_actual_state & " --- input : " & n_input.ToString)
Exit For
End If
' vera la espressione ?
If (((Not b_invert) And b_bit(n_bit)) Or (b_invert And (Not b_bit(n_bit)))) Then
' FrmMain.TextBox1.Text = FrmMain.TextBox1.Text & " parliamo della regola " & Rules.IndexOf(act_rule) & vbCrLf
' FrmMain.TextBox1.Text = FrmMain.TextBox1.Text & " parliamo del bit " & n_bit.ToString & " - " & Bits(n_bit)
' FrmMain.TextBox1.Text = FrmMain.TextBox1.Text & " ------------->>scatta la regola " & act_rule.state & " - " & act_rule.expression & " next " & act_rule.next_state & vbCrLf & vbCrLf
sz_line = ""
If (States.IndexOf(act_rule.next_state) <> i) Then ' andrei allo stesso stato ?
' "IdxFamigliaIngresso;IdxMicroStato;ValoreIngresso;IdxTipoEvento;next_IdxMicroStato"
sz_line = n_state_machine_index.ToString & ";" &
i.ToString & ";" &
n_input.ToString & ";" &
Events_to_send.IndexOf(act_rule.event_to_send).ToString & ";" &
States.IndexOf(act_rule.next_state).ToString()
Call write_mac_file(sz_line)
Else
sz_line = "----" & n_state_machine_index.ToString & ";" &
i.ToString & ";" &
n_input.ToString & ";" &
Events_to_send.IndexOf(act_rule.event_to_send).ToString & ";" &
States.IndexOf(act_rule.next_state).ToString()
'Call write_mac_file(sz_line)
End If ' andrei allo stesso stato
Exit For ' esco da questo caso
End If ' vera la espressione
End If ' vale la regola
Next ' ciclo per tutte le regole
Next n_input ' ciclo per ogni ingresso
Next i ' ciclo per ogni stato
Call close_mac_file()
End Sub
#End Region
End Module