Integrating enum and struct in one method call; fixing minor bugs
[iot2.git] / iotjava / iotpolicy / tree / InterfaceDecl.java
1 package iotpolicy.tree;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
6 import java.util.List;
7 import java.util.Map;
8 import java.util.Set;
9
10 /** Class InterfaceDecl is a data structure for interface
11  *  declaration section in the policy file.
12  *
13  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
14  * @version     1.0
15  * @since       2016-09-20
16  */
17 public class InterfaceDecl extends Declaration {
18
19         /**
20          * A "interface" statement:
21          *              public interface Camera {
22      *                  public void MethodA(int A, int B);
23      *                  public int MethodB(int C, string D);
24      *                  public string MethodC(string E, int F);
25      *                  public float MethodD(int G, float H);
26      *                  public boolean MethodE(Camera I, boolean J);
27      *                  public void MethodF();
28          *              }
29          * In this data structure we will record its interface name, i.e. Camera
30          *              its method names and the parameters for each method.
31          */
32
33         /**
34          * Class properties
35          */
36         private List<String> listMethods;                                       // Method signature (no spaces), e.g. MethodA(intA,SpeakerB)
37         private List<String> listMethodIds;                                     // Method identifiers, e.g. MethodA
38         private List<String> listMethodTypes;                           // Method types, e.g. void
39         private List<List<String>> listMethodParams;            // Method parameter names, e.g. A, B
40         private List<List<String>> listMethodParamTypes;        // Method parameter types, e.g. int, int
41         private Map<String,Integer> mapHelperNumMethodId;       // Helper method Id, e.g. for callbacks, structs.
42
43         private static int helperMethodIdNum = -9999;
44
45         /**
46          * Class constructors
47          */
48         public InterfaceDecl() {
49
50                 super();
51                 listMethods = new ArrayList<String>();
52                 listMethodIds = new ArrayList<String>();
53                 listMethodTypes = new ArrayList<String>();
54                 listMethodParams = new ArrayList<List<String>>();
55                 listMethodParamTypes = new ArrayList<List<String>>();
56                 mapHelperNumMethodId = new HashMap<String,Integer>();
57         }
58
59
60         public InterfaceDecl(String _origInt) {
61
62                 super(_origInt);
63                 listMethods = new ArrayList<String>();
64                 listMethodIds = new ArrayList<String>();
65                 listMethodTypes = new ArrayList<String>();
66                 listMethodParams = new ArrayList<List<String>>();
67                 listMethodParamTypes = new ArrayList<List<String>>();
68                 mapHelperNumMethodId = new HashMap<String,Integer>();
69         }
70
71
72         /**
73          * addNewMethod() adds a new method name and type into the list
74          */
75         public void addNewMethod(String newMethod, String newMethodId, String newMethodType) {
76
77                 listMethods.add(newMethod);
78                 listMethodIds.add(newMethodId);
79                 listMethodTypes.add(newMethodType);
80                 listMethodParams.add(new ArrayList<String>());
81                 listMethodParamTypes.add(new ArrayList<String>());
82         }
83
84
85         /**
86          * addMethodParam() adds the name and type of a parameter
87          */
88         public void addMethodParam(String method, String paramName, String paramType) {
89
90                 int index = listMethods.indexOf(method);
91                 List<String> listMethodParam = listMethodParams.get(index);
92                 listMethodParam.add(paramName);
93                 List<String> listMethodParamType = listMethodParamTypes.get(index);
94                 listMethodParamType.add(paramType);
95         }
96
97
98         /**
99          * getMethods() gets list of methods
100          */
101         public List<String> getMethods() {
102
103                 return listMethods;
104         }
105
106
107         /**
108          * getMethodNumId() gets Id number for a method
109          */
110         public int getMethodNumId(String method) {
111
112                 return listMethods.indexOf(method);
113         }
114
115
116         /**
117          * getHelperMethodNumId() gets Id number for a method
118          */
119         public int getHelperMethodNumId(String method) {
120
121                 if (!mapHelperNumMethodId.containsKey(method)) {
122                         mapHelperNumMethodId.put(method, helperMethodIdNum++);
123                         return mapHelperNumMethodId.get(method);
124                 } else {
125                         return mapHelperNumMethodId.get(method);
126                 }
127         }
128
129
130         /**
131          * getMethodIds() gets method identifiers
132          */
133         public List<String> getMethodIds() {
134
135                 return listMethodIds;
136         }
137
138
139         /**
140          * getMethodTypes() gets method types
141          */
142         public List<String> getMethodTypes() {
143
144                 return listMethodTypes;
145         }
146
147
148         /**
149          * getMethodId() gets a method identifier
150          */
151         public String getMethodId(String method) {
152
153                 int index = listMethods.indexOf(method);
154                 // If index=-1, it means that it's not found.
155                 // There is perhaps a discrepancy in the policy file
156                 //              between the method signatures in the interface 
157                 //              and capability sections
158                 if (index == -1)
159                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
160                                 method + "! Please check your policy file...");
161                 return listMethodIds.get(index);
162         }
163
164
165         /**
166          * getMethodType() gets a method type
167          */
168         public String getMethodType(String method) {
169
170                 int index = listMethods.indexOf(method);
171                 // If index=-1, it means that it's not found.
172                 // There is perhaps a discrepancy in the policy file
173                 //              between the method signatures in the interface 
174                 //              and capability sections
175                 if (index == -1)
176                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
177                                 method + "! Please check your policy file...");
178                 return listMethodTypes.get(index);
179         }
180
181
182         /**
183          * getMethodParams() gets list of method parameters for a method
184          */
185         public List<String> getMethodParams(String method) {
186
187                 int index = listMethods.indexOf(method);
188                 // If index=-1, it means that it's not found.
189                 // There is perhaps a discrepancy in the policy file
190                 //              between the method signatures in the interface 
191                 //              and capability sections
192                 if (index == -1)
193                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
194                                 method + "! Please check your policy file...");
195                 return listMethodParams.get(index);
196         }
197         
198
199         /**
200          * getMethodParams() gets list of method parameter types for a method
201          */
202         public List<String> getMethodParamTypes(String method) {
203
204                 int index = listMethods.indexOf(method);
205                 // If index=-1, it means that it's not found.
206                 // There is perhaps a discrepancy in the policy file
207                 //              between the method signatures in the interface 
208                 //              and capability sections
209                 if (index == -1)
210                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
211                                 method + "! Please check your policy file...");
212                 return listMethodParamTypes.get(index);
213         }
214 }