Skip to content

Commit

Permalink
Merge branch 'master' of github.com:networknt/yaml-rule
Browse files Browse the repository at this point in the history
  • Loading branch information
stevehu committed Dec 18, 2023
2 parents 7ccc7ad + c4b0617 commit 55a7004
Showing 1 changed file with 20 additions and 6 deletions.
26 changes: 20 additions & 6 deletions src/main/java/com/networknt/rule/RuleEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class RuleEngine {
private static Logger logger = LoggerFactory.getLogger(RuleEngine.class);

private Map<String, Rule> ruleMap;
private Map<String, Collection<Rule>> groupMap;
private static final Logger logger = LoggerFactory.getLogger(RuleEngine.class);
private final Map<String, Rule> ruleMap;
private final Map<String, Collection<Rule>> groupMap;
// cache for the rule action classes
public final Map<String, IAction> actionClassCache = new ConcurrentHashMap<>();

public RuleEngine(Map<String, Rule> ruleMap, Map<String, Collection<Rule>> groupMap) {
this.ruleMap = ruleMap;
Expand Down Expand Up @@ -42,7 +44,13 @@ public Map executeRules(String groupId, Map<String, Object> objMap) throws Excep
RuleAction ra = (RuleAction)itActions.next();
String actionType = ra.getActionClassName();
Collection ravs = ra.getActionValues();
IAction ia = (IAction)Class.forName(actionType).getDeclaredConstructor().newInstance();
// first check the cache to see if the action class is already loaded. If not, load it.
// the RuleLoaderStartupHook will load all the action classes during server startup.
IAction ia = actionClassCache.get(actionType);
if(ia == null) {
ia = (IAction)Class.forName(actionType).getDeclaredConstructor().newInstance();
actionClassCache.put(actionType, ia);
}
ia.performAction(objMap, resultMap, ravs);
}
}
Expand Down Expand Up @@ -78,7 +86,13 @@ public Map executeRule(String ruleId, Map<String, Object> objMap) throws Excepti
RuleAction ra = (RuleAction)it.next();
String actionType = ra.getActionClassName();
Collection<RuleActionValue> ravs = ra.getActionValues();
IAction ia = (IAction)Class.forName(actionType).getDeclaredConstructor().newInstance();
// first check the cache to see if the action class is already loaded. If not, load it.
// the RuleLoaderStartupHook will load all the action classes during server startup.
IAction ia = actionClassCache.get(actionType);
if(ia == null) {
ia = (IAction)Class.forName(actionType).getDeclaredConstructor().newInstance();
actionClassCache.put(actionType, ia);
}
ia.performAction(objMap, resultMap, ravs);
}
}
Expand Down

0 comments on commit 55a7004

Please sign in to comment.