12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- import csv
- # read state table csv
- def parse_st_csv(file_path):
- with open(file_path, newline='') as csvfile:
- reader = csv.reader(csvfile)
- data = [row for row in reader]
- state_table = []
- events = []
- actions = []
- section = None
- for row in data:
- if row[0] == 'States/Events':
- section = 'state_table'
- continue
- elif row[0] == 'Events':
- section = 'events'
- continue
- elif row[0] == 'Actions':
- section = 'actions'
- continue
- if section == 'state_table':
- state_table.append(row)
- elif section == 'events':
- events.append(row)
- elif section == 'actions':
- actions.append(row)
- return state_table, events, actions
- # state table transpiler
- def transpile_st(state_table, events, actions):
- # actions 0-n (starting from 0)
- actions = [a[0] for a in actions]
- # conditions for events
- conditions = [e[0] for e in events]
- # events on conditions
- events = [e[2:] for e in events]
- # state names
- states = [s[0] for s in state_table]
- # columns of state table - event_states[0] is E1
- # this is used because action/nextstate is choosed based on event
- event_states = [[erows[i] for erows in state_table] for i in range(2, len(state_table[2]))]
- print(event_states)
- state_table, events, actions = parse_st_csv('TTExample.csv')
- transpile_st(state_table, events, actions)
|