Skip to content

Commit a447716

Browse files
committed
replace arithmetic leaf node with unary node
1 parent 9f8b543 commit a447716

5 files changed

Lines changed: 66 additions & 86 deletions

File tree

src/main/java/com/github/sidhant92/boolparser/application/ArithmeticExpressionEvaluator.java

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
import com.github.sidhant92.boolparser.constant.Operator;
1212
import com.github.sidhant92.boolparser.domain.EvaluatedNode;
1313
import com.github.sidhant92.boolparser.domain.UnaryNode;
14-
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticLeafNode;
1514
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticNode;
1615
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticUnaryNode;
1716
import com.github.sidhant92.boolparser.domain.Node;
@@ -56,7 +55,7 @@ private Object evaluateToken(final Node node, final Map<String, Object> data) {
5655
case ARITHMETIC:
5756
return evaluateArithmeticToken((ArithmeticNode) node, data);
5857
case ARITHMETIC_LEAF:
59-
return evaluateArithmeticLeafToken((ArithmeticLeafNode) node, data);
58+
return evaluateUnaryNode((UnaryNode) node, data);
6059
case ARITHMETIC_UNARY:
6160
return evaluateUnaryArithmeticToken((ArithmeticUnaryNode) node, data);
6261
case ARITHMETIC_FUNCTION:
@@ -74,12 +73,12 @@ private Object evaluateUnaryToken(final UnaryNode unaryNode, final Map<String, O
7473
.orElse(unaryNode.getValue()) : unaryNode.getValue();
7574
}
7675

77-
private EvaluatedNode evaluateArithmeticLeafToken(final ArithmeticLeafNode arithmeticLeafNode, final Map<String, Object> data) {
78-
final Optional<Object> fetchedValue = arithmeticLeafNode.getDataType() != DataType.STRING ? Optional.of(
79-
arithmeticLeafNode.getOperand()) : ValueUtils.getValueFromMap(arithmeticLeafNode.getOperand().toString(), data);
76+
private EvaluatedNode evaluateUnaryNode(final UnaryNode unaryNode, final Map<String, Object> data) {
77+
final Optional<Object> fetchedValue = unaryNode.getDataType() != DataType.STRING ? Optional.of(
78+
unaryNode.getValue()) : ValueUtils.getValueFromMap(unaryNode.getValue().toString(), data);
8079
return fetchedValue
8180
.map(o -> EvaluatedNode.builder().value(o).dataType(ValueUtils.getDataType(o)).build())
82-
.orElseGet(() -> EvaluatedNode.builder().value(arithmeticLeafNode.getOperand()).dataType(arithmeticLeafNode.getDataType()).build());
81+
.orElseGet(() -> EvaluatedNode.builder().value(unaryNode.getValue()).dataType(unaryNode.getDataType()).build());
8382
}
8483

8584
private Object evaluateUnaryArithmeticToken(final ArithmeticUnaryNode arithmeticUnaryNode, final Map<String, Object> data) {
@@ -94,16 +93,26 @@ private Object evaluateUnaryArithmeticToken(final ArithmeticUnaryNode arithmetic
9493
}
9594

9695
private Object evaluateArithmeticFunctionToken(final ArithmeticFunctionNode arithmeticFunctionNode, final Map<String, Object> data) {
97-
final List<EvaluatedNode> resolvedValues = arithmeticFunctionNode.getItems()
96+
final List<Object> resolvedValues = arithmeticFunctionNode.getItems()
9897
.stream()
99-
.map(item -> evaluateArithmeticLeafToken(item, data))
98+
.map(item -> evaluate(item, data))
10099
.collect(Collectors.toList());
101100
final List<EvaluatedNode> flattenedValues = new ArrayList<>();
102101
resolvedValues.forEach(value -> {
103-
if (value.getValue() instanceof Collection) {
104-
((Collection<?>) value.getValue()).forEach(v -> flattenedValues.add(EvaluatedNode.builder().value(v).dataType(ValueUtils.getDataType(v)).build()));
102+
if (value instanceof EvaluatedNode) {
103+
final EvaluatedNode node = (EvaluatedNode) value;
104+
if (node.getValue() instanceof Collection) {
105+
((Collection<?>) node.getValue()).forEach(
106+
v -> flattenedValues.add(EvaluatedNode.builder().value(v).dataType(ValueUtils.getDataType(v)).build()));
107+
} else {
108+
flattenedValues.add(node);
109+
}
110+
}
111+
if (value instanceof Collection) {
112+
((Collection<?>) value).forEach(
113+
v -> flattenedValues.add(EvaluatedNode.builder().value(v).dataType(ValueUtils.getDataType(v)).build()));
105114
} else {
106-
flattenedValues.add(value);
115+
flattenedValues.add(EvaluatedNode.builder().value(value).dataType(ValueUtils.getDataType(value)).build());
107116
}
108117
});
109118
return functionEvaluatorService.evaluateArithmeticFunction(arithmeticFunctionNode.getFunctionType(), flattenedValues);
@@ -115,8 +124,8 @@ private Object evaluateArithmeticToken(final ArithmeticNode arithmeticNode, fina
115124
if (leftValue instanceof EvaluatedNode && rightValue instanceof EvaluatedNode) {
116125
final EvaluatedNode leftPair = (EvaluatedNode) leftValue;
117126
final EvaluatedNode rightPair = (EvaluatedNode) rightValue;
118-
return operatorService.evaluateArithmeticOperator(leftPair.getValue(), leftPair.getDataType(), rightPair.getValue(), rightPair.getDataType(),
119-
arithmeticNode.getOperator(), ContainerDataType.PRIMITIVE);
127+
return operatorService.evaluateArithmeticOperator(leftPair.getValue(), leftPair.getDataType(), rightPair.getValue(),
128+
rightPair.getDataType(), arithmeticNode.getOperator(), ContainerDataType.PRIMITIVE);
120129
} else if (leftValue instanceof EvaluatedNode) {
121130
final EvaluatedNode leftPair = (EvaluatedNode) leftValue;
122131
final DataType rightDataType = ValueUtils.getDataType(rightValue);

src/main/java/com/github/sidhant92/boolparser/domain/arithmetic/ArithmeticFunctionNode.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import java.util.List;
44
import com.github.sidhant92.boolparser.constant.FunctionType;
55
import com.github.sidhant92.boolparser.constant.NodeType;
6+
import com.github.sidhant92.boolparser.domain.Node;
67
import lombok.AllArgsConstructor;
78
import lombok.Builder;
89
import lombok.Getter;
@@ -20,7 +21,7 @@ public class ArithmeticFunctionNode extends ArithmeticBaseNode {
2021
private FunctionType functionType;
2122

2223

23-
private final List<ArithmeticLeafNode> items;
24+
private final List<Node> items;
2425

2526

2627
@Override

src/main/java/com/github/sidhant92/boolparser/domain/arithmetic/ArithmeticLeafNode.java

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/main/java/com/github/sidhant92/boolparser/parser/antlr/BooleanFilterListener.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
import com.github.sidhant92.boolparser.constant.LogicalOperationType;
1515
import com.github.sidhant92.boolparser.constant.Operator;
1616
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticFunctionNode;
17-
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticLeafNode;
1817
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticNode;
1918
import com.github.sidhant92.boolparser.domain.arithmetic.ArithmeticUnaryNode;
2019
import com.github.sidhant92.boolparser.domain.ArrayNode;
@@ -72,27 +71,27 @@ public void exitComparatorExpression(BooleanExpressionParser.ComparatorExpressio
7271
super.enterComparatorExpression(ctx);
7372
}
7473

75-
private ArithmeticLeafNode getArithmeticLeafNode(final BooleanExpressionParser.TypesExpressionContext ctx) {
74+
private UnaryNode getUnaryNode(final BooleanExpressionParser.TypesExpressionContext ctx) {
7675
final DataType dataType = getDataType(ctx.getStart());
7776
final Object operand = ValueUtils.convertValue(ctx.getText(), dataType);
78-
return ArithmeticLeafNode.builder().operand(operand).dataType(dataType).build();
77+
return UnaryNode.builder().value(operand).dataType(dataType).build();
7978
}
8079

8180
@Override
8281
public void exitArithmeticExpression(BooleanExpressionParser.ArithmeticExpressionContext ctx) {
8382
final Operator operator = Operator.getOperatorFromSymbol(ctx.op.getText()).orElse(Operator.EQUALS);
8483
if (ctx.left instanceof BooleanExpressionParser.TypesExpressionContext && ctx.right instanceof BooleanExpressionParser.TypesExpressionContext) {
85-
final ArithmeticLeafNode left = getArithmeticLeafNode((BooleanExpressionParser.TypesExpressionContext) ctx.left);
86-
final ArithmeticLeafNode right = getArithmeticLeafNode((BooleanExpressionParser.TypesExpressionContext) ctx.right);
84+
final UnaryNode left = getUnaryNode((BooleanExpressionParser.TypesExpressionContext) ctx.left);
85+
final UnaryNode right = getUnaryNode((BooleanExpressionParser.TypesExpressionContext) ctx.right);
8786
final ArithmeticNode node = ArithmeticNode.builder().left(left).right(right).operator(operator).build();
8887
currentNodes.add(node);
8988
} else if (ctx.left instanceof BooleanExpressionParser.TypesExpressionContext) {
90-
final ArithmeticLeafNode left = getArithmeticLeafNode((BooleanExpressionParser.TypesExpressionContext) ctx.left);
89+
final UnaryNode left = getUnaryNode((BooleanExpressionParser.TypesExpressionContext) ctx.left);
9190
final Node right = currentNodes.pop();
9291
final ArithmeticNode node = ArithmeticNode.builder().left(left).right(right).operator(operator).build();
9392
currentNodes.add(node);
9493
} else if (ctx.right instanceof BooleanExpressionParser.TypesExpressionContext) {
95-
final ArithmeticLeafNode right = getArithmeticLeafNode((BooleanExpressionParser.TypesExpressionContext) ctx.right);
94+
final UnaryNode right = getUnaryNode((BooleanExpressionParser.TypesExpressionContext) ctx.right);
9695
final Node left = currentNodes.pop();
9796
final ArithmeticNode node = ArithmeticNode.builder().left(left).right(right).operator(operator).build();
9897
currentNodes.add(node);
@@ -113,7 +112,7 @@ public void exitArithmeticExpression(BooleanExpressionParser.ArithmeticExpressio
113112
public void exitUnaryArithmeticExpression(BooleanExpressionParser.UnaryArithmeticExpressionContext ctx) {
114113
final DataType dataType = getDataType(ctx.exp.getStart());
115114
final Object operand = ValueUtils.convertValue(ctx.exp.getText(), dataType);
116-
final ArithmeticLeafNode leafNode = ArithmeticLeafNode.builder().operand(operand).dataType(dataType).build();
115+
final UnaryNode leafNode = UnaryNode.builder().value(operand).dataType(dataType).build();
117116
currentNodes.add(ArithmeticUnaryNode.builder().operand(leafNode).build());
118117
super.enterUnaryArithmeticExpression(ctx);
119118
}
@@ -150,7 +149,7 @@ public void exitArithmeticFunctionExpression(BooleanExpressionParser.ArithmeticF
150149
log.error("Error parsing expression for the string {}", ctx.getText());
151150
return new InvalidExpressionException();
152151
});
153-
final List<ArithmeticLeafNode> items = getArithmeticArrayElements(ctx.data.children);
152+
final List<Node> items = getArithmeticArrayElements(ctx.data.children);
154153
currentNodes.add(new ArithmeticFunctionNode(functionType, items));
155154
super.exitArithmeticFunctionExpression(ctx);
156155
}
@@ -185,14 +184,14 @@ private List<Pair<DataType, Object>> getArrayElements(final List<ParseTree> tree
185184
.collect(Collectors.toList());
186185
}
187186

188-
private List<ArithmeticLeafNode> getArithmeticArrayElements(final List<ParseTree> trees) {
187+
private List<Node> getArithmeticArrayElements(final List<ParseTree> trees) {
189188
return trees
190189
.stream()
191190
.filter(child -> child instanceof BooleanExpressionParser.TypesExpressionContext)
192191
.map(child -> {
193192
final DataType dataType = getDataType(((BooleanExpressionParser.TypesExpressionContext) child).start);
194193
final Object value = ValueUtils.convertValue(child.getText(), dataType);
195-
return new ArithmeticLeafNode(value, dataType);
194+
return new UnaryNode(dataType, value);
196195
})
197196
.collect(Collectors.toList());
198197
}

0 commit comments

Comments
 (0)