Skip to content

Commit 6f1a8e4

Browse files
committed
update readme
1 parent 39bf89b commit 6f1a8e4

1 file changed

Lines changed: 39 additions & 6 deletions

File tree

README.md

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ A Boolean Expression Parser for Java
44
The library can help parse complex and nested boolean expressions.
55
The expressions are in SQL-like syntax, where you can use boolean operators and parentheses to combine individual expressions.
66

7-
An expression can be as simple as `name = Sidhant`.
7+
An expression can be as simple as `name = 'Sidhant'`.
88
A Complex expression is formed by combining these small expressions by logical operators and giving precedence using parenthesis
99

1010
### Examples
1111
#### Textual Equality
1212

1313
Format: `${attributeName} = ${value}`
1414

15-
Example: `name = john`
15+
Example: `name = 'john'`
1616

1717
#### Numeric Comparisons
1818

@@ -34,7 +34,7 @@ Example: `price 5.99 TO 100`
3434

3535
Example:
3636

37-
`price < 10 AND (category:Book OR NOT category:Ebook)`
37+
`price < 10 AND (category:'Book' OR NOT category:'Ebook')`
3838

3939
Individual filters can be combined via boolean operators. The following operators are supported:
4040

@@ -45,6 +45,8 @@ Individual filters can be combined via boolean operators. The following operator
4545
Parentheses, `(` and `)`, can be used for grouping.
4646

4747
#### 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.
4850
* Phrases that includes quotes, like `content = "It's a wonderful day"`
4951
* Phrases that includes quotes, like `attribute = 'She said "Hello World"'`
5052
* For nested keys in data map you can use the dot notation, like `person.age`
@@ -72,7 +74,7 @@ dependencies {
7274
Code
7375
```
7476
final BoolParser boolParser = new BoolParser();
75-
final Try<Node> nodeOptional = boolParser.parseExpression("name = test");
77+
final Try<Node> nodeOptional = boolParser.parseExpression("name = 'test'");
7678
```
7779

7880
### Node Types Post Parsing
@@ -120,6 +122,12 @@ private final DataType dataType;
120122
private final Object value;
121123
```
122124

125+
####
126+
FieldNode
127+
```
128+
private final String field;
129+
```
130+
123131
####
124132
InNode
125133
```
@@ -160,7 +168,7 @@ final BooleanExpressionEvaluator booleanExpressionEvaluator = new BooleanExpress
160168
final Map<String, Object> data = new HashMap<>();
161169
data.put("age", 25);
162170
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);
164172
assertTrue(resultOptional.isPresent());
165173
assertTrue(resultOptional.get());
166174
```
@@ -171,7 +179,7 @@ final Map<String, Object> data = new HashMap<>();
171179
data.put("age", 25);
172180
data.put("name", "sid");
173181
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);
175183
assertTrue(resultOptional.isPresent());
176184
assertTrue(resultOptional.get());
177185
```
@@ -211,6 +219,22 @@ The following Operators are supported:
211219
5. Modulus (%)
212220
6. Exponent (^)
213221

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+
214238
Usage examples:
215239

216240
Simple Addition Operation
@@ -232,5 +256,14 @@ final Try<Object> resultOptional = evaluator.evaluate("((5 * 2) + a) * 2 + (1 +
232256
assertTrue(resultOptional.isPresent());
233257
assertTrue(resultOptional.get(), 56);
234258
```
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+
```
235268

236269
[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

Comments
 (0)