4 import java.util.HashSet;
5 import java.util.Collection;
7 import java.util.ArrayList;
9 import java.nio.ByteBuffer;
11 import javax.script.ScriptEngine;
12 import javax.script.ScriptEngineManager;
13 import javax.script.ScriptException;
14 import java.lang.NullPointerException;
19 static final byte Equal = 1;
20 static final byte NotEqual = 2;
21 private IoTString booleanExpression;
22 private List<KeyValue> keyValsNeeded = null;
25 booleanExpression = null;
28 public Guard(IoTString _booleanExpression) {
29 booleanExpression = _booleanExpression;
33 * Create an equality expression for a key value.
36 public static String createExpression(IoTString keyName, IoTString keyValue, byte op) {
38 return keyName.toString() + "=='" + keyValue.toString() + "'";
39 } else if (op == NotEqual) {
40 return keyName.toString() + "!='" + keyValue.toString() + "'";
48 * Add a boolean expression to the guard.
51 public void setGuardExpression(String expr) {
52 booleanExpression = new IoTString(expr);
56 * Evaluate the guard expression for a given set of key value pairs.
59 public boolean evaluate(Collection<KeyValue> kvSet) throws ScriptException, NullPointerException {
61 // There are no conditions to evaluate
62 if (booleanExpression == null) {
66 ScriptEngine engine = new ScriptEngineManager().getEngineByName("JavaScript");
68 if (keyValsNeeded == null) {
69 keyValsNeeded = new ArrayList<KeyValue>();
71 String booleanExprString = booleanExpression.toString();
72 for (KeyValue kv : kvSet) {
73 if (booleanExprString.contains(kv.getKey().toString())) {
74 keyValsNeeded.add(kv);
79 // All the current key value pairs that we need to evaluate the condition
80 // String[] variables = new String[kvSet.size()];
82 // Fill the variables array
84 // for (KeyValue kv : kvSet) {
85 // for (KeyValue kv : keyValsNeeded) {
86 // variables[i] = kv.getKey() + " ='" + kv.getValue() + "'";
91 for (KeyValue kv : keyValsNeeded) {
92 varEval += "var " + kv.getKey() + " ='" + kv.getValue() + "'; \n";
95 varEval += booleanExpression.toString();
97 // Prep the evaluation engine (script engine)
99 // for (String s : variables) {
102 // engine.eval(varEval);
106 // boolean engineEval = (Boolean)engine.eval(booleanExpression.toString());
107 boolean engineEval = false;
110 engineEval = (Boolean)engine.eval(varEval);
111 } catch (Exception e) {
112 // If there was an error then the script evaluated to false
116 // Evaluate the guard condition
117 // return 1 == (Integer)engine.eval(booleanExpression.toString());
122 * Get the size of the guard condition
125 public int getSize() {
127 if (booleanExpression == null) {
128 return Integer.BYTES;
131 return Integer.BYTES + booleanExpression.length();
134 public void encode(ByteBuffer bb) {
135 if (booleanExpression == null) {
138 bb.putInt(booleanExpression.length());
139 bb.put(booleanExpression.internalBytes());
143 static Guard decode(ByteBuffer bb) {
144 int exprLength = bb.getInt();
146 if (exprLength != 0) {
147 byte[] expr = new byte[exprLength];
149 return new Guard(IoTString.shallow(expr));
151 return new Guard(null);
154 public Guard getCopy() {
156 if (booleanExpression == null) {
157 return new Guard(null);
160 return new Guard(IoTString.shallow(booleanExpression.internalBytes()));