33import com .google .common .base .Preconditions ;
44import com .google .gson .*;
55import 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 ;
610import io .github .jamsesso .jsonlogic .JsonLogic ;
711import io .github .jamsesso .jsonlogic .JsonLogicException ;
12+ import io .github .jamsesso .jsonlogic .ast .JsonLogicParseException ;
13+ import io .github .jamsesso .jsonlogic .evaluator .JsonLogicEvaluationException ;
814import org .slf4j .Logger ;
915import 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 }
0 commit comments