Adding handling for primitives, non-primitives, and user-defined types; doesn't handl...
[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 {
18
19         /**
20          * Class properties
21          */
22         private String origInt;
23
24         /**
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();
33          *              }
34          * In this data structure we will record its interface name, i.e. Camera
35          *              its method names and the parameters for each method.
36          */
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
42
43         /**
44          * Class constructors
45          */
46         public InterfaceDecl() {
47
48                 origInt = null;
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>>();
54         }
55
56
57         public InterfaceDecl(String _origInt) {
58
59                 origInt = _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>>();
65         }
66
67
68         /**
69          * addNewMethod() adds a new method name and type into the list
70          */
71         public void addNewMethod(String newMethod, String newMethodId, String newMethodType) {
72
73                 listMethods.add(newMethod);
74                 listMethodIds.add(newMethodId);
75                 listMethodTypes.add(newMethodType);
76                 listMethodParams.add(new ArrayList<String>());
77                 listMethodParamTypes.add(new ArrayList<String>());
78         }
79
80
81         /**
82          * addMethodParam() adds the name and type of a parameter
83          */
84         public void addMethodParam(String method, String paramName, String paramType) {
85
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);
91         }
92
93
94         /**
95          * getMethods() gets list of methods
96          */
97         public List<String> getMethods() {
98
99                 return listMethods;
100         }
101
102
103         /**
104          * getMethodIds() gets method identifiers
105          */
106         public List<String> getMethodIds() {
107
108                 return listMethodIds;
109         }
110
111
112         /**
113          * getMethodTypes() gets method types
114          */
115         public List<String> getMethodTypes() {
116
117                 return listMethodTypes;
118         }
119
120
121         /**
122          * getMethodId() gets a method identifier
123          */
124         public String getMethodId(String method) {
125
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
131                 if (index == -1)
132                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
133                                 method + "! Please check your policy file...");
134                 return listMethodIds.get(index);
135         }
136
137
138         /**
139          * getMethodType() gets a method type
140          */
141         public String getMethodType(String method) {
142
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
148                 if (index == -1)
149                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
150                                 method + "! Please check your policy file...");
151                 return listMethodTypes.get(index);
152         }
153
154
155         /**
156          * getMethodParams() gets list of method parameters for a method
157          */
158         public List<String> getMethodParams(String method) {
159
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
165                 if (index == -1)
166                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
167                                 method + "! Please check your policy file...");
168                 return listMethodParams.get(index);
169         }
170         
171
172         /**
173          * getMethodParams() gets list of method parameter types for a method
174          */
175         public List<String> getMethodParamTypes(String method) {
176
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
182                 if (index == -1)
183                         throw new Error("InterfaceDecl: Discrepancies in method signature for " + 
184                                 method + "! Please check your policy file...");
185                 return listMethodParamTypes.get(index);
186         }
187
188
189         public static void main(String[] args) {
190
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");
199
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());
207         }
208 }