Integrating enum and struct in one method call; fixing minor bugs
[iot2.git] / iotjava / iotpolicy / tree / InterfaceDecl.java
index 27e932615d7c40776e560dd5167e675811b20902..30dd3ce002804cb6e0b84a3f9c4f10624416038a 100644 (file)
@@ -1,7 +1,11 @@
 package iotpolicy.tree;
 
 import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 /** Class InterfaceDecl is a data structure for interface
  *  declaration section in the policy file.
@@ -10,12 +14,7 @@ import java.util.List;
  * @version     1.0
  * @since       2016-09-20
  */
-public final class InterfaceDecl {
-
-       /**
-        * Class properties
-        */
-       private String origInt;
+public class InterfaceDecl extends Declaration {
 
        /**
         * A "interface" statement:
@@ -30,40 +29,53 @@ public final class InterfaceDecl {
         * In this data structure we will record its interface name, i.e. Camera
         *              its method names and the parameters for each method.
         */
-       private List<String> listMethods;                                       // Method names, e.g. MethodA
+
+       /**
+        * Class properties
+        */
+       private List<String> listMethods;                                       // Method signature (no spaces), e.g. MethodA(intA,SpeakerB)
+       private List<String> listMethodIds;                                     // Method identifiers, e.g. MethodA
        private List<String> listMethodTypes;                           // Method types, e.g. void
        private List<List<String>> listMethodParams;            // Method parameter names, e.g. A, B
        private List<List<String>> listMethodParamTypes;        // Method parameter types, e.g. int, int
+       private Map<String,Integer> mapHelperNumMethodId;       // Helper method Id, e.g. for callbacks, structs.
+
+       private static int helperMethodIdNum = -9999;
 
        /**
         * Class constructors
         */
        public InterfaceDecl() {
 
-               origInt = null;
+               super();
                listMethods = new ArrayList<String>();
+               listMethodIds = new ArrayList<String>();
                listMethodTypes = new ArrayList<String>();
                listMethodParams = new ArrayList<List<String>>();
                listMethodParamTypes = new ArrayList<List<String>>();
+               mapHelperNumMethodId = new HashMap<String,Integer>();
        }
 
 
        public InterfaceDecl(String _origInt) {
 
-               origInt = _origInt;
+               super(_origInt);
                listMethods = new ArrayList<String>();
+               listMethodIds = new ArrayList<String>();
                listMethodTypes = new ArrayList<String>();
                listMethodParams = new ArrayList<List<String>>();
                listMethodParamTypes = new ArrayList<List<String>>();
+               mapHelperNumMethodId = new HashMap<String,Integer>();
        }
 
 
        /**
         * addNewMethod() adds a new method name and type into the list
         */
-       public void addNewMethod(String newMethod, String newMethodType) {
+       public void addNewMethod(String newMethod, String newMethodId, String newMethodType) {
 
                listMethods.add(newMethod);
+               listMethodIds.add(newMethodId);
                listMethodTypes.add(newMethodType);
                listMethodParams.add(new ArrayList<String>());
                listMethodParamTypes.add(new ArrayList<String>());
@@ -90,8 +102,40 @@ public final class InterfaceDecl {
 
                return listMethods;
        }
-       
-       
+
+
+       /**
+        * getMethodNumId() gets Id number for a method
+        */
+       public int getMethodNumId(String method) {
+
+               return listMethods.indexOf(method);
+       }
+
+
+       /**
+        * getHelperMethodNumId() gets Id number for a method
+        */
+       public int getHelperMethodNumId(String method) {
+
+               if (!mapHelperNumMethodId.containsKey(method)) {
+                       mapHelperNumMethodId.put(method, helperMethodIdNum++);
+                       return mapHelperNumMethodId.get(method);
+               } else {
+                       return mapHelperNumMethodId.get(method);
+               }
+       }
+
+
+       /**
+        * getMethodIds() gets method identifiers
+        */
+       public List<String> getMethodIds() {
+
+               return listMethodIds;
+       }
+
+
        /**
         * getMethodTypes() gets method types
         */
@@ -101,12 +145,36 @@ public final class InterfaceDecl {
        }
 
 
+       /**
+        * getMethodId() gets a method identifier
+        */
+       public String getMethodId(String method) {
+
+               int index = listMethods.indexOf(method);
+               // If index=-1, it means that it's not found.
+               // There is perhaps a discrepancy in the policy file
+               //              between the method signatures in the interface 
+               //              and capability sections
+               if (index == -1)
+                       throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
+                               method + "! Please check your policy file...");
+               return listMethodIds.get(index);
+       }
+
+
        /**
         * getMethodType() gets a method type
         */
        public String getMethodType(String method) {
 
                int index = listMethods.indexOf(method);
+               // If index=-1, it means that it's not found.
+               // There is perhaps a discrepancy in the policy file
+               //              between the method signatures in the interface 
+               //              and capability sections
+               if (index == -1)
+                       throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
+                               method + "! Please check your policy file...");
                return listMethodTypes.get(index);
        }
 
@@ -117,6 +185,13 @@ public final class InterfaceDecl {
        public List<String> getMethodParams(String method) {
 
                int index = listMethods.indexOf(method);
+               // If index=-1, it means that it's not found.
+               // There is perhaps a discrepancy in the policy file
+               //              between the method signatures in the interface 
+               //              and capability sections
+               if (index == -1)
+                       throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
+                               method + "! Please check your policy file...");
                return listMethodParams.get(index);
        }
        
@@ -127,31 +202,13 @@ public final class InterfaceDecl {
        public List<String> getMethodParamTypes(String method) {
 
                int index = listMethods.indexOf(method);
+               // If index=-1, it means that it's not found.
+               // There is perhaps a discrepancy in the policy file
+               //              between the method signatures in the interface 
+               //              and capability sections
+               if (index == -1)
+                       throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
+                               method + "! Please check your policy file...");
                return listMethodParamTypes.get(index);
        }
-
-
-       public static void main(String[] args) {
-
-               InterfaceDecl id = new InterfaceDecl("Camera");
-               id.addNewMethod("MethodA", "void");
-               id.addNewMethod("MethodB", "int");
-               id.addNewMethod("MethodC", "String");
-               id.addMethodParam("MethodA", "A", "int");
-               id.addMethodParam("MethodA", "B", "int");
-               id.addMethodParam("MethodB", "C", "int");
-               id.addMethodParam("MethodB", "D", "string");
-               id.addMethodParam("MethodC", "E", "string");
-               id.addMethodParam("MethodC", "F", "int");
-
-
-
-               System.out.println("Set of methods: " + id.getMethods().toString());
-               System.out.println("Set of params: " + id.getMethodParams("MethodA").toString());
-               System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodA").toString());
-               System.out.println("Set of params: " + id.getMethodParams("MethodB").toString());
-               System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodB").toString());
-               System.out.println("Set of params: " + id.getMethodParams("MethodC").toString());
-               System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodC").toString());
-       }
 }