-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
149 lines (125 loc) · 4 KB
/
model.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
class SingleState(object):
# TODO: from statematrix.py - include in StateMatrix object and access there? or global?
# default timer duration: more time than a task is expected to last
# conceptually equivalent to infinite duration
VERYLONGTIME = 100
# A unique list of transitions that occur in this state
# transition matrix; this list gets augmented, but does not
# track counts of instances to support removal (if there are
# unused transition actions, they will end up being an empty
# column in the computed matrix representation)
TRANSITION_LIST = set()
# the state will be assigned a unique identifier within the state matrix
# when it gets added to the StateMatrix object
def __init__(self,name,isNew=True):
# required values
if not name:
raise Exception('Each state must have a name.')
self._name = name
self._statetimer = SingleState.VERYLONGTIME
# fill in default values, as needed
self._transitions = {}
self._outputsOn = []
self._outputsOff = []
self._trigger = []
self._serialOut = 0
self._isNew = isNew
@property
def name(self):
""" (Human friendly) label for the state """
return self._name
@property
def statetimer(self):
""" Duration of the state (max time when setting a countdown timer) """
return statetimer._name
def addTransition(self,toName,action):
self._transitions[action]=toName
SingleState.TRANSITION_LIST.add(action)
""" ------------------------------------------------------------------------
MATRIX VIEW
Functionalty to support a (text-based) matrix view of the finite state
machine
"""
@staticmethod
def toStringHeader():
result = ' label \t\t'
for a in SingleState.TRANSITION_LIST:
result += a+'\t'
return result
def toString(self):
result = self._name+'\t'
for a in SingleState.TRANSITION_LIST:
if a in self._transitions:
result += self._transitions[a]+'\t'
else:
result += '--None--\t'
return result
# needs to be Python3
# uses Tk...
class StateMatrixModel(object):
def __init__(self):
self.nodeList = {}
"""
Create a SingleState object to collect all of the details for a
new state
Returns true on success, false otherwise
"""
def createState(self,name):
if(name in self.nodeList):
print('... A node by that name already exists: '+name)
return False
else:
self.stateInProgress = SingleState(name)
return True
def loadState(self,name):
if(name in self.nodeList):
self.stateInProgress = self.nodeList[name]
return True
else:
print('... Unable to find node: '+name)
return False
def addTransition(self,toName,action):
if(self.stateInProgress):
self.stateInProgress.addTransition(toName,action)
else:
print('... Create or load a state before attempting to modify')
def recordState(self):
if(self.stateInProgress):
self.nodeList[self.stateInProgress.name]=self.stateInProgress
self.stateInProgress = None
else:
print('... No state in progress')
""" ------------------------------------------------------------------------
MATRIX VIEW
Functionalty to support a (text-based) matrix view of the finite state
machine
"""
def toString(self):
result = SingleState.toStringHeader()+'\n'
i=0
for n in self.nodeList:
result += str(i)+' '+self.nodeList[n].toString()+'\n'
i+=1
return result
def writeToFile(self,filename):
with open(filename,'wb') as outfile:
outfile.write("need to fill this in!")
# parse a 2D array from a file to interpret as finite state machine
def readFromFile(self,filename):
with open(filename,'rb') as infile:
for line in file:
print '.'
print 'Do something...'
def saveToStateMatrix(self):
# instantiate a statematrix object and populate it with all of the
# accumulated details
self.sm.add_state(
self.stateInProgress.name,
self.stateInProgress.statetimer,
self.stateInProgress.transitions,
self.stateInProgress.outputsOn,
self.stateInProgress.outputsOff,
self.stateInProgress.trigger,
self.stateInProgress.serialOut);
print self.sm
# end class def: statematrixDesigner