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