|
1 | 1 | package com.github.sidhant92.boolparser.application; |
2 | 2 |
|
3 | | -import java.util.ArrayList; |
4 | | -import java.util.Collection; |
5 | 3 | import java.util.List; |
6 | 4 | import java.util.Map; |
7 | | -import java.util.Optional; |
8 | 5 | import java.util.stream.Collectors; |
9 | | -import org.apache.commons.lang3.tuple.Pair; |
10 | 6 | import com.github.sidhant92.boolparser.constant.ContainerDataType; |
11 | 7 | import com.github.sidhant92.boolparser.constant.DataType; |
12 | 8 | import com.github.sidhant92.boolparser.constant.Operator; |
13 | | -import com.github.sidhant92.boolparser.domain.StringNode; |
14 | | -import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticLeafNode; |
| 9 | +import com.github.sidhant92.boolparser.domain.EvaluatedNode; |
| 10 | +import com.github.sidhant92.boolparser.domain.FieldNode; |
| 11 | +import com.github.sidhant92.boolparser.domain.arithmetic.UnaryNode; |
15 | 12 | import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticNode; |
16 | | -import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticUnaryNode; |
17 | | -import com.github.sidhant92.boolparser.domain.Node; |
| 13 | +import com.github.sidhant92.boolparser.domain.logical.Node; |
18 | 14 | import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticFunctionNode; |
| 15 | +import com.github.sidhant92.boolparser.exception.DataNotFoundException; |
19 | 16 | import com.github.sidhant92.boolparser.exception.UnsupportedToken; |
20 | 17 | import com.github.sidhant92.boolparser.function.FunctionEvaluatorService; |
21 | 18 | import com.github.sidhant92.boolparser.operator.OperatorService; |
@@ -55,76 +52,66 @@ private Object evaluateToken(final Node node, final Map<String, Object> data) { |
55 | 52 | switch (node.getTokenType()) { |
56 | 53 | case ARITHMETIC: |
57 | 54 | return evaluateArithmeticToken((ArithmeticNode) node, data); |
58 | | - case ARITHMETIC_LEAF: |
59 | | - return evaluateArithmeticLeafToken((ArithmeticLeafNode) node, data); |
60 | | - case ARITHMETIC_UNARY: |
61 | | - return evaluateUnaryArithmeticToken((ArithmeticUnaryNode) node, data); |
62 | 55 | case ARITHMETIC_FUNCTION: |
63 | 56 | return evaluateArithmeticFunctionToken((ArithmeticFunctionNode) node, data); |
64 | | - case STRING: |
65 | | - return evaluateStringToken((StringNode) node, data); |
| 57 | + case UNARY: |
| 58 | + return evaluateUnaryToken((UnaryNode) node, data); |
| 59 | + case FIELD: |
| 60 | + return evaluateFieldToken((FieldNode) node, data); |
66 | 61 | default: |
67 | 62 | log.error("unsupported token {}", node.getTokenType()); |
68 | 63 | throw new UnsupportedToken(); |
69 | 64 | } |
70 | 65 | } |
71 | 66 |
|
72 | | - private Object evaluateStringToken(final StringNode stringNode, final Map<String, Object> data) { |
73 | | - return ValueUtils.getValueFromMap(stringNode.getField(), data).orElse(stringNode.getField()); |
74 | | - } |
75 | | - |
76 | | - private Pair<Object, DataType> evaluateArithmeticLeafToken(final ArithmeticLeafNode arithmeticLeafNode, final Map<String, Object> data) { |
77 | | - final Optional<Object> fetchedValue = arithmeticLeafNode.getDataType() != DataType.STRING ? Optional.of( |
78 | | - arithmeticLeafNode.getOperand()) : ValueUtils.getValueFromMap(arithmeticLeafNode.getOperand().toString(), data); |
79 | | - return fetchedValue |
80 | | - .map(o -> Pair.of(o, ValueUtils.getDataType(o))) |
81 | | - .orElseGet(() -> Pair.of(arithmeticLeafNode.getOperand(), arithmeticLeafNode.getDataType())); |
| 67 | + private Object evaluateFieldToken(final FieldNode fieldNode, final Map<String, Object> data) { |
| 68 | + if (!data.containsKey(fieldNode.getField())) { |
| 69 | + throw new DataNotFoundException(); |
| 70 | + } |
| 71 | + return data.get(fieldNode.getField()); |
82 | 72 | } |
83 | 73 |
|
84 | | - private Object evaluateUnaryArithmeticToken(final ArithmeticUnaryNode arithmeticUnaryNode, final Map<String, Object> data) { |
85 | | - final Object resolvedValue = evaluateToken(arithmeticUnaryNode.getOperand(), data); |
86 | | - if (resolvedValue instanceof Pair) { |
87 | | - final Pair<Object, DataType> pair = (Pair<Object, DataType>) resolvedValue; |
88 | | - return operatorService.evaluateArithmeticOperator(pair.getLeft(), pair.getRight(), null, null, Operator.UNARY, |
89 | | - ContainerDataType.PRIMITIVE); |
90 | | - } |
91 | | - final DataType dataType = ValueUtils.getDataType(resolvedValue); |
92 | | - return operatorService.evaluateArithmeticOperator(resolvedValue, dataType, null, null, Operator.UNARY, ContainerDataType.PRIMITIVE); |
| 74 | + private Object evaluateUnaryToken(final UnaryNode unaryNode, final Map<String, Object> data) { |
| 75 | + return unaryNode.getValue(); |
93 | 76 | } |
94 | 77 |
|
95 | 78 | private Object evaluateArithmeticFunctionToken(final ArithmeticFunctionNode arithmeticFunctionNode, final Map<String, Object> data) { |
96 | | - final List<Pair<Object, DataType>> resolvedValues = arithmeticFunctionNode.getItems() |
| 79 | + final List<Object> resolvedValues = arithmeticFunctionNode.getItems() |
97 | 80 | .stream() |
98 | | - .map(item -> evaluateArithmeticLeafToken(item, data)) |
| 81 | + .map(item -> evaluate(item, data)) |
99 | 82 | .collect(Collectors.toList()); |
100 | | - final List<Pair<Object, DataType>> flattenedValues = new ArrayList<>(); |
101 | | - resolvedValues.forEach(value -> { |
102 | | - if (value.getKey() instanceof Collection) { |
103 | | - ((Collection<?>) value.getKey()).forEach(v -> flattenedValues.add(Pair.of(v, ValueUtils.getDataType(v)))); |
104 | | - } else { |
105 | | - flattenedValues.add(value); |
106 | | - } |
107 | | - }); |
| 83 | + final List<EvaluatedNode> flattenedValues = ValueUtils.mapToEvaluatedNodes(resolvedValues); |
108 | 84 | return functionEvaluatorService.evaluateArithmeticFunction(arithmeticFunctionNode.getFunctionType(), flattenedValues); |
109 | 85 | } |
110 | 86 |
|
111 | 87 | private Object evaluateArithmeticToken(final ArithmeticNode arithmeticNode, final Map<String, Object> data) { |
112 | 88 | final Object leftValue = evaluateToken(arithmeticNode.getLeft(), data); |
| 89 | + if (arithmeticNode.getOperator().equals(Operator.UNARY)) { |
| 90 | + if (leftValue instanceof EvaluatedNode) { |
| 91 | + final EvaluatedNode left = (EvaluatedNode) leftValue; |
| 92 | + return operatorService.evaluateArithmeticOperator(left.getValue(), left.getDataType(), null, null, arithmeticNode.getOperator(), |
| 93 | + ContainerDataType.PRIMITIVE); |
| 94 | + } else { |
| 95 | + final DataType leftDataType = ValueUtils.getDataType(leftValue); |
| 96 | + return operatorService.evaluateArithmeticOperator(leftValue, leftDataType, null, null, arithmeticNode.getOperator(), |
| 97 | + ContainerDataType.PRIMITIVE); |
| 98 | + } |
| 99 | + } |
113 | 100 | final Object rightValue = evaluateToken(arithmeticNode.getRight(), data); |
114 | | - if (leftValue instanceof Pair && rightValue instanceof Pair) { |
115 | | - final Pair<Object, DataType> leftPair = (Pair<Object, DataType>) leftValue; |
116 | | - final Pair<Object, DataType> rightPair = (Pair<Object, DataType>) rightValue; |
117 | | - return operatorService.evaluateArithmeticOperator(leftPair.getLeft(), leftPair.getRight(), rightPair.getLeft(), rightPair.getRight(), |
| 101 | + if (leftValue instanceof EvaluatedNode && rightValue instanceof EvaluatedNode) { |
| 102 | + final EvaluatedNode left = (EvaluatedNode) leftValue; |
| 103 | + final EvaluatedNode right = (EvaluatedNode) rightValue; |
| 104 | + return operatorService.evaluateArithmeticOperator(left.getValue(), left.getDataType(), right.getValue(), right.getDataType(), |
118 | 105 | arithmeticNode.getOperator(), ContainerDataType.PRIMITIVE); |
119 | | - } else if (leftValue instanceof Pair) { |
120 | | - final Pair<Object, DataType> leftPair = (Pair<Object, DataType>) leftValue; |
| 106 | + } else if (leftValue instanceof EvaluatedNode) { |
| 107 | + final EvaluatedNode left = (EvaluatedNode) leftValue; |
121 | 108 | final DataType rightDataType = ValueUtils.getDataType(rightValue); |
122 | | - return operatorService.evaluateArithmeticOperator(leftPair.getLeft(), leftPair.getRight(), rightValue, rightDataType, |
| 109 | + return operatorService.evaluateArithmeticOperator(left.getValue(), left.getDataType(), rightValue, rightDataType, |
123 | 110 | arithmeticNode.getOperator(), ContainerDataType.PRIMITIVE); |
124 | | - } else if (rightValue instanceof Pair) { |
125 | | - final Pair<Object, DataType> rightPair = (Pair<Object, DataType>) rightValue; |
| 111 | + } else if (rightValue instanceof EvaluatedNode) { |
| 112 | + final EvaluatedNode right = (EvaluatedNode) rightValue; |
126 | 113 | final DataType leftDataType = ValueUtils.getDataType(leftValue); |
127 | | - return operatorService.evaluateArithmeticOperator(leftValue, leftDataType, rightPair.getLeft(), rightPair.getRight(), |
| 114 | + return operatorService.evaluateArithmeticOperator(leftValue, leftDataType, right.getValue(), right.getDataType(), |
128 | 115 | arithmeticNode.getOperator(), ContainerDataType.PRIMITIVE); |
129 | 116 | } else { |
130 | 117 | final DataType leftDataType = ValueUtils.getDataType(leftValue); |
|
0 commit comments