1 package iotpolicy.tree;
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.HashSet;
10 /** Class InterfaceDecl is a data structure for interface
11 * declaration section in the policy file.
13 * @author Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
17 public class InterfaceDecl extends Declaration {
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();
29 * In this data structure we will record its interface name, i.e. Camera
30 * its method names and the parameters for each method.
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.
43 private static int helperMethodIdNum = -9999;
48 public InterfaceDecl() {
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>();
60 public InterfaceDecl(String _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>();
73 * addNewMethod() adds a new method name and type into the list
75 public void addNewMethod(String newMethod, String newMethodId, String newMethodType) {
77 listMethods.add(newMethod);
78 listMethodIds.add(newMethodId);
79 listMethodTypes.add(newMethodType);
80 listMethodParams.add(new ArrayList<String>());
81 listMethodParamTypes.add(new ArrayList<String>());
86 * addMethodParam() adds the name and type of a parameter
88 public void addMethodParam(String method, String paramName, String paramType) {
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);
99 * getMethods() gets list of methods
101 public List<String> getMethods() {
108 * getMethodNumId() gets Id number for a method
110 public int getMethodNumId(String method) {
112 return listMethods.indexOf(method);
117 * getHelperMethodNumId() gets Id number for a method
119 public int getHelperMethodNumId(String method) {
121 if (!mapHelperNumMethodId.containsKey(method)) {
122 mapHelperNumMethodId.put(method, helperMethodIdNum++);
123 return mapHelperNumMethodId.get(method);
125 return mapHelperNumMethodId.get(method);
131 * getMethodIds() gets method identifiers
133 public List<String> getMethodIds() {
135 return listMethodIds;
140 * getMethodTypes() gets method types
142 public List<String> getMethodTypes() {
144 return listMethodTypes;
149 * getMethodId() gets a method identifier
151 public String getMethodId(String method) {
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
159 throw new Error("InterfaceDecl: Discrepancies in method signature for " +
160 method + "! Please check your policy file...");
161 return listMethodIds.get(index);
166 * getMethodType() gets a method type
168 public String getMethodType(String method) {
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
176 throw new Error("InterfaceDecl: Discrepancies in method signature for " +
177 method + "! Please check your policy file...");
178 return listMethodTypes.get(index);
183 * getMethodParams() gets list of method parameters for a method
185 public List<String> getMethodParams(String method) {
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
193 throw new Error("InterfaceDecl: Discrepancies in method signature for " +
194 method + "! Please check your policy file...");
195 return listMethodParams.get(index);
200 * getMethodParams() gets list of method parameter types for a method
202 public List<String> getMethodParamTypes(String method) {
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
210 throw new Error("InterfaceDecl: Discrepancies in method signature for " +
211 method + "! Please check your policy file...");
212 return listMethodParamTypes.get(index);