Cleaning up compiler; Adding import statements and package headers appropriately...
authorrtrimana <rtrimana@uci.edu>
Mon, 2 Apr 2018 23:25:20 +0000 (16:25 -0700)
committerrtrimana <rtrimana@uci.edu>
Mon, 2 Apr 2018 23:25:20 +0000 (16:25 -0700)
iotjava/Makefile
iotjava/iotpolicy/IoTCompiler.java

index 44237a78d51782db818233597c5e16155adc6569..a54c835eb4aaa2111faef5842cce42747168269d 100644 (file)
@@ -29,24 +29,6 @@ PHONY += compiler
 compiler:
        $(JAVAC) -cp .:$(PARSERJARS) -d $(BIN_DIR) iotpolicy/*.java
 
 compiler:
        $(JAVAC) -cp .:$(PARSERJARS) -d $(BIN_DIR) iotpolicy/*.java
 
-PHONY += run-compiler-dev
-run-compiler-dev:
-       cp ../localconfig/iotpolicy/development/*.pol $(BIN_DIR)/iotpolicy/
-       #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler testclasspolicy_profiling.pol testclassrequires_profiling.pol -cplus Cplus -java Java
-       cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler testclasspolicy.pol testclassrequires.pol callbackpolicy.pol callbackrequires.pol -cplus Cplus -java Java
-       #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler callbackpolicy.pol callbackrequires.pol testclasspolicy_advanced.pol testclassrequires_advanced.pol -cplus Cplus -java Java
-       #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler callbackpolicy.pol callbackrequires.pol testclasspolicy_callbacks.pol testclassrequires_callbacks.pol -cplus Cplus -java Java
-       #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler testclasspolicy_callbacks.pol testclassrequires_callbacks.pol callbackpolicy.pol callbackrequires.pol -cplus Cplus -java Java
-       #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler testclasspolicy_callbacks_three.pol testclassrequires_callbacks_three.pol callbackpolicy.pol callbackrequires.pol callbackpolicy_two.pol callbackrequires_two.pol -cplus Cplus -java Java
-
-# TODO: Can remove this later - just to test-compile the resulted files from the compiler
-PHONY += compile
-compile:
-       cp ./iotrmi/Java/basics/* $(BIN_DIR)/iotpolicy/output_files/Java
-       #cp ./iotrmi/C++/basics/* $(BIN_DIR)/iotpolicy/output_files/Cplus
-       #cd $(BIN_DIR)/iotpolicy/output_files; cp *.java ./Java
-       #cd $(BIN_DIR)/iotpolicy/output_files; cp *.hpp ./Cplus
-
 # Cleaning the compiler output
 PHONY += output-clean
 output-clean:
 # Cleaning the compiler output
 PHONY += output-clean
 output-clean:
@@ -113,14 +95,15 @@ PHONY += run-compiler-room
 run-compiler-room:
        cp ../localconfig/iotpolicy/Room/*.pol $(BIN_DIR)/iotpolicy/
        cp ../localconfig/iotpolicy/Room/*.req $(BIN_DIR)/iotpolicy/
 run-compiler-room:
        cp ../localconfig/iotpolicy/Room/*.pol $(BIN_DIR)/iotpolicy/
        cp ../localconfig/iotpolicy/Room/*.req $(BIN_DIR)/iotpolicy/
-       #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler room.pol roomsmart.req -java Java
-       cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler room.pol roomsmart.req -cplus C++
+       cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler room.pol roomsmart.req -java Java
+       #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler room.pol roomsmart.req -cplus C++
 
 PHONY += run-compiler-cam
 run-compiler-cam:
        cp ../localconfig/iotpolicy/AmcrestCamera/*.pol $(BIN_DIR)/iotpolicy/
        cp ../localconfig/iotpolicy/AmcrestCamera/*.req $(BIN_DIR)/iotpolicy/
 
 PHONY += run-compiler-cam
 run-compiler-cam:
        cp ../localconfig/iotpolicy/AmcrestCamera/*.pol $(BIN_DIR)/iotpolicy/
        cp ../localconfig/iotpolicy/AmcrestCamera/*.req $(BIN_DIR)/iotpolicy/
-       cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler amcrestcamera.pol smartlightscam.req motiondetection.pol motiondetection.req -java Java
+       cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler amcrestcamera.pol smartlightscam.req motiondetection.pol motiondetection.req -drv AmcrestCamera -cont SmartLightsController -java Java
+       #cd $(BIN_DIR)/iotpolicy; $(JAVA) -cp .:..:../$(PARSERJARS):../$(BIN_DIR) iotpolicy.IoTCompiler
 
 # IrrigationController
 PHONY += run-compiler-irrigation
 
 # IrrigationController
 PHONY += run-compiler-irrigation
index 58538c9c00d4455840045e03ac306452c36c4795..a5825a9141d835f9b9cae0424bc342417dda5765 100644 (file)
@@ -63,12 +63,17 @@ public class IoTCompiler {
        private static int portCount = 0;
        private static int countObjId = 1;                      // Always increment object Id for a new stub/skeleton
        private String mainClass;
        private static int portCount = 0;
        private static int countObjId = 1;                      // Always increment object Id for a new stub/skeleton
        private String mainClass;
+       private String driverClass;
+       private String controllerClass;
 
 
        /**
         * Class constants
         */
        private final static String OUTPUT_DIRECTORY = "output_files";
 
 
        /**
         * Class constants
         */
        private final static String OUTPUT_DIRECTORY = "output_files";
+       private final static String CODE_PREFIX = "iotcode";
+       private final static String INTERFACE_PACKAGE = "iotcode.interfaces";
+
 
        private enum ParamCategory {
 
 
        private enum ParamCategory {
 
@@ -102,6 +107,26 @@ public class IoTCompiler {
                dir = OUTPUT_DIRECTORY;
                subdir = null;
                mainClass = null;
                dir = OUTPUT_DIRECTORY;
                subdir = null;
                mainClass = null;
+               driverClass = null;
+               controllerClass = null;
+       }
+
+
+       /**
+        * setDriverClass() sets the name of the driver class.
+        */
+       public void setDriverClass(String _driverClass) {
+               
+               driverClass = _driverClass;
+       }
+
+
+       /**
+        * setControllerClass() sets the name of the controller class.
+        */
+       public void setControllerClass(String _controllerClass) {
+               
+               controllerClass = _controllerClass;
        }
 
 
        }
 
 
@@ -346,6 +371,7 @@ public class IoTCompiler {
                        Set<String> importClasses = getImportClasses(methods, intDecl);
                        List<String> stdImportClasses = getStandardJavaIntfaceImportClasses();
                        List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
                        Set<String> importClasses = getImportClasses(methods, intDecl);
                        List<String> stdImportClasses = getStandardJavaIntfaceImportClasses();
                        List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
+                       println("package " + INTERFACE_PACKAGE + ";\n");
                        printImportStatements(allImportClasses);
                        // Write interface header
                        println("");
                        printImportStatements(allImportClasses);
                        // Write interface header
                        println("");
@@ -393,6 +419,7 @@ public class IoTCompiler {
                                Set<String> importClasses = getImportClasses(methods, intDecl);
                                List<String> stdImportClasses = getStandardJavaIntfaceImportClasses();
                                List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
                                Set<String> importClasses = getImportClasses(methods, intDecl);
                                List<String> stdImportClasses = getStandardJavaIntfaceImportClasses();
                                List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
+                       println("package " + INTERFACE_PACKAGE + ";\n");
                                printImportStatements(allImportClasses);
                                // Write interface header
                                println("");
                                printImportStatements(allImportClasses);
                                // Write interface header
                                println("");
@@ -1124,10 +1151,15 @@ public class IoTCompiler {
                                Set<String> importClasses = getImportClasses(methods, intDecl);
                                List<String> stdImportClasses = getStandardJavaImportClasses();
                                List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
                                Set<String> importClasses = getImportClasses(methods, intDecl);
                                List<String> stdImportClasses = getStandardJavaImportClasses();
                                List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
-                               printImportStatements(allImportClasses); println("");
                                // Find out if there are callback objects
                                Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
                                boolean callbackExist = !callbackClasses.isEmpty();
                                // Find out if there are callback objects
                                Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
                                boolean callbackExist = !callbackClasses.isEmpty();
+                               if (callbackExist)
+                                       println("package " + controllerClass + ";\n");
+                               else
+                                   println("package " + CODE_PREFIX + "." + driverClass + ";\n");
+                               printImportStatements(allImportClasses); 
+                               println("\nimport " + INTERFACE_PACKAGE + ".*;\n");
                                // Write class header
                                println("public class " + newStubClass + " implements " + newIntface + " {\n");
                                // Write properties
                                // Write class header
                                println("public class " + newStubClass + " implements " + newIntface + " {\n");
                                // Write properties
@@ -2235,12 +2267,16 @@ public class IoTCompiler {
                        Set<String> importClasses = getImportClasses(methods, intDecl);
                        List<String> stdImportClasses = getStandardJavaImportClasses();
                        List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
                        Set<String> importClasses = getImportClasses(methods, intDecl);
                        List<String> stdImportClasses = getStandardJavaImportClasses();
                        List<String> allImportClasses = getAllLibClasses(stdImportClasses, importClasses);
-                       printImportStatements(allImportClasses);
                        // Find out if there are callback objects
                        Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
                        boolean callbackExist = !callbackClasses.isEmpty();
                        // Find out if there are callback objects
                        Set<String> callbackClasses = getCallbackClasses(methods, intDecl);
                        boolean callbackExist = !callbackClasses.isEmpty();
+                       if (callbackExist)
+                   println("package " + CODE_PREFIX + "." + driverClass + ";\n");
+               else
+                       println("package " + controllerClass + ";\n");
+                       printImportStatements(allImportClasses);
+                       println("\nimport " + INTERFACE_PACKAGE + ".*;\n");
                        // Write class header
                        // Write class header
-                       println("");
                        println("public class " + newSkelClass  + " implements " + intface + " {\n");
                        // Write properties
                        writePropertiesJavaSkeleton(intface, intDecl);
                        println("public class " + newSkelClass  + " implements " + intface + " {\n");
                        // Write properties
                        writePropertiesJavaSkeleton(intface, intDecl);
@@ -2610,8 +2646,8 @@ public class IoTCompiler {
                println(callbackType + "_Skeleton* skel" + counter + " = new " + callbackType + "_Skeleton(" + paramIdent + ", rmiComm, newObjIdSent);");
                println("IoTRMIUtil::mapSkel->insert(make_pair(" + paramIdent + ", skel" + counter + "));");
                println("IoTRMIUtil::mapSkelId->insert(make_pair(" + paramIdent + ", newObjIdSent));");
                println(callbackType + "_Skeleton* skel" + counter + " = new " + callbackType + "_Skeleton(" + paramIdent + ", rmiComm, newObjIdSent);");
                println("IoTRMIUtil::mapSkel->insert(make_pair(" + paramIdent + ", skel" + counter + "));");
                println("IoTRMIUtil::mapSkelId->insert(make_pair(" + paramIdent + ", newObjIdSent));");
-               println("thread th" + counter + " (&" + callbackType + "_Skeleton::___waitRequestInvokeMethod, std::ref(skel" + counter + 
-                       "), std::ref(skel" + counter +"));");
+               println("thread th" + counter + " (&" + callbackType + "_Skeleton::___waitRequestInvokeMethod, skel" + counter + 
+                       ", skel" + counter +");");
                println("th" + counter + ".detach();");
                println("while(!skel" + counter + "->didInitWaitInvoke());");
                println("}");
                println("th" + counter + ".detach();");
                println("while(!skel" + counter + "->didInitWaitInvoke());");
                println("}");
@@ -4175,7 +4211,7 @@ public class IoTCompiler {
                                helperMethod = helperMethod + methodNumId;
                        else
                                uniqueMethodIds.add(methodId);
                                helperMethod = helperMethod + methodNumId;
                        else
                                uniqueMethodIds.add(methodId);
-                       print(helperMethod + ", std::ref(skel), ");
+                       print(helperMethod + ", skel, ");
                        boolean structExists = writeInputCountVarStructCplusSkeleton(method, intDecl);
                        if (structExists)
                                print(", ");
                        boolean structExists = writeInputCountVarStructCplusSkeleton(method, intDecl);
                        if (structExists)
                                print(", ");
@@ -4388,8 +4424,11 @@ public class IoTCompiler {
                System.out.println("\tjava IoTCompiler [<main-policy-file> <req-policy-file>] [options]\n");
                System.out.println("\t\tTake one or more pairs of main-req policy files, and generate Java and/or C++ files\n");
                System.out.println("Options:");
                System.out.println("\tjava IoTCompiler [<main-policy-file> <req-policy-file>] [options]\n");
                System.out.println("\t\tTake one or more pairs of main-req policy files, and generate Java and/or C++ files\n");
                System.out.println("Options:");
-               System.out.println("\t-java\t<directory>\tGenerate Java stub files");
-               System.out.println("\t-cplus\t<directory>\tGenerate C++ stub files");
+               System.out.println("\t-cont\t<controller-class>\tSpecify controller class name");
+               System.out.println("\t-drv\t<driver-class>\t\tSpecify driver class name");
+               System.out.println("\t-cplus\t<directory>\t\tGenerate C++ stub files");
+               System.out.println("\t-java\t<directory>\t\tGenerate Java stub files\n");
+               System.out.println("\t\tNote: The options -cont and -drv have to be defined before -cplus and -java");
                System.out.println();
        }
 
                System.out.println();
        }
 
@@ -5169,33 +5208,55 @@ public class IoTCompiler {
        public static void main(String[] args) throws Exception {
 
                // If there is no argument or just "--help" or "-h", then invoke printUsage()
        public static void main(String[] args) throws Exception {
 
                // If there is no argument or just "--help" or "-h", then invoke printUsage()
-               if ((args[0].equals("-help") ||
+               if ((args.length == 0        ||
+                        args[0].equals("-help") ||
                         args[0].equals("--help")||
                         args[0].equals("--help")||
-                        args[0].equals("-h"))   ||
-                       (args.length == 0)) {
+                        args[0].equals("-h"))) {
 
                        IoTCompiler.printUsage();
 
                } else if (args.length > 1) {
 
                        IoTCompiler comp = new IoTCompiler();
 
                        IoTCompiler.printUsage();
 
                } else if (args.length > 1) {
 
                        IoTCompiler comp = new IoTCompiler();
-                       int i = 0;                              
+                       int i = 0;
+                       boolean driverDefined = false;
+                       boolean controllerDefined = false;
                        do {
                        do {
-                               // Parse main policy file
-                               ParseNode pnPol = IoTCompiler.parseFile(args[i]);
-                               // Parse "requires" policy file
-                               ParseNode pnReq = IoTCompiler.parseFile(args[i+1]);
-                               // Get interface name
-                               String intface = ParseTreeHandler.getOrigIntface(pnPol);
-                               comp.setDataStructures(intface, pnPol, pnReq);
-                               comp.getMethodsForIntface(intface);
+                           // Look for "-drv" and "-cont"
+                           // These two need to be defined to complete the stub/skeleton generation
+                           //  with the appropriate headers
+                               if (args[i].equals("-drv")) {
+                                       comp.setDriverClass(args[i+1]);
+                                       driverDefined = true;
+                               } else if (args[i].equals("-cont")) {
+                                       System.out.println("DEBUG: Controller: " + args[i+1]);
+                                       comp.setControllerClass(args[i+1]);
+                                       controllerDefined = true;
+                               } else {
+                                   // Parse main policy file
+                                   ParseNode pnPol = IoTCompiler.parseFile(args[i]);
+                                   // Parse "requires" policy file
+                                   ParseNode pnReq = IoTCompiler.parseFile(args[i+1]);
+                                   // Get interface name
+                                   String intface = ParseTreeHandler.getOrigIntface(pnPol);
+                                   comp.setDataStructures(intface, pnPol, pnReq);
+                                   comp.getMethodsForIntface(intface);
+                               }
                                i = i + 2;
                        // 1) Check if this is the last option before "-java" or "-cplus"
                                i = i + 2;
                        // 1) Check if this is the last option before "-java" or "-cplus"
-                       // 2) Check if this is really the last option (no "-java" or "-cplus")
+                       // 2) Check if this is really the last option
                        } while(!args[i].equals("-java") &&
                                        !args[i].equals("-cplus") &&
                                        (i < args.length));
 
                        } while(!args[i].equals("-java") &&
                                        !args[i].equals("-cplus") &&
                                        (i < args.length));
 
+                       // Generate error if we haven't seen -drv and -cont at this point
+                       if (!driverDefined || !controllerDefined) {
+                               String error = "IoTCompiler: ERROR - please provide arguments -drv and -cont " +
+                                               "to specify both driver and controller class names.\n" +
+                                               "Note: The two options have to come before -java and -cplus.\n";
+                               throw new Error(error);
+                       }
+
                        // Generate everything if we don't see "-java" or "-cplus"
                        if (i == args.length) {
                                comp.generateEnumJava();
                        // Generate everything if we don't see "-java" or "-cplus"
                        if (i == args.length) {
                                comp.generateEnumJava();