From: rtrimana Date: Sat, 3 Dec 2016 00:29:38 +0000 (-0800) Subject: Testing struct for array and list (Java and C++); fixing bugs X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=2da700a5060b2fbbe59363070515db175a5ce222;p=iot2.git Testing struct for array and list (Java and C++); fixing bugs --- diff --git a/config/iotpolicy/testclasspolicy.pol b/config/iotpolicy/testclasspolicy.pol index 3c03d53..1d9d47a 100644 --- a/config/iotpolicy/testclasspolicy.pol +++ b/config/iotpolicy/testclasspolicy.pol @@ -29,6 +29,8 @@ public interface TestClassInterface { public List handleEnumList(List en); public Struct handleStruct(Struct str); + public Struct[] handleStructArray(Struct str[]); + public List handleStructList(List str); public int getA(); public void setA(int _int); @@ -67,6 +69,8 @@ public interface TestClassInterface { method = "handleEnumList(List en)"; method = "handleStruct(Struct str)"; + method = "handleStructArray(Struct str[])"; + method = "handleStructList(List str)"; method = "getA()"; method = "setA(int _int)"; diff --git a/iotjava/iotpolicy/IoTCompiler.java b/iotjava/iotpolicy/IoTCompiler.java index 6555f9d..c23896f 100644 --- a/iotjava/iotpolicy/IoTCompiler.java +++ b/iotjava/iotpolicy/IoTCompiler.java @@ -629,7 +629,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { // Check if this is enum type int methodNumId = intDecl.getMethodNumId(method); @@ -661,7 +661,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) return true; } @@ -682,10 +682,10 @@ public class IoTCompiler { if (isStructClass(simpleType)) { int members = getNumOfMembers(simpleType); if (isArray(param)) { // An array - String structLen = param + ".length"; + String structLen = getSimpleArrayType(param) + ".length"; print(members + "*" + structLen); } else if (isList(paramType)) { // A list - String structLen = param + ".size()"; + String structLen = getSimpleArrayType(param) + ".size()"; print(members + "*" + structLen); } else print(Integer.toString(members)); @@ -708,15 +708,21 @@ public class IoTCompiler { List memTypes = structDecl.getMemberTypes(simpleType); List members = structDecl.getMembers(simpleType); if (isArray(param)) { // An array - println("for(int i = 0; i < " + param + ".length; i++) {"); + println("for(int i = 0; i < " + getSimpleIdentifier(param) + ".length; i++) {"); + for (int i = 0; i < members.size(); i++) { + String prmType = checkAndGetArray(memTypes.get(i), members.get(i)); + println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;"); + print("paramObj[pos++] = " + getSimpleIdentifier(param) + "[i]."); + print(getSimpleIdentifier(members.get(i))); + println(";"); + } + println("}"); } else if (isList(paramType)) { // A list - println("for(int i = 0; i < " + param + ".size(); i++) {"); - } - if (isArrayOrList(param, paramType)) { // An array or list + println("for(int i = 0; i < " + getSimpleIdentifier(param) + ".size(); i++) {"); for (int i = 0; i < members.size(); i++) { String prmType = checkAndGetArray(memTypes.get(i), members.get(i)); println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;"); - print("paramObj[pos++] = " + param + "[i]."); + print("paramObj[pos++] = " + getSimpleIdentifier(param) + ".get(i)."); print(getSimpleIdentifier(members.get(i))); println(";"); } @@ -725,7 +731,7 @@ public class IoTCompiler { for (int i = 0; i < members.size(); i++) { String prmType = checkAndGetArray(memTypes.get(i), members.get(i)); println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;"); - print("paramObj[pos++] = " + param + "."); + print("paramObj[pos++] = " + getSimpleIdentifier(param) + "."); print(getSimpleIdentifier(members.get(i))); println(";"); } @@ -1229,7 +1235,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { int methodNumId = intDecl.getMethodNumId(method); String helperMethod = methodNumId + "struct" + i; @@ -1514,14 +1520,13 @@ public class IoTCompiler { StructDecl structDecl = getStructDecl(simpleType); List memTypes = structDecl.getMemberTypes(simpleType); List members = structDecl.getMembers(simpleType); - if (isArrayOrList(param, paramType)) { // An array or list + println("int pos = 0;"); + if (isArrayOrList(paramType, param)) { // An array or list int methodNumId = intDecl.getMethodNumId(method); String counter = "struct" + methodNumId + "Size" + iVar; println("for(int i = 0; i < " + counter + "; i++) {"); } - println("int pos = 0;"); - if (isArrayOrList(param, paramType)) { // An array or list - println("for(int i = 0; i < retLen; i++) {"); + if (isArrayOrList(paramType, param)) { // An array or list for (int i = 0; i < members.size(); i++) { String prmType = checkAndGetArray(memTypes.get(i), members.get(i)); println("paramCls[pos] = " + getSimpleType(getEnumType(prmType)) + ".class;"); @@ -1566,7 +1571,7 @@ public class IoTCompiler { StructDecl structDecl = getStructDecl(simpleType); List members = structDecl.getMembers(simpleType); List memTypes = structDecl.getMemberTypes(simpleType); - if (isArrayOrList(param, paramType)) { // An array or list + if (isArrayOrList(paramType, param)) { // An array or list println("for(int i = 0; i < " + counter + "; i++) {"); } if (isArray(param)) { // An array @@ -1622,7 +1627,7 @@ public class IoTCompiler { StructDecl structDecl = getStructDecl(simpleType); List memTypes = structDecl.getMemberTypes(simpleType); List members = structDecl.getMembers(simpleType); - if (isArrayOrList(retType, retType)) { // An array or list + if (isArray(retType)) { // An array or list println("for(int i = 0; i < retLen; i++) {"); for (int i = 0; i < members.size(); i++) { String prmType = checkAndGetArray(memTypes.get(i), members.get(i)); @@ -1632,6 +1637,16 @@ public class IoTCompiler { println(";"); } println("}"); + } else if (isList(retType)) { // An array or list + println("for(int i = 0; i < retLen; i++) {"); + for (int i = 0; i < members.size(); i++) { + String prmType = checkAndGetArray(memTypes.get(i), members.get(i)); + println("retCls[retPos] = " + getSimpleType(getEnumType(prmType)) + ".class;"); + print("retObj[retPos++] = retStruct.get(i)."); + print(getEnumParam(memTypes.get(i), getSimpleIdentifier(members.get(i)), i)); + println(";"); + } + println("}"); } else { // Just one struct element for (int i = 0; i < members.size(); i++) { String prmType = checkAndGetArray(memTypes.get(i), members.get(i)); @@ -1821,7 +1836,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { // Print size variables String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { if (!begin) { // Generate comma for not the beginning variable print(", "); begin = false; @@ -1876,7 +1891,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { int methodNumId = intDecl.getMethodNumId(method); print("public int ___"); @@ -1907,7 +1922,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { int methodNumId = intDecl.getMethodNumId(method); print("public int ___"); @@ -1937,7 +1952,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { int methodNumId = intDecl.getMethodNumId(method); println("int struct" + methodNumId + "Size" + i + " = 0;"); @@ -1959,7 +1974,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); boolean begin = true; if (isStructClass(simpleType)) { structExist = true; @@ -1988,7 +2003,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { int methodNumId = intDecl.getMethodNumId(method); print("case "); @@ -2017,7 +2032,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { int methodNumId = intDecl.getMethodNumId(method); print("case "); @@ -2197,7 +2212,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { // Print size variables String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { if (!begin) { // Generate comma for not the beginning variable print(", "); begin = false; @@ -2738,7 +2753,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { // Check if this is enum type println("int numParam" + i + " = 1;"); @@ -2748,7 +2763,7 @@ public class IoTCompiler { println("string retTypeStruct" + i + " = \"void\";"); println("string paramClsStruct" + i + "[] = { \"int\" };"); print("int structLen" + i + " = "); - if (isArrayOrList(param, paramType)) { // An array + if (isArrayOrList(paramType, param)) { // An array println(getSimpleArrayType(param) + ".size();"); } else { // Just one element println("1;"); @@ -2775,8 +2790,8 @@ public class IoTCompiler { String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { int members = getNumOfMembers(simpleType); - if (isArrayOrList(param, paramType)) { // An array - String structLen = param + ".size()"; + if (isArrayOrList(paramType, param)) { // An array or list + String structLen = getSimpleIdentifier(param) + ".size()"; print(members + "*" + structLen); } else print(Integer.toString(members)); @@ -2798,14 +2813,14 @@ public class IoTCompiler { StructDecl structDecl = getStructDecl(simpleType); List memTypes = structDecl.getMemberTypes(simpleType); List members = structDecl.getMembers(simpleType); - if (isArrayOrList(param, paramType)) { // An array or list - println("for(int i = 0; i < " + param + ".size(); i++) {"); + if (isArrayOrList(paramType, param)) { // An array or list + println("for(int i = 0; i < " + getSimpleIdentifier(param) + ".size(); i++) {"); } - if (isArrayOrList(param, paramType)) { // An array or list + if (isArrayOrList(paramType, param)) { // An array or list for (int i = 0; i < members.size(); i++) { String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i)); println("paramCls[pos] = \"" + prmTypeC + "\";"); - print("paramObj[pos++] = &" + param + "[i]."); + print("paramObj[pos++] = &" + getSimpleIdentifier(param) + "[i]."); print(getSimpleIdentifier(members.get(i))); println(";"); } @@ -3403,7 +3418,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { int methodNumId = intDecl.getMethodNumId(method); String helperMethod = methodNumId + "struct" + i; @@ -3796,11 +3811,9 @@ public class IoTCompiler { List members = structDecl.getMembers(simpleType); int methodNumId = intDecl.getMethodNumId(method); String counter = "struct" + methodNumId + "Size" + iVar; - if (isArrayOrList(param, paramType)) { // An array or list - println("for(int i = 0; i < " + counter + "; i++) {"); - } + println("int pos = 0;"); // Set up variables - if (isArrayOrList(param, paramType)) { // An array or list + if (isArrayOrList(paramType, param)) { // An array or list for (int i = 0; i < members.size(); i++) { String prmTypeC = checkAndGetCplusType(memTypes.get(i)); String prmType = checkAndGetCplusArrayType(prmTypeC, members.get(i)); @@ -3813,9 +3826,10 @@ public class IoTCompiler { println(getSimpleType(getEnumType(prmType)) + " param" + i + ";"); } } - println("int pos = 0;"); - if (isArrayOrList(param, paramType)) { // An array or list - println("for(int i = 0; i < retLen; i++) {"); + if (isArrayOrList(paramType, param)) { // An array or list + println("for(int i = 0; i < " + counter + "; i++) {"); + } + if (isArrayOrList(paramType, param)) { // An array or list for (int i = 0; i < members.size(); i++) { String prmTypeC = checkAndGetCplusArgClsType(memTypes.get(i), members.get(i)); println("paramCls[pos] = \"" + prmTypeC + "\";"); @@ -3846,15 +3860,15 @@ public class IoTCompiler { int methodNumId = intDecl.getMethodNumId(method); String counter = "struct" + methodNumId + "Size" + i; // Declaration - if (isArrayOrList(param, paramType)) { // An array or list - println("vector<" + simpleType + "> paramStruct" + i + ";"); + if (isArrayOrList(paramType, param)) { // An array or list + println("vector<" + simpleType + "> paramStruct" + i + "(" + counter + ");"); } else println(simpleType + " paramStruct" + i + ";"); // Initialize members StructDecl structDecl = getStructDecl(simpleType); List members = structDecl.getMembers(simpleType); List memTypes = structDecl.getMemberTypes(simpleType); - if (isArrayOrList(param, paramType)) { // An array or list + if (isArrayOrList(paramType, param)) { // An array or list println("for(int i = 0; i < " + counter + "; i++) {"); for (int j = 0; j < members.size(); j++) { print("paramStruct" + i + "[i]." + getSimpleIdentifier(members.get(j))); @@ -3993,7 +4007,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { // Print size variables String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { if (!begin) { // Generate comma for not the beginning variable print(", "); begin = false; @@ -4041,7 +4055,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { int methodNumId = intDecl.getMethodNumId(method); print("int ___"); @@ -4076,7 +4090,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { int methodNumId = intDecl.getMethodNumId(method); print("int ___"); @@ -4308,7 +4322,7 @@ public class IoTCompiler { for (int i = 0; i < methParams.size(); i++) { // Print size variables String paramType = methPrmTypes.get(i); String param = methParams.get(i); - String simpleType = getSimpleType(paramType); + String simpleType = getGenericType(paramType); if (isStructClass(simpleType)) { if (!begin) { // Generate comma for not the beginning variable print(", "); begin = false; diff --git a/iotjava/iotrmi/C++/basics/TestClass.hpp b/iotjava/iotrmi/C++/basics/TestClass.hpp index 1892ca0..58a6d2a 100644 --- a/iotjava/iotrmi/C++/basics/TestClass.hpp +++ b/iotjava/iotrmi/C++/basics/TestClass.hpp @@ -50,6 +50,8 @@ class TestClass : public TestClassInterface { // Struct Struct handleStruct(Struct str); + vector handleStructArray(vector vecStr); + vector handleStructList(vector vecStr); int getA(); void setA(int _int); @@ -310,7 +312,7 @@ Struct TestClass::handleStruct(Struct str) { } -/*vector TestClass::handleStruct(vector vecStr) { +vector TestClass::handleStructArray(vector vecStr) { for (Struct str : vecStr) { @@ -325,7 +327,25 @@ Struct TestClass::handleStruct(Struct str) { vecStr.push_back(test); return vecStr; -}*/ +} + + +vector TestClass::handleStructList(vector vecStr) { + + for (Struct str : vecStr) { + + cout << "Name: " << str.name << endl; + cout << "Value: " << str.value << endl; + cout << "Year: " << str.year << endl; + } + Struct test; + test.name = "Trimananda"; + test.value = 1.33; + test.year = 2016; + vecStr.push_back(test); + + return vecStr; +} string TestClass::sumArray(vector newA) { diff --git a/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp b/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp index c65cd64..96011d1 100644 --- a/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp +++ b/iotjava/iotrmi/C++/basics/TestClass_Stub.cpp @@ -109,6 +109,25 @@ int main(int argc, char *argv[]) cout << "Name: " << resStr.name << endl; cout << "Value:" << resStr.value << endl; cout << "Year" << resStr.year << endl; + Struct str2; + str2.name = "Trimananda"; + str2.value = 0.124; + str2.year = 2017; + vector vecStr; + vecStr.push_back(str); + vecStr.push_back(str2); + vector vecRetStr = tcStub->handleStructArray(vecStr); + for (Struct st : vecRetStr) { + cout << "Name: " << st.name << endl; + cout << "Value:" << st.value << endl; + cout << "Year" << st.year << endl; + } + vector vecRetStr2 = tcStub->handleStructList(vecStr); + for (Struct st : vecRetStr2) { + cout << "Name: " << st.name << endl; + cout << "Value:" << st.value << endl; + cout << "Year" << st.year << endl; + } cout << "==== OTHERS ====" << endl; cout << "Return value: " << tcStub->getA() << endl; diff --git a/iotjava/iotrmi/Java/basics/TestClass.java b/iotjava/iotrmi/Java/basics/TestClass.java index 8844ffb..a5c0c11 100644 --- a/iotjava/iotrmi/Java/basics/TestClass.java +++ b/iotjava/iotrmi/Java/basics/TestClass.java @@ -261,12 +261,12 @@ public class TestClass implements TestClassInterface { } - /*public Struct[] handleStruct(Struct[] data) { + public Struct[] handleStructArray(Struct str[]) { - for (Struct str : data) { - System.out.println("Name: " + str.name); - System.out.println("Value: " + str.value); - System.out.println("Year: " + str.year); + for (Struct st : str) { + System.out.println("Name: " + st.name); + System.out.println("Value: " + st.value); + System.out.println("Year: " + st.year); } Struct test = new Struct(); @@ -274,10 +274,29 @@ public class TestClass implements TestClassInterface { test.value = 1.33f; test.year = 2016; - data[0] = test; + str[0] = test; - return data; - }*/ + return str; + } + + + public List handleStructList(List str) { + + for (Struct st : str) { + System.out.println("Name: " + st.name); + System.out.println("Value: " + st.value); + System.out.println("Year: " + st.year); + } + + Struct test = new Struct(); + test.name = "Tests"; + test.value = 1.34f; + test.year = 2017; + + str.add(test); + + return str; + } // Getters diff --git a/iotjava/iotrmi/Java/basics/TestClass_Stub.java b/iotjava/iotrmi/Java/basics/TestClass_Stub.java index 6f99992..01daf30 100644 --- a/iotjava/iotrmi/Java/basics/TestClass_Stub.java +++ b/iotjava/iotrmi/Java/basics/TestClass_Stub.java @@ -80,6 +80,24 @@ public class TestClass_Stub { System.out.println("Name: " + strRes.name); System.out.println("Value: " + strRes.value); System.out.println("Year: " + strRes.year); + Struct str2 = new Struct(); + str2.name = "Trimananda"; + str2.value = 0.124f; + str2.year = 2017; + Struct[] arrStr = { str, str2 }; + Struct[] arrRet = tcstub.handleStructArray(arrStr); + for(Struct st : arrRet) { + System.out.println("Name: " + st.name); + System.out.println("Value: " + st.value); + System.out.println("Year: " + st.year); + } + List stList = new ArrayList(Arrays.asList(arrStr)); + List stRetList = tcstub.handleStructList(stList); + for(Struct st : stRetList) { + System.out.println("Name: " + st.name); + System.out.println("Value: " + st.value); + System.out.println("Year: " + st.year); + } System.out.println("==== OTHERS ===="); System.out.println("Return value: " + tcstub.getA());