Fixing a bug in ExtractorScript.py: consuming too many tokens.
[smartthings-infrastructure.git] / Extractor / ExtractorScript.py
index e925f5ff6cc089b7ea5a76cb735116cab9285c76..4551ca0b2a17bdc68cb08e3d18116319ecd8bbd8 100644 (file)
@@ -1,10 +1,15 @@
+import os
 readyToReturn = 0
 ToReturn = ""
+eventMap = []
+app1Capabilities = []
+app2Capabilities = []
 
 def GetToken(f):
        global readyToReturn
        global ToReturn
-       Skip = ["(", "\"", ":", ",", "{", "}", ")", '\n', '\t', ' ', "/"]
+       Skip = ['\n', '\t', ' ']
+       Special = ["(", "\"", ":", ",", "{", "}", ")", "/", "*"]
        S = ""
        if (readyToReturn):
                readyToReturn = 0
@@ -12,242 +17,257 @@ def GetToken(f):
        ToReturn = ""
        c = f.read(1)
        while(True):
-               if (c in Skip):
+               if (c in Special):
                        if (S != ""):
-                               if (c == "{" or c == "}"):
-                                       readyToReturn = 1
-                                       ToReturn = c
-                                       return S
-                               else:
-                                       return S
-                                       
+                               readyToReturn = 1
+                               ToReturn = c
+                               return S
                        else:
-                               if (c == "{" or c == "}"):
-                                       return c
-                               else:
-                                       c = f.read(1)
-                                       continue
+                               return c
+               elif (c in Skip):
+                       if (S != ""):
+                               return S        
+                       else:
+                               c = f.read(1)
+                               continue
                S += c
                c = f.read(1)
                if not c:
                        return "EOF"
-               
-#For both apps 
-outGlobal = open("Extractor/outGlobal.groovy", "w+")
-lockIsSet = 0
-ContactIsSet = 0
-SwitchIsSet = 0
-
-
-def ExtractorFunc(F, outApp, outConstructorApp, Temp):
-       global outGlobal
-       global lockIsSet
-       global ContactIsSet
-       global SwitchIsSet
 
+def ExtractFunctions(F, appName):
+       global eventMap
+       Temp = GetToken(F)
        while (Temp != "EOF"):
-               #Extract the global objects for input
-               if (Temp == "input"):
-                       Object = ""
-                       Type = ""
-                       Temp = GetToken(F) #name or "name"
-                       #input name: "name", type: "type",...
-                       if (Temp == "name"):
-                               Temp = GetToken(F) #"name"
-                               Object = Temp
-                               GetToken(F) #type
-                               Temp = GetToken(F) #"type"
-                               Type = Temp             
-                       #input "name", "type",...
-                       else:
-                               Object = Temp
-                               Temp = GetToken(F) #"type"
-                               Type = Temp
+               if (Temp == "def" or Temp == "private"):
                        Temp = GetToken(F)
-                       Title = ""
-                       Required = ""
-                       Multiple = ""
-                       while (Temp != "input" and Temp != "}"):
-                               if (Temp == "title"):
-                                       Temp = GetToken(F)
-                                       Title = Temp
-                               elif (Temp == "required"):
-                                       Temp = GetToken(F)
-                                       Required = Temp
-                               elif (Temp == "multiple"):
-                                       Temp = GetToken(F)
-                                       Multiple = Temp
-                               Temp = GetToken(F)                      
-                       if (Type == "capability.lock"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               if (Multiple != "" and Multiple == "true" and lockIsSet != 1):
-                                       lockIsSet = 1
-                                       g = raw_input("Enter the number of locks to control: (1, 2, or 3)\n") 
-                                       outGlobal.write("//Global Object for class lock!\n")
-                                       outGlobal.write("@Field def lockObject = new Locking(sendEvent, ")
-                                       outGlobal.write("%s)\n" % g)
-                               elif ((Multiple == "" or Multiple == "false") and lockIsSet != 1):
-                                       lockIsSet = 1
-                                       outGlobal.write("//Global Object for class lock!\n")
-                                       outGlobal.write("@Field def lockObject = new Locking(sendEvent, 1)\n")
-                               outApp.write("//Object for class lock!\n")
-                               outApp.write("def %s\n" % Object)
-                               outConstructorApp.write("%s = obj.lockObject\n" % Object)
-                       #elif (Type == "capability.alarm"):
-
-                       #elif (Type == "capability.battery"):
-
-                       #elif (Type == "capability.beacon"):
-
-                       #elif (Type == "capability.carbonMonoxideDetector"):
-
-                       #elif (Type == "capability.colorControl"):
-
-                       elif (Type == "capability.contactSensor"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               if (Multiple != "" and Multiple == "true" and ContactIsSet != 1):
-                                       ContactIsSet = 1
-                                       g = raw_input("Enter the number of contact sensors to control: (1, 2, or 3)\n") 
-                                       outGlobal.write("//Global Object for class contactSensor!\n")
-                                       outGlobal.write("@Field def contactObject = new Contacting(sendEvent, ")
-                                       outGlobal.write("%s)\n" % g)
-                               elif ((Multiple == "" or Multiple == "false") and ContactIsSet != 1):
-                                       ContactIsSet = 1
-                                       outGlobal.write("//Global Object for class contactSensor!\n")
-                                       outGlobal.write("@Field def contactObject = new Contacting(sendEvent, 1)\n")
-                               outApp.write("//Object for class contactSensor!\n")
-                               outApp.write("def %s\n" % Object)
-                               outConstructorApp.write("%s = obj.contactObject\n" % Object)
-                       #elif (Type == "capability.doorControl"):
-
-                       #elif (Type == "capability.energyMeter"):
-
-                       #elif (Type == "capability.illuminanceMeasurement"):
-
-                       #elif (Type == "capability.accelerationSensor"):
-
-                       #elif (Type == "capability.motionSensor"):
-
-                       #elif (Type == "capability.musicPlayer"):
-
-                       #elif (Type == "capability.powerMeter"):
-
-                       #elif (Type == "capability.presenceSensor"):
-
-                       #elif (Type == "capability.relativeHumidityMeasurement"):
-
-                       #elif (Type == "capability.relaySwitch"):
-
-                       #elif (Type == "capability.sleepSensor"):
-
-                       #elif (Type == "capability.smokeDetector"):
-
-                       #elif (Type == "capability.stepSensor"):
-
-                       elif (Type == "capability.switch"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               if (Multiple != "" and Multiple == "true" and SwitchIsSet != 1):
-                                       SwitchIsSet = 1
-                                       g = raw_input("Enter the number of switches to control: (1, 2, or 3)\n") 
-                                       outGlobal.write("//Global Object for class Switch!\n")
-                                       outGlobal.write("@Field def switchObject = new Switching(sendEvent, ")
-                                       outGlobal.write("%s)\n" % g)
-                               elif ((Multiple == "" or Multiple == "false") and SwitchIsSet != 1):
-                                       SwitchIsSet = 1
-                                       outGlobal.write("//Global Object for class Switch!\n")
-                                       outGlobal.write("@Field def switchObject = new Switching(sendEvent, 1)\n")
-                               outApp.write("//Object for class Switch!\n")
-                               outApp.write("def %s\n" % Object)
-                               outConstructorApp.write("%s = obj.switchObject\n" % Object)
-                       #elif (Type == "capability.switchLevel"):
-
-                       #elif (Type == "capability.temperatureMeasurement"):
+                       NameofFunc = Temp
+                       if (GetToken(F) != "="): #We have a function to create object for
+                               if (appName == "App1"):
+                                       extractedFunctionsApp1.write("//Global Object for functions in subscribe method!\n")    
+                                       extractedFunctionsApp1.write("def %s = this.&" % NameofFunc)
+                                       extractedFunctionsApp1.write("%s\n" % NameofFunc)
+                               else:
+                                       extractedFunctionsApp2.write("//Global Object for functions in subscribe method!\n")    
+                                       extractedFunctionsApp2.write("def %s = this.&" % NameofFunc)
+                                       extractedFunctionsApp2.write("%s\n" % NameofFunc)
                
-                       #elif (Type == "capability.thermostat"):
-                       
-                       #elif (Type == "capability.valve"):
-
-                       #elif (Type == "capability.waterSensor"):
-
-                       #elif (Type == "capability.touchSensor"):
-
-                       #elif (Type == "capability.imageCapture"):
+               #Check subscribed events
+               if (Temp == "subscribe"):
+                       counter = 0
+                       while (counter < 5 and Temp != "\""):
+                               Temp = GetToken(F)
+                               counter = counter + 1
+                       Temp = GetToken(F)
+                       #If counter >= 5 that means it is not found, so it must be appTouch
+                       if (counter >= 5):
+                               Temp = "Touched"
+                       if Temp not in eventMap:
+                               eventMap.append(Temp)
 
-                       #elif (Type == "device.mobilePresence"):
+               #Check and analyze capabilities for physical interaction
+               AnalyzeCapabilities(Temp, appName)
 
-                       #elif (Type == "device.aeonKeyFob"):
+               Temp = GetToken(F)
+       
+       #Warn if there is a potential for physical interaction
+       AnalyzePhysicalInteraction(app1Capabilities, app2Capabilities)
+       AnalyzePhysicalInteraction(app2Capabilities, app1Capabilities)
 
-                       elif (Type == "mode"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               g = raw_input("Enter the mode: ") 
-                               outApp.write("//Global variable for mode!\n")
-                               outApp.write("def %s = " % Object)
-                               outApp.write("\"%s\"\n" % g)
-                       #elif (Type == "decimal"):
+               
 
-                       #elif (Type == "text"):
+def AnalyzeCapabilities(Temp, appName):
+                       #Illuminance related
+       if (Temp == "capability.switch" or
+                       Temp == "capability.switchLevel" or
+                       Temp == "capability.illuminanceMeasurement" or
+                       #Motion related
+                       Temp == "capability.motionSensor" or
+                       #Water related
+                       Temp == "capability.valve" or
+                       Temp == "capability.waterSensor" or
+                       #Sound related
+                       Temp == "capability.musicPlayer" or
+                       Temp == "capability.alarm" or
+                       Temp == "capability.speechSynthesis" or
+                       Temp == "capability.soundSensor"):
+               if (appName == "App1"):
+                       app1Capabilities.append(Temp)
+               else:
+                       app2Capabilities.append(Temp)
                        
-                       elif (Type == "number"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               g = raw_input("Enter the number: ") 
-                               outApp.write("//Global variable for number!\n")
-                               outApp.write("def %s = " % Object)
-                               outApp.write("%s\n" % g)
-                       #elif (Type == "time"):
-
-                       #elif (Type == "enum"):
-
-                       #elif (Type == "bool"):
-
-                       elif (Type == "phone"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               g = raw_input("Enter the number to send notification to:\n") 
-                               outApp.write("//Global variable for phone number!\n")
-                               outApp.write("def %s = " % Object)
-                               outApp.write("%s\n" % g)
-                       elif (Type == "contact"):
-                               if (Title != ""):
-                                       print(Object+", "+Title)
-                               g = raw_input("Enter the name of the recipients:\n") 
-                               outApp.write("//Global variable for recipients!\n")
-                               g = g.split()
-                               outApp.write("def %s = " % Object)
-                               outApp.write("%s\n" % g)
-               #Extract the global object for functions
-               elif (Temp == "def"):
-                       Temp = GetToken(F)
-                       NameofFunc = Temp
-                       if (GetToken(F) != "="): #We have a function to create object for
-                               outApp.write("//Global Object for functions in subscribe method!\n")    
-                               outApp.write("def %s = this.&" % NameofFunc)
-                               outApp.write("%s\n" % NameofFunc)
-               if (Temp != "input"):
-                       Temp = GetToken(F)
-       
-       F.close()
-       outApp.close()
-       outConstructorApp.close()
-
-#For app1
-F = open("Extractor/App1.groovy", "r")
-outApp = open("Extractor/extractedObjectsApp1.groovy", "w+")
-outConstructorApp = open("Extractor/extractedObjectsConstructorApp1.groovy", "w+")
-Temp = GetToken(F)
+def AnalyzePhysicalInteraction(app1Capab, app2Capab):
+       #Light
+       if ("capability.illuminanceMeasurement" in app1Capab) and ("capability.switch" in app2Capab or 
+                       "capability.switchLevel" in app2Capab):
+               print ("\nWARNING: Potential PHYSICAL CONFLICT (light) detected between App1 and App2!\n")
+       #Motion
+       if ("capability.motionSensor" in app1Capab):
+               print ("\nWARNING: Potential PHYSICAL CONFLICT (motion) detected between App1 and App2!\n")             
+       #Water
+       if ("capability.waterSensor" in app1Capab) and ("capability.valve" in app2Capab or 
+                       "capability.switch" in app2Capab):
+               print ("\nWARNING: Potential PHYSICAL CONFLICT (water) detected between App1 and App2!\n")
+       #Sound
+       if ("capability.soundSensor" in app1Capab) and ("capability.musicPlayer" in app2Capab or 
+                       "capability.alarm" in app2Capab or "capability.speechSynthesis" in app2Capab):
+               print ("\nWARNING: Potential PHYSICAL CONFLICT (sound) detected between App1 and App2!\n")
+
+def ExtractEvents(extractedEvents):
+       global eventMap
+       extractedEvents.write("Random random = new Random(42)\n")
+       extractedEvents.write("counter = 1000\n")
+       extractedEvents.write("while(counter > 0) {\n")
+       extractedEvents.write("\tdef eventNumber = random.nextInt(%d)\n" % (len(eventMap) - 1))
+       extractedEvents.write("\tswitch(eventNumber) {\n")
+       eventCounter = 0;
+       for i in range(len(eventMap)):
+               extractedEvents.write("\t\tcase %d:\n" % eventCounter)
+               if eventMap[i] == "lock":
+                       event = open("eventSimulator/lockEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "unlock":
+                       event = open("eventSimulator/unlockEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "contact.open":
+                       event = open("eventSimulator/contactOpenEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "contact.closed":
+                       event = open("eventSimulator/contactClosedEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "switch.on":
+                       event = open("eventSimulator/switchOnEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "switch.off":
+                       event = open("eventSimulator/switchOffEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "nfcTouch":
+                       event = open("eventSimulator/nfcTouchEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "Touched":
+                       event = open("eventSimulator/appTouchEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "button":
+                       #Write two events subsequently
+                       event = open("eventSimulator/buttonPushedEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+                       extractedEvents.write("\t\t\tbreak\n")
+                       eventCounter = eventCounter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % eventCounter)
+                       event = open("eventSimulator/buttonHeldEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "presence":
+                       #Write two events subsequently
+                       event = open("eventSimulator/presencePresentEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+                       extractedEvents.write("\t\t\tbreak\n")
+                       eventCounter = eventCounter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % eventCounter)
+                       event = open("eventSimulator/presenceLeftEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               elif eventMap[i] == "doorState":
+                       #Write two events subsequently
+                       event = open("eventSimulator/doorOpenEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+                       extractedEvents.write("\t\t\tbreak\n")
+                       eventCounter = eventCounter + 1
+                       extractedEvents.write("\t\tcase %d:\n" % eventCounter)
+                       event = open("eventSimulator/doorClosedEvent.groovy", "r")
+                       for line in event:
+                               extractedEvents.write(line)
+                       event.close()
+               eventCounter = eventCounter + 1
+
+               ###TODO: Add more events later
+               extractedEvents.write("\n\t\t\tbreak\n")
+       extractedEvents.write("\t}\n")
+       extractedEvents.write("\tcounter--\n")
+       extractedEvents.write("}\n")
+               
+#Extract objects to call functions from App1
+F1 = open("Extractor/App1/App1.groovy", "r")
+extractedFunctionsApp1 = open("Extractor/App1/extractedFunctionsApp1.groovy", "w+")
+ExtractFunctions(F1, "App1")
+F1.close()
+
+#Extract objects to call functions from App2
+F2 = open("Extractor/App2/App2.groovy", "r")
+extractedFunctionsApp2 = open("Extractor/App2/extractedFunctionsApp2.groovy", "w+")
+ExtractFunctions(F2, "App2")
+F2.close()
+
+#Prepare eventSimulator file while parsing the App1 and App2 files
+extractedEvents = open("eventSimulator/eventSimulator.groovy", "w+")
+ExtractEvents(extractedEvents)
+extractedEvents.close()
+
+#Save the extracted methods and app1 in a same file to extract information
+extractorFile = open("Extractor/extractorFile.groovy", "w+")
+Extractor = open("Extractor/Extractor.groovy", "r")
+F1 = open("Extractor/App1/App1.groovy", "r")
+
+extractorFile.write("////////////////////\n")
+extractorFile.write("@Field App\n")
+extractorFile.write("App = \"App1\"")
+extractorFile.write("\n")
+for line in Extractor:
+       extractorFile.write(line)
+extractorFile.write("\n\n")
+for line in F1:
+       extractorFile.write(line)
+extractorFile.close()
+Extractor.close()
+F1.close()
+#Run the file to extract the objects
+os.system("groovy Extractor/extractorFile.groovy")
+
+
+#Save the extracted methods and app2 in a same file to extract information
+extractorFile = open("Extractor/extractorFile.groovy", "w+")
+Extractor = open("Extractor/Extractor.groovy", "r")
+F2 = open("Extractor/App2/App2.groovy", "r")
+
+extractorFile.write("////////////////////\n")
+extractorFile.write("@Field App\n")
+extractorFile.write("App = \"App2\"")
+extractorFile.write("\n")
+for line in Extractor:
+       extractorFile.write(line)
+extractorFile.write("\n\n")
+for line in F2:
+       extractorFile.write(line)
+#Run the file to extract the objects
+extractorFile.close()
+Extractor.close()
+F2.close()
+os.system("groovy Extractor/extractorFile.groovy")
 
-ExtractorFunc(F, outApp, outConstructorApp, Temp)
 
-#For app2
-F = open("Extractor/App2.groovy", "r")
-outApp = open("Extractor/extractedObjectsApp2.groovy", "w+")
-outConstructorApp = open("Extractor/extractedObjectsConstructorApp2.groovy", "w+")
-Temp = GetToken(F)
 
-ExtractorFunc(F, outApp, outConstructorApp, Temp)
 
-outGlobal.close()