View Javadoc

1   /*
2    * $Id: RulesComponent.java 20895 2011-01-05 13:16:57Z tcarlson $
3    * --------------------------------------------------------------------------------------
4    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
5    *
6    * The software in this package is published under the terms of the CPAL v1.0
7    * license, a copy of which has been included with this distribution in the
8    * LICENSE.txt file.
9    */
10  
11  package org.mule.module.bpm;
12  
13  import org.mule.api.MuleEvent;
14  import org.mule.api.lifecycle.Initialisable;
15  import org.mule.api.lifecycle.InitialisationException;
16  import org.mule.component.AbstractComponent;
17  import org.mule.config.i18n.MessageFactory;
18  
19  import java.util.Collection;
20  
21  /**
22   * A service backed by a Business Rules engine such as Drools. 
23   */
24  public class RulesComponent extends AbstractComponent
25  {
26      /** The ruleset */
27      protected Rules rules;
28      
29      /** The underlying Rules Engine */
30      protected RulesEngine rulesEngine;
31  
32      /** The resource containing the rules definition. */
33      private String resource;
34  
35      /** Initial facts to be asserted at startup. */
36      private Collection initialFacts;
37  
38      /** Entry point for event stream (used by CEP) */
39      private String entryPoint;
40      
41      /** Is the knowledge base intended to be stateless? (default = false) */
42      private boolean stateless = false;
43  
44      /** Are we using the knowledge base for CEP (Complex Event Processing)? (default = false) */
45      private boolean cepMode = false;
46  
47      @Override
48      protected void doInitialise() throws InitialisationException
49      {
50          if (rulesEngine == null)
51          {
52              try
53              {
54                  rulesEngine = muleContext.getRegistry().lookupObject(RulesEngine.class);
55              }
56              catch (Exception e)
57              {
58                  throw new InitialisationException(e, this);
59              }
60          }
61          if (rulesEngine == null)
62          {
63              throw new InitialisationException(MessageFactory.createStaticMessage("The rulesEngine property must be set for this component."), this);
64          }
65          if (rulesEngine instanceof Initialisable)
66          {
67              ((Initialisable) rulesEngine).initialise();
68          }
69          
70          rules = new Rules(rulesEngine, resource, null, entryPoint, initialFacts, stateless, cepMode, flowConstruct, muleContext);
71          // Inject a callback so that the Rules Engine may generate messages within Mule.
72          rulesEngine.setMessageService(rules);        
73          rules.initialise();
74  
75          super.doInitialise();
76      }
77  
78      /**
79       * @return a handle to the fact for future reference
80       */
81      @Override
82      protected Object doInvoke(MuleEvent event) throws Exception
83      {
84          return rules.handleEvent(event);
85      }
86  
87      public void setResource(String resource)
88      {
89          this.resource = resource;
90      }
91  
92      public String getResource()
93      {
94          return resource;
95      }    
96      
97      public Collection getInitialFacts()
98      {
99          return initialFacts;
100     }
101 
102     public void setInitialFacts(Collection initialFacts)
103     {
104         this.initialFacts = initialFacts;
105     }
106     
107     public String getEntryPoint()
108     {
109         return entryPoint;
110     }
111 
112     public void setEntryPoint(String entryPoint)
113     {
114         this.entryPoint = entryPoint;
115     }
116 
117     public void setStateless(boolean stateless)
118     {
119         this.stateless = stateless;
120     }
121 
122     public boolean isStateless()
123     {
124         return stateless;
125     }
126 
127     public boolean isCepMode()
128     {
129         return cepMode;
130     }
131 
132     public void setCepMode(boolean cepMode)
133     {
134         this.cepMode = cepMode;
135     }
136 }
137 
138