Adding feature for enum and struct generations
authorrtrimana <rtrimana@uci.edu>
Sat, 12 Nov 2016 01:03:58 +0000 (17:03 -0800)
committerrtrimana <rtrimana@uci.edu>
Sat, 12 Nov 2016 01:03:58 +0000 (17:03 -0800)
12 files changed:
config/iotpolicy/lightbulbpolicy.pol
iotjava/iotpolicy/IoTCompiler.java
iotjava/iotpolicy/tree/DeclarationHandler.java
iotjava/iotpolicy/tree/ParseTreeHandler.java
iotjava/iotpolicy/tree/StructDecl.java
iotjava/iotrmi/C++/IoTRMICall.cpp [new file with mode: 0644]
iotjava/iotrmi/C++/IoTRMIObject.cpp [new file with mode: 0644]
iotjava/iotrmi/C++/IoTSocketClient.cpp [new file with mode: 0644]
iotjava/iotrmi/C++/IoTSocketServer.cpp [new file with mode: 0644]
iotjava/iotrmi/C++/Test.cpp [new file with mode: 0644]
iotjava/iotrmi/C++/sample/EnumC.hpp
iotjava/iotrmi/Java/sample/TestClassInterface.java

index 3d131b6a3ddae2d20d1ba0d90604364bece5b1c2..b8796266fa49c58423c4f840935d1015b94b9a3f 100644 (file)
@@ -24,7 +24,8 @@ public interface LightBulb {
 
                APPLE,
                ORANGE,
-               GRAPE
+               GRAPE,
+               MANGO
        }
 }
 
index 3dbc21f297ecad90da6a589d3945aadcab9d5c2b..f3a1ce13398aea7bc5e886ae2d3ae9eadbd36eeb 100644 (file)
@@ -24,6 +24,8 @@ import iotpolicy.tree.DeclarationHandler;
 import iotpolicy.tree.CapabilityDecl;
 import iotpolicy.tree.InterfaceDecl;
 import iotpolicy.tree.RequiresDecl;
+import iotpolicy.tree.EnumDecl;
+import iotpolicy.tree.StructDecl;
 
 import iotrmi.Java.IoTRMITypes;
 
@@ -107,15 +109,26 @@ public class IoTCompiler {
                ParseTreeHandler ptHandler = new ParseTreeHandler(origInt, pnPol, pnReq);
                DeclarationHandler decHandler = new DeclarationHandler();
                // Process ParseNode and generate Declaration objects
+               // Interface
                ptHandler.processInterfaceDecl();
                InterfaceDecl intDecl = ptHandler.getInterfaceDecl();
                decHandler.addInterfaceDecl(origInt, intDecl);
+               // Capabilities
                ptHandler.processCapabilityDecl();
                CapabilityDecl capDecl = ptHandler.getCapabilityDecl();
                decHandler.addCapabilityDecl(origInt, capDecl);
+               // Requires
                ptHandler.processRequiresDecl();
                RequiresDecl reqDecl = ptHandler.getRequiresDecl();
                decHandler.addRequiresDecl(origInt, reqDecl);
+               // Enumeration
+               ptHandler.processEnumDecl();
+               EnumDecl enumDecl = ptHandler.getEnumDecl();
+               decHandler.addEnumDecl(origInt, enumDecl);
+               // Struct
+               ptHandler.processStructDecl();
+               StructDecl structDecl = ptHandler.getStructDecl();
+               decHandler.addStructDecl(origInt, structDecl);
 
                mapIntfacePTH.put(origInt, ptHandler);
                mapIntDeclHand.put(origInt, decHandler);
@@ -193,6 +206,55 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: writeEnumJava() writes the enumeration declaration
+        */
+       private void writeEnumJava(EnumDecl enumDecl) {
+
+               Set<String> enumTypes = enumDecl.getEnumDeclarations();
+               // Iterate over enum declarations
+               for (String enType : enumTypes) {
+
+                       println("public enum " + enType + " {");
+                       List<String> enumMembers = enumDecl.getMembers(enType);
+                       for (int i = 0; i < enumMembers.size(); i++) {
+
+                               String member = enumMembers.get(i);
+                               print(member);
+                               // Check if this is the last element (don't print a comma)
+                               if (i != enumMembers.size() - 1)
+                                       println(",");
+                               else
+                                       println("");
+                       }
+                       println("}\n");
+               }
+       }
+
+
+       /**
+        * HELPER: writeStructJava() writes the struct declaration
+        */
+       private void writeStructJava(StructDecl structDecl) {
+
+               List<String> structTypes = structDecl.getStructTypes();
+               // Iterate over enum declarations
+               for (String stType : structTypes) {
+
+                       println("public class " + stType + " {");
+                       List<String> structMemberTypes = structDecl.getMemberTypes(stType);
+                       List<String> structMembers = structDecl.getMembers(stType);
+                       for (int i = 0; i < structMembers.size(); i++) {
+
+                               String memberType = structMemberTypes.get(i);
+                               String member = structMembers.get(i);
+                               println("public static " + memberType + " " + member + ";");
+                       }
+                       println("}\n");
+               }
+       }
+
+
        /**
         * generateJavaLocalInterface() writes the local interface and provides type-checking.
         * <p>
@@ -218,6 +280,12 @@ public class IoTCompiler {
                        // Write interface header
                        println("");
                        println("public interface " + intface + " {");
+                       // Write enum if any...
+                       EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
+                       writeEnumJava(enumDecl);
+                       // Write struct if any...
+                       StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+                       writeStructJava(structDecl);
                        // Write methods
                        writeMethodJavaInterface(methods, intDecl);
                        println("}");
@@ -250,7 +318,7 @@ public class IoTCompiler {
                                printImportStatements(importClasses);
                                // Write interface header
                                println("");
-                               println("public interface " + newIntface + " {");
+                               println("public interface " + newIntface + " {\n");
                                // Write methods
                                writeMethodJavaInterface(intMeth.getValue(), intDecl);
                                println("}");
@@ -671,6 +739,57 @@ public class IoTCompiler {
        }
 
 
+       /**
+        * HELPER: writeEnumCplus() writes the enumeration declaration
+        */
+       private void writeEnumCplus(EnumDecl enumDecl) {
+
+               Set<String> enumTypes = enumDecl.getEnumDeclarations();
+               // Iterate over enum declarations
+               for (String enType : enumTypes) {
+
+                       println("enum " + enType + " {");
+                       List<String> enumMembers = enumDecl.getMembers(enType);
+                       for (int i = 0; i < enumMembers.size(); i++) {
+
+                               String member = enumMembers.get(i);
+                               print(member);
+                               // Check if this is the last element (don't print a comma)
+                               if (i != enumMembers.size() - 1)
+                                       println(",");
+                               else
+                                       println("");
+                       }
+                       println("};\n");
+               }
+       }
+
+
+       /**
+        * HELPER: writeStructCplus() writes the struct declaration
+        */
+       private void writeStructCplus(StructDecl structDecl) {
+
+               List<String> structTypes = structDecl.getStructTypes();
+               // Iterate over enum declarations
+               for (String stType : structTypes) {
+
+                       println("struct " + stType + " {");
+                       List<String> structMemberTypes = structDecl.getMemberTypes(stType);
+                       List<String> structMembers = structDecl.getMembers(stType);
+                       for (int i = 0; i < structMembers.size(); i++) {
+
+                               String memberType = structMemberTypes.get(i);
+                               String member = structMembers.get(i);
+                               String structTypeC = checkAndGetCplusType(memberType);
+                               String structComplete = checkAndGetCplusArray(structTypeC, member);
+                               println(structComplete + ";");
+                       }
+                       println("};\n");
+               }
+       }
+
+
        /**
         * generateCplusLocalInterfaces() writes the local interfaces and provides type-checking.
         * <p>
@@ -698,6 +817,12 @@ public class IoTCompiler {
                        Set<String> includeClasses = getIncludeClasses(methods, intDecl);
                        printIncludeStatements(includeClasses); println("");
                        println("using namespace std;\n");
+                       // Write enum if any...
+                       EnumDecl enumDecl = (EnumDecl) decHandler.getEnumDecl(intface);
+                       writeEnumCplus(enumDecl);
+                       // Write struct if any...
+                       StructDecl structDecl = (StructDecl) decHandler.getStructDecl(intface);
+                       writeStructCplus(structDecl);
                        println("class " + intface); println("{");
                        println("public:");
                        // Write methods
index 0b8089fd1db918bdabd972b6bd850e2aba2f1fbc..48856cad3f95ad6553d8efbbbdc2a0049b540849 100644 (file)
@@ -18,6 +18,8 @@ public class DeclarationHandler {
        private Map<String,Declaration> mapInt2IntfaceDecl;
        private Map<String,Declaration> mapInt2CapabDecl;
        private Map<String,Declaration> mapInt2ReqDecl;
+       private Map<String,Declaration> mapInt2EnumDecl;
+       private Map<String,Declaration> mapInt2StructDecl;
 
        /**
         * Class constructors
@@ -27,6 +29,8 @@ public class DeclarationHandler {
                mapInt2IntfaceDecl = new HashMap<String,Declaration>();
                mapInt2CapabDecl = new HashMap<String,Declaration>();
                mapInt2ReqDecl = new HashMap<String,Declaration>();
+               mapInt2EnumDecl = new HashMap<String,Declaration>();
+               mapInt2StructDecl = new HashMap<String,Declaration>();
        }
 
 
@@ -51,6 +55,18 @@ public class DeclarationHandler {
        }
 
 
+       public void addEnumDecl(String origInt, Declaration enumDecl) {
+
+               mapInt2EnumDecl.put(origInt, enumDecl);
+       }
+
+
+       public void addStructDecl(String origInt, Declaration structDecl) {
+
+               mapInt2StructDecl.put(origInt, structDecl);
+       }
+
+
        /**
         * Getters
         */
@@ -70,4 +86,16 @@ public class DeclarationHandler {
 
                return mapInt2ReqDecl.get(origInt);
        }
+
+
+       public Declaration getEnumDecl(String origInt) {
+
+               return mapInt2EnumDecl.get(origInt);
+       }
+
+
+       public Declaration getStructDecl(String origInt) {
+
+               return mapInt2StructDecl.get(origInt);
+       }
 }
index 0d2b1c524e8c8062f7220d58ac1adb00ccfbe94f..42f42f16e185c5235c7c6113700d505251355029 100644 (file)
@@ -204,6 +204,71 @@ public final class ParseTreeHandler {
        }
 
 
+       /**
+        * processEnumDecl() processes "enum" declaration part
+        */
+       public void processEnumDecl() {
+
+               ParseNodeVector pnv = pnPol.getChildren();
+               ParseNode pnRoot = pnv.elementAt(0);
+               ParseNodeVector pnvGen2 = pnRoot.getChildren();
+               // Get the third child of root for "enum"
+               ParseNode pnGen2 = pnvGen2.elementAt(3);
+               ParseNodeVector pnvGen3 = pnGen2.getChildren();
+               // Iterate over a list of enum declaration - can be empty too
+               for(int i = 0; i < pnvGen3.size(); i++) {
+
+                       ParseNode pnGen4 = pnvGen3.elementAt(i);
+                       ParseNodeVector pnvGen5 = pnGen4.getChildren();
+                       // Get the type of the enum
+                       ParseNode pnGen5_enum_ident = pnvGen5.elementAt(0);
+                       ParseNode pnGen5_enum_list = pnvGen5.elementAt(1);
+                       ParseNodeVector pnvGen6_members = pnGen5_enum_list.getChildren();
+                       // Browse through enum declarations
+                       for (int j = 0; j < pnvGen6_members.size(); j++) {
+                               ParseNode pnGen6 = pnvGen6_members.elementAt(j);
+                               ParseNodeVector pnvGen7 = pnGen6.getChildren();
+                               ParseNode pnGen7 = pnvGen7.elementAt(0);
+                               enumDecl.addNewMember(pnGen5_enum_ident.getLiteral().toString(),
+                                       pnGen7.getLiteral().toString());
+                       }
+               }
+       }
+
+
+       /**
+        * processStructDecl() processes "struct" declaration part
+        */
+       public void processStructDecl() {
+
+               ParseNodeVector pnv = pnPol.getChildren();
+               ParseNode pnRoot = pnv.elementAt(0);
+               ParseNodeVector pnvGen2 = pnRoot.getChildren();
+               // Get the fourth child of root for "struct"
+               ParseNode pnGen2 = pnvGen2.elementAt(4);
+               ParseNodeVector pnvGen3 = pnGen2.getChildren();
+               // Iterate over a list of struct declaration - can be empty too
+               for(int i = 0; i < pnvGen3.size(); i++) {
+
+                       ParseNode pnGen4 = pnvGen3.elementAt(i);
+                       ParseNodeVector pnvGen5 = pnGen4.getChildren();
+                       // Get the type of the enum
+                       ParseNode pnGen5_enum_ident = pnvGen5.elementAt(0);
+                       ParseNode pnGen5_enum_list = pnvGen5.elementAt(1);
+                       ParseNodeVector pnvGen6_members = pnGen5_enum_list.getChildren();
+                       // Browse through enum declarations
+                       for (int j = 0; j < pnvGen6_members.size(); j++) {
+                               ParseNode pnGen6 = pnvGen6_members.elementAt(j);
+                               ParseNodeVector pnvGen7 = pnGen6.getChildren();
+                               ParseNode pnGen7_type = pnvGen7.elementAt(0);
+                               ParseNode pnGen7_ident = pnvGen7.elementAt(1);
+                               structDecl.addNewMember(pnGen5_enum_ident.getLiteral().toString(),
+                                       pnGen7_type.getLiteral().toString(), pnGen7_ident.getLiteral().toString());
+                       }
+               }
+       }
+
+
        /**
         * getInterfaceDecl() returns InterfaceDecl object
         */
index 56a6fe47542617595d0cd7cb1a176178ee503951..ef58b55a614a6b57bcfcb1ae66001713a71c6e77 100644 (file)
@@ -67,6 +67,7 @@ public class StructDecl extends Declaration {
                        memberList.add(newMember);
                } else {
                // New declaration
+                       listStructs.add(structType);
                        List<String> newMemberTypeList = new ArrayList<String>();
                        newMemberTypeList.add(newMemberType);
                        listMemberTypes.add(newMemberTypeList);
diff --git a/iotjava/iotrmi/C++/IoTRMICall.cpp b/iotjava/iotrmi/C++/IoTRMICall.cpp
new file mode 100644 (file)
index 0000000..aefe533
--- /dev/null
@@ -0,0 +1,29 @@
+#include <iostream>
+#include <string>
+#include "IoTRMICall.hpp"
+
+using namespace std;
+
+int main(int argc, char *argv[])
+{
+       int port = 5010;
+       const char* address = "localhost";
+       int rev = 0;
+       bool bResult = false;
+
+       int numRet = 3;
+       string retCls[] = { "int", "string", "int" };
+       int param1 = 0;
+       string param2 = "";
+       int param3 = 0;
+       void* retObj[] = { &param1, &param2, &param3 };
+
+       IoTRMICall *rc = new IoTRMICall(port, address, rev, &bResult);
+       char retBytes[] = { 0, 0, 4, -46, 0, 0, 0, 10, 116, 101, 115, 116, 115, 116, 114, 105, 110, 103, 0, 0, 21, 56 };
+       rc->getReturnObjects(retBytes, retCls, numRet, retObj);
+       cout << "Param1: " << param1 << endl;
+       cout << "Param2: " << param2 << endl;
+       cout << "Param3: " << param3 << endl;
+
+       return 0;
+}
diff --git a/iotjava/iotrmi/C++/IoTRMIObject.cpp b/iotjava/iotrmi/C++/IoTRMIObject.cpp
new file mode 100644 (file)
index 0000000..a2e321a
--- /dev/null
@@ -0,0 +1,22 @@
+#include <iostream>
+#include <string>
+#include "IoTRMIObject.hpp"
+
+using namespace std;
+
+int main(int argc, char *argv[])
+{
+       int port = 5010;
+       bool bResult = false;
+       IoTRMIObject *ro = new IoTRMIObject(port, &bResult);
+
+       int numRet = 3;
+       string retCls[] = { "int", "string", "int" };
+       int param1 = 1234;
+       string param2 = "teststring";
+       int param3 = 5432;
+       void* retObj[] = { &param1, &param2, &param3 };
+       ro->sendReturnObj(retObj, retCls, numRet);
+       
+       return 0;
+}
diff --git a/iotjava/iotrmi/C++/IoTSocketClient.cpp b/iotjava/iotrmi/C++/IoTSocketClient.cpp
new file mode 100644 (file)
index 0000000..ce9f0a5
--- /dev/null
@@ -0,0 +1,60 @@
+#include <iostream>
+#include <string>
+#include "IoTSocketClient.hpp"
+
+using namespace std;
+
+#define SIZE 10                         /* how many items per packet */
+#define NUM_PACKS 3     /* number of times we'll do it */
+
+int main(int argc, char *argv[])
+{
+       char D[SIZE];
+
+       /* if no command line arguments passed, we'll default to 
+               these two port number */
+       int port = 5010;
+       int rev = 0;
+       bool bResult = false;
+
+       fflush(NULL);
+       IoTSocketClient mylink(port, "127.0.0.1", rev, &bResult);
+
+       if (!bResult)
+       {
+               printf("Failed to create Client object!\n");
+               return 0;
+       }       
+
+       printf("Client, made connection...\n");
+       fflush(NULL);
+
+       /* put some dummy data in our arrays */
+
+       for (int i = 0; i < SIZE; i++)
+       {
+               D[i] = i;
+       }
+
+       for (int i = 0; i < NUM_PACKS; i++)
+       {
+               printf("Client, receiving bytes, iteration %d\n", i);
+               fflush(NULL);
+               mylink.receiveBytes(D);
+       }
+
+       char str[50];
+       char* str2;
+       fflush(NULL);
+       str2 = mylink.receiveBytes(str);
+       string s(str2);
+       cout << "Received text: " << s << endl;
+
+       printf("Client, closing connection...\n");
+       fflush(NULL);
+       mylink.close();
+
+       printf("Client, done...\n");
+       fflush(NULL);
+       return 0;
+}
diff --git a/iotjava/iotrmi/C++/IoTSocketServer.cpp b/iotjava/iotrmi/C++/IoTSocketServer.cpp
new file mode 100644 (file)
index 0000000..95e1e60
--- /dev/null
@@ -0,0 +1,54 @@
+#include <iostream>
+#include <string>
+#include "IoTSocketServer.hpp"
+#include "IoTRMIUtil.hpp"
+
+using namespace std;
+
+
+#define SIZE 10                  /* how many items per packet */
+#define NUM_PACKS 3   /* number of times we'll do it */
+
+int main(int argc, char *argv[])
+{
+       char D[SIZE];
+       bool bResult = false;
+
+       /* if no command line arguments passed, we'll default to 
+               these two port number */
+       int port = 5010;
+       
+       fflush(NULL);
+
+       IoTSocketServer mylink(port, &bResult);
+       if (!bResult)
+       {
+               printf("Failed to create Server object!\n");
+               return 0;
+       }
+
+       /* put some dummy data in our arrays */
+       for (int i = 0,j = 100; i < SIZE; i++, j--)
+       {
+               //D[i] = i;
+               D[i] = j;
+       }
+       printf("Server, waiting for connection...\n");
+       fflush(NULL);
+       mylink.connect();
+       printf("Server, got a connection...\n");
+       fflush(NULL);
+
+       char bytes[24];
+       mylink.receiveBytes(bytes);
+       cout << "Received bytes: ";
+       IoTRMIUtil::printBytes(bytes, 24, false);
+
+       printf("Server, closing connection...\n");
+       fflush(NULL);
+       mylink.close();
+
+       printf("Server, done...\n");
+       fflush(NULL);
+       return 0;
+}
diff --git a/iotjava/iotrmi/C++/Test.cpp b/iotjava/iotrmi/C++/Test.cpp
new file mode 100644 (file)
index 0000000..7c77d30
--- /dev/null
@@ -0,0 +1,37 @@
+#include <iostream>
+#include <string>
+#include <cstring>
+#include "IoTRMITypes.hpp"
+
+using namespace std;
+
+
+int main(int argc, char *argv[])
+{
+       std::array<int,5> myints;
+       std::cout << "size of myints: " << myints.size() << std::endl;
+       std::cout << "sizeof(myints): " << sizeof(myints) << std::endl;
+       
+       int test[5] = { 0 };
+       std::memcpy(myints.data(), test, 5);
+
+       std::vector<int> test2 (test, test + sizeof(test)/sizeof(int));
+
+       string test3[2] = { "test1", "test2" };
+       std::vector<string> test4 (test3, test3 + sizeof(test3)/sizeof(string));
+       std::cout << "vector[0]: " << test4[0] << std::endl;
+
+       std::vector<string> primJava (IoTRMITypes::primitivesJava, 
+               IoTRMITypes::primitivesJava + sizeof(IoTRMITypes::primitivesJava)/sizeof(string));
+       std::vector<string> primCplus (IoTRMITypes::primitivesCplus, 
+               IoTRMITypes::primitivesCplus + sizeof(IoTRMITypes::primitivesCplus)/sizeof(string));
+
+       map<string,string> mymap;
+       IoTRMITypes::arraysToMap(mymap, primJava, primCplus);
+       for (std::map<string,string>::iterator it=mymap.begin(); it!=mymap.end(); ++it)
+               std::cout << it->first << " => " << it->second << '\n';
+
+       std::cout << "Result of find: " << mymap.find("Boolean")->second << std::endl;
+
+       return 0;
+}
index 529ae73880c2bf23a9f098bd766b4958a8408214..055f28b21726cca6c18c506436495f62e330ceef 100644 (file)
@@ -9,5 +9,11 @@ enum EnumC {
        GRAPE
 } enumC;
 
+/*enum EnumD {
+       APPLE,
+       ORANGE,
+       GRAPE
+};*/
+
 #endif
 
index 4f7c62a796736702f37e45f5e5cb4d5ac524e8f5..7f7bb3eb285373a5869e5fd69f6307abc6117b0b 100644 (file)
@@ -4,6 +4,20 @@ import java.util.Set;
 
 public interface TestClassInterface {
 
+       public class StructJ {
+
+               public static String name;
+               public static float value;
+               public static int year;
+       }
+
+       public enum EnumJ {
+
+               APPLE,
+               ORANGE,
+               GRAPE
+       }
+
        public void setA(int _int);
        public void setB(float _float);
        public void setC(String _string);