Perfecting parser, lexer, and parse-tree handling for policy files; Generating skelet...
[iot2.git] / iotjava / iotpolicy / tree / CapabilityDecl.java
1 package iotpolicy.tree;
2
3 import java.util.ArrayList;
4 import java.util.List;
5
6 /** Class CapabilityDecl is a data structure for capability
7  *  declaration section (list of capabilities) in the policy file.
8  *
9  * @author      Rahmadi Trimananda <rahmadi.trimananda @ uci.edu>
10  * @version     1.0
11  * @since       2016-09-20
12  */
13 public final class CapabilityDecl {
14
15         /**
16          * Class properties
17          */
18         private String origInt;
19
20         /**
21          * A "capability" statement:
22          *              capability Camera.ImageCapture {
23          *                      description = "The quick brown fox jumps over the smart dog";
24          *                      description = "Another description";
25          *                      method = MethodA;
26          *                      method = MethodB;
27          *              }
28          * In this data structure we will record its capability name, i.e. ImageCapture
29          *              and its descriptions and methods.
30          */
31         private List<String> listCapabs;                // list of capabilities
32         private List<List<String>> listDescs;   // list of descriptions
33         private List<List<String>> listMethods; // list of methods
34
35         /**
36          * Class constructors
37          */
38         public CapabilityDecl() {
39
40                 origInt = null;
41                 listCapabs = new ArrayList<String>();
42                 listDescs = new ArrayList<List<String>>();
43                 listMethods = new ArrayList<List<String>>();
44         }
45
46
47         public CapabilityDecl(String _origInt) {
48
49                 origInt = _origInt;
50                 listCapabs = new ArrayList<String>();
51                 listDescs = new ArrayList<List<String>>();
52                 listMethods = new ArrayList<List<String>>();
53         }
54
55
56         /**
57          * addNewCapability() adds a new capability into the list
58          */
59         public void addNewCapability(String newCap) {
60
61                 listCapabs.add(newCap);
62                 listDescs.add(new ArrayList<String>());
63                 listMethods.add(new ArrayList<String>());
64         }
65
66
67         /**
68          * addNewDescription() adds a new description into the list
69          */
70         public void addNewDescription(String cap, String newDesc) {
71
72                 int index = listCapabs.indexOf(cap);
73                 List<String> listDesc = listDescs.get(index);
74                 listDesc.add(newDesc);
75         }
76
77
78         /**
79          * addNewMethod() adds a new method into the list
80          */
81         public void addNewMethod(String cap, String newMethod) {
82
83                 int index = listCapabs.indexOf(cap);
84                 List<String> listMethod = listMethods.get(index);
85                 listMethod.add(newMethod);
86         }
87
88
89         /**
90          * getCapabilities() gets list of capabilities
91          */
92         public List<String> getCapabilities() {
93
94                 return listCapabs;
95         }
96
97
98         /**
99          * getDescriptions() gets list of descriptions
100          */
101         public List<String> getDescriptions(String cap) {
102
103                 int index = listCapabs.indexOf(cap);
104                 return listDescs.get(index);
105         }
106
107
108         /**
109          * getMethods() gets list of methods
110          */
111         public List<String> getMethods(String cap) {
112
113                 int index = listCapabs.indexOf(cap);
114                 return listMethods.get(index);
115         }
116
117
118         public static void main(String[] args) {
119
120                 CapabilityDecl cd = new CapabilityDecl("Camera");
121                 cd.addNewCapability("ImageCapture");
122                 cd.addNewDescription("ImageCapture", "The quick brown fox jumps over the smart dog");
123                 cd.addNewDescription("ImageCapture", "ImageCapture capability");
124                 cd.addNewMethod("ImageCapture", "MethodA");
125                 cd.addNewMethod("ImageCapture", "MethodC");
126                 cd.addNewMethod("ImageCapture", "MethodD");
127
128                 cd.addNewCapability("VideoRecording");
129                 cd.addNewDescription("VideoRecording", "The quick brown fox jumps over the smart dog");
130                 cd.addNewDescription("VideoRecording", "VideoRecording ");
131                 cd.addNewMethod("VideoRecording", "MethodE");
132                 cd.addNewMethod("VideoRecording", "MethodF");
133
134                 System.out.println("Set of capabilities: " + cd.getCapabilities().toString());
135                 System.out.println("Set of descriptions: " + cd.getDescriptions("VideoRecording").toString());
136                 System.out.println("Set of methods: " + cd.getMethods("VideoRecording").toString());
137                 System.out.println("Set of descriptions: " + cd.getDescriptions("ImageCapture").toString());
138                 System.out.println("Set of methods: " + cd.getMethods("ImageCapture").toString());
139         }
140 }