|
| 1 | +# bool-parser-java |
| 2 | +A Boolean Expression Parser for Java |
| 3 | + |
| 4 | +The library can help parse complex and nested boolean expressions. |
| 5 | +The expressions are in SQL-like syntax, where you can use boolean operators and parentheses to combine individual expressions. |
| 6 | + |
| 7 | +An expression can be as simple as `name = Sidhant`. |
| 8 | +A Complex expression is formed by combining these small expressions by logical operators and giving precedence using parenthesis |
| 9 | + |
| 10 | +### Examples |
| 11 | +#### Textual Equality |
| 12 | + |
| 13 | +Format: `${attributeName} = ${value}` |
| 14 | + |
| 15 | +Example: `name = john` |
| 16 | + |
| 17 | +#### Numeric Comparisons |
| 18 | + |
| 19 | +Format: `${attributeName} ${operator} ${value}` |
| 20 | + |
| 21 | +Example: `price > 12.99` |
| 22 | + |
| 23 | +The ${value} must be numeric. Supported operators are `<`, `<=`, `=`, `!=`, `>=` and `>`, with the same semantics as in virtually all programming languages. |
| 24 | + |
| 25 | +#### Numeric Range |
| 26 | + |
| 27 | +Format: `${attributeName} ${lowerBound} TO ${upperBound}` |
| 28 | + |
| 29 | +Example: `price 5.99 TO 100` |
| 30 | + |
| 31 | +`${lowerBound}` and `${upperBound}` must be numeric. Both are inclusive. |
| 32 | + |
| 33 | +#### Boolean operators |
| 34 | + |
| 35 | +Example: |
| 36 | + |
| 37 | +`price < 10 AND (category:Book OR NOT category:Ebook)` |
| 38 | + |
| 39 | +Individual filters can be combined via boolean operators. The following operators are supported: |
| 40 | + |
| 41 | +* `OR`: must match any of the combined conditions (disjunction) |
| 42 | +* `AND`: must match all of the combined conditions (conjunction) |
| 43 | +* `NOT`: negate a filter |
| 44 | + |
| 45 | +Parentheses, `(` and `)`, can be used for grouping. |
| 46 | + |
| 47 | +#### Usage Notes |
| 48 | +* Phrases that includes quotes, like `content = "It's a wonderful day"` |
| 49 | +* Phrases that includes quotes, like `attribute = 'She said "Hello World"'` |
| 50 | +* For nested keys in data map you can use the dot notation, like `person.age` |
| 51 | + |
| 52 | +## Usage |
| 53 | +POM |
| 54 | +```xml |
| 55 | + <dependencies> |
| 56 | + <dependency> |
| 57 | + <groupId>com.github.sidhant92</groupId> |
| 58 | + <artifactId>bool-parser-java</artifactId> |
| 59 | + <version>1.0.0</version> |
| 60 | + </dependency> |
| 61 | + </dependencies> |
| 62 | +``` |
| 63 | +Gradle |
| 64 | +``` |
| 65 | +dependencies { |
| 66 | + implementation "com.github.sidhant92:bool-parser-java:1.0.0" |
| 67 | +} |
| 68 | +``` |
| 69 | + |
| 70 | + |
| 71 | +Code |
| 72 | +``` |
| 73 | +final BoolParser boolParser = new BoolParser(); |
| 74 | +final Try<Node> nodeOptional = boolParser.parseExpression("name = test"); |
| 75 | +``` |
| 76 | + |
| 77 | +### Node Types Post Parsing |
| 78 | +#### |
| 79 | +NumericNode |
| 80 | +``` |
| 81 | +private final String field; |
| 82 | +
|
| 83 | +private final Object value; |
| 84 | +
|
| 85 | +private final Operator operator; |
| 86 | +
|
| 87 | +private final DataType dataType; |
| 88 | +``` |
| 89 | + |
| 90 | +#### |
| 91 | +NumericRangeNode |
| 92 | +``` |
| 93 | +private final String field; |
| 94 | +
|
| 95 | +private final Object fromValue; |
| 96 | +
|
| 97 | +private final Object toValue; |
| 98 | +
|
| 99 | +private final DataType fromDataType; |
| 100 | +
|
| 101 | +private final DataType toDataType; |
| 102 | +``` |
| 103 | + |
| 104 | +#### |
| 105 | +BooleanNode |
| 106 | +``` |
| 107 | +private Node left; |
| 108 | +
|
| 109 | +private Node right; |
| 110 | +
|
| 111 | +private LogicalOperationType operator; |
| 112 | +``` |
| 113 | + |
| 114 | +#### |
| 115 | +UnaryNode |
| 116 | +``` |
| 117 | +private final DataType dataType; |
| 118 | +
|
| 119 | +private final Object value; |
| 120 | +``` |
| 121 | + |
| 122 | +#### |
| 123 | +InNode |
| 124 | +``` |
| 125 | +private final String field; |
| 126 | +
|
| 127 | +private final List<Pair<DataType, Object>> items; |
| 128 | +``` |
| 129 | + |
| 130 | + |
| 131 | +## Applications |
| 132 | + |
| 133 | +### Boolean Expression Evaluator |
| 134 | + |
| 135 | +The library can be used to evaluate a boolean expression. |
| 136 | + |
| 137 | +The following Data Types are supported: |
| 138 | +1. String |
| 139 | +2. Integer |
| 140 | +3. Long |
| 141 | +4. Decimal |
| 142 | +5. Boolean |
| 143 | +6. Semantic Version |
| 144 | + |
| 145 | +Usage examples: |
| 146 | + |
| 147 | +Simple Numerical Comparison |
| 148 | +``` |
| 149 | +final BooleanExpressionEvaluator booleanExpressionEvaluator = new BooleanExpressionEvaluator(); |
| 150 | +final Map<String, Object> data = new HashMap<>(); |
| 151 | +data.put("age", 26); |
| 152 | +final Try<Boolean> result = booleanExpressionEvaluator.evaluate("age >= 27", data); |
| 153 | +assertTrue(booleanOptional.isPresent()); |
| 154 | +assertFalse(booleanOptional.get()); |
| 155 | +``` |
| 156 | +Boolean Comparison |
| 157 | +``` |
| 158 | +final BooleanExpressionEvaluator booleanExpressionEvaluator = new BooleanExpressionEvaluator(); |
| 159 | +final Map<String, Object> data = new HashMap<>(); |
| 160 | +data.put("age", 25); |
| 161 | +data.put("name", "sid"); |
| 162 | +final Try<Boolean> result = booleanExpressionEvaluator.evaluate("name = sid AND age = 25", data); |
| 163 | +assertTrue(booleanOptional.isPresent()); |
| 164 | +assertTrue(booleanOptional.get()); |
| 165 | +``` |
| 166 | +Nested Boolean Comparison |
| 167 | +``` |
| 168 | +final BooleanExpressionEvaluator booleanExpressionEvaluator = new BooleanExpressionEvaluator(); |
| 169 | +final Map<String, Object> data = new HashMap<>(); |
| 170 | +data.put("age", 25); |
| 171 | +data.put("name", "sid"); |
| 172 | +data.put("num", 45); |
| 173 | +final Try<Boolean> result = booleanExpressionEvaluator.evaluate("name:sid AND (age = 25 OR num = 44)", data); |
| 174 | +assertTrue(booleanOptional.isPresent()); |
| 175 | +assertTrue(booleanOptional.get()); |
| 176 | +``` |
| 177 | +App Version Comparison |
| 178 | +``` |
| 179 | +final BooleanExpressionEvaluator booleanExpressionEvaluator = new BooleanExpressionEvaluator(); |
| 180 | +final Map<String, Object> data = new HashMap<>(); |
| 181 | +data.put("app_version", "1.5.9"); |
| 182 | +final Try<Boolean> result = booleanExpressionEvaluator.evaluate("app_version < 1.5.10", data); |
| 183 | +assertTrue(booleanOptional.isPresent()); |
| 184 | +assertTrue(booleanOptional.get()); |
| 185 | +``` |
| 186 | + |
| 187 | +The return type is `Try<Boolean>`. Failure means that parsing has failed and any fallback can be used. |
| 188 | + |
| 189 | + |
| 190 | +[For a complete list of examples please check out the test file](src/test/java/com/github/sidhant92/boolparser/application/BooleanExpressionEvaluatorTest.java) |
0 commit comments