/machinelearning/5.0.x/drools-core/src/main/java/org/drools/event/RuleFlowEventSupport.java

https://github.com/sotty/droolsjbpm-contributed-experiments · Java · 244 lines · 173 code · 52 blank · 19 comment · 25 complexity · b7648b888c4dae5ce86179047b9070be MD5 · raw file

  1. package org.drools.event;
  2. /*
  3. * Copyright 2005 JBoss Inc
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. import java.io.Externalizable;
  18. import java.io.IOException;
  19. import java.io.ObjectInput;
  20. import java.io.ObjectOutput;
  21. import java.util.Collections;
  22. import java.util.List;
  23. import java.util.concurrent.CopyOnWriteArrayList;
  24. import org.drools.common.InternalWorkingMemory;
  25. import org.drools.process.instance.ProcessInstance;
  26. import org.drools.runtime.process.NodeInstance;
  27. import org.drools.runtime.process.WorkflowProcessInstance;
  28. import org.drools.spi.RuleFlowGroup;
  29. /**
  30. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  31. */
  32. public class RuleFlowEventSupport implements Externalizable {
  33. // TODO separate out process level stuff
  34. private static final long serialVersionUID = 400L;
  35. private List<RuleFlowEventListener> listeners = new CopyOnWriteArrayList<RuleFlowEventListener>();
  36. public RuleFlowEventSupport() {
  37. }
  38. public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  39. listeners = (List<RuleFlowEventListener>)in.readObject();
  40. }
  41. public void writeExternal(ObjectOutput out) throws IOException {
  42. out.writeObject(listeners);
  43. }
  44. public void addEventListener(final RuleFlowEventListener listener) {
  45. if ( !this.listeners.contains( listener ) ) {
  46. this.listeners.add( listener );
  47. }
  48. }
  49. public void removeEventListener(final RuleFlowEventListener listener) {
  50. this.listeners.remove( listener );
  51. }
  52. public List<RuleFlowEventListener> getEventListeners() {
  53. return Collections.unmodifiableList( this.listeners );
  54. }
  55. public int size() {
  56. return this.listeners.size();
  57. }
  58. public boolean isEmpty() {
  59. return this.listeners.isEmpty();
  60. }
  61. public void fireBeforeRuleFlowProcessStarted(
  62. final ProcessInstance instance,
  63. final InternalWorkingMemory workingMemory) {
  64. if (this.listeners.isEmpty()) {
  65. return;
  66. }
  67. final RuleFlowStartedEvent event = new RuleFlowStartedEvent( instance );
  68. for ( RuleFlowEventListener listener: listeners ) {
  69. listener.beforeRuleFlowStarted( event, workingMemory );
  70. }
  71. }
  72. public void fireAfterRuleFlowProcessStarted(
  73. final ProcessInstance instance,
  74. final InternalWorkingMemory workingMemory) {
  75. if (this.listeners.isEmpty()) {
  76. return;
  77. }
  78. final RuleFlowStartedEvent event = new RuleFlowStartedEvent( instance );
  79. for ( RuleFlowEventListener listener: listeners ) {
  80. listener.afterRuleFlowStarted( event, workingMemory );
  81. }
  82. }
  83. public void fireBeforeRuleFlowProcessCompleted(
  84. final WorkflowProcessInstance instance,
  85. final InternalWorkingMemory workingMemory) {
  86. if (this.listeners.isEmpty()) {
  87. return;
  88. }
  89. final RuleFlowCompletedEvent event = new RuleFlowCompletedEvent( instance );
  90. for ( RuleFlowEventListener listener: listeners ) {
  91. listener.beforeRuleFlowCompleted( event, workingMemory );
  92. }
  93. }
  94. public void fireAfterRuleFlowProcessCompleted(
  95. final WorkflowProcessInstance instance,
  96. final InternalWorkingMemory workingMemory) {
  97. if (this.listeners.isEmpty()) {
  98. return;
  99. }
  100. final RuleFlowCompletedEvent event = new RuleFlowCompletedEvent( instance );
  101. for ( RuleFlowEventListener listener: listeners ) {
  102. listener.afterRuleFlowCompleted( event, workingMemory );
  103. }
  104. }
  105. public void fireBeforeRuleFlowGroupActivated(final RuleFlowGroup ruleFlowGroup,
  106. final InternalWorkingMemory workingMemory) {
  107. if ( this.listeners.isEmpty() ) {
  108. return;
  109. }
  110. final RuleFlowGroupActivatedEvent event = new RuleFlowGroupActivatedEvent( ruleFlowGroup );
  111. for ( RuleFlowEventListener listener: listeners ) {
  112. listener.beforeRuleFlowGroupActivated( event, workingMemory );
  113. }
  114. }
  115. public void fireAfterRuleFlowGroupActivated(final RuleFlowGroup ruleFlowGroup,
  116. final InternalWorkingMemory workingMemory) {
  117. if ( this.listeners.isEmpty() ) {
  118. return;
  119. }
  120. final RuleFlowGroupActivatedEvent event = new RuleFlowGroupActivatedEvent( ruleFlowGroup );
  121. for ( RuleFlowEventListener listener: listeners ) {
  122. listener.afterRuleFlowGroupActivated( event, workingMemory );
  123. }
  124. }
  125. public void fireBeforeRuleFlowGroupDeactivated(final RuleFlowGroup ruleFlowGroup,
  126. final InternalWorkingMemory workingMemory) {
  127. if ( this.listeners.isEmpty() ) {
  128. return;
  129. }
  130. final RuleFlowGroupDeactivatedEvent event = new RuleFlowGroupDeactivatedEvent( ruleFlowGroup );
  131. for ( RuleFlowEventListener listener: listeners ) {
  132. listener.beforeRuleFlowGroupDeactivated( event, workingMemory );
  133. }
  134. }
  135. public void fireAfterRuleFlowGroupDeactivated(final RuleFlowGroup ruleFlowGroup,
  136. final InternalWorkingMemory workingMemory) {
  137. if ( this.listeners.isEmpty() ) {
  138. return;
  139. }
  140. final RuleFlowGroupDeactivatedEvent event = new RuleFlowGroupDeactivatedEvent( ruleFlowGroup );
  141. for ( RuleFlowEventListener listener: listeners ) {
  142. listener.afterRuleFlowGroupDeactivated( event, workingMemory );
  143. }
  144. }
  145. public void fireBeforeRuleFlowNodeTriggered(
  146. final NodeInstance ruleFlowNodeInstance,
  147. final InternalWorkingMemory workingMemory) {
  148. if (this.listeners.isEmpty()) {
  149. return;
  150. }
  151. final RuleFlowNodeTriggeredEvent event = new RuleFlowNodeTriggeredEvent( ruleFlowNodeInstance );
  152. for ( RuleFlowEventListener listener: listeners ) {
  153. listener.beforeRuleFlowNodeTriggered( event, workingMemory );
  154. }
  155. }
  156. public void fireAfterRuleFlowNodeTriggered(
  157. final NodeInstance ruleFlowNodeInstance,
  158. final InternalWorkingMemory workingMemory) {
  159. if (this.listeners.isEmpty()) {
  160. return;
  161. }
  162. final RuleFlowNodeTriggeredEvent event = new RuleFlowNodeTriggeredEvent( ruleFlowNodeInstance );
  163. for ( RuleFlowEventListener listener: listeners ) {
  164. listener.afterRuleFlowNodeTriggered( event, workingMemory );
  165. }
  166. }
  167. public void fireBeforeRuleFlowNodeLeft(
  168. final NodeInstance ruleFlowNodeInstance,
  169. final InternalWorkingMemory workingMemory) {
  170. if (this.listeners.isEmpty()) {
  171. return;
  172. }
  173. final RuleFlowNodeTriggeredEvent event = new RuleFlowNodeTriggeredEvent( ruleFlowNodeInstance );
  174. for ( RuleFlowEventListener listener: listeners ) {
  175. listener.beforeRuleFlowNodeLeft( event, workingMemory );
  176. }
  177. }
  178. public void fireAfterRuleFlowNodeLeft(
  179. final NodeInstance ruleFlowNodeInstance,
  180. final InternalWorkingMemory workingMemory) {
  181. if (this.listeners.isEmpty()) {
  182. return;
  183. }
  184. final RuleFlowNodeTriggeredEvent event = new RuleFlowNodeTriggeredEvent( ruleFlowNodeInstance );
  185. for ( RuleFlowEventListener listener: listeners ) {
  186. listener.afterRuleFlowNodeLeft( event, workingMemory );
  187. }
  188. }
  189. public void reset() {
  190. this.listeners.clear();
  191. }
  192. }