Skip to content

Commit

Permalink
refactored result handling
Browse files Browse the repository at this point in the history
  • Loading branch information
uwegeercken committed May 8, 2024
1 parent 85f8eb1 commit 1ec58ee
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 21 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>com.datamelt</groupId>
<artifactId>evaluator</artifactId>
<version>1.5.3-SNAPSHOT</version>
<version>1.5.4-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
Expand Down
29 changes: 16 additions & 13 deletions src/main/java/com/datamelt/evaluate/check/EvaluationResult.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
package com.datamelt.evaluate.check;

import java.util.ArrayList;
import java.util.List;

public class EvaluationResult<T>
{
private final List<GroupResult<T>> groupResults= new ArrayList<>();
private final List<GroupResult<T>> groupResults;
private boolean passed;

public void addGroupResult(GroupResult<T> groupResult)
public EvaluationResult(List<GroupResult<T>> groupResults)
{
groupResults.add(groupResult);
this.groupResults = groupResults;
this.evaluate();
}

public List<GroupResult<T>> getGroupResults()
Expand All @@ -18,6 +19,11 @@ public List<GroupResult<T>> getGroupResults()
}

public boolean passed()
{
return passed;
}

private void evaluate()
{
boolean result = false;
if(!groupResults.isEmpty())
Expand All @@ -28,33 +34,30 @@ public boolean passed()
result = combineResults(result, groupResults.get(i));
}
}
return result;
passed = result;
}

private boolean combineResults(boolean groupResult1, GroupResult<T> groupResult2)
private boolean combineResults(boolean intermediateResult, GroupResult<T> groupResult2)
{
switch (groupResult2.getConnectorToPreviousGroup())
{
case AND ->
{
return groupResult2.passed() && groupResult1;
return groupResult2.passed() && intermediateResult;
}
case OR ->
{
return groupResult2.passed() || groupResult1;
return groupResult2.passed() || intermediateResult;
}
case NOT ->
{
return !(groupResult2.passed() && groupResult1);
return !(groupResult2.passed() && intermediateResult);
}
case NOR ->
{
return !(groupResult2.passed() || groupResult1);
return !(groupResult2.passed() || intermediateResult);
}
default -> throw new IllegalStateException("unexpected value: " + groupResult2.getConnectorToPreviousGroup());
}
}



}
12 changes: 6 additions & 6 deletions src/main/java/com/datamelt/evaluate/check/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ public Group<T> getGroup(String name)

public EvaluationResult<T> evaluate(T data)
{
EvaluationResult<T> evaluationResult = new EvaluationResult<>();
for(Group<T> group : getGroups())
{
evaluationResult.addGroupResult(new GroupResult<>(group, data));
}
return evaluationResult;
return new EvaluationResult<>(
getGroups()
.stream()
.map(group -> new GroupResult<>(group, data))
.toList()
);
}

public String getGroupConnectionLogic()
Expand Down
2 changes: 1 addition & 1 deletion src/test/java/com/datamelt/evaluate/check/LogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public void testLogicPassedUsingSingleRowObject()
.build();

EvaluationResult<Row> evaluationResult = logic.evaluate(testRow);
assert(logic.evaluate(testRow).passed());
assert(evaluationResult.passed());
}

@Test
Expand Down

0 comments on commit 1ec58ee

Please sign in to comment.