import iotpolicy.tree.CapabilityDecl;
import iotpolicy.tree.InterfaceDecl;
import iotpolicy.tree.RequiresDecl;
+import iotpolicy.tree.EnumDecl;
+import iotpolicy.tree.StructDecl;
import iotrmi.Java.IoTRMITypes;
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);
}
+ /**
+ * 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>
// 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("}");
printImportStatements(importClasses);
// Write interface header
println("");
- println("public interface " + newIntface + " {");
+ println("public interface " + newIntface + " {\n");
// Write methods
writeMethodJavaInterface(intMeth.getValue(), intDecl);
println("}");
}
+ /**
+ * 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>
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