-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstatemachinegenerate.py
203 lines (165 loc) · 7.92 KB
/
statemachinegenerate.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
'''
Created on 29 Nov 2022
@author: pakala
'''
from pybars import Compiler
import os
import platform
class StateTransition(object):
def __init__(self, StartState, InputDocument, Condition, OutputDocument, TargetState) :
self.StartState = StartState
self.InputDocument = InputDocument
self.Condition = Condition
self.OutputDocument = OutputDocument
self.TargetState = TargetState
class StatMachineGenerator(object):
def __init__(self,statemachineJSON):
self.statemachineJSON = statemachineJSON
self.processedJSON = {}
self.finalDictJSON = {}
self.platform = platform.system()
base_dir = os.path.dirname(os.path.realpath(__file__))
if self.platform == "Windows":
self.skillTemplate = (base_dir) + "/scripts/skill_statemachine_template.py"
elif self.platform == "Linux":
self.skillTemplate = (base_dir) + "/scripts/skill_statemachine_template.py"
def preProcessStateMachineDATA(self):
sMachine_dict = self.statemachineJSON
TransitionsList = []
statesListSet = set()
inputMessageTypeSet = set()
outputMessageTypeSet = set()
for stateTransition in sMachine_dict['StateMachine']['Transitions']:
st = StateTransition(str(stateTransition['StartState']), str(stateTransition['InputDocument']), str(stateTransition['Condition']),
str(stateTransition['OutputDocument']), str(stateTransition['TargetState']))
statesListSet.add(str(stateTransition['StartState']))
statesListSet.add(str(stateTransition['TargetState']))
TransitionsList.append(st)
statesListDict = []
if "Exit" in statesListSet:
self.finalDictJSON['ExitState'] = [{"Exit":"Yes"}]
else :
pass
for state in statesListSet:
stateDict = {}
stateDict['StateName'] = state
statesListDict.append(stateDict)
stateandTransitionL = {}
for state in statesListSet:
temp = []
for transition in TransitionsList:
if state == transition.StartState:
temp.append(transition)
stateandTransitionL[state] = temp
temp1 = []
for state in stateandTransitionL.keys():
try:
tempDict = {}
transitionList = stateandTransitionL.get(state)
tempDict['InputDocument'] = transitionList[0].InputDocument
tempDict['StateName'] = state
tempDict['OutputDocument'] = transitionList[0].OutputDocument
tempList1 = []
iDListTemp = []
oDListTemp = []
for tansit in transitionList:
tempDict1 = {}
if tansit.InputDocument != "NA":
iDListTemp.append({"IDC":tansit.InputDocument,
"StateName":state})
inputMessageTypeSet.add(state)
if tansit.OutputDocument != "NA":
tempDict['OutputDocument_absent'] = True
outputMessageTypeSet.add(state)
oDListTemp.append({"ODC":tansit.OutputDocument,
"StateName":state})
else:
tempDict['OutputDocument_absent'] = False
if ('sleep') in (tansit.Condition):
tempDict1['Condition'] = tansit.Condition + " == None"
elif tansit.Condition == "":
tempDict1['Condition'] = True
else:
tempDict1['Condition'] = tansit.Condition
tempDict1['targetstate'] = tansit.TargetState
tempList1.append(tempDict1)
tempDict['ConditionList'] = tempList1
tempDict["InDocumentCondition"] = iDListTemp
tempDict["OutDocumentCondition"] = oDListTemp
if len(iDListTemp) > 0 :
tempDict["IDCYes"] = [{"StateName":state}]
tList = []
for dict in iDListTemp:
tList.append(list(dict.values())[0])
tempDict['InMessageList'] = (list(set(tList)))
else:
tempDict["IDCYes"] = []
tempDict['InMessageList'] = []
if len(oDListTemp) > 0 :
otList = []
for dict in oDListTemp:
otList.append(list(dict.values())[0])
otl = (list(set(otList)))
tempDict["ODCYes"] = [{"StateName":state,"oMessage": ' / '.join(otl)}]
tempDict['OutMessageList'] = (list(set(otList)))
else:
tempDict["ODCYes"] = []
temp1.append(tempDict)
except:
pass
self.finalDictJSON['InitialState'] = sMachine_dict['StateMachine']['InitialState']
self.finalDictJSON['semanticProtocol'] = sMachine_dict['MetaData']['semanticProtocol']
self.finalDictJSON['enabled'] = sMachine_dict['MetaData']['enabled']
self.finalDictJSON['StateANDTransitionList'] = temp1
IDCList = []
ODCList = []
in_messageTypeList = []
out_messageTypeList = []
for _temp1 in temp1:
try:
for idc in _temp1["InDocumentCondition"]:
if idc["IDC"] not in in_messageTypeList:
in_messageTypeList.append(idc["IDC"])
IDCList.append(idc)
except Exception as e:
pass
try:
for odc in _temp1["OutDocumentCondition"]:
if odc["ODC"] not in out_messageTypeList:
out_messageTypeList.append(odc["ODC"])
ODCList.append(odc)
except Exception as e:
pass
metaData = sMachine_dict['MetaData']
tempDict2 = {}
tempDict2['Name'] = str(metaData['Name'])
tempDict2['Author'] = str(metaData['Author'])
tempDict2['Date'] = str(metaData['Date'])
tempDict2['enabled'] = str(metaData['enabled'])
tempDict2['semanticProtocol'] = str(metaData['semanticProtocol'])
tempDict2['InitialState'] = str(sMachine_dict['StateMachine']['InitialState'])
tempDict2['SkillService'] = str(metaData['SkillService'])
self.finalDictJSON['MetaData'] = tempDict2
self.finalDictJSON['StatesList'] = statesListDict
self.finalDictJSON['IDCList'] = IDCList
self.finalDictJSON['ODCList'] = ODCList
inputMessageTypeDictList = []
for eachEntry in inputMessageTypeSet:
inputMessageTypeDictList.append({"StateName" : eachEntry })
outputMessageTypeDictList = []
for eachEntry in outputMessageTypeSet:
outputMessageTypeDictList.append({"StateName" : eachEntry })
self.finalDictJSON['InputMessageTypeList'] = inputMessageTypeDictList
self.finalDictJSON['OutputMessageTypeList'] = outputMessageTypeDictList
def codeGenerator(self, filename):
processedJSON = self.finalDictJSON
compiler = Compiler()
skillFile = os.path.join(filename)
# Compile the template
f = open(self.skillTemplate, "r")
template = compiler.compile((f.read()))
f.close()
# Render the template
output = template((processedJSON))
f.close()
return output