Adding config file for sharing.
[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 class CapabilityDecl extends Declaration {
14
15         /**
16          * A "capability" statement:
17          *              capability Camera.ImageCapture {
18          *                      description = "The quick brown fox jumps over the smart dog";
19          *                      description = "Another description";
20          *                      method = MethodA;
21          *                      method = MethodB;
22          *              }
23          * In this data structure we will record its capability name, i.e. ImageCapture
24          *              and its descriptions and methods.
25          */
26
27         /**
28          * Class properties
29          */
30         private List<String> listCapabs;                // list of capabilities
31         private List<List<String>> listDescs;   // list of descriptions
32         private List<List<String>> listMethods; // list of methods
33
34         /**
35          * Class constructors
36          */
37         public CapabilityDecl() {
38
39                 super();
40                 listCapabs = new ArrayList<String>();
41                 listDescs = new ArrayList<List<String>>();
42                 listMethods = new ArrayList<List<String>>();
43         }
44
45
46         public CapabilityDecl(String _origInt) {
47
48                 super(_origInt);
49                 listCapabs = new ArrayList<String>();
50                 listDescs = new ArrayList<List<String>>();
51                 listMethods = new ArrayList<List<String>>();
52         }
53
54
55         /**
56          * addNewCapability() adds a new capability into the list
57          */
58         public void addNewCapability(String newCap) {
59
60                 listCapabs.add(newCap);
61                 listDescs.add(new ArrayList<String>());
62                 listMethods.add(new ArrayList<String>());
63         }
64
65
66         /**
67          * addNewDescription() adds a new description into the list
68          */
69         public void addNewDescription(String cap, String newDesc) {
70
71                 int index = listCapabs.indexOf(cap);
72                 List<String> listDesc = listDescs.get(index);
73                 listDesc.add(newDesc);
74         }
75
76
77         /**
78          * addNewMethod() adds a new method into the list
79          */
80         public void addNewMethod(String cap, String newMethod) {
81
82                 int index = listCapabs.indexOf(cap);
83                 List<String> listMethod = listMethods.get(index);
84                 listMethod.add(newMethod);
85         }
86
87
88         /**
89          * getCapabilities() gets list of capabilities
90          */
91         public List<String> getCapabilities() {
92
93                 return listCapabs;
94         }
95
96
97         /**
98          * getDescriptions() gets list of descriptions
99          */
100         public List<String> getDescriptions(String cap) {
101
102                 int index = listCapabs.indexOf(cap);
103                 return listDescs.get(index);
104         }
105
106
107         /**
108          * getMethods() gets list of methods
109          */
110         public List<String> getMethods(String cap) {
111
112                 int index = listCapabs.indexOf(cap);
113                 // If index=-1, it means that it's not found.
114                 // There is perhaps a discrepancy in the policy file
115                 //              between the list of capabilities and requires
116                 //              sections
117                 if (index == -1)
118                         throw new Error("CapabilityDecl: Capability " + cap + 
119                                 " does not exist in this interface! Please check your (requires) policy file...");
120                 return listMethods.get(index);
121         }
122 }