Block Chain Transactions, Commits multiple parts version
[iotcloud.git] / version2 / backup / src / java / iotcloud / Guard.java_backup
1 package iotcloud;
2
3 import java.util.Set;
4 import java.util.HashSet;
5 import java.util.Collection;
6 import java.util.List;
7 import java.util.ArrayList;
8
9 import java.nio.ByteBuffer;
10
11 import javax.script.ScriptEngine;
12 import javax.script.ScriptEngineManager;
13 import javax.script.ScriptException;
14 import java.lang.NullPointerException;
15
16
17 class Guard {
18
19     static final byte Equal = 1;
20     static final byte NotEqual = 2;
21     private IoTString booleanExpression;
22     private List<KeyValue> keyValsNeeded = null;
23
24     public Guard() {
25         booleanExpression = null;
26     }
27
28     public Guard(IoTString _booleanExpression) {
29         booleanExpression = _booleanExpression;
30     }
31
32     /**
33      * Create an equality expression for a key value.
34      *
35      */
36     public static String createExpression(IoTString keyName, IoTString keyValue, byte op) {
37         if (op == Equal) {
38             return keyName.toString() + "=='" + keyValue.toString() + "'";
39         } else if (op == NotEqual) {
40             return keyName.toString() + "!='" + keyValue.toString() + "'";
41         }
42
43         // Unrecognized op
44         return null;
45     }
46
47     /**
48      * Add a boolean expression to the guard.
49      *
50      */
51     public void setGuardExpression(String expr) {
52         booleanExpression = new IoTString(expr);
53     }
54
55     /**
56      * Evaluate the guard expression for a given set of key value pairs.
57      *
58      */
59     public boolean evaluate(Collection<KeyValue> kvSet) throws ScriptException, NullPointerException {
60
61         // There are no conditions to evaluate
62         if (booleanExpression == null) {
63             return true;
64         }
65
66         ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
67
68         if (keyValsNeeded == null) {
69             keyValsNeeded = new ArrayList<KeyValue>();
70
71             String booleanExprString = booleanExpression.toString();
72             for (KeyValue kv : kvSet) {
73                 if (booleanExprString.contains(kv.getKey().toString())) {
74                     keyValsNeeded.add(kv);
75                 }
76             }
77         }
78
79         // All the current key value pairs that we need to evaluate the condition
80         // String[] variables = new String[kvSet.size()];
81
82         // Fill the variables array
83         // int i = 0;
84         // for (KeyValue kv : kvSet) {
85         // for (KeyValue kv : keyValsNeeded) {
86         //     variables[i] = kv.getKey() + " ='" + kv.getValue() + "'";
87         //     i++;
88         // }
89
90         String varEval = "";
91         for (KeyValue kv : keyValsNeeded) {
92             varEval += "var " + kv.getKey() + " ='" + kv.getValue() + "'; \n";
93         }
94
95         varEval += booleanExpression.toString();
96
97         // Prep the evaluation engine (script engine)
98
99         // for (String s : variables) {
100         //     engine.eval(s);
101         // }
102         // engine.eval(varEval);
103
104
105
106         // boolean engineEval = (Boolean)engine.eval(booleanExpression.toString());
107         boolean engineEval = false;
108
109         try {
110             engineEval = (Boolean)engine.eval(varEval);
111         } catch (Exception e) {
112             // If there was an error then the script evaluated to false 
113             engineEval = false;
114         }
115
116         // Evaluate the guard condition
117         // return 1 == (Integer)engine.eval(booleanExpression.toString());
118         return engineEval;
119     }
120
121     /**
122      * Get the size of the guard condition
123      *
124      */
125     public int getSize() {
126
127         if (booleanExpression == null) {
128             return Integer.BYTES;
129         }
130
131         return Integer.BYTES + booleanExpression.length();
132     }
133
134     public void encode(ByteBuffer bb) {
135         if (booleanExpression == null) {
136             bb.putInt(0);
137         } else {
138             bb.putInt(booleanExpression.length());
139             bb.put(booleanExpression.internalBytes());
140         }
141     }
142
143     static Guard decode(ByteBuffer bb) {
144         int exprLength = bb.getInt();
145
146         if (exprLength != 0) {
147             byte[] expr = new byte[exprLength];
148             bb.get(expr);
149             return new Guard(IoTString.shallow(expr));
150         }
151         return new Guard(null);
152     }
153
154     public Guard getCopy() {
155
156         if (booleanExpression == null) {
157             return new Guard(null);
158         }
159
160         return new Guard(IoTString.shallow(booleanExpression.internalBytes()));
161     }
162
163 }