Skip to content

Commit 6d0079a

Browse files
art920kalisar
andauthored
feat: add exceptions and adjust rule maker evaluate (#37)
Adjust rule maker and add exceptions Co-authored-by: kalisar <[email protected]>
1 parent 0ed2318 commit 6d0079a

File tree

5 files changed

+88
-17
lines changed

5 files changed

+88
-17
lines changed

src/main/java/com/hlag/rulemaker/RuleMaker.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,14 @@
33
import com.google.common.base.Preconditions;
44
import com.google.gson.*;
55
import com.google.gson.reflect.TypeToken;
6+
import com.hlag.rulemaker.exception.RuleMakerEvaluationException;
7+
import com.hlag.rulemaker.exception.RuleMakerException;
8+
import com.hlag.rulemaker.exception.RuleMakerMissingVariablesException;
9+
import com.hlag.rulemaker.exception.RuleMakerParseExpressionException;
610
import io.github.jamsesso.jsonlogic.JsonLogic;
711
import io.github.jamsesso.jsonlogic.JsonLogicException;
12+
import io.github.jamsesso.jsonlogic.ast.JsonLogicParseException;
13+
import io.github.jamsesso.jsonlogic.evaluator.JsonLogicEvaluationException;
814
import org.slf4j.Logger;
915
import org.slf4j.LoggerFactory;
1016

@@ -502,37 +508,37 @@ public String toJson() {
502508
return gson.toJson(expression);
503509
}
504510

511+
public Object evaluate(Object data) {
512+
return evaluate(toJson(), data);
513+
}
514+
505515
/**
506516
* Evaluates the JSON expression with the given data.
507517
*
508518
* @param data The data to evaluate the JSON expression with.
509519
* @return The result of the evaluation.
510520
*/
511521
@SuppressWarnings({"squid:S1166", "squid:S112"})
512-
public Object evaluate(Object data) {
513-
Object result = null;
522+
public static Object evaluate(String expression, Object data) {
514523
try {
515-
String json = toJson();
516524
// TODO a strange behavior, need to investigate it further
517525
if (data instanceof Map) {
518526
Map<String, Object> normalizedMap = gson.fromJson(gson.toJson(data), type);
519-
Set<String> missingVariables = findMissingVariables(normalizedMap);
527+
Set<String> missingVariables = findMissingVariables(expression, normalizedMap);
520528
if (!missingVariables.isEmpty()) {
521-
throw new IllegalArgumentException("Missing variables: " + missingVariables);
529+
throw new RuleMakerMissingVariablesException(missingVariables);
522530
}
523-
result = jsonLogic.apply(json, normalizedMap);
531+
return jsonLogic.apply(expression, normalizedMap);
524532
} else {
525-
result = jsonLogic.apply(json, data);
533+
return jsonLogic.apply(expression, data);
526534
}
527-
} catch (NullPointerException e) {
528-
return null; // bad data
535+
} catch (JsonLogicParseException e) {
536+
throw new RuleMakerParseExpressionException(e.getMessage(), e);
537+
} catch (JsonLogicEvaluationException e) {
538+
throw new RuleMakerEvaluationException(e.getMessage(), e);
529539
} catch (JsonLogicException e) {
530-
throw new RuntimeException(e.getMessage(), e);
531-
}
532-
if (log.isDebugEnabled()) {
533-
log.debug("RuleMaker: {} -> {}", toJson(), result);
540+
throw new RuleMakerException(e.getMessage(), e);
534541
}
535-
return result;
536542
}
537543

538544
/**
@@ -580,8 +586,8 @@ private static void findVarFields(JsonElement element, List<String> fields) {
580586
* @param data The data map to check for variable presence.
581587
* @return A set of variables that are missing in the data map.
582588
*/
583-
private Set<String> findMissingVariables(Map<String, Object> data) {
584-
return findVariables(toJson()).stream()
589+
private static Set<String> findMissingVariables(String expression, Map<String, Object> data) {
590+
return findVariables(expression).stream()
585591
.filter(variable -> !isVariablePresent(data, variable))
586592
.collect(Collectors.toSet());
587593
}
@@ -596,7 +602,7 @@ private Set<String> findMissingVariables(Map<String, Object> data) {
596602
* @return true if the variable is present in the data map; false otherwise.
597603
*/
598604
@SuppressWarnings("unchecked")
599-
private boolean isVariablePresent(Map<String, Object> data, String variable) {
605+
private static boolean isVariablePresent(Map<String, Object> data, String variable) {
600606
if (variable == null || variable.isBlank()) {
601607
return true;
602608
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.hlag.rulemaker.exception;
2+
3+
public class RuleMakerEvaluationException extends RuleMakerException {
4+
public RuleMakerEvaluationException(String msg) {
5+
super(msg);
6+
}
7+
8+
public RuleMakerEvaluationException(Throwable cause) {
9+
super(cause);
10+
}
11+
12+
public RuleMakerEvaluationException(String msg, Throwable cause) {
13+
super(msg, cause);
14+
}
15+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.hlag.rulemaker.exception;
2+
3+
public class RuleMakerException extends RuntimeException {
4+
5+
private RuleMakerException() {
6+
}
7+
8+
public RuleMakerException(String msg) {
9+
super(msg);
10+
}
11+
12+
public RuleMakerException(Throwable cause) {
13+
super(cause);
14+
}
15+
16+
public RuleMakerException(String msg, Throwable cause) {
17+
super(msg, cause);
18+
}
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.hlag.rulemaker.exception;
2+
3+
import lombok.Getter;
4+
5+
import java.util.Set;
6+
7+
@Getter
8+
public class RuleMakerMissingVariablesException extends RuleMakerException {
9+
10+
private final Set<String> missingVariables;
11+
12+
public RuleMakerMissingVariablesException(Set<String> missingVariables) {
13+
super("Missing variables: " + missingVariables);
14+
this.missingVariables = missingVariables;
15+
}
16+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.hlag.rulemaker.exception;
2+
3+
public class RuleMakerParseExpressionException extends RuleMakerException {
4+
public RuleMakerParseExpressionException(String msg) {
5+
super(msg);
6+
}
7+
8+
public RuleMakerParseExpressionException(Throwable cause) {
9+
super(cause);
10+
}
11+
12+
public RuleMakerParseExpressionException(String msg, Throwable cause) {
13+
super(msg, cause);
14+
}
15+
}

0 commit comments

Comments
 (0)