d4b26d23a397d7e8bea0aa76e2725bbc668e9785
[smartthings-infrastructure.git] / Extractor / ExtractorScript.py
1 import os
2 readyToReturn = 0
3 ToReturn = ""
4 eventMap = []
5 app1Capabilities = []
6 app2Capabilities = []
7
8 def GetToken(f):
9         global readyToReturn
10         global ToReturn
11         Skip = ['\n', '\t', ' ']
12         Special = ["(", "\"", ":", ",", "{", "}", ")", "/", "*"]
13         S = ""
14         if (readyToReturn):
15                 readyToReturn = 0
16                 return ToReturn
17         ToReturn = ""
18         c = f.read(1)
19         while(True):
20                 if (c in Special):
21                         if (S != ""):
22                                 readyToReturn = 1
23                                 ToReturn = c
24                                 return S
25                         else:
26                                 return c
27                 elif (c in Skip):
28                         if (S != ""):
29                                 return S        
30                         else:
31                                 c = f.read(1)
32                                 continue
33                 S += c
34                 c = f.read(1)
35                 if not c:
36                         return "EOF"
37
38 def ExtractFunctions(F, appName):
39         global eventMap
40         Temp = GetToken(F)
41         while (Temp != "EOF"):
42                 if (Temp == "def" or Temp == "private"):
43                         Temp = GetToken(F)
44                         if (Temp == "def" or Temp == "private"):
45                                 Temp = GetToken(F)
46                         NameofFunc = Temp
47                         if (GetToken(F) == "("): #We have a function to create object for
48                                 if (appName == "App1"):
49                                         extractedFunctionsApp1.write("//Global Object for functions in subscribe method!\n")    
50                                         extractedFunctionsApp1.write("def %s = this.&" % NameofFunc)
51                                         extractedFunctionsApp1.write("%s\n" % NameofFunc)
52                                 else:
53                                         extractedFunctionsApp2.write("//Global Object for functions in subscribe method!\n")    
54                                         extractedFunctionsApp2.write("def %s = this.&" % NameofFunc)
55                                         extractedFunctionsApp2.write("%s\n" % NameofFunc)
56
57                 #Check subscribed events
58                 if (Temp == "subscribe"):
59                         counter = 0
60                         while (counter < 5 and Temp != "\""):
61                                 Temp = GetToken(F)
62                                 counter = counter + 1
63                         Temp = GetToken(F)
64                         #If counter >= 5 that means it is not found, so it must be appTouch
65                         if (counter >= 5):
66                                 Temp = "Touched"
67                         if Temp not in eventMap:
68                                 eventMap.append(Temp)
69
70                 #Check and analyze capabilities for physical interaction
71                 AnalyzeCapabilities(Temp, appName)
72
73                 Temp = GetToken(F)
74
75         #Warn if there is a potential for physical interaction
76         AnalyzePhysicalInteraction(app1Capabilities, app2Capabilities)
77         AnalyzePhysicalInteraction(app2Capabilities, app1Capabilities)
78
79
80 def AnalyzeCapabilities(Temp, appName):
81                         #Illuminance related
82         if (Temp == "capability.switch" or
83                         Temp == "capability.switchLevel" or
84                         Temp == "capability.illuminanceMeasurement" or
85                         #Motion related
86                         Temp == "capability.motionSensor" or
87                         #Water related
88                         Temp == "capability.valve" or
89                         Temp == "capability.waterSensor" or
90                         #Sound related
91                         Temp == "capability.musicPlayer" or
92                         Temp == "capability.alarm" or
93                         Temp == "capability.speechSynthesis" or
94                         Temp == "capability.soundSensor"):
95                 if (appName == "App1"):
96                         app1Capabilities.append(Temp)
97                 else:
98                         app2Capabilities.append(Temp)
99                         
100 def AnalyzePhysicalInteraction(app1Capab, app2Capab):
101         #Light
102         if ("capability.illuminanceMeasurement" in app1Capab) and ("capability.switch" in app2Capab or 
103                         "capability.switchLevel" in app2Capab):
104                 print ("\nWARNING: Potential PHYSICAL CONFLICT (light) detected between App1 and App2!\n")
105         #Motion
106         if ("capability.motionSensor" in app1Capab):
107                 print ("\nWARNING: Potential PHYSICAL CONFLICT (motion) detected between App1 and App2!\n")             
108         #Water
109         if ("capability.waterSensor" in app1Capab) and ("capability.valve" in app2Capab or 
110                         "capability.switch" in app2Capab):
111                 print ("\nWARNING: Potential PHYSICAL CONFLICT (water) detected between App1 and App2!\n")
112         #Sound
113         if ("capability.soundSensor" in app1Capab) and ("capability.musicPlayer" in app2Capab or 
114                         "capability.alarm" in app2Capab or "capability.speechSynthesis" in app2Capab):
115                 print ("\nWARNING: Potential PHYSICAL CONFLICT (sound) detected between App1 and App2!\n")
116
117 def ExtractEvents(extractedEvents):
118         global eventMap
119         extractedEvents.write("Random random = new Random(42)\n")
120         extractedEvents.write("counter = 1000\n")
121         extractedEvents.write("while(counter > 0) {\n")
122         extractedEvents.write("\tdef eventNumber = random.nextInt(%d)\n" % (len(eventMap) - 1))
123         extractedEvents.write("\tswitch(eventNumber) {\n")
124         eventCounter = 0;
125         for i in range(len(eventMap)):
126                 extractedEvents.write("\t\tcase %d:\n" % eventCounter)
127                 if eventMap[i] == "lock":
128                         event = open("eventSimulator/lockEvent.groovy", "r")
129                         for line in event:
130                                 extractedEvents.write(line)
131                         event.close()
132                 elif eventMap[i] == "unlock":
133                         event = open("eventSimulator/unlockEvent.groovy", "r")
134                         for line in event:
135                                 extractedEvents.write(line)
136                         event.close()
137                 elif eventMap[i] == "contact.open":
138                         event = open("eventSimulator/contactOpenEvent.groovy", "r")
139                         for line in event:
140                                 extractedEvents.write(line)
141                         event.close()
142                 elif eventMap[i] == "contact.closed":
143                         event = open("eventSimulator/contactClosedEvent.groovy", "r")
144                         for line in event:
145                                 extractedEvents.write(line)
146                         event.close()
147                 elif eventMap[i] == "switch.on":
148                         event = open("eventSimulator/switchOnEvent.groovy", "r")
149                         for line in event:
150                                 extractedEvents.write(line)
151                         event.close()
152                 elif eventMap[i] == "switch.off":
153                         event = open("eventSimulator/switchOffEvent.groovy", "r")
154                         for line in event:
155                                 extractedEvents.write(line)
156                         event.close()
157                 elif eventMap[i] == "nfcTouch":
158                         event = open("eventSimulator/nfcTouchEvent.groovy", "r")
159                         for line in event:
160                                 extractedEvents.write(line)
161                         event.close()
162                 elif eventMap[i] == "Touched":
163                         event = open("eventSimulator/appTouchEvent.groovy", "r")
164                         for line in event:
165                                 extractedEvents.write(line)
166                         event.close()
167                 elif eventMap[i] == "button":
168                         #Write two events subsequently
169                         event = open("eventSimulator/buttonPushedEvent.groovy", "r")
170                         for line in event:
171                                 extractedEvents.write(line)
172                         event.close()
173                         extractedEvents.write("\t\t\tbreak\n")
174                         eventCounter = eventCounter + 1
175                         extractedEvents.write("\t\tcase %d:\n" % eventCounter)
176                         event = open("eventSimulator/buttonHeldEvent.groovy", "r")
177                         for line in event:
178                                 extractedEvents.write(line)
179                         event.close()
180                 elif eventMap[i] == "presence":
181                         #Write two events subsequently
182                         event = open("eventSimulator/presencePresentEvent.groovy", "r")
183                         for line in event:
184                                 extractedEvents.write(line)
185                         event.close()
186                         extractedEvents.write("\t\t\tbreak\n")
187                         eventCounter = eventCounter + 1
188                         extractedEvents.write("\t\tcase %d:\n" % eventCounter)
189                         event = open("eventSimulator/presenceLeftEvent.groovy", "r")
190                         for line in event:
191                                 extractedEvents.write(line)
192                         event.close()
193                 elif eventMap[i] == "doorState":
194                         #Write two events subsequently
195                         event = open("eventSimulator/doorOpenEvent.groovy", "r")
196                         for line in event:
197                                 extractedEvents.write(line)
198                         event.close()
199                         extractedEvents.write("\t\t\tbreak\n")
200                         eventCounter = eventCounter + 1
201                         extractedEvents.write("\t\tcase %d:\n" % eventCounter)
202                         event = open("eventSimulator/doorClosedEvent.groovy", "r")
203                         for line in event:
204                                 extractedEvents.write(line)
205                         event.close()
206                 eventCounter = eventCounter + 1
207
208                 ###TODO: Add more events later
209                 extractedEvents.write("\n\t\t\tbreak\n")
210         extractedEvents.write("\t}\n")
211         extractedEvents.write("\tcounter--\n")
212         extractedEvents.write("}\n")
213
214
215 def AnalyzeCapabilities(Temp, appName):
216                         #Illuminance related
217         if (Temp == "capability.switch" or
218                         Temp == "capability.switchLevel" or
219                         Temp == "capability.illuminanceMeasurement" or
220                         #Motion related
221                         Temp == "capability.motionSensor" or
222                         #Water related
223                         Temp == "capability.valve" or
224                         Temp == "capability.waterSensor" or
225                         #Sound related
226                         Temp == "capability.musicPlayer" or
227                         Temp == "capability.alarm" or
228                         Temp == "capability.speechSynthesis" or
229                         Temp == "capability.soundSensor"):
230                 if (appName == "App1"):
231                         app1Capabilities.append(Temp)
232                 else:
233                         app2Capabilities.append(Temp)
234                         
235 def AnalyzePhysicalInteraction(app1Capab, app2Capab):
236         #Light
237         if ("capability.illuminanceMeasurement" in app1Capab) and ("capability.switch" in app2Capab or 
238                         "capability.switchLevel" in app2Capab):
239                 print ("\nWARNING: Potential PHYSICAL CONFLICT (light) detected between App1 and App2!\n")
240         #Motion
241         if ("capability.motionSensor" in app1Capab):
242                 print ("\nWARNING: Potential PHYSICAL CONFLICT (motion) detected between App1 and App2!\n")             
243         #Water
244         if ("capability.waterSensor" in app1Capab) and ("capability.valve" in app2Capab or 
245                         "capability.switch" in app2Capab):
246                 print ("\nWARNING: Potential PHYSICAL CONFLICT (water) detected between App1 and App2!\n")
247         #Sound
248         if ("capability.soundSensor" in app1Capab) and ("capability.musicPlayer" in app2Capab or 
249                         "capability.alarm" in app2Capab or "capability.speechSynthesis" in app2Capab):
250                 print ("\nWARNING: Potential PHYSICAL CONFLICT (sound) detected between App1 and App2!\n")
251
252 def ExtractEvents(extractedEvents):
253         global eventMap
254         extractedEvents.write("Random random = new Random(42)\n")
255         extractedEvents.write("counter = 1000\n")
256         extractedEvents.write("while(counter > 0) {\n")
257         extractedEvents.write("\tdef eventNumber = random.nextInt(%d)\n" % (len(eventMap) - 1))
258         extractedEvents.write("\tswitch(eventNumber) {\n")
259         eventCounter = 0;
260         for i in range(len(eventMap)):
261                 extractedEvents.write("\t\tcase %d:\n" % eventCounter)
262                 if eventMap[i] == "lock":
263                         event = open("eventSimulator/lockEvent.groovy", "r")
264                         for line in event:
265                                 extractedEvents.write(line)
266                         event.close()
267                 elif eventMap[i] == "unlock":
268                         event = open("eventSimulator/unlockEvent.groovy", "r")
269                         for line in event:
270                                 extractedEvents.write(line)
271                         event.close()
272                 elif eventMap[i] == "contact.open":
273                         event = open("eventSimulator/contactOpenEvent.groovy", "r")
274                         for line in event:
275                                 extractedEvents.write(line)
276                         event.close()
277                 elif eventMap[i] == "contact.closed":
278                         event = open("eventSimulator/contactClosedEvent.groovy", "r")
279                         for line in event:
280                                 extractedEvents.write(line)
281                         event.close()
282                 elif eventMap[i] == "switch.on":
283                         event = open("eventSimulator/switchOnEvent.groovy", "r")
284                         for line in event:
285                                 extractedEvents.write(line)
286                         event.close()
287                 elif eventMap[i] == "switch.off":
288                         event = open("eventSimulator/switchOffEvent.groovy", "r")
289                         for line in event:
290                                 extractedEvents.write(line)
291                         event.close()
292                 elif eventMap[i] == "nfcTouch":
293                         event = open("eventSimulator/nfcTouchEvent.groovy", "r")
294                         for line in event:
295                                 extractedEvents.write(line)
296                         event.close()
297                 elif eventMap[i] == "Touched":
298                         event = open("eventSimulator/appTouchEvent.groovy", "r")
299                         for line in event:
300                                 extractedEvents.write(line)
301                         event.close()
302                 elif eventMap[i] == "button":
303                         #Write two events subsequently
304                         event = open("eventSimulator/buttonPushedEvent.groovy", "r")
305                         for line in event:
306                                 extractedEvents.write(line)
307                         event.close()
308                         extractedEvents.write("\t\t\tbreak\n")
309                         eventCounter = eventCounter + 1
310                         extractedEvents.write("\t\tcase %d:\n" % eventCounter)
311                         event = open("eventSimulator/buttonHeldEvent.groovy", "r")
312                         for line in event:
313                                 extractedEvents.write(line)
314                         event.close()
315                 elif eventMap[i] == "presence":
316                         #Write two events subsequently
317                         event = open("eventSimulator/presencePresentEvent.groovy", "r")
318                         for line in event:
319                                 extractedEvents.write(line)
320                         event.close()
321                         extractedEvents.write("\t\t\tbreak\n")
322                         eventCounter = eventCounter + 1
323                         extractedEvents.write("\t\tcase %d:\n" % eventCounter)
324                         event = open("eventSimulator/presenceLeftEvent.groovy", "r")
325                         for line in event:
326                                 extractedEvents.write(line)
327                         event.close()
328                 elif eventMap[i] == "doorState":
329                         #Write two events subsequently
330                         event = open("eventSimulator/doorOpenEvent.groovy", "r")
331                         for line in event:
332                                 extractedEvents.write(line)
333                         event.close()
334                         extractedEvents.write("\t\t\tbreak\n")
335                         eventCounter = eventCounter + 1
336                         extractedEvents.write("\t\tcase %d:\n" % eventCounter)
337                         event = open("eventSimulator/doorClosedEvent.groovy", "r")
338                         for line in event:
339                                 extractedEvents.write(line)
340                         event.close()
341                 eventCounter = eventCounter + 1
342
343                 ###TODO: Add more events later
344                 extractedEvents.write("\n\t\t\tbreak\n")
345         extractedEvents.write("\t}\n")
346         extractedEvents.write("\tcounter--\n")
347         extractedEvents.write("}\n")
348                 
349 #Extract objects to call functions from App1
350 F1 = open("Extractor/App1/App1.groovy", "r")
351 extractedFunctionsApp1 = open("Extractor/App1/extractedFunctionsApp1.groovy", "w+")
352 ExtractFunctions(F1, "App1")
353 F1.close()
354
355 #Extract objects to call functions from App2
356 F2 = open("Extractor/App2/App2.groovy", "r")
357 extractedFunctionsApp2 = open("Extractor/App2/extractedFunctionsApp2.groovy", "w+")
358 ExtractFunctions(F2, "App2")
359 F2.close()
360
361 #Prepare eventSimulator file while parsing the App1 and App2 files
362 extractedEvents = open("eventSimulator/eventSimulator.groovy", "w+")
363 ExtractEvents(extractedEvents)
364 extractedEvents.close()
365
366 #Save the extracted methods and app1 in a same file to extract information
367 extractorFile = open("Extractor/extractorFile.groovy", "w+")
368 Extractor = open("Extractor/Extractor.groovy", "r")
369 F1 = open("Extractor/App1/App1.groovy", "r")
370
371 extractorFile.write("////////////////////\n")
372 extractorFile.write("@Field App\n")
373 extractorFile.write("App = \"App1\"")
374 extractorFile.write("\n")
375 for line in Extractor:
376         extractorFile.write(line)
377 extractorFile.write("\n\n")
378 for line in F1:
379         extractorFile.write(line)
380 extractorFile.close()
381 Extractor.close()
382 F1.close()
383 #Run the file to extract the objects
384 os.system("groovy Extractor/extractorFile.groovy")
385
386
387 #Save the extracted methods and app2 in a same file to extract information
388 extractorFile = open("Extractor/extractorFile.groovy", "w+")
389 Extractor = open("Extractor/Extractor.groovy", "r")
390 F2 = open("Extractor/App2/App2.groovy", "r")
391
392 extractorFile.write("////////////////////\n")
393 extractorFile.write("@Field App\n")
394 extractorFile.write("App = \"App2\"")
395 extractorFile.write("\n")
396 for line in Extractor:
397         extractorFile.write(line)
398 extractorFile.write("\n\n")
399 for line in F2:
400         extractorFile.write(line)
401 #Run the file to extract the objects
402 extractorFile.close()
403 Extractor.close()
404 F2.close()
405 os.system("groovy Extractor/extractorFile.groovy")
406
407
408
409