Translating array into vector in C++; supporting only List/ArrayList/list for Collect...
authorrtrimana <rtrimana@uci.edu>
Fri, 28 Oct 2016 18:12:58 +0000 (11:12 -0700)
committerrtrimana <rtrimana@uci.edu>
Fri, 28 Oct 2016 18:12:58 +0000 (11:12 -0700)
config/iotpolicy/camerapolicy.pol
config/iotpolicy/lightbulbpolicy.pol
iotjava/Makefile
iotjava/iotpolicy/IoTCompiler.java
iotjava/iotrmi/Java/IoTRMITypes.java
iotjava/iotrmi/Java/IoTRMIUtil.java

index 7565bca..c2aa98b 100644 (file)
@@ -3,8 +3,8 @@ public interface Camera {
     public void MethodA(int A, int B);
     public void MethodA(int A, int B, int AB);
     public int MethodB(int C, String D[]);
-    public String MethodC(String E, Map<String,Integer> F);
-    public float MethodD(Set<String> G, float H);
+    public String MethodC(String E, List<Integer> F);
+    public float MethodD(List<String> G, float H);
     public boolean MethodE(String I, boolean J);
        public void MethodF(LightBulb K);
 
@@ -13,14 +13,14 @@ public interface Camera {
                description = "Another description";
                method = "MethodA(int A, int B)";
                method = "MethodB(int C, String D[])";
-               method = "MethodC(String E, Map<String,Integer> F)";
+               method = "MethodC(String E, List<Integer> F)";
        }
 
        capability VideoRecording {
                description = "The quick brown fox jumps over the cool dog";
                method = "MethodA(int A, int B)";
                method = "MethodA(int A, int B, int AB)";
-               method = "MethodD(Set<String> G, float H)";
+               method = "MethodD(List<String> G, float H)";
        }
 
        capability BackupData {
index 900089c..3d0ea74 100644 (file)
@@ -3,21 +3,21 @@ public interface LightBulb {
     public void MethodA(int A, float B);
     public void MethodA(int A, float B, int AB);
     public int MethodB(String C, String D);
-    public byte MethodC(String E, Map<String,Integer> F);
+    public byte MethodC(String E, List<Integer> F);
 
        capability Flicker {
                description = "The quick brown fox jumps over the smart dog";
                description = "Another description";
                method = "MethodA(int A, float B)";
                method = "MethodB(String C, String D)";
-               method = "MethodC(String E, Map<String,Integer> F)";
+               method = "MethodC(String E, List<Integer> F)";
        }
 
        capability AutoOnOff {
                description = "The quick brown fox jumps over the cool dog";
                method = "MethodA(int A, float B)";
                method = "MethodA(int A, float B, int AB)";
-               method = "MethodC(String E, Map<String,Integer> F)";
+               method = "MethodC(String E, List<Integer> F)";
        }
 }
 
index 4b8c972..bd36e8b 100644 (file)
@@ -45,11 +45,11 @@ rmi:
 
 PHONY += run-rmiserver
 run-rmiserver:
-       $(JAVA) -cp .:$(BIN_DIR) iotrmi.Java.IoTRMIObject
+       $(JAVA) -cp .:$(BIN_DIR) iotrmi.Java.sample.TestClass_Skeleton
 
 PHONY += run-rmiclient
 run-rmiclient:
-       $(JAVA) -cp .:$(BIN_DIR) iotrmi.Java.IoTRMICall
+       $(JAVA) -cp .:$(BIN_DIR) iotrmi.Java.sample.TestClass_Stub
 
 PHONY += doc
 doc: iotruntime iotinstaller
index f4248f2..0afc699 100644 (file)
@@ -23,6 +23,8 @@ import iotpolicy.tree.CapabilityDecl;
 import iotpolicy.tree.InterfaceDecl;
 import iotpolicy.tree.RequiresDecl;
 
+import iotrmi.Java.IoTRMITypes;
+
 
 /** Class IoTCompiler is the main interface/stub compiler for
  *  files generation. This class calls helper classes
@@ -55,97 +57,6 @@ public class IoTCompiler {
         */
        private final static String OUTPUT_DIRECTORY = "output_files";
 
-       /**
-        * Primitive data types
-        */
-       private final static String[] primitives = new String[] {
-
-               "byte",
-               "Byte",
-               "short",
-               "Short",
-               "int",
-               "Integer",
-               "long",
-               "Long",
-               "float",
-               "Float",
-               "double",
-               "Double",
-               "boolean",
-               "Boolean",
-               "char",
-               "Character",
-               "string",
-               "String",
-               "void"
-       };
-
-       /**
-        * Primitive data types in C++ to map the primitives list
-        */
-       private final static String[] primitivesCplus = new String[] {
-
-               "char",
-               "char",
-               "short",
-               "short",
-               "int",
-               "int",
-               "long",
-               "long",
-               "float",
-               "float",
-               "double",
-               "double",
-               "bool",
-               "bool",
-               "char",
-               "char",
-               "string",
-               "string",
-               "void"
-       };
-
-       /**
-        * Non-primitive data types supported by this compiler
-        */
-       private final static String[] nonPrimitives = new String[] {
-
-               "Set",
-               "HashSet",
-               "Map",
-               "HashMap",
-               "List",
-               "ArrayList"
-       };
-
-       /**
-        * Non-primitive Java libraries based on the list above
-        */
-       private final static String[] nonPrimitiveJavaLibs = new String[] {
-
-               "java.util.Set",
-               "java.util.HashSet",
-               "java.util.Map",
-               "java.util.HashMap",
-               "java.util.List",
-               "java.util.ArrayList"
-       };
-
-       /**
-        * Non-primitive C++ libraries based on the list above
-        */
-       private final static String[] nonPrimitiveCplusLibs = new String[] {
-
-               "set",
-               "unordered_set",
-               "map",
-               "unordered_map",
-               "list",
-               "list"
-       };
-
        private enum ParamCategory {
 
                PRIMITIVES,             // All the primitive types, e.g. byte, short, int, long, etc.
@@ -162,11 +73,11 @@ public class IoTCompiler {
                mapIntDeclHand = new HashMap<String,DeclarationHandler>();
                mapInt2NewInts = new HashMap<String,Map<String,Set<String>>>();
                mapPrimitives = new HashMap<String,String>();
-                       arraysToMap(mapPrimitives, primitives, primitivesCplus);
+                       arraysToMap(mapPrimitives, IoTRMITypes.primitivesJava, IoTRMITypes.primitivesCplus);
                mapNonPrimitivesJava = new HashMap<String,String>();
-                       arraysToMap(mapNonPrimitivesJava, nonPrimitives, nonPrimitiveJavaLibs);
+                       arraysToMap(mapNonPrimitivesJava, IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitiveJavaLibs);
                mapNonPrimitivesCplus = new HashMap<String,String>();
-                       arraysToMap(mapNonPrimitivesCplus, nonPrimitives, nonPrimitiveCplusLibs);
+                       arraysToMap(mapNonPrimitivesCplus, IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
                pw = null;
                dir = OUTPUT_DIRECTORY;
                subdir = null;
@@ -284,7 +195,9 @@ public class IoTCompiler {
                                        // Check for params with driver class types and exchange it 
                                        //              with its remote interface
                                        String paramType = checkAndGetParamClass(methPrmTypes.get(i));
-                                       print(paramType + " " + methParams.get(i));
+                                       String paramComplete = checkAndGetCplusArray(paramType, methParams.get(i));
+                                       //print(paramType + " " + methParams.get(i));
+                                       print(paramComplete);
                                        // Check if this is the last element (don't print a comma)
                                        if (i != methParams.size() - 1) {
                                                print(", ");
@@ -341,7 +254,10 @@ public class IoTCompiler {
                                        //              with its remote interface
                                        String paramType = checkAndGetParamClass(methPrmTypes.get(i));
                                        paramType = checkAndGetCplusType(paramType);
-                                       print(paramType + " " + methParams.get(i));
+                                       // Check for arrays - translate into vector in C++
+                                       String paramComplete = checkAndGetCplusArray(paramType, methParams.get(i));
+                                       //print(paramType + " " + param);
+                                       print(paramComplete);
                                        // Check if this is the last element (don't print a comma)
                                        if (i != methParams.size() - 1) {
                                                print(", ");
@@ -447,7 +363,9 @@ public class IoTCompiler {
                                        for (int i = 0; i < methParams.size(); i++) {
 
                                                String methPrmType = checkAndGetCplusType(methPrmTypes.get(i));
-                                               print(methPrmType + " " + methParams.get(i));
+                                               String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
+                                               //print(methPrmType + " " + methParam);
+                                               print(methParamComplete);
                                                // Check if this is the last element (don't print a comma)
                                                if (i != methParams.size() - 1) {
                                                        print(", ");
@@ -553,12 +471,18 @@ public class IoTCompiler {
 
                                        List<String> methParams = intDecl.getMethodParams(method);
                                        List<String> methPrmTypes = intDecl.getMethodParamTypes(method);
+
+                                       //System.out.println("\n\nMethod param: " + intDecl.getMethodParams(method));
+                                       //System.out.println("\n\nMethod param type: " + intDecl.getMethodParamTypes(method));
+                                       //System.out.println("\n\n");
+
                                        print(convertType(intDecl.getMethodType(method)) + " " +
                                                intDecl.getMethodId(method) + "(");
                                        for (int i = 0; i < methParams.size(); i++) {
-
                                                String methPrmType = checkAndGetCplusType(methPrmTypes.get(i));
-                                               print(methPrmType + " " + methParams.get(i));
+                                               String methParamComplete = checkAndGetCplusArray(methPrmType, methParams.get(i));
+                                               //print(methPrmType + " " + methParam);
+                                               print(methParamComplete);
                                                // Check if this is the last element (don't print a comma)
                                                if (i != methParams.size() - 1) {
                                                        print(", ");
@@ -895,6 +819,22 @@ public class IoTCompiler {
        }
 
 
+       // Detect array declaration, e.g. int A[],
+       //              then generate "int A[]" in C++ as "vector<int> A"
+       private String checkAndGetCplusArray(String paramType, String param) {
+
+               String paramComplete = null;
+               // Check for array declaration
+               if (param.contains("[]")) {
+                       paramComplete = "vector<" + paramType + "> " + param.replace("[]","");
+               } else
+                       // Just return it as is if it's not an array
+                       paramComplete = paramType + " " + param;
+
+               return paramComplete;
+       }
+
+
        // Get simple types, e.g. HashSet for HashSet<...>
        // Basically strip off the "<...>"
        private String checkAndGetParamClass(String paramType) {
@@ -930,7 +870,11 @@ public class IoTCompiler {
                } else {
                // NULL value - this means policy files missing
                        throw new Error("IoTCompiler: Parameter type lookup failed for " + intface +
-                               "... Please provide the necessary policy files for user-defined types.");
+                               "... Please provide the necessary policy files for user-defined types." +
+                               " If this is an array please type the brackets after the variable name," +
+                               " e.g. \"String str[]\", not \"String[] str\"." +
+                               " If this is a Collections (Java) / STL (C++) type, this compiler only" +
+                               " supports List/ArrayList (Java) or list (C++).");
                }
        }
 
index a72c38d..9ae0485 100644 (file)
@@ -52,16 +52,16 @@ public class IoTRMITypes {
                "short",                // 2 bytes
                "int",                  // 4 bytes
                "int",                  // 4 bytes
-               "long",                 // 4 bytes
-               "long",                 // 4 bytes
+               "int64_t",              // 8 bytes
+               "int64_t",              // 8 bytes
                "float",                // 4 bytes
                "float",                // 4 bytes
                "double",               // 8 bytes
                "double",               // 8 bytes
                "bool",                 // 1 byte
                "bool",                 // 1 byte
-               "char",                 // 1 byte
-               "char",                 // 1 byte
+               "char",                 // 2 bytes - C++ is made to follow Java convention
+               "char",                 // 2 bytes -    i.e. 2 bytes for a char
                "string",               // indefinite
                "string",               // indefinite
                "void"                  // 0 byte
@@ -71,32 +71,37 @@ public class IoTRMITypes {
        /**
         * Primitive sizes in Java - Long is 8 bytes and char is 2 bytes
         */
-       public final static Integer[] primitivesJavaSizes = new Integer[] {
+       public final static Integer[] primitivesSizes = new Integer[] {
 
                1, 1, 2, 2, 4, 4, 8, 8, 4, 4, 8, 8, 1, 1, 2, 2, -1, -1, 0
        };
 
 
        /**
-        * Primitive sizes in Cplus - Long is 4 bytes and char is 1 byte
+        * Non-primitive Java data types
         */
-       public final static Integer[] primitivesCplusSizes = new Integer[] {
+       public final static String[] nonPrimitivesJava = new String[] {
 
-               1, 1, 2, 2, 4, 4, 4, 4, 4, 4, 8, 8, 1, 1, 1, 1, -1, -1, 0
+               //"Set",
+               //"HashSet",
+               //"Map",
+               //"HashMap",
+               "List",
+               "ArrayList"
        };
 
 
        /**
-        * Non-primitive Java data types
+        * Non-primitive Java libraries based on the list above
         */
-       public final static String[] nonPrimitivesJava = new String[] {
-
-               "Set",
-               "HashSet",
-               "Map",
-               "HashMap",
-               "List",
-               "ArrayList"
+       public final static String[] nonPrimitiveJavaLibs = new String[] {
+
+               //"java.util.Set",
+               //"java.util.HashSet",
+               //"java.util.Map",
+               //"java.util.HashMap",
+               "java.util.List",
+               "java.util.ArrayList"
        };
 
 
@@ -105,10 +110,10 @@ public class IoTRMITypes {
         */
        public final static String[] nonPrimitivesCplus = new String[] {
 
-               "set",
-               "unordered_set",
-               "map",
-               "unordered_map",
+               //"set",
+               //"unordered_set",
+               //"map",
+               //"unordered_map",
                "list",
                "list"
        };
index 7816106..91496ed 100644 (file)
@@ -31,8 +31,7 @@ public class IoTRMIUtil {
         * Class Properties
         */
        private Map<String,String> mapPrimitives;
-       private Map<String,Integer> mapPrimitiveSizesJava;
-       private Map<String,Integer> mapPrimitiveSizesCplus;
+       private Map<String,Integer> mapPrimitiveSizes;
        private Map<String,String> mapNonPrimitives;
 
        /**
@@ -58,12 +57,9 @@ public class IoTRMIUtil {
                mapPrimitives = new HashMap<String,String>();
                        IoTRMITypes.arraysToMap(mapPrimitives, 
                                IoTRMITypes.primitivesJava, IoTRMITypes.primitivesCplus);
-               mapPrimitiveSizesJava = new HashMap<String,Integer>();
-                       IoTRMITypes.arraysToMap(mapPrimitiveSizesJava, 
-                               IoTRMITypes.primitivesJava, IoTRMITypes.primitivesJavaSizes);
-               mapPrimitiveSizesCplus = new HashMap<String,Integer>();
-                       IoTRMITypes.arraysToMap(mapPrimitiveSizesCplus, 
-                               IoTRMITypes.primitivesCplus, IoTRMITypes.primitivesCplusSizes);
+               mapPrimitiveSizes = new HashMap<String,Integer>();
+                       IoTRMITypes.arraysToMap(mapPrimitiveSizes, 
+                               IoTRMITypes.primitivesJava, IoTRMITypes.primitivesSizes);
                mapNonPrimitives = new HashMap<String,String>();
                        IoTRMITypes.arraysToMap(mapNonPrimitives, 
                                IoTRMITypes.nonPrimitivesJava, IoTRMITypes.nonPrimitivesCplus);
@@ -107,12 +103,10 @@ public class IoTRMIUtil {
         */
        public int getTypeSize(String type) {
 
-               if (mapPrimitiveSizesJava.containsKey(type))
-                       return mapPrimitiveSizesJava.get(type);
-               else if (mapPrimitiveSizesCplus.containsKey(type))
-                       return mapPrimitiveSizesCplus.get(type);
+               if (mapPrimitiveSizes.containsKey(type))
+                       return mapPrimitiveSizes.get(type);
                else
-                       return -1; // Size is unknown
+                       return -1; // Size is unknown (variable length)
        }