9e0f1c180218f8258ba8fccd439865af67cad752
[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
301                         extractedEvents.write("\t\t\t}\n")
302                         event.close()
303                 elif eventList[i] == "presence.present": #Case for Touched event
304                         event = open("eventSimulator/presencePresencePresentEvent.groovy", "r")
305                         for line in event:
306                                 extractedEvents.write("\t\t" + line)
307                         event.close()
308                 elif eventList[i] == "doorState":
309                         #Write two events subsequently
310                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
311                         extractedEvents.write("\t\t\tif (event == 0) {\n")
312                         event = open("eventSimulator/doorOpenEvent.groovy", "r")                        
313                         for line in event:
314                                 extractedEvents.write("\t\t" + line)
315                         event.close()
316                         extractedEvents.write("\t\t\t} else {\n")
317                         event = open("eventSimulator/doorClosedEvent.groovy", "r")
318                         for line in event:
319                                 extractedEvents.write("\t\t" + line)
320                         event.close()
321                         extractedEvents.write("\t\t\t}\n")
322                 elif eventList[i] == "motion":
323                         #Write two events subsequently
324                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
325                         extractedEvents.write("\t\t\tif (event == 0) {\n")
326                         event = open("eventSimulator/motionActiveEvent.groovy", "r")                    
327                         for line in event:
328                                 extractedEvents.write("\t\t" + line)
329                         event.close()
330                         extractedEvents.write("\t\t\t} else {\n")
331                         event = open("eventSimulator/motionInactiveEvent.groovy", "r")
332                         for line in event:
333                                 extractedEvents.write("\t\t" + line)
334                         event.close()
335                         extractedEvents.write("\t\t\t}\n")
336                 elif eventList[i] == "motion.active":
337                         event = open("eventSimulator/motionDefaultActiveEvent.groovy", "r")
338                         for line in event:
339                                 extractedEvents.write("\t\t" + line)
340                         event.close()
341                 elif eventList[i] == "motion.inactive":
342                         event = open("eventSimulator/motionDefaultInactiveEvent.groovy", "r")
343                         for line in event:
344                                 extractedEvents.write("\t\t" + line)
345                         event.close()
346                 elif eventList[i] == "smoke":
347                         #Write three events subsequently
348                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
349                         extractedEvents.write("\t\t\tif (event == 0) {\n")
350                         event = open("eventSimulator/smokeClearEvent.groovy", "r")                      
351                         for line in event:
352                                 extractedEvents.write("\t\t" + line)
353                         event.close()
354                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
355                         event = open("eventSimulator/smokeDetectedEvent.groovy", "r")
356                         for line in event:
357                                 extractedEvents.write("\t\t" + line)
358                         event.close()
359                         extractedEvents.write("\t\t\t} else {\n")
360                         event = open("eventSimulator/smokeTestedEvent.groovy", "r")
361                         for line in event:
362                                 extractedEvents.write("\t\t" + line)
363                         event.close()
364                         extractedEvents.write("\t\t\t}\n")
365                 elif eventList[i] == "smoke.clear":
366                         event = open("eventSimulator/smokeDefaultClearEvent.groovy", "r")
367                         for line in event:
368                                 extractedEvents.write("\t\t" + line)
369                         event.close()
370                 elif eventList[i] == "smoke.detected":
371                         event = open("eventSimulator/smokeDefaultDetectedEvent.groovy", "r")
372                         for line in event:
373                                 extractedEvents.write("\t\t" + line)
374                         event.close()
375                 elif eventList[i] == "smoke.tested":
376                         event = open("eventSimulator/smokeDefaultTestedEvent.groovy", "r")
377                         for line in event:
378                                 extractedEvents.write("\t\t" + line)
379                         event.close()
380                 elif eventList[i] == "carbonMonoxide":
381                         #Check which capability
382                         variable = eventVarMap[eventList[i]]
383                         if eventList[i] not in eventVarCounterMap.keys():
384                                 eventVarCounterMap[eventList[i]] = 1
385                                 eventVarCount = 0
386                         else:
387                                 eventVarCount = eventVarCounterMap[eventList[i]]
388                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
389                         capability = capabilityMap[variable[eventVarCount]]
390                         #Write three events subsequently
391                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
392                         extractedEvents.write("\t\t\tif (event == 0) {\n")                      
393                         if capability == "capability.smokeDetector":
394                                 event = open("eventSimulator/smokeCarbonMonoxideClearEvent.groovy", "r")
395                         elif capability == "capability.carbonMonoxideDetector":
396                                 event = open("eventSimulator/carbonMonoxideClearEvent.groovy", "r")
397                         for line in event:
398                                 extractedEvents.write("\t\t" + line)
399                         event.close()
400                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
401                         if capability == "capability.smokeDetector":
402                                 event = open("eventSimulator/smokeCarbonMonoxideDetectedEvent.groovy", "r")
403                         elif capability == "capability.carbonMonoxideDetector":
404                                 event = open("eventSimulator/carbonMonoxideDetectedEvent.groovy", "r")
405                         for line in event:
406                                 extractedEvents.write("\t\t" + line)
407                         event.close()
408                         extractedEvents.write("\t\t\t} else {\n")
409                         if capability == "capability.smokeDetector":
410                                 event = open("eventSimulator/smokeCarbonMonoxideTestedEvent.groovy", "r")
411                         elif capability == "capability.carbonMonoxideDetector":
412                                 event = open("eventSimulator/carbonMonoxideTestedEvent.groovy", "r")
413                         for line in event:
414                                 extractedEvents.write("\t\t" + line)
415                         event.close()
416                         extractedEvents.write("\t\t\t}\n")
417                 elif eventList[i] == "carbonMonoxide.clear":
418                         if capability == "capability.smokeDetector":
419                                 event = open("eventSimulator/smokeCarbonMonoxideDefaultClearEvent.groovy", "r")
420                         elif capability == "capability.carbonMonoxideDetector":
421                                 event = open("eventSimulator/carbonMonoxideDefaultClearEvent.groovy", "r")
422                         for line in event:
423                                 extractedEvents.write("\t\t" + line)
424                         event.close()
425                 elif eventList[i] == "carbonMonoxide.detected":
426                         if capability == "capability.smokeDetector":
427                                 event = open("eventSimulator/smokeCarbonMonoxideDefaultDetectedEvent.groovy", "r")
428                         elif capability == "capability.carbonMonoxideDetector":
429                                 event = open("eventSimulator/carbonMonoxideDefaultDetectedEvent.groovy", "r")
430                         for line in event:
431                                 extractedEvents.write("\t\t" + line)
432                         event.close()
433                 elif eventList[i] == "carbonMonoxide.tested":
434                         if capability == "capability.smokeDetector":
435                                 event = open("eventSimulator/smokeCarbonMonoxideDefaultTestedEvent.groovy", "r")
436                         elif capability == "capability.carbonMonoxideDetector":
437                                 event = open("eventSimulator/carbonMonoxideDefaultTestedEvent.groovy", "r")
438                         for line in event:
439                                 extractedEvents.write("\t\t" + line)
440                         event.close()
441                 elif eventList[i] == "battery":
442                         #Check which capability
443                         variable = eventVarMap[eventList[i]]
444                         if eventList[i] not in eventVarCounterMap.keys():
445                                 eventVarCounterMap[eventList[i]] = 1
446                                 eventVarCount = 0
447                         else:
448                                 eventVarCount = eventVarCounterMap[eventList[i]]
449                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
450                         capability = capabilityMap[variable[eventVarCount]]
451                         if capability == "capability.smokeDetector":
452                                 event = open("eventSimulator/smokeDetectorBatteryEvent.groovy", "r")
453                         elif capability == "capability.battery":
454                                 event = open("eventSimulator/batteryBatteryEvent.groovy", "r")
455                         for line in event:
456                                 extractedEvents.write(line)
457                         event.close()
458                 elif eventList[i] == "thermostatMode":
459                         #Write five events subsequently
460                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,4)\n")
461                         extractedEvents.write("\t\t\tif (event == 0) {\n")
462                         event = open("eventSimulator/thermostatAutoModeEvent.groovy", "r")                      
463                         for line in event:
464                                 extractedEvents.write("\t\t" + line)
465                         event.close()
466                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
467                         event = open("eventSimulator/thermostatCoolModeEvent.groovy", "r")
468                         for line in event:
469                                 extractedEvents.write("\t\t" + line)
470                         event.close()
471                         extractedEvents.write("\t\t\t} else if (event == 2) {\n")
472                         event = open("eventSimulator/thermostatEmergencyHeatModeEvent.groovy", "r")
473                         for line in event:
474                                 extractedEvents.write("\t\t" + line)
475                         event.close()
476                         extractedEvents.write("\t\t\t} else if (event == 3) {\n")
477                         event = open("eventSimulator/thermostatHeatModeEvent.groovy", "r")
478                         for line in event:
479                                 extractedEvents.write("\t\t" + line)
480                         event.close()
481                         extractedEvents.write("\t\t\t} else {\n")
482                         event = open("eventSimulator/thermostatOffModeEvent.groovy", "r")
483                         for line in event:
484                                 extractedEvents.write("\t\t" + line)
485                         event.close()
486                         extractedEvents.write("\t\t\t}\n")
487                 elif eventList[i] == "thermostatFanMode":
488                         #Write five events subsequently
489                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,4)\n")
490                         extractedEvents.write("\t\t\tif (event == 0) {\n")
491                         event = open("eventSimulator/thermostatAutoFanModeEvent.groovy", "r")                   
492                         for line in event:
493                                 extractedEvents.write("\t\t" + line)
494                         event.close()
495                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
496                         event = open("eventSimulator/thermostatFanCirculateFanModeEvent.groovy", "r")
497                         for line in event:
498                                 extractedEvents.write("\t\t" + line)
499                         event.close()
500                         extractedEvents.write("\t\t\t} else if (event == 2) {\n")
501                         event = open("eventSimulator/thermostatCirculateFanModeEvent.groovy", "r")
502                         for line in event:
503                                 extractedEvents.write("\t\t" + line)
504                         event.close()
505                         extractedEvents.write("\t\t\t} else if (event == 3) {\n")
506                         event = open("eventSimulator/thermostatFanOnFanModeEvent.groovy", "r")
507                         for line in event:
508                                 extractedEvents.write("\t\t" + line)
509                         event.close()
510                         extractedEvents.write("\t\t\t} else {\n")
511                         event = open("eventSimulator/thermostatOnFanModeEvent.groovy", "r")
512                         for line in event:
513                                 extractedEvents.write("\t\t" + line)
514                         event.close()
515                         extractedEvents.write("\t\t\t}\n")
516                 elif eventList[i] == "thermostatOperatingState":
517                         #Write five events subsequently
518                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,4)\n")
519                         extractedEvents.write("\t\t\tif (event == 0) {\n")
520                         event = open("eventSimulator/thermostatOperatingStateAutoEvent.groovy", "r")                    
521                         for line in event:
522                                 extractedEvents.write("\t\t" + line)
523                         event.close()
524                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
525                         event = open("eventSimulator/thermostatOperatingStateCoolEvent.groovy", "r")
526                         for line in event:
527                                 extractedEvents.write("\t\t" + line)
528                         event.close()
529                         extractedEvents.write("\t\t\t} else if (event == 2) {\n")
530                         event = open("eventSimulator/thermostatOperatingStateOffEvent.groovy", "r")
531                         for line in event:
532                                 extractedEvents.write("\t\t" + line)
533                         event.close()
534                         extractedEvents.write("\t\t\t} else if (event == 3) {\n")
535                         event = open("eventSimulator/thermostatOperatingStateEmergencyHeatEvent.groovy", "r")
536                         for line in event:
537                                 extractedEvents.write("\t\t" + line)
538                         event.close()
539                         extractedEvents.write("\t\t\t} else {\n")
540                         event = open("eventSimulator/thermostatOperatingStateHeatEvent.groovy", "r")
541                         for line in event:
542                                 extractedEvents.write("\t\t" + line)
543                         event.close()
544                         extractedEvents.write("\t\t\t}\n")
545                 elif eventList[i] == "switch":
546                         #Check which capability
547                         variable = eventVarMap[eventList[i]]
548                         if eventList[i] not in eventVarCounterMap.keys():
549                                 eventVarCounterMap[eventList[i]] = 1
550                                 eventVarCount = 0
551                         else:
552                                 eventVarCount = eventVarCounterMap[eventList[i]]
553                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
554                         capability = capabilityMap[variable[eventVarCount]]
555                         #Write two events subsequently
556                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
557                         extractedEvents.write("\t\t\tif (event == 0) {\n")
558                         if capability == "capability.switch":
559                                 event = open("eventSimulator/switchOnEvent.groovy", "r")
560                         elif capability == "capability.switchLevel":
561                                 event = open("eventSimulator/switchLevelOnEvent.groovy", "r")
562                         elif capability == "capability.relaySwitch":
563                                 event = open("eventSimulator/relaySwitchOnEvent.groovy", "r")
564                         elif capability == "capability.colorControl":
565                                 event = open("eventSimulator/colorControlSwitchOnEvent.groovy", "r")
566                         for line in event:
567                                 extractedEvents.write("\t\t" + line)
568                         event.close()
569                         extractedEvents.write("\t\t\t} else {\n")
570                         if capability == "capability.switch":
571                                 event = open("eventSimulator/switchOffEvent.groovy", "r")
572                         elif capability == "capability.switchLevel":
573                                 event = open("eventSimulator/switchLevelOffEvent.groovy", "r")
574                         elif capability == "capability.relaySwitch":
575                                 event = open("eventSimulator/relaySwitchOffEvent.groovy", "r")
576                         elif capability == "capability.colorControl":
577                                 event = open("eventSimulator/colorControlSwitchOffEvent.groovy", "r")
578                         for line in event:
579                                 extractedEvents.write("\t\t" + line)
580                         event.close()
581                         extractedEvents.write("\t\t\t}\n")
582                 elif eventList[i] == "location": #Case for Location event
583                         #Write three events subsequently
584                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
585                         extractedEvents.write("\t\t\tif (event == 0) {\n")
586                         event = open("eventSimulator/locationHomeEvent.groovy", "r")                    
587                         for line in event:
588                                 extractedEvents.write("\t\t" + line)
589                         event.close()
590                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
591                         event = open("eventSimulator/locationAwayEvent.groovy", "r")
592                         for line in event:
593                                 extractedEvents.write("\t\t" + line)
594                         event.close()
595                         extractedEvents.write("\t\t\t} else {\n")
596                         event = open("eventSimulator/locationNightEvent.groovy", "r")
597                         for line in event:
598                                 extractedEvents.write("\t\t" + line)
599                         event.close()
600                         extractedEvents.write("\t\t\t}\n")
601                 elif eventList[i] == "mode":
602                         #Write three events subsequently
603                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
604                         extractedEvents.write("\t\t\tif (event == 0) {\n")
605                         event = open("eventSimulator/modeHomeEvent.groovy", "r")                        
606                         for line in event:
607                                 extractedEvents.write("\t\t" + line)
608                         event.close()
609                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
610                         event = open("eventSimulator/modeAwayEvent.groovy", "r")
611                         for line in event:
612                                 extractedEvents.write("\t\t" + line)
613                         event.close()
614                         extractedEvents.write("\t\t\t} else {\n")
615                         event = open("eventSimulator/modeNightEvent.groovy", "r")
616                         for line in event:
617                                 extractedEvents.write("\t\t" + line)
618                         event.close()
619                         extractedEvents.write("\t\t\t}\n")
620                 elif eventList[i] == "acceleration":
621                         #Write two events subsequently
622                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
623                         extractedEvents.write("\t\t\tif (event == 0) {\n")
624                         event = open("eventSimulator/accelerationActiveEvent.groovy", "r")                      
625                         for line in event:
626                                 extractedEvents.write("\t\t" + line)
627                         event.close()
628                         extractedEvents.write("\t\t\t} else {\n")
629                         event = open("eventSimulator/accelerationInactiveEvent.groovy", "r")
630                         for line in event:
631                                 extractedEvents.write("\t\t" + line)
632                         event.close()
633                         extractedEvents.write("\t\t\t}\n")
634                 elif eventList[i] == "acceleration.active":
635                         event = open("eventSimulator/accelerationDefaultActiveEvent.groovy", "r")
636                         for line in event:
637                                 extractedEvents.write("\t\t" + line)
638                         event.close()
639                 elif eventList[i] == "acceleration.inactive":
640                         event = open("eventSimulator/accelerationDefaultInactiveEvent.groovy", "r")
641                         for line in event:
642                                 extractedEvents.write("\t\t" + line)
643                         event.close()
644                 elif eventList[i] == "sleeping":
645                         #Write two events subsequently
646                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
647                         extractedEvents.write("\t\t\tif (event == 0) {\n")
648                         event = open("eventSimulator/sleepSleepingEvent.groovy", "r")                   
649                         for line in event:
650                                 extractedEvents.write("\t\t" + line)
651                         event.close()
652                         extractedEvents.write("\t\t\t} else {\n")
653                         event = open("eventSimulator/sleepNotSleepingEvent.groovy", "r")
654                         for line in event:
655                                 extractedEvents.write("\t\t" + line)
656                         event.close()
657                         extractedEvents.write("\t\t\t}\n")
658                 elif eventList[i] == "goal":
659                         event = open("eventSimulator/stepGoalEvent.groovy", "r")
660                         for line in event:
661                                 extractedEvents.write(line)
662                         event.close()
663                 elif eventList[i] == "steps":
664                         event = open("eventSimulator/stepStepsEvent.groovy", "r")
665                         for line in event:
666                                 extractedEvents.write(line)
667                         event.close()
668                 elif eventList[i] == "color":
669                         event = open("eventSimulator/colorChangeEvent.groovy", "r")
670                         for line in event:
671                                 extractedEvents.write(line)
672                         event.close()
673                 elif eventList[i] == "colorTemperature":
674                         event = open("eventSimulator/colorTemperatureEvent.groovy", "r")
675                         for line in event:
676                                 extractedEvents.write(line)
677                         event.close()
678                 elif eventList[i] == "hue":
679                         event = open("eventSimulator/hueChangeEvent.groovy", "r")
680                         for line in event:
681                                 extractedEvents.write(line)
682                         event.close()
683                 elif eventList[i] == "saturation":
684                         event = open("eventSimulator/saturationChangeEvent.groovy", "r")
685                         for line in event:
686                                 extractedEvents.write(line)
687                         event.close()
688                 elif eventList[i] == "energy":
689                         event = open("eventSimulator/energyMeterEvent.groovy", "r")
690                         for line in event:
691                                 extractedEvents.write(line)
692                         event.close()
693                 elif eventList[i] == "power":
694                         event = open("eventSimulator/powerMeterEvent.groovy", "r")
695                         for line in event:
696                                 extractedEvents.write(line)
697                         event.close()
698                 elif eventList[i] == "illuminance":
699                         event = open("eventSimulator/illuminanceMeasurementEvent.groovy", "r")
700                         for line in event:
701                                 extractedEvents.write(line)
702                         event.close()
703                 elif eventList[i] == "humidity":
704                         event = open("eventSimulator/humidityMeasurementEvent.groovy", "r")
705                         for line in event:
706                                 extractedEvents.write(line)
707                         event.close()
708                 elif eventList[i] == "alarm":
709                         #Write four events subsequently
710                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,3)\n")
711                         extractedEvents.write("\t\t\tif (event == 0) {\n")
712                         event = open("eventSimulator/alarmBothEvent.groovy", "r")                       
713                         for line in event:
714                                 extractedEvents.write("\t\t" + line)
715                         event.close()
716                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
717                         event = open("eventSimulator/alarmSirenEvent.groovy", "r")
718                         for line in event:
719                                 extractedEvents.write("\t\t" + line)
720                         event.close()
721                         extractedEvents.write("\t\t\t} else if (event == 2) {\n")
722                         event = open("eventSimulator/alarmStrobeEvent.groovy", "r")
723                         for line in event:
724                                 extractedEvents.write("\t\t" + line)
725                         event.close()
726                         extractedEvents.write("\t\t\t} else {\n")
727                         event = open("eventSimulator/alarmOffEvent.groovy", "r")
728                         for line in event:
729                                 extractedEvents.write("\t\t" + line)
730                         event.close()
731                         extractedEvents.write("\t\t\t}\n")
732                 elif eventList[i] == "contact":
733                         #Check which capability
734                         variable = eventVarMap[eventList[i]]
735                         if eventList[i] not in eventVarCounterMap.keys():
736                                 eventVarCounterMap[eventList[i]] = 1
737                                 eventVarCount = 0
738                         else:
739                                 eventVarCount = eventVarCounterMap[eventList[i]]
740                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
741                         capability = capabilityMap[variable[eventVarCount]]
742                         #Write two events subsequently
743                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
744                         extractedEvents.write("\t\t\tif (event == 0) {\n")
745                         if capability == "capability.contactSensor":
746                                 event = open("eventSimulator/contactDefaultClosedEvent.groovy", "r")
747                         elif capability == "capability.valve":
748                                 event = open("eventSimulator/valveClosedEvent.groovy", "r")
749                         for line in event:
750                                 extractedEvents.write("\t\t" + line)
751                         event.close()
752                         extractedEvents.write("\t\t\t} else {\n")
753                         if capability == "capability.contactSensor":
754                                 event = open("eventSimulator/contactDefaultOpenEvent.groovy", "r")
755                         elif capability == "capability.valve":
756                                 event = open("eventSimulator/valveOpenEvent.groovy", "r")
757                         for line in event:
758                                 extractedEvents.write("\t\t" + line)
759                         event.close()
760                         extractedEvents.write("\t\t\t}\n")
761                 elif eventList[i] == "status":
762                         #Write three events subsequently
763                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,2)\n")
764                         extractedEvents.write("\t\t\tif (event == 0) {\n")
765                         event = open("eventSimulator/musicPlayerPlayingEvent.groovy", "r")                      
766                         for line in event:
767                                 extractedEvents.write("\t\t" + line)
768                         event.close()
769                         extractedEvents.write("\t\t\t} else if (event == 1) {\n")
770                         event = open("eventSimulator/musicPlayerStoppedEvent.groovy", "r")
771                         for line in event:
772                                 extractedEvents.write("\t\t" + line)
773                         event.close()
774                         extractedEvents.write("\t\t\t} else {\n")
775                         event = open("eventSimulator/musicPlayerPausedEvent.groovy", "r")
776                         for line in event:
777                                 extractedEvents.write("\t\t" + line)
778                         event.close()
779                         extractedEvents.write("\t\t\t}\n")
780                 elif eventList[i] == "level":
781                         #Check which capability
782                         variable = eventVarMap[eventList[i]]
783                         if eventList[i] not in eventVarCounterMap.keys():
784                                 eventVarCounterMap[eventList[i]] = 1
785                                 eventVarCount = 0
786                         else:
787                                 eventVarCount = eventVarCounterMap[eventList[i]]
788                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
789                         capability = capabilityMap[variable[eventVarCount]]
790                         if capability == "capability.musicPlayer":
791                                 event = open("eventSimulator/musicPlayerLevelEvent.groovy", "r")
792                         elif capability == "capability.switchLevel":
793                                 event = open("eventSimulator/switchLevelEvent.groovy", "r")
794                         for line in event:
795                                 extractedEvents.write(line)
796                         event.close()
797                 elif eventList[i] == "trackDescription":
798                         event = open("eventSimulator/musicPlayerTrackDescriptionEvent.groovy", "r")
799                         for line in event:
800                                 extractedEvents.write(line)
801                         event.close()
802                 elif eventList[i] == "trackData":
803                         event = open("eventSimulator/musicPlayerTrackDataEvent.groovy", "r")
804                         for line in event:
805                                 extractedEvents.write(line)
806                         event.close()
807                 elif eventList[i] == "mute":
808                         #Write two events subsequently
809                         extractedEvents.write("\t\t\tdef event = Verify.getInt(0,1)\n")
810                         extractedEvents.write("\t\t\tif (event == 0) {\n")
811                         event = open("eventSimulator/musicPlayerUnmutedEvent.groovy", "r")                      
812                         for line in event:
813                                 extractedEvents.write("\t\t" + line)
814                         event.close()
815                         extractedEvents.write("\t\t\t} else {\n")
816                         event = open("eventSimulator/musicPlayerMutedEvent.groovy", "r")
817                         for line in event:
818                                 extractedEvents.write("\t\t" + line)
819                         event.close()
820                         extractedEvents.write("\t\t\t}\n")
821                 elif eventList[i] == "temperature":
822                         #Check which capability
823                         variable = eventVarMap[eventList[i]]
824                         if eventList[i] not in eventVarCounterMap.keys():
825                                 eventVarCounterMap[eventList[i]] = 1
826                                 eventVarCount = 0
827                         else:
828                                 eventVarCount = eventVarCounterMap[eventList[i]]
829                                 eventVarCounterMap[eventList[i]] = eventVarCount + 1
830                         capability = capabilityMap[variable[eventVarCount]]
831                         if capability == "capability.thermostat":
832                                 event = open("eventSimulator/temperatureEvent.groovy", "r")
833                         elif capability == "capability.temperatureMeasurement":
834                                 event = open("eventSimulator/temperatureMeasurementEvent.groovy", "r")
835                         for line in event:
836                                 extractedEvents.write(line)
837                         event.close()
838                 elif eventList[i] == "heatingSetpoint":
839                         event = open("eventSimulator/heatingSetpointEvent.groovy", "r")
840                         for line in event:
841                                 extractedEvents.write(line)
842                         event.close()
843                 elif eventList[i] == "coolingSetpoint":
844                         event = open("eventSimulator/coolingSetpointEvent.groovy", "r")
845                         for line in event:
846                                 extractedEvents.write(line)
847                         event.close()
848                 elif eventList[i] == "thermostatSetpoint":
849                         event = open("eventSimulator/thermostatSetpointEvent.groovy", "r")
850                         for line in event:
851                                 extractedEvents.write(line)
852                         event.close()
853                         
854                 ###TODO: Add more events later
855                 extractedEvents.write("\t\t\tbreak\n")
856         extractedEvents.write("\t}\n")
857         extractedEvents.write("}\n")
858         
859 def CheckIfOnlyTouchEvents():
860         #Check and throw an error if it is all touch events
861         #This is called Direct-Direct interaction and we do not model-check for this case
862         onlyTouchEvents = True
863         for item in eventList:
864                 if item != "nfcTouch" and item != "app":
865                         onlyTouchEvents = False
866         if onlyTouchEvents is True and app1Subscribe is True and app2Subscribe is True:
867                 # Write error log file
868                 extractError = open("appCreationError.log", "w+")
869                 extractError.write("Direct-Direct Interaction detected: we are skipping this pair...\n")
870                 extractError.close()
871                 raise Exception("\n\nDirect-Direct Interaction detected: we are skipping this pair...\n\n")
872
873
874 #Extract objects to call functions from App1
875 F1 = open("Extractor/App1/App1.groovy", "r")
876 extractedFunctionsApp1 = open("Extractor/App1/extractedFunctionsApp1.groovy", "w+")
877 ExtractFunctions(F1, "App1")
878 F1.close()
879
880 #Extract objects to call functions from App2
881 F2 = open("Extractor/App2/App2.groovy", "r")
882 extractedFunctionsApp2 = open("Extractor/App2/extractedFunctionsApp2.groovy", "w+")
883 ExtractFunctions(F2, "App2")
884 F2.close()
885
886 #Prepare eventSimulator file while parsing the App1 and App2 files
887 extractedEvents = open("eventSimulator/eventSimulator.groovy", "w+")
888 CheckIfOnlyTouchEvents()
889 ExtractEvents(extractedEvents)
890 extractedEvents.close()
891
892 #Save the extracted methods and app1 in a same file to extract information
893 extractorFile = open("Extractor/extractorFile.groovy", "w+")
894 Extractor = open("Extractor/Extractor.groovy", "r")
895 F1 = open("Extractor/App1/App1.groovy", "r")
896
897 extractorFile.write("////////////////////\n")
898 extractorFile.write("@Field App\n")
899 extractorFile.write("App = \"App1\"")
900 extractorFile.write("\n")
901 for line in Extractor:
902         extractorFile.write(line)
903 extractorFile.write("\n\n")
904 for line in F1:
905         extractorFile.write(line)
906 extractorFile.close()
907 Extractor.close()
908 F1.close()
909 #Run the file to extract the objects
910 os.system("groovy -classpath lib/jpf.jar Extractor/extractorFile.groovy")
911
912
913 #Save the extracted methods and app2 in a same file to extract information
914 extractorFile = open("Extractor/extractorFile.groovy", "w+")
915 Extractor = open("Extractor/Extractor.groovy", "r")
916 F2 = open("Extractor/App2/App2.groovy", "r")
917
918 extractorFile.write("////////////////////\n")
919 extractorFile.write("@Field App\n")
920 extractorFile.write("App = \"App2\"")
921 extractorFile.write("\n")
922 for line in Extractor:
923         extractorFile.write(line)
924 extractorFile.write("\n\n")
925 for line in F2:
926         extractorFile.write(line)
927 #Run the file to extract the objects
928 extractorFile.close()
929 Extractor.close()
930 F2.close()
931 os.system("groovy -classpath lib/jpf.jar Extractor/extractorFile.groovy")
932
933