a minor bug in schedule method!
[smartthings-infrastructure.git] / Extractor / ExtractorScript.py
1 import os
2 readyToReturn = 0
3 ToReturn = ""
4 eventList = []
5 eventVarMap = {}
6 eventVarCounterMap = {}
7 capabilityMap = {}
8 capabilityList = []
9 app1Capabilities = []
10 app2Capabilities = []
11 app1Subscribe = False
12 app2Subscribe = False
13
14 def GetToken(f):
15         global readyToReturn
16         global ToReturn
17         Skip = ['\n', '\t', ' ']
18         Special = ["(", "\"", ":", ",", "{", "}", ")", "/", "*"]
19         S = ""
20         if (readyToReturn):
21                 readyToReturn = 0
22                 return ToReturn
23         ToReturn = ""
24         c = f.read(1)
25         while(True):
26                 if (c in Special):
27                         if (S != ""):
28                                 readyToReturn = 1
29                                 ToReturn = c
30                                 return S
31                         else:
32                                 return c
33                 elif (c in Skip):
34                         if (S != ""):
35                                 return S        
36                         else:
37                                 c = f.read(1)
38                                 continue
39                 S += c
40                 c = f.read(1)
41                 if not c:
42                         return "EOF"
43
44 def ExtractFunctions(F, appName):
45         global eventList
46         global app1Subscribe
47         global app2Subscribe
48         Temp = GetToken(F)
49         while (Temp != "EOF"):
50                 if (Temp == "def" or Temp == "private"):
51                         Temp = GetToken(F)
52                         if (Temp == "def" or Temp == "private"):
53                                 Temp = GetToken(F)
54                         NameofFunc = Temp
55                         if (GetToken(F) == "("): #We have a function to create object for
56                                 if (appName == "App1"):
57                                         extractedFunctionsApp1.write("//Global Object for functions in subscribe method!\n")    
58                                         extractedFunctionsApp1.write("def %s = this.&" % NameofFunc)
59                                         extractedFunctionsApp1.write("%s\n" % NameofFunc)
60                                 else:
61                                         extractedFunctionsApp2.write("//Global Object for functions in subscribe method!\n")    
62                                         extractedFunctionsApp2.write("def %s = this.&" % NameofFunc)
63                                         extractedFunctionsApp2.write("%s\n" % NameofFunc)
64                 
65                 #Check input capability
66                 if (Temp == "input"):
67                         while Temp != "\"":
68                                 Temp = GetToken(F)
69                         variable = GetToken(F)
70                         Temp = GetToken(F) #Get '"'
71                         Temp = GetToken(F) #Get ','
72                         while Temp != "\"":
73                                 Temp = GetToken(F)
74                         capability = GetToken(F)
75                         capabilityMap[variable] = capability
76                 
77                 #Check subscribed events
78                 if (Temp == "subscribe"):
79                         if (appName == "App1"):
80                                 app1Subscribe = True
81                         else:
82                                 app2Subscribe = True
83                         Temp = GetToken(F)
84                         if (Temp == "("):
85                                 Temp = GetToken(F)
86                         variable = Temp
87                         while (Temp != "\"" and Temp != "app" and Temp != "location"):
88                                 Temp = GetToken(F)
89                         if Temp == "\"":
90                                 Temp = GetToken(F)              
91
92                         print "DEBUG: %s - %s" % (variable, Temp)
93                         print capabilityMap
94                         if Temp == "app" or Temp == "location":
95                                 if Temp not in eventList:
96                                         eventList.append(Temp)
97                         elif Temp not in eventList or (variable in capabilityMap.keys() and capabilityMap[variable] not in capabilityList):
98                                 # We do not repeat the same capability twice
99                                 capability = capabilityMap[variable]
100                                 capabilityList.append(capability)
101                                 eventList.append(Temp)
102                                 if Temp in eventVarMap.keys():
103                                         listOfEvents = eventVarMap[Temp]
104                                         listOfEvents.append(variable)
105                                 else:
106                                         eventVarMap[Temp] = [variable]
107                         #print "DEBUG: %s - %s" % (variable, Temp)
108                         #print capabilityMap
109                         #print eventList
110
111                 #Check and analyze capabilities for physical interaction
112                 AnalyzeCapabilities(Temp, appName, F)
113
114                 Temp = GetToken(F)
115         
116         #Warn if there is a potential for physical interaction
117         AnalyzePhysicalInteraction(app1Capabilities, app2Capabilities)
118         AnalyzePhysicalInteraction(app2Capabilities, app1Capabilities)
119
120 def AnalyzeCapabilities(Temp, appName, F):
121                         #Illuminance related
122         if (Temp == "capability.switch" or
123                         Temp == "capability.switchLevel" or
124                         Temp == "capability.illuminanceMeasurement" or
125                         Temp == "capability.colorControl" or
126                         #Motion related
127                         Temp == "capability.motionSensor" or
128                         Temp == "capability.accelerationSensor" or
129                         #Water related
130                         Temp == "capability.valve" or
131                         Temp == "capability.waterSensor" or
132                         #Sound related
133                         Temp == "capability.musicPlayer" or
134                         Temp == "capability.alarm" or
135                         Temp == "capability.speechSynthesis" or
136                         Temp == "capability.soundSensor"):
137                 if (appName == "App1"):
138                         app1Capabilities.append(Temp)
139                 else:
140                         app2Capabilities.append(Temp)
141         if (Temp == "capability"):
142                 Temp = GetToken(F) #Get '"'
143                 Temp = GetToken(F) #Get 'Music'
144                 Temp = Temp + GetToken(F) #Get 'Player'
145                 if (Temp == "MusicPlayer"):
146                         if (appName == "App1"):
147                                 app1Capabilities.append("capability.musicPlayer")
148                         else:
149                                 app2Capabilities.append("capability.musicPlayer")
150                         
151 def AnalyzePhysicalInteraction(app1Capab, app2Capab):
152         #Light
153         if ("capability.illuminanceMeasurement" in app1Capab) and ("capability.switch" in app2Capab or 
154                         "capability.switchLevel" in app2Capab or "capability.colorControl" in app2Capab):
155                 print ("\nWARNING: Potential PHYSICAL CONFLICT (light) detected between App1 and App2!\n")
156         #Motion
157         # TODO: Technically this is not entirely precise since we need to be able to detect that the other app creates motion
158         if ("capability.motionSensor" in app1Capab) or ("capability.accelerationSensor" in app1Capab):
159                 print ("\nWARNING: Potential PHYSICAL CONFLICT (motion) detected between App1 and App2!\n")             
160         #Water
161         if ("capability.waterSensor" in app1Capab) and ("capability.valve" in app2Capab or 
162                         "capability.switch" in app2Capab):
163                 print ("\nWARNING: Potential PHYSICAL CONFLICT (water) detected between App1 and App2!\n")
164         #Sound
165         if ("capability.soundSensor" in app1Capab) and ("capability.musicPlayer" in app2Capab or 
166                         "capability.alarm" in app2Capab or "capability.speechSynthesis" in app2Capab):
167                 print ("\nWARNING: Potential PHYSICAL CONFLICT (sound) detected between App1 and App2!\n")
168
169 def ExtractEvents(extractedEvents):
170         global eventList
171         global eventVarMap
172         global capabilityMap
173         global eventVarCounterMap
174         extractedEvents.write("while(true) {\n")
175         extractedEvents.write("\tdef eventNumber = Verify.getInt(0,%d)\n" % (len(eventList) - 1))
176         extractedEvents.write("\tswitch(eventNumber) {\n")
177         for i in range(len(eventList)):
178                 extractedEvents.write("\t\tcase %d:\n" % i)
179                 if eventList[i] == "lock":
180                         #Write two events subsequently
181                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
182                         extractedEvents.write("\t\t\tif (event == 0) {\n")
183                         event = open("eventSimulator/lockLockedEvent.groovy", "r")                      
184                         for line in event:
185                                 extractedEvents.write("\t\t" + line)
186                         event.close()
187                         extractedEvents.write("\t\t\t} else {\n")
188                         event = open("eventSimulator/lockUnlockedEvent.groovy", "r")
189                         for line in event:
190                                 extractedEvents.write("\t\t" + line)
191                         event.close()
192                         extractedEvents.write("\t\t\t}\n")
193                 elif eventList[i] == "unlock":
194                         event = open("eventSimulator/unlockEvent.groovy", "r")
195                         for line in event:
196                                 extractedEvents.write(line)
197                         event.close()
198                 elif eventList[i] == "contact.open":
199                         event = open("eventSimulator/contactOpenEvent.groovy", "r")
200                         for line in event:
201                                 extractedEvents.write(line)
202                         event.close()
203                 elif eventList[i] == "contact.closed":
204                         event = open("eventSimulator/contactClosedEvent.groovy", "r")
205                         for line in event:
206                                 extractedEvents.write(line)
207                         event.close()
208                 elif eventList[i] == "tamper.tampered":
209                         #Write two events subsequently
210                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
211                         extractedEvents.write("\t\t\tif (event == 0) {\n")
212                         event = open("eventSimulator/tamperTamperedOpenEvent.groovy", "r")                      
213                         for line in event:
214                                 extractedEvents.write("\t\t" + line)
215                         event.close()
216                         extractedEvents.write("\t\t\t} else {\n")
217                         event = open("eventSimulator/tamperTamperedClosedEvent.groovy", "r")
218                         for line in event:
219                                 extractedEvents.write("\t\t" + line)
220                         event.close()
221                         extractedEvents.write("\t\t\t}\n")
222                 elif eventList[i] == "nfcTouch":
223                         event = open("eventSimulator/nfcTouchEvent.groovy", "r")
224                         for line in event:
225                                 extractedEvents.write(line)
226                         event.close()
227                 elif eventList[i] == "app": #Case for Touched event
228                         event = open("eventSimulator/appTouchEvent.groovy", "r")
229                         for line in event:
230                                 extractedEvents.write(line)
231                         event.close()
232                 elif eventList[i] == "button":
233                         #Write two events subsequently
234                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
235                         extractedEvents.write("\t\t\tif (event == 0) {\n")
236                         event = open("eventSimulator/buttonPushedEvent.groovy", "r")                    
237                         for line in event:
238                                 extractedEvents.write("\t\t" + line)
239                         event.close()
240                         extractedEvents.write("\t\t\t} else {\n")
241                         event = open("eventSimulator/buttonHeldEvent.groovy", "r")
242                         for line in event:
243                                 extractedEvents.write("\t\t" + line)
244                         event.close()
245                         extractedEvents.write("\t\t\t}\n")
246                 elif eventList[i] == "water":
247                         #Write two events subsequently
248                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
249                         extractedEvents.write("\t\t\tif (event == 0) {\n")
250                         event = open("eventSimulator/waterDryEvent.groovy", "r")                        
251                         for line in event:
252                                 extractedEvents.write("\t\t" + line)
253                         event.close()
254                         extractedEvents.write("\t\t\t} else {\n")
255                         event = open("eventSimulator/waterWetEvent.groovy", "r")
256                         for line in event:
257                                 extractedEvents.write("\t\t" + line)
258                         event.close()
259                         extractedEvents.write("\t\t\t}\n")
260                 elif eventList[i] == "water.dry":
261                         event = open("eventSimulator/waterDefaultDryEvent.groovy", "r")
262                         for line in event:
263                                 extractedEvents.write(line)
264                         event.close()
265                 elif eventList[i] == "water.wet":
266                         event = open("eventSimulator/waterDefaultWetEvent.groovy", "r")
267                         for line in event:
268                                 extractedEvents.write(line)
269                         event.close()
270                 elif eventList[i] == "presence":
271                         #Check which capability
272                         variable = eventVarMap[eventList[i]]
273                         if eventList[i] not in eventVarCounterMap.keys():
274                                 eventVarCounterMap[eventList[i]] = 1
275                                 eventVarCount = 0
276                         else:
277                                 eventVarCount = eventVarCounterMap[eventList[i]]
278                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
279                         capability = capabilityMap[variable[eventVarCount]]
280                         #Write three events subsequently
281                         if capability == "capability.presenceSensor":
282                                 extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
283                         elif capability == "capability.beacon":
284                                 extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
285                         extractedEvents.write("\t\t\tif (event == 0) {\n")
286                         if capability == "capability.presenceSensor":
287                                 event = open("eventSimulator/presencePresentEvent.groovy", "r")
288                         elif capability == "capability.beacon":
289                                 event = open("eventSimulator/beaconPresentEvent.groovy", "r")
290                         for line in event:
291                                 extractedEvents.write("\t\t" + line)
292                         event.close()
293                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
294                         if capability == "capability.presenceSensor":
295                                 event = open("eventSimulator/presenceLeftEvent.groovy", "r")
296                         elif capability == "capability.beacon":
297                                 event = open("eventSimulator/beaconLeftEvent.groovy", "r")
298                         for line in event:
299                                 extractedEvents.write("\t\t" + line)
300                         event.close()
301                 elif eventList[i] == "presence.present": #Case for Touched event
302                         event = open("eventSimulator/presencePresencePresentEvent.groovy", "r")
303                         for line in event:
304                                 extractedEvents.write("\t\t" + line)
305                         event.close()
306                 elif eventList[i] == "doorState":
307                         #Write two events subsequently
308                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
309                         extractedEvents.write("\t\t\tif (event == 0) {\n")
310                         event = open("eventSimulator/doorOpenEvent.groovy", "r")                        
311                         for line in event:
312                                 extractedEvents.write("\t\t" + line)
313                         event.close()
314                         extractedEvents.write("\t\t\t} else {\n")
315                         event = open("eventSimulator/doorClosedEvent.groovy", "r")
316                         for line in event:
317                                 extractedEvents.write("\t\t" + line)
318                         event.close()
319                         extractedEvents.write("\t\t\t}\n")
320                 elif eventList[i] == "motion":
321                         #Write two events subsequently
322                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
323                         extractedEvents.write("\t\t\tif (event == 0) {\n")
324                         event = open("eventSimulator/motionActiveEvent.groovy", "r")                    
325                         for line in event:
326                                 extractedEvents.write("\t\t" + line)
327                         event.close()
328                         extractedEvents.write("\t\t\t} else {\n")
329                         event = open("eventSimulator/motionInactiveEvent.groovy", "r")
330                         for line in event:
331                                 extractedEvents.write("\t\t" + line)
332                         event.close()
333                         extractedEvents.write("\t\t\t}\n")
334                 elif eventList[i] == "motion.active":
335                         event = open("eventSimulator/motionDefaultActiveEvent.groovy", "r")
336                         for line in event:
337                                 extractedEvents.write("\t\t" + line)
338                         event.close()
339                 elif eventList[i] == "motion.inactive":
340                         event = open("eventSimulator/motionDefaultInactiveEvent.groovy", "r")
341                         for line in event:
342                                 extractedEvents.write("\t\t" + line)
343                         event.close()
344                 elif eventList[i] == "smoke":
345                         #Write three events subsequently
346                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
347                         extractedEvents.write("\t\t\tif (event == 0) {\n")
348                         event = open("eventSimulator/smokeClearEvent.groovy", "r")                      
349                         for line in event:
350                                 extractedEvents.write("\t\t" + line)
351                         event.close()
352                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
353                         event = open("eventSimulator/smokeDetectedEvent.groovy", "r")
354                         for line in event:
355                                 extractedEvents.write("\t\t" + line)
356                         event.close()
357                         extractedEvents.write("\t\t\t} else {\n")
358                         event = open("eventSimulator/smokeTestedEvent.groovy", "r")
359                         for line in event:
360                                 extractedEvents.write("\t\t" + line)
361                         event.close()
362                         extractedEvents.write("\t\t\t}\n")
363                 elif eventList[i] == "smoke.clear":
364                         event = open("eventSimulator/smokeDefaultClearEvent.groovy", "r")
365                         for line in event:
366                                 extractedEvents.write("\t\t" + line)
367                         event.close()
368                 elif eventList[i] == "smoke.detected":
369                         event = open("eventSimulator/smokeDefaultDetectedEvent.groovy", "r")
370                         for line in event:
371                                 extractedEvents.write("\t\t" + line)
372                         event.close()
373                 elif eventList[i] == "smoke.tested":
374                         event = open("eventSimulator/smokeDefaultTestedEvent.groovy", "r")
375                         for line in event:
376                                 extractedEvents.write("\t\t" + line)
377                         event.close()
378                 elif eventList[i] == "carbonMonoxide":
379                         #Check which capability
380                         variable = eventVarMap[eventList[i]]
381                         if eventList[i] not in eventVarCounterMap.keys():
382                                 eventVarCounterMap[eventList[i]] = 1
383                                 eventVarCount = 0
384                         else:
385                                 eventVarCount = eventVarCounterMap[eventList[i]]
386                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
387                         capability = capabilityMap[variable[eventVarCount]]
388                         #Write three events subsequently
389                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
390                         extractedEvents.write("\t\t\tif (event == 0) {\n")                      
391                         if capability == "capability.smokeDetector":
392                                 event = open("eventSimulator/smokeCarbonMonoxideClearEvent.groovy", "r")
393                         elif capability == "capability.carbonMonoxideDetector":
394                                 event = open("eventSimulator/carbonMonoxideClearEvent.groovy", "r")
395                         for line in event:
396                                 extractedEvents.write("\t\t" + line)
397                         event.close()
398                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
399                         if capability == "capability.smokeDetector":
400                                 event = open("eventSimulator/smokeCarbonMonoxideDetectedEvent.groovy", "r")
401                         elif capability == "capability.carbonMonoxideDetector":
402                                 event = open("eventSimulator/carbonMonoxideDetectedEvent.groovy", "r")
403                         for line in event:
404                                 extractedEvents.write("\t\t" + line)
405                         event.close()
406                         extractedEvents.write("\t\t\t} else {\n")
407                         if capability == "capability.smokeDetector":
408                                 event = open("eventSimulator/smokeCarbonMonoxideTestedEvent.groovy", "r")
409                         elif capability == "capability.carbonMonoxideDetector":
410                                 event = open("eventSimulator/carbonMonoxideTestedEvent.groovy", "r")
411                         for line in event:
412                                 extractedEvents.write("\t\t" + line)
413                         event.close()
414                         extractedEvents.write("\t\t\t}\n")
415                 elif eventList[i] == "carbonMonoxide.clear":
416                         if capability == "capability.smokeDetector":
417                                 event = open("eventSimulator/smokeCarbonMonoxideDefaultClearEvent.groovy", "r")
418                         elif capability == "capability.carbonMonoxideDetector":
419                                 event = open("eventSimulator/carbonMonoxideDefaultClearEvent.groovy", "r")
420                         for line in event:
421                                 extractedEvents.write("\t\t" + line)
422                         event.close()
423                 elif eventList[i] == "carbonMonoxide.detected":
424                         if capability == "capability.smokeDetector":
425                                 event = open("eventSimulator/smokeCarbonMonoxideDefaultDetectedEvent.groovy", "r")
426                         elif capability == "capability.carbonMonoxideDetector":
427                                 event = open("eventSimulator/carbonMonoxideDefaultDetectedEvent.groovy", "r")
428                         for line in event:
429                                 extractedEvents.write("\t\t" + line)
430                         event.close()
431                 elif eventList[i] == "carbonMonoxide.tested":
432                         if capability == "capability.smokeDetector":
433                                 event = open("eventSimulator/smokeCarbonMonoxideDefaultTestedEvent.groovy", "r")
434                         elif capability == "capability.carbonMonoxideDetector":
435                                 event = open("eventSimulator/carbonMonoxideDefaultTestedEvent.groovy", "r")
436                         for line in event:
437                                 extractedEvents.write("\t\t" + line)
438                         event.close()
439                 elif eventList[i] == "battery":
440                         #Check which capability
441                         variable = eventVarMap[eventList[i]]
442                         if eventList[i] not in eventVarCounterMap.keys():
443                                 eventVarCounterMap[eventList[i]] = 1
444                                 eventVarCount = 0
445                         else:
446                                 eventVarCount = eventVarCounterMap[eventList[i]]
447                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
448                         capability = capabilityMap[variable[eventVarCount]]
449                         if capability == "capability.smokeDetector":
450                                 event = open("eventSimulator/smokeDetectorBatteryEvent.groovy", "r")
451                         elif capability == "capability.battery":
452                                 event = open("eventSimulator/batteryBatteryEvent.groovy", "r")
453                         for line in event:
454                                 extractedEvents.write(line)
455                         event.close()
456                 elif eventList[i] == "thermostatMode":
457                         #Write five events subsequently
458                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,4)\n")
459                         extractedEvents.write("\t\t\tif (event == 0) {\n")
460                         event = open("eventSimulator/thermostatAutoModeEvent.groovy", "r")                      
461                         for line in event:
462                                 extractedEvents.write("\t\t" + line)
463                         event.close()
464                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
465                         event = open("eventSimulator/thermostatCoolModeEvent.groovy", "r")
466                         for line in event:
467                                 extractedEvents.write("\t\t" + line)
468                         event.close()
469                         extractedEvents.write("\t\t\t} else if (event == 2) {\n")
470                         event = open("eventSimulator/thermostatEmergencyHeatModeEvent.groovy", "r")
471                         for line in event:
472                                 extractedEvents.write("\t\t" + line)
473                         event.close()
474                         extractedEvents.write("\t\t\t} else if (event == 3) {\n")
475                         event = open("eventSimulator/thermostatHeatModeEvent.groovy", "r")
476                         for line in event:
477                                 extractedEvents.write("\t\t" + line)
478                         event.close()
479                         extractedEvents.write("\t\t\t} else {\n")
480                         event = open("eventSimulator/thermostatOffModeEvent.groovy", "r")
481                         for line in event:
482                                 extractedEvents.write("\t\t" + line)
483                         event.close()
484                         extractedEvents.write("\t\t\t}\n")
485                 elif eventList[i] == "thermostatFanMode":
486                         #Write five events subsequently
487                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,4)\n")
488                         extractedEvents.write("\t\t\tif (event == 0) {\n")
489                         event = open("eventSimulator/thermostatAutoFanModeEvent.groovy", "r")                   
490                         for line in event:
491                                 extractedEvents.write("\t\t" + line)
492                         event.close()
493                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
494                         event = open("eventSimulator/thermostatFanCirculateFanModeEvent.groovy", "r")
495                         for line in event:
496                                 extractedEvents.write("\t\t" + line)
497                         event.close()
498                         extractedEvents.write("\t\t\t} else if (event == 2) {\n")
499                         event = open("eventSimulator/thermostatCirculateFanModeEvent.groovy", "r")
500                         for line in event:
501                                 extractedEvents.write("\t\t" + line)
502                         event.close()
503                         extractedEvents.write("\t\t\t} else if (event == 3) {\n")
504                         event = open("eventSimulator/thermostatFanOnFanModeEvent.groovy", "r")
505                         for line in event:
506                                 extractedEvents.write("\t\t" + line)
507                         event.close()
508                         extractedEvents.write("\t\t\t} else {\n")
509                         event = open("eventSimulator/thermostatOnFanModeEvent.groovy", "r")
510                         for line in event:
511                                 extractedEvents.write("\t\t" + line)
512                         event.close()
513                         extractedEvents.write("\t\t\t}\n")
514                 elif eventList[i] == "thermostatOperatingState":
515                         #Write five events subsequently
516                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,4)\n")
517                         extractedEvents.write("\t\t\tif (event == 0) {\n")
518                         event = open("eventSimulator/thermostatOperatingStateAutoEvent.groovy", "r")                    
519                         for line in event:
520                                 extractedEvents.write("\t\t" + line)
521                         event.close()
522                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
523                         event = open("eventSimulator/thermostatOperatingStateCoolEvent.groovy", "r")
524                         for line in event:
525                                 extractedEvents.write("\t\t" + line)
526                         event.close()
527                         extractedEvents.write("\t\t\t} else if (event == 2) {\n")
528                         event = open("eventSimulator/thermostatOperatingStateOffEvent.groovy", "r")
529                         for line in event:
530                                 extractedEvents.write("\t\t" + line)
531                         event.close()
532                         extractedEvents.write("\t\t\t} else if (event == 3) {\n")
533                         event = open("eventSimulator/thermostatOperatingStateEmergencyHeatEvent.groovy", "r")
534                         for line in event:
535                                 extractedEvents.write("\t\t" + line)
536                         event.close()
537                         extractedEvents.write("\t\t\t} else {\n")
538                         event = open("eventSimulator/thermostatOperatingStateHeatEvent.groovy", "r")
539                         for line in event:
540                                 extractedEvents.write("\t\t" + line)
541                         event.close()
542                         extractedEvents.write("\t\t\t}\n")
543                 elif eventList[i] == "switch":
544                         #Check which capability
545                         variable = eventVarMap[eventList[i]]
546                         if eventList[i] not in eventVarCounterMap.keys():
547                                 eventVarCounterMap[eventList[i]] = 1
548                                 eventVarCount = 0
549                         else:
550                                 eventVarCount = eventVarCounterMap[eventList[i]]
551                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
552                         capability = capabilityMap[variable[eventVarCount]]
553                         #Write two events subsequently
554                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
555                         extractedEvents.write("\t\t\tif (event == 0) {\n")
556                         if capability == "capability.switch":
557                                 event = open("eventSimulator/switchOnEvent.groovy", "r")
558                         elif capability == "capability.switchLevel":
559                                 event = open("eventSimulator/switchLevelOnEvent.groovy", "r")
560                         elif capability == "capability.relaySwitch":
561                                 event = open("eventSimulator/relaySwitchOnEvent.groovy", "r")
562                         elif capability == "capability.colorControl":
563                                 event = open("eventSimulator/colorControlSwitchOnEvent.groovy", "r")
564                         for line in event:
565                                 extractedEvents.write("\t\t" + line)
566                         event.close()
567                         extractedEvents.write("\t\t\t} else {\n")
568                         if capability == "capability.switch":
569                                 event = open("eventSimulator/switchOffEvent.groovy", "r")
570                         elif capability == "capability.switchLevel":
571                                 event = open("eventSimulator/switchLevelOffEvent.groovy", "r")
572                         elif capability == "capability.relaySwitch":
573                                 event = open("eventSimulator/relaySwitchOffEvent.groovy", "r")
574                         elif capability == "capability.colorControl":
575                                 event = open("eventSimulator/colorControlSwitchOffEvent.groovy", "r")
576                         for line in event:
577                                 extractedEvents.write("\t\t" + line)
578                         event.close()
579                         extractedEvents.write("\t\t\t}\n")
580                 elif eventList[i] == "location": #Case for Location event
581                         #Write three events subsequently
582                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
583                         extractedEvents.write("\t\t\tif (event == 0) {\n")
584                         event = open("eventSimulator/locationHomeEvent.groovy", "r")                    
585                         for line in event:
586                                 extractedEvents.write("\t\t" + line)
587                         event.close()
588                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
589                         event = open("eventSimulator/locationAwayEvent.groovy", "r")
590                         for line in event:
591                                 extractedEvents.write("\t\t" + line)
592                         event.close()
593                         extractedEvents.write("\t\t\t} else {\n")
594                         event = open("eventSimulator/locationNightEvent.groovy", "r")
595                         for line in event:
596                                 extractedEvents.write("\t\t" + line)
597                         event.close()
598                         extractedEvents.write("\t\t\t}\n")
599                 elif eventList[i] == "mode":
600                         #Write three events subsequently
601                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
602                         extractedEvents.write("\t\t\tif (event == 0) {\n")
603                         event = open("eventSimulator/modeHomeEvent.groovy", "r")                        
604                         for line in event:
605                                 extractedEvents.write("\t\t" + line)
606                         event.close()
607                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
608                         event = open("eventSimulator/modeAwayEvent.groovy", "r")
609                         for line in event:
610                                 extractedEvents.write("\t\t" + line)
611                         event.close()
612                         extractedEvents.write("\t\t\t} else {\n")
613                         event = open("eventSimulator/modeNightEvent.groovy", "r")
614                         for line in event:
615                                 extractedEvents.write("\t\t" + line)
616                         event.close()
617                         extractedEvents.write("\t\t\t}\n")
618                 elif eventList[i] == "acceleration":
619                         #Write two events subsequently
620                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
621                         extractedEvents.write("\t\t\tif (event == 0) {\n")
622                         event = open("eventSimulator/accelerationActiveEvent.groovy", "r")                      
623                         for line in event:
624                                 extractedEvents.write("\t\t" + line)
625                         event.close()
626                         extractedEvents.write("\t\t\t} else {\n")
627                         event = open("eventSimulator/accelerationInactiveEvent.groovy", "r")
628                         for line in event:
629                                 extractedEvents.write("\t\t" + line)
630                         event.close()
631                         extractedEvents.write("\t\t\t}\n")
632                 elif eventList[i] == "acceleration.active":
633                         event = open("eventSimulator/accelerationDefaultActiveEvent.groovy", "r")
634                         for line in event:
635                                 extractedEvents.write("\t\t" + line)
636                         event.close()
637                 elif eventList[i] == "acceleration.inactive":
638                         event = open("eventSimulator/accelerationDefaultInactiveEvent.groovy", "r")
639                         for line in event:
640                                 extractedEvents.write("\t\t" + line)
641                         event.close()
642                 elif eventList[i] == "sleeping":
643                         #Write two events subsequently
644                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
645                         extractedEvents.write("\t\t\tif (event == 0) {\n")
646                         event = open("eventSimulator/sleepSleepingEvent.groovy", "r")                   
647                         for line in event:
648                                 extractedEvents.write("\t\t" + line)
649                         event.close()
650                         extractedEvents.write("\t\t\t} else {\n")
651                         event = open("eventSimulator/sleepNotSleepingEvent.groovy", "r")
652                         for line in event:
653                                 extractedEvents.write("\t\t" + line)
654                         event.close()
655                         extractedEvents.write("\t\t\t}\n")
656                 elif eventList[i] == "goal":
657                         event = open("eventSimulator/stepGoalEvent.groovy", "r")
658                         for line in event:
659                                 extractedEvents.write(line)
660                         event.close()
661                 elif eventList[i] == "steps":
662                         event = open("eventSimulator/stepStepsEvent.groovy", "r")
663                         for line in event:
664                                 extractedEvents.write(line)
665                         event.close()
666                 elif eventList[i] == "color":
667                         event = open("eventSimulator/colorChangeEvent.groovy", "r")
668                         for line in event:
669                                 extractedEvents.write(line)
670                         event.close()
671                 elif eventList[i] == "colorTemperature":
672                         event = open("eventSimulator/colorTemperatureEvent.groovy", "r")
673                         for line in event:
674                                 extractedEvents.write(line)
675                         event.close()
676                 elif eventList[i] == "hue":
677                         event = open("eventSimulator/hueChangeEvent.groovy", "r")
678                         for line in event:
679                                 extractedEvents.write(line)
680                         event.close()
681                 elif eventList[i] == "saturation":
682                         event = open("eventSimulator/saturationChangeEvent.groovy", "r")
683                         for line in event:
684                                 extractedEvents.write(line)
685                         event.close()
686                 elif eventList[i] == "energy":
687                         event = open("eventSimulator/energyMeterEvent.groovy", "r")
688                         for line in event:
689                                 extractedEvents.write(line)
690                         event.close()
691                 elif eventList[i] == "power":
692                         event = open("eventSimulator/powerMeterEvent.groovy", "r")
693                         for line in event:
694                                 extractedEvents.write(line)
695                         event.close()
696                 elif eventList[i] == "illuminance":
697                         event = open("eventSimulator/illuminanceMeasurementEvent.groovy", "r")
698                         for line in event:
699                                 extractedEvents.write(line)
700                         event.close()
701                 elif eventList[i] == "humidity":
702                         event = open("eventSimulator/humidityMeasurementEvent.groovy", "r")
703                         for line in event:
704                                 extractedEvents.write(line)
705                         event.close()
706                 elif eventList[i] == "alarm":
707                         #Write four events subsequently
708                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,3)\n")
709                         extractedEvents.write("\t\t\tif (event == 0) {\n")
710                         event = open("eventSimulator/alarmBothEvent.groovy", "r")                       
711                         for line in event:
712                                 extractedEvents.write("\t\t" + line)
713                         event.close()
714                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
715                         event = open("eventSimulator/alarmSirenEvent.groovy", "r")
716                         for line in event:
717                                 extractedEvents.write("\t\t" + line)
718                         event.close()
719                         extractedEvents.write("\t\t\t} else if (event == 2) {\n")
720                         event = open("eventSimulator/alarmStrobeEvent.groovy", "r")
721                         for line in event:
722                                 extractedEvents.write("\t\t" + line)
723                         event.close()
724                         extractedEvents.write("\t\t\t} else {\n")
725                         event = open("eventSimulator/alarmOffEvent.groovy", "r")
726                         for line in event:
727                                 extractedEvents.write("\t\t" + line)
728                         event.close()
729                         extractedEvents.write("\t\t\t}\n")
730                 elif eventList[i] == "contact":
731                         #Check which capability
732                         variable = eventVarMap[eventList[i]]
733                         if eventList[i] not in eventVarCounterMap.keys():
734                                 eventVarCounterMap[eventList[i]] = 1
735                                 eventVarCount = 0
736                         else:
737                                 eventVarCount = eventVarCounterMap[eventList[i]]
738                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
739                         capability = capabilityMap[variable[eventVarCount]]
740                         #Write two events subsequently
741                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
742                         extractedEvents.write("\t\t\tif (event == 0) {\n")
743                         if capability == "capability.contactSensor":
744                                 event = open("eventSimulator/contactDefaultClosedEvent.groovy", "r")
745                         elif capability == "capability.valve":
746                                 event = open("eventSimulator/valveClosedEvent.groovy", "r")
747                         for line in event:
748                                 extractedEvents.write("\t\t" + line)
749                         event.close()
750                         extractedEvents.write("\t\t\t} else {\n")
751                         if capability == "capability.contactSensor":
752                                 event = open("eventSimulator/contactDefaultOpenEvent.groovy", "r")
753                         elif capability == "capability.valve":
754                                 event = open("eventSimulator/valveOpenEvent.groovy", "r")
755                         for line in event:
756                                 extractedEvents.write("\t\t" + line)
757                         event.close()
758                         extractedEvents.write("\t\t\t}\n")
759                 elif eventList[i] == "status":
760                         #Write three events subsequently
761                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
762                         extractedEvents.write("\t\t\tif (event == 0) {\n")
763                         event = open("eventSimulator/musicPlayerPlayingEvent.groovy", "r")                      
764                         for line in event:
765                                 extractedEvents.write("\t\t" + line)
766                         event.close()
767                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
768                         event = open("eventSimulator/musicPlayerStoppedEvent.groovy", "r")
769                         for line in event:
770                                 extractedEvents.write("\t\t" + line)
771                         event.close()
772                         extractedEvents.write("\t\t\t} else {\n")
773                         event = open("eventSimulator/musicPlayerPausedEvent.groovy", "r")
774                         for line in event:
775                                 extractedEvents.write("\t\t" + line)
776                         event.close()
777                         extractedEvents.write("\t\t\t}\n")
778                 elif eventList[i] == "level":
779                         #Check which capability
780                         variable = eventVarMap[eventList[i]]
781                         if eventList[i] not in eventVarCounterMap.keys():
782                                 eventVarCounterMap[eventList[i]] = 1
783                                 eventVarCount = 0
784                         else:
785                                 eventVarCount = eventVarCounterMap[eventList[i]]
786                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
787                         capability = capabilityMap[variable[eventVarCount]]
788                         if capability == "capability.musicPlayer":
789                                 event = open("eventSimulator/musicPlayerLevelEvent.groovy", "r")
790                         elif capability == "capability.switchLevel":
791                                 event = open("eventSimulator/switchLevelEvent.groovy", "r")
792                         for line in event:
793                                 extractedEvents.write(line)
794                         event.close()
795                 elif eventList[i] == "trackDescription":
796                         event = open("eventSimulator/musicPlayerTrackDescriptionEvent.groovy", "r")
797                         for line in event:
798                                 extractedEvents.write(line)
799                         event.close()
800                 elif eventList[i] == "trackData":
801                         event = open("eventSimulator/musicPlayerTrackDataEvent.groovy", "r")
802                         for line in event:
803                                 extractedEvents.write(line)
804                         event.close()
805                 elif eventList[i] == "mute":
806                         #Write two events subsequently
807                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
808                         extractedEvents.write("\t\t\tif (event == 0) {\n")
809                         event = open("eventSimulator/musicPlayerUnmutedEvent.groovy", "r")                      
810                         for line in event:
811                                 extractedEvents.write("\t\t" + line)
812                         event.close()
813                         extractedEvents.write("\t\t\t} else {\n")
814                         event = open("eventSimulator/musicPlayerMutedEvent.groovy", "r")
815                         for line in event:
816                                 extractedEvents.write("\t\t" + line)
817                         event.close()
818                         extractedEvents.write("\t\t\t}\n")
819                 elif eventList[i] == "temperature":
820                         #Check which capability
821                         variable = eventVarMap[eventList[i]]
822                         if eventList[i] not in eventVarCounterMap.keys():
823                                 eventVarCounterMap[eventList[i]] = 1
824                                 eventVarCount = 0
825                         else:
826                                 eventVarCount = eventVarCounterMap[eventList[i]]
827                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
828                         capability = capabilityMap[variable[eventVarCount]]
829                         if capability == "capability.thermostat":
830                                 event = open("eventSimulator/temperatureEvent.groovy", "r")
831                         elif capability == "capability.temperatureMeasurement":
832                                 event = open("eventSimulator/temperatureMeasurementEvent.groovy", "r")
833                         for line in event:
834                                 extractedEvents.write(line)
835                         event.close()
836                 elif eventList[i] == "heatingSetpoint":
837                         event = open("eventSimulator/heatingSetpointEvent.groovy", "r")
838                         for line in event:
839                                 extractedEvents.write(line)
840                         event.close()
841                 elif eventList[i] == "coolingSetpoint":
842                         event = open("eventSimulator/coolingSetpointEvent.groovy", "r")
843                         for line in event:
844                                 extractedEvents.write(line)
845                         event.close()
846                 elif eventList[i] == "thermostatSetpoint":
847                         event = open("eventSimulator/thermostatSetpointEvent.groovy", "r")
848                         for line in event:
849                                 extractedEvents.write(line)
850                         event.close()
851                         
852                 ###TODO: Add more events later
853                 extractedEvents.write("\t\t\tbreak\n")
854         extractedEvents.write("\t}\n")
855         extractedEvents.write("}\n")
856         
857 def CheckIfOnlyTouchEvents():
858         #Check and throw an error if it is all touch events
859         #This is called Direct-Direct interaction and we do not model-check for this case
860         onlyTouchEvents = True
861         for item in eventList:
862                 if item != "nfcTouch" and item != "app":
863                         onlyTouchEvents = False
864         if onlyTouchEvents is True and app1Subscribe is True and app2Subscribe is True:
865                 # Write error log file
866                 extractError = open("appCreationError.log", "w+")
867                 extractError.write("Direct-Direct Interaction detected: we are skipping this pair...\n")
868                 extractError.close()
869                 raise Exception("\n\nDirect-Direct Interaction detected: we are skipping this pair...\n\n")
870
871
872 #Extract objects to call functions from App1
873 F1 = open("Extractor/App1/App1.groovy", "r")
874 extractedFunctionsApp1 = open("Extractor/App1/extractedFunctionsApp1.groovy", "w+")
875 ExtractFunctions(F1, "App1")
876 F1.close()
877
878 #Extract objects to call functions from App2
879 F2 = open("Extractor/App2/App2.groovy", "r")
880 extractedFunctionsApp2 = open("Extractor/App2/extractedFunctionsApp2.groovy", "w+")
881 ExtractFunctions(F2, "App2")
882 F2.close()
883
884 #Prepare eventSimulator file while parsing the App1 and App2 files
885 extractedEvents = open("eventSimulator/eventSimulator.groovy", "w+")
886 CheckIfOnlyTouchEvents()
887 ExtractEvents(extractedEvents)
888 extractedEvents.close()
889
890 #Save the extracted methods and app1 in a same file to extract information
891 extractorFile = open("Extractor/extractorFile.groovy", "w+")
892 Extractor = open("Extractor/Extractor.groovy", "r")
893 F1 = open("Extractor/App1/App1.groovy", "r")
894
895 extractorFile.write("////////////////////\n")
896 extractorFile.write("@Field App\n")
897 extractorFile.write("App = \"App1\"")
898 extractorFile.write("\n")
899 for line in Extractor:
900         extractorFile.write(line)
901 extractorFile.write("\n\n")
902 for line in F1:
903         extractorFile.write(line)
904 extractorFile.close()
905 Extractor.close()
906 F1.close()
907 #Run the file to extract the objects
908 os.system("groovy -classpath lib/jpf.jar Extractor/extractorFile.groovy")
909
910
911 #Save the extracted methods and app2 in a same file to extract information
912 extractorFile = open("Extractor/extractorFile.groovy", "w+")
913 Extractor = open("Extractor/Extractor.groovy", "r")
914 F2 = open("Extractor/App2/App2.groovy", "r")
915
916 extractorFile.write("////////////////////\n")
917 extractorFile.write("@Field App\n")
918 extractorFile.write("App = \"App2\"")
919 extractorFile.write("\n")
920 for line in Extractor:
921         extractorFile.write(line)
922 extractorFile.write("\n\n")
923 for line in F2:
924         extractorFile.write(line)
925 #Run the file to extract the objects
926 extractorFile.close()
927 Extractor.close()
928 F2.close()
929 os.system("groovy -classpath lib/jpf.jar Extractor/extractorFile.groovy")
930
931