1616import org .openmrs .module .drools .event .DroolsEventsManager ;
1717import org .openmrs .module .drools .session .*;
1818import org .openmrs .module .drools .utils .CommonUtils ;
19+ import org .slf4j .Logger ;
20+ import org .slf4j .LoggerFactory ;
1921import org .springframework .beans .factory .annotation .Autowired ;
2022
2123import java .util .*;
2426
2527public class DroolsEngineServiceImpl extends BaseOpenmrsService implements DroolsEngineService {
2628
29+ private static final Logger log = LoggerFactory .getLogger (DroolsEngineServiceImpl .class );
30+
2731 private KieContainer kieContainer ;
2832
2933 @ Autowired
@@ -40,47 +44,83 @@ public class DroolsEngineServiceImpl extends BaseOpenmrsService implements Drool
4044
4145 @ Override
4246 public KieSession requestSession (String sessionId ) {
47+ log .info ("Requesting Drools session: {}" , sessionId );
4348 KieSession session ;
4449 if (ruleConfigs == null ) {
50+ log .debug ("Initializing session configurations" );
4551 ruleConfigs = initializeSessionConfigs ();
4652 }
4753 if (kieContainer == null ) {
54+ log .debug ("Building KieContainer" );
4855 kieContainer = kieContainerBuilder .build ();
4956 }
5057 if (ruleConfigs .get (sessionId ) != null ) {
58+ log .debug ("Creating KieSession for sessionId: {}" , sessionId );
5159 session = CommonUtils .createKieSession (kieContainer , ruleConfigs .get (sessionId ), droolsConfig .getExternalEvaluatorManager (), globalBindings );
5260 eventsManager .subscribeSessionEventListenersIfNecessary (sessionId , session , ruleConfigs );
53-
61+ log . info ( "Successfully created session: {}" , sessionId );
5462 return session ;
5563 } else {
64+ log .error ("Session configuration not found for: {}" , sessionId );
5665 throw new DroolsSessionException ("Can't find session configuration for: " + sessionId );
5766 }
5867 }
5968
6069 @ Override
6170 public KieSession evaluate (String sessionId , Collection <? extends OpenmrsObject > facts ) {
71+ log .info ("Evaluating session: {} with {} facts" , sessionId , facts .size ());
72+ long startTime = System .currentTimeMillis ();
73+
6274 KieSession currentSession = requestSession (sessionId );
6375 if (currentSession != null ) {
64- facts .forEach (currentSession ::insert );
65- currentSession .fireAllRules (getSessionAgendaFilter (currentSession , ruleConfigs .get (sessionId )));
76+ log .debug ("Inserting {} facts into session: {}" , facts .size (), sessionId );
77+ facts .forEach (fact -> {
78+ log .trace ("Inserting fact: {} ({})" , fact .getClass ().getSimpleName (), fact );
79+ currentSession .insert (fact );
80+ });
81+
82+ log .debug ("Firing all rules for session: {}" , sessionId );
83+ int rulesFired = currentSession .fireAllRules (getSessionAgendaFilter (currentSession , ruleConfigs .get (sessionId )));
84+ long duration = System .currentTimeMillis () - startTime ;
85+
86+ log .info ("Session {} evaluation completed: {} rules fired in {}ms" , sessionId , rulesFired , duration );
6687 } else {
88+ log .error ("Could not establish KIE session: {}" , sessionId );
6789 throw new DroolsSessionException ("Could not establish a KIE session of ID: " + sessionId );
6890 }
6991 return currentSession ;
7092 }
7193
7294 @ Override
7395 public DroolsExecutionResult evaluate (String sessionId , Collection <Object > facts , String resultClassName ) {
96+ log .info ("Evaluating session: {} with {} facts, expecting results of type: {}" , sessionId , facts .size (), resultClassName );
97+ long startTime = System .currentTimeMillis ();
98+
7499 KieSession currentSession = requestSession (sessionId );
75100 DroolsExecutionResult result ;
76101 if (currentSession != null ) {
77- facts .forEach (currentSession ::insert );
102+ log .debug ("Inserting {} facts into session: {}" , facts .size (), sessionId );
103+ facts .forEach (fact -> {
104+ log .trace ("Inserting fact: {} ({})" , fact .getClass ().getSimpleName (), fact );
105+ currentSession .insert (fact );
106+ });
107+
108+ log .debug ("Firing all rules for session: {}" , sessionId );
78109 int fired = currentSession .fireAllRules (getSessionAgendaFilter (currentSession , ruleConfigs .get (sessionId )));
110+
111+ log .debug ("Retrieving session objects of type: {}" , resultClassName );
79112 List <?> results = getSessionObjects (currentSession , resolveClass (resultClassName , currentSession .getKieBase ()));
80113 result = new DroolsExecutionResult (sessionId , fired , (List <Object >) results );
114+
115+ long duration = System .currentTimeMillis () - startTime ;
116+ log .info ("Session {} evaluation completed: {} rules fired, {} results returned in {}ms" ,
117+ sessionId , fired , results .size (), duration );
118+
81119 currentSession .dispose ();
120+ log .debug ("Session {} disposed" , sessionId );
82121
83122 } else {
123+ log .error ("Could not establish KIE session: {}" , sessionId );
84124 throw new DroolsSessionException ("Could not establish a KIE session of ID: " + sessionId );
85125 }
86126
0 commit comments