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
45 public InterfaceDecl() {
48 listMethods = new ArrayList<String>();
49 listMethodIds = new ArrayList<String>();
50 listMethodTypes = new ArrayList<String>();
51 listMethodParams = new ArrayList<List<String>>();
52 listMethodParamTypes = new ArrayList<List<String>>();
56 public InterfaceDecl(String _origInt) {
59 listMethods = new ArrayList<String>();
60 listMethodIds = new ArrayList<String>();
61 listMethodTypes = new ArrayList<String>();
62 listMethodParams = new ArrayList<List<String>>();
63 listMethodParamTypes = new ArrayList<List<String>>();
68 * addNewMethod() adds a new method name and type into the list
70 public void addNewMethod(String newMethod, String newMethodId, String newMethodType) {
72 listMethods.add(newMethod);
73 listMethodIds.add(newMethodId);
74 listMethodTypes.add(newMethodType);
75 listMethodParams.add(new ArrayList<String>());
76 listMethodParamTypes.add(new ArrayList<String>());
81 * addMethodParam() adds the name and type of a parameter
83 public void addMethodParam(String method, String paramName, String paramType) {
85 int index = listMethods.indexOf(method);
86 List<String> listMethodParam = listMethodParams.get(index);
87 listMethodParam.add(paramName);
88 List<String> listMethodParamType = listMethodParamTypes.get(index);
89 listMethodParamType.add(paramType);
94 * getMethods() gets list of methods
96 public List<String> getMethods() {
103 * getMethodIds() gets method identifiers
105 public List<String> getMethodIds() {
107 return listMethodIds;
112 * getMethodTypes() gets method types
114 public List<String> getMethodTypes() {
116 return listMethodTypes;
121 * getMethodId() gets a method identifier
123 public String getMethodId(String method) {
125 int index = listMethods.indexOf(method);
126 // If index=-1, it means that it's not found.
127 // There is perhaps a discrepancy in the policy file
128 // between the method signatures in the interface
129 // and capability sections
131 throw new Error("InterfaceDecl: Discrepancies in method signature for " +
132 method + "! Please check your policy file...");
133 return listMethodIds.get(index);
138 * getMethodType() gets a method type
140 public String getMethodType(String method) {
142 int index = listMethods.indexOf(method);
143 // If index=-1, it means that it's not found.
144 // There is perhaps a discrepancy in the policy file
145 // between the method signatures in the interface
146 // and capability sections
148 throw new Error("InterfaceDecl: Discrepancies in method signature for " +
149 method + "! Please check your policy file...");
150 return listMethodTypes.get(index);
155 * getMethodParams() gets list of method parameters for a method
157 public List<String> getMethodParams(String method) {
159 int index = listMethods.indexOf(method);
160 // If index=-1, it means that it's not found.
161 // There is perhaps a discrepancy in the policy file
162 // between the method signatures in the interface
163 // and capability sections
165 throw new Error("InterfaceDecl: Discrepancies in method signature for " +
166 method + "! Please check your policy file...");
167 return listMethodParams.get(index);
172 * getMethodParams() gets list of method parameter types for a method
174 public List<String> getMethodParamTypes(String method) {
176 int index = listMethods.indexOf(method);
177 // If index=-1, it means that it's not found.
178 // There is perhaps a discrepancy in the policy file
179 // between the method signatures in the interface
180 // and capability sections
182 throw new Error("InterfaceDecl: Discrepancies in method signature for " +
183 method + "! Please check your policy file...");
184 return listMethodParamTypes.get(index);
188 public static void main(String[] args) {
190 InterfaceDecl id = new InterfaceDecl("Camera");
191 id.addNewMethod("MethodA(intA,SpeakerB)", "MethodA", "void");
192 id.addMethodParam("MethodA", "A", "int");
193 id.addMethodParam("MethodA", "B", "int");
194 id.addMethodParam("MethodB", "C", "int");
195 id.addMethodParam("MethodB", "D", "string");
196 id.addMethodParam("MethodC", "E", "string");
197 id.addMethodParam("MethodC", "F", "int");
199 System.out.println("Set of methods: " + id.getMethods().toString());
200 System.out.println("Set of params: " + id.getMethodParams("MethodA").toString());
201 System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodA").toString());
202 System.out.println("Set of params: " + id.getMethodParams("MethodB").toString());
203 System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodB").toString());
204 System.out.println("Set of params: " + id.getMethodParams("MethodC").toString());
205 System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodC").toString());