View Javadoc
1   package org.kuali.ole.deliver.drools;
2   
3   import org.apache.log4j.Logger;
4   import org.jfree.util.Log;
5   import org.kie.api.event.rule.AgendaEventListener;
6   import org.kie.api.runtime.KieSession;
7   import org.kie.internal.runtime.StatefulKnowledgeSession;
8   
9   import java.util.Collection;
10  import java.util.Iterator;
11  import java.util.List;
12  
13  /**
14   * Created by pvsubrah on 3/24/15.
15   */
16  public class DroolsEngineLogger {
17      private static final Logger LOG = Logger.getLogger(DroolsEngineLogger.class);
18      private static DroolsEngineLogger droolsEngineLogger;
19  
20  
21      private DroolsEngineLogger() {
22  
23      }
24  
25      public static DroolsEngineLogger getInstance(){
26          if(null == droolsEngineLogger){
27              droolsEngineLogger = new DroolsEngineLogger();
28          }
29          return droolsEngineLogger;
30      }
31  
32      public void logRulesFired(String info){
33  
34          StatefulKnowledgeSession session = DroolsEngine.getInstance().getSession();
35  
36          Collection<AgendaEventListener> agendaEventListeners = session
37                  .getAgendaEventListeners();
38          CustomAgendaEventListener agendaEventListener = (CustomAgendaEventListener) (agendaEventListeners.toArray
39                  ()[0]);
40  
41  
42          List<String> rulesFiredList = agendaEventListener.getRulesFired();
43          if (rulesFiredList.size() == 0) {
44              System.out.println("No rules fired");
45          } else {
46              System.out.println("Rules Fired for " + info);
47          }
48          for (Iterator<String> stringIterator = rulesFiredList.iterator(); stringIterator.hasNext(); ) {
49              String rulesFired = stringIterator.next();
50              Log.info(rulesFired);
51              System.out.println(rulesFired);
52          }
53      }
54  
55      public void logRulesFired(String info, KieSession session){
56  
57          Collection<AgendaEventListener> agendaEventListeners = session
58                  .getAgendaEventListeners();
59          CustomAgendaEventListener agendaEventListener = (CustomAgendaEventListener) (agendaEventListeners.toArray
60                  ()[0]);
61  
62  
63          List<String> rulesFiredList = agendaEventListener.getRulesFired();
64          if (rulesFiredList.size() == 0) {
65              System.out.println("No rules fired");
66          } else {
67              System.out.println("Rules Fired for " + info);
68          }
69          for (Iterator<String> stringIterator = rulesFiredList.iterator(); stringIterator.hasNext(); ) {
70              String rulesFired = stringIterator.next();
71              Log.info(rulesFired);
72              System.out.println(rulesFired);
73          }
74      }
75  }