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 {
22 private String origInt;
25 * A "interface" statement:
26 * public interface Camera {
27 * public void MethodA(int A, int B);
28 * public int MethodB(int C, string D);
29 * public string MethodC(string E, int F);
30 * public float MethodD(int G, float H);
31 * public boolean MethodE(Camera I, boolean J);
32 * public void MethodF();
34 * In this data structure we will record its interface name, i.e. Camera
35 * its method names and the parameters for each method.
37 private List<String> listMethods; // Method signature (no spaces), e.g. MethodA(intA,SpeakerB)
38 private List<String> listMethodIds; // Method identifiers, e.g. MethodA
39 private List<String> listMethodTypes; // Method types, e.g. void
40 private List<List<String>> listMethodParams; // Method parameter names, e.g. A, B
41 private List<List<String>> listMethodParamTypes; // Method parameter types, e.g. int, int
46 public InterfaceDecl() {
49 listMethods = new ArrayList<String>();
50 listMethodIds = new ArrayList<String>();
51 listMethodTypes = new ArrayList<String>();
52 listMethodParams = new ArrayList<List<String>>();
53 listMethodParamTypes = new ArrayList<List<String>>();
57 public InterfaceDecl(String _origInt) {
60 listMethods = new ArrayList<String>();
61 listMethodIds = new ArrayList<String>();
62 listMethodTypes = new ArrayList<String>();
63 listMethodParams = new ArrayList<List<String>>();
64 listMethodParamTypes = new ArrayList<List<String>>();
69 * addNewMethod() adds a new method name and type into the list
71 public void addNewMethod(String newMethod, String newMethodId, String newMethodType) {
73 listMethods.add(newMethod);
74 listMethodIds.add(newMethodId);
75 listMethodTypes.add(newMethodType);
76 listMethodParams.add(new ArrayList<String>());
77 listMethodParamTypes.add(new ArrayList<String>());
82 * addMethodParam() adds the name and type of a parameter
84 public void addMethodParam(String method, String paramName, String paramType) {
86 int index = listMethods.indexOf(method);
87 List<String> listMethodParam = listMethodParams.get(index);
88 listMethodParam.add(paramName);
89 List<String> listMethodParamType = listMethodParamTypes.get(index);
90 listMethodParamType.add(paramType);
95 * getMethods() gets list of methods
97 public List<String> getMethods() {
104 * getMethodIds() gets method identifiers
106 public List<String> getMethodIds() {
108 return listMethodIds;
113 * getMethodTypes() gets method types
115 public List<String> getMethodTypes() {
117 return listMethodTypes;
122 * getMethodId() gets a method identifier
124 public String getMethodId(String method) {
126 int index = listMethods.indexOf(method);
127 // If index=-1, it means that it's not found.
128 // There is perhaps a discrepancy in the policy file
129 // between the method signatures in the interface
130 // and capability sections
132 throw new Error("InterfaceDecl: Discrepancies in method signature for " +
133 method + "! Please check your policy file...");
134 return listMethodIds.get(index);
139 * getMethodType() gets a method type
141 public String getMethodType(String method) {
143 int index = listMethods.indexOf(method);
144 // If index=-1, it means that it's not found.
145 // There is perhaps a discrepancy in the policy file
146 // between the method signatures in the interface
147 // and capability sections
149 throw new Error("InterfaceDecl: Discrepancies in method signature for " +
150 method + "! Please check your policy file...");
151 return listMethodTypes.get(index);
156 * getMethodParams() gets list of method parameters for a method
158 public List<String> getMethodParams(String method) {
160 int index = listMethods.indexOf(method);
161 // If index=-1, it means that it's not found.
162 // There is perhaps a discrepancy in the policy file
163 // between the method signatures in the interface
164 // and capability sections
166 throw new Error("InterfaceDecl: Discrepancies in method signature for " +
167 method + "! Please check your policy file...");
168 return listMethodParams.get(index);
173 * getMethodParams() gets list of method parameter types for a method
175 public List<String> getMethodParamTypes(String method) {
177 int index = listMethods.indexOf(method);
178 // If index=-1, it means that it's not found.
179 // There is perhaps a discrepancy in the policy file
180 // between the method signatures in the interface
181 // and capability sections
183 throw new Error("InterfaceDecl: Discrepancies in method signature for " +
184 method + "! Please check your policy file...");
185 return listMethodParamTypes.get(index);
189 public static void main(String[] args) {
191 InterfaceDecl id = new InterfaceDecl("Camera");
192 id.addNewMethod("MethodA(intA,SpeakerB)", "MethodA", "void");
193 id.addMethodParam("MethodA", "A", "int");
194 id.addMethodParam("MethodA", "B", "int");
195 id.addMethodParam("MethodB", "C", "int");
196 id.addMethodParam("MethodB", "D", "string");
197 id.addMethodParam("MethodC", "E", "string");
198 id.addMethodParam("MethodC", "F", "int");
200 System.out.println("Set of methods: " + id.getMethods().toString());
201 System.out.println("Set of params: " + id.getMethodParams("MethodA").toString());
202 System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodA").toString());
203 System.out.println("Set of params: " + id.getMethodParams("MethodB").toString());
204 System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodB").toString());
205 System.out.println("Set of params: " + id.getMethodParams("MethodC").toString());
206 System.out.println("Set of paramtypes: " + id.getMethodParamTypes("MethodC").toString());