|
1 | 1 | # bool-parser-java |
2 | | -Boolean Expression Parser in 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 | + |
| 51 | +## Usage |
| 52 | +POM |
| 53 | +```xml |
| 54 | + <dependencies> |
| 55 | + <dependency> |
| 56 | + <groupId>com.github.sidhant92</groupId> |
| 57 | + <artifactId>bool-parser-java</artifactId> |
| 58 | + <version>1.0.0</version> |
| 59 | + </dependency> |
| 60 | + </dependencies> |
| 61 | +``` |
| 62 | +Gradle |
| 63 | +``` |
| 64 | +dependencies { |
| 65 | + implementation "com.github.sidhant92:bool-parser-java:1.0.0" |
| 66 | +} |
| 67 | +``` |
| 68 | + |
| 69 | + |
| 70 | +Code |
| 71 | +``` |
| 72 | +final BoolParser boolParser = new BoolParser(); |
| 73 | +final Try<Token> tokenOptional = boolParser.parseExpression("name = test"); |
| 74 | +``` |
| 75 | + |
| 76 | +### Token Types Post Parsing |
| 77 | +#### |
| 78 | +StringToken |
| 79 | +``` |
| 80 | +private final String field; |
| 81 | +
|
| 82 | +private final String value; |
| 83 | +``` |
| 84 | + |
| 85 | +#### |
| 86 | +NumericToken |
| 87 | +``` |
| 88 | +private final String field; |
| 89 | +
|
| 90 | +private final Object value; |
| 91 | +
|
| 92 | +private final Operator operator; |
| 93 | +
|
| 94 | +private final DataType dataType; |
| 95 | +``` |
| 96 | + |
| 97 | +#### |
| 98 | +NumericRangeToken |
| 99 | +``` |
| 100 | +private final String field; |
| 101 | +
|
| 102 | +private final Object fromValue; |
| 103 | +
|
| 104 | +private final Object toValue; |
| 105 | +
|
| 106 | +private final DataType fromDataType; |
| 107 | +
|
| 108 | +private final DataType toDataType; |
| 109 | +``` |
| 110 | + |
| 111 | +#### |
| 112 | +BooleanToken |
| 113 | +``` |
| 114 | +private Token left; |
| 115 | +
|
| 116 | +private Token right; |
| 117 | +
|
| 118 | +private LogicalOperationType operator; |
| 119 | +``` |
| 120 | + |
| 121 | +#### |
| 122 | +UnaryToken |
| 123 | +``` |
| 124 | +private final DataType dataType; |
| 125 | +
|
| 126 | +private final Object value; |
| 127 | +``` |
| 128 | + |
| 129 | +#### |
| 130 | +InToken |
| 131 | +``` |
| 132 | +private final String field; |
| 133 | +
|
| 134 | +private final List<Pair<DataType, Object>> items; |
| 135 | +``` |
| 136 | + |
| 137 | + |
| 138 | +[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