You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+39-6Lines changed: 39 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,15 +4,15 @@ A Boolean Expression Parser for Java
4
4
The library can help parse complex and nested boolean expressions.
5
5
The expressions are in SQL-like syntax, where you can use boolean operators and parentheses to combine individual expressions.
6
6
7
-
An expression can be as simple as `name = Sidhant`.
7
+
An expression can be as simple as `name = 'Sidhant'`.
8
8
A Complex expression is formed by combining these small expressions by logical operators and giving precedence using parenthesis
9
9
10
10
### Examples
11
11
#### Textual Equality
12
12
13
13
Format: `${attributeName} = ${value}`
14
14
15
-
Example: `name = john`
15
+
Example: `name = 'john'`
16
16
17
17
#### Numeric Comparisons
18
18
@@ -34,7 +34,7 @@ Example: `price 5.99 TO 100`
34
34
35
35
Example:
36
36
37
-
`price < 10 AND (category:Book OR NOT category:Ebook)`
37
+
`price < 10 AND (category:'Book' OR NOT category:'Ebook')`
38
38
39
39
Individual filters can be combined via boolean operators. The following operators are supported:
40
40
@@ -45,6 +45,8 @@ Individual filters can be combined via boolean operators. The following operator
45
45
Parentheses, `(` and `)`, can be used for grouping.
46
46
47
47
#### Usage Notes
48
+
* String must be enclosed either in single or double quotes.
49
+
* Variables substitution is supported by passing the name of the variable without the quotes.
48
50
* Phrases that includes quotes, like `content = "It's a wonderful day"`
49
51
* Phrases that includes quotes, like `attribute = 'She said "Hello World"'`
50
52
* For nested keys in data map you can use the dot notation, like `person.age`
@@ -72,7 +74,7 @@ dependencies {
72
74
Code
73
75
```
74
76
final BoolParser boolParser = new BoolParser();
75
-
final Try<Node> nodeOptional = boolParser.parseExpression("name = test");
77
+
final Try<Node> nodeOptional = boolParser.parseExpression("name = 'test'");
76
78
```
77
79
78
80
### Node Types Post Parsing
@@ -120,6 +122,12 @@ private final DataType dataType;
120
122
private final Object value;
121
123
```
122
124
125
+
####
126
+
FieldNode
127
+
```
128
+
private final String field;
129
+
```
130
+
123
131
####
124
132
InNode
125
133
```
@@ -160,7 +168,7 @@ final BooleanExpressionEvaluator booleanExpressionEvaluator = new BooleanExpress
160
168
final Map<String, Object> data = new HashMap<>();
161
169
data.put("age", 25);
162
170
data.put("name", "sid");
163
-
final Try<Boolean> resultOptional = booleanExpressionEvaluator.evaluate("name = sid AND age = 25", data);
171
+
final Try<Boolean> resultOptional = booleanExpressionEvaluator.evaluate("name = 'sid' AND age = 25", data);
164
172
assertTrue(resultOptional.isPresent());
165
173
assertTrue(resultOptional.get());
166
174
```
@@ -171,7 +179,7 @@ final Map<String, Object> data = new HashMap<>();
171
179
data.put("age", 25);
172
180
data.put("name", "sid");
173
181
data.put("num", 45);
174
-
final Try<Boolean> resultOptional = booleanExpressionEvaluator.evaluate("name:sid AND (age = 25 OR num = 44)", data);
182
+
final Try<Boolean> resultOptional = booleanExpressionEvaluator.evaluate("name = sid AND (age = 25 OR num = 44)", data);
175
183
assertTrue(resultOptional.isPresent());
176
184
assertTrue(resultOptional.get());
177
185
```
@@ -211,6 +219,22 @@ The following Operators are supported:
211
219
5. Modulus (%)
212
220
6. Exponent (^)
213
221
222
+
The following functions are supported:
223
+
1. Minimum (min)
224
+
2. Maximum (max)
225
+
3. Average (avg)
226
+
4. Sum (sum)
227
+
5. Mean (mean)
228
+
6. Mode (mode)
229
+
7. Median (median)
230
+
8. Integer (int) - converts the input to integer
231
+
9. Length (len) - Returns length of the give array
232
+
233
+
Syntax For using functions
234
+
Format: `${FunctionIdentifier} (item1, item2...)`
235
+
236
+
Example: `min (1,2,3)` or with variable substitution `min (a,b,c)`
237
+
214
238
Usage examples:
215
239
216
240
Simple Addition Operation
@@ -232,5 +256,14 @@ final Try<Object> resultOptional = evaluator.evaluate("((5 * 2) + a) * 2 + (1 +
232
256
assertTrue(resultOptional.isPresent());
233
257
assertTrue(resultOptional.get(), 56);
234
258
```
259
+
Function Usage
260
+
```
261
+
final ArithmeticExpressionEvaluator evaluator = new ArithmeticExpressionEvaluator(new Boolparser());
262
+
final Map<String, Object> data = new HashMap<>();
263
+
data.put("a", 10);
264
+
final Try<Object> resultOptional = arithmeticExpressionEvaluator.evaluate("min (1,2,3)", data);
265
+
assertTrue(resultOptional.isSuccess());
266
+
assertEquals(resultOptional.get(), 1);
267
+
```
235
268
236
269
[For a complete list of examples please check out the test file](src/test/java/com/github/sidhant92/boolparser/application/ArithmeticExpressionEvaluatorTest.java)
0 commit comments