/machinelearning/5.0/drools-core/src/main/java/org/drools/common/RuleFlowGroupImpl.java

https://github.com/droolsjbpm/droolsjbpm-contributed-experiments · Java · 291 lines · 207 code · 43 blank · 41 comment · 32 complexity · 19de69191f3532862e418be6a17270be MD5 · raw file

  1. package org.drools.common;
  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.IOException;
  18. import java.io.ObjectInput;
  19. import java.io.ObjectInputStream;
  20. import java.io.ObjectOutput;
  21. import java.util.List;
  22. import java.util.concurrent.CopyOnWriteArrayList;
  23. import org.drools.marshalling.PersisterEnums;
  24. import org.drools.marshalling.MarshallerReaderContext;
  25. import org.drools.marshalling.MarshallerWriteContext;
  26. import org.drools.spi.Activation;
  27. import org.drools.util.Iterator;
  28. import org.drools.util.LinkedList;
  29. /**
  30. * Implementation of a <code>RuleFlowGroup</code> that collects activations
  31. * of rules of this ruleflow-group.
  32. * If this group is activated, all its activations are added to the agenda.
  33. * As long as this group is active, its activations are added to the agenda.
  34. * Deactivating the group removes all its activations from the agenda and
  35. * collects them until it is activated again.
  36. * By default, <code>RuleFlowGroups</code> are automatically deactivated when there are no more
  37. * activations in the <code>RuleFlowGroup</code>. However, this can be configured.
  38. *
  39. * @author <a href="mailto:mark.proctor@jboss.com">Mark Proctor</a>
  40. * @author <a href="mailto:kris_verlaenen@hotmail.com">Kris Verlaenen</a>
  41. *
  42. */
  43. public class RuleFlowGroupImpl
  44. implements
  45. InternalRuleFlowGroup {
  46. private static final long serialVersionUID = 400L;
  47. private InternalWorkingMemory workingMemory;
  48. private String name;
  49. private boolean active = false;
  50. private boolean autoDeactivate = true;
  51. private LinkedList list;
  52. private List<RuleFlowGroupListener> listeners;
  53. public RuleFlowGroupImpl() {
  54. }
  55. /**
  56. * Construct a <code>RuleFlowGroupImpl</code> with the given name.
  57. *
  58. * @param name
  59. * The RuleFlowGroup name.
  60. */
  61. public RuleFlowGroupImpl(final String name) {
  62. this.name = name;
  63. this.list = new LinkedList();
  64. }
  65. public RuleFlowGroupImpl(final String name, final boolean active, final boolean autoDeactivate) {
  66. this.name = name;
  67. this.active = active;
  68. this.autoDeactivate = autoDeactivate;
  69. this.list = new LinkedList();
  70. }
  71. public void readExternal(ObjectInput in) throws IOException,
  72. ClassNotFoundException {
  73. workingMemory = (InternalWorkingMemory) in.readObject();
  74. name = (String) in.readObject();
  75. active = in.readBoolean();
  76. list = (LinkedList) in.readObject();
  77. autoDeactivate = in.readBoolean();
  78. listeners = (List<RuleFlowGroupListener>) in.readObject();
  79. }
  80. public void writeExternal(ObjectOutput out) throws IOException {
  81. out.writeObject( workingMemory );
  82. out.writeObject( name );
  83. out.writeBoolean( active );
  84. out.writeObject( list );
  85. out.writeBoolean( autoDeactivate );
  86. out.writeObject( listeners );
  87. }
  88. public String getName() {
  89. return this.name;
  90. }
  91. public void setWorkingMemory(InternalWorkingMemory workingMemory) {
  92. this.workingMemory = workingMemory;
  93. }
  94. public InternalWorkingMemory getWorkingMemory() {
  95. return this.workingMemory;
  96. }
  97. public void setActive(final boolean active) {
  98. if ( this.active == active ) {
  99. return;
  100. }
  101. this.active = active;
  102. if ( active ) {
  103. ((EventSupport) this.workingMemory).getRuleFlowEventSupport().fireBeforeRuleFlowGroupActivated( this,
  104. this.workingMemory );
  105. if ( this.list.isEmpty() ) {
  106. if ( this.autoDeactivate ) {
  107. // if the list of activations is empty and
  108. // auto-deactivate is on, deactivate this group
  109. WorkingMemoryAction action = new DeactivateCallback( this );
  110. this.workingMemory.queueWorkingMemoryAction( action );
  111. }
  112. } else {
  113. triggerActivations();
  114. }
  115. ((EventSupport) this.workingMemory).getRuleFlowEventSupport().fireAfterRuleFlowGroupActivated( this,
  116. this.workingMemory );
  117. } else {
  118. ((EventSupport) this.workingMemory).getRuleFlowEventSupport().fireBeforeRuleFlowGroupDeactivated( this,
  119. this.workingMemory );
  120. final Iterator it = this.list.iterator();
  121. for ( RuleFlowGroupNode node = (RuleFlowGroupNode) it.next(); node != null; node = (RuleFlowGroupNode) it.next() ) {
  122. final Activation activation = node.getActivation();
  123. activation.remove();
  124. if ( activation.getActivationGroupNode() != null ) {
  125. activation.getActivationGroupNode().getActivationGroup().removeActivation( activation );
  126. }
  127. }
  128. notifyRuleFlowGroupListeners();
  129. ((EventSupport) this.workingMemory).getRuleFlowEventSupport().fireAfterRuleFlowGroupDeactivated( this,
  130. this.workingMemory );
  131. }
  132. }
  133. public boolean isActive() {
  134. return this.active;
  135. }
  136. public boolean isAutoDeactivate() {
  137. return this.autoDeactivate;
  138. }
  139. public void setAutoDeactivate(final boolean autoDeactivate) {
  140. this.autoDeactivate = autoDeactivate;
  141. if ( autoDeactivate && this.active && this.list.isEmpty() ) {
  142. this.active = false;
  143. }
  144. }
  145. private void triggerActivations() {
  146. // iterate all activations adding them to their AgendaGroups
  147. final Iterator it = this.list.iterator();
  148. for ( RuleFlowGroupNode node = (RuleFlowGroupNode) it.next(); node != null; node = (RuleFlowGroupNode) it.next() ) {
  149. final Activation activation = node.getActivation();
  150. ((InternalAgendaGroup) activation.getAgendaGroup()).add( activation );
  151. }
  152. }
  153. public void clear() {
  154. this.list.clear();
  155. }
  156. public int size() {
  157. return this.list.size();
  158. }
  159. public void addActivation(final Activation activation) {
  160. final RuleFlowGroupNode node = new RuleFlowGroupNode( activation,
  161. this );
  162. activation.setRuleFlowGroupNode( node );
  163. this.list.add( node );
  164. if ( this.active ) {
  165. ((InternalAgendaGroup) activation.getAgendaGroup()).add( activation );
  166. }
  167. }
  168. public void removeActivation(final Activation activation) {
  169. final RuleFlowGroupNode node = activation.getRuleFlowGroupNode();
  170. this.list.remove( node );
  171. activation.setActivationGroupNode( null );
  172. if ( this.active && this.autoDeactivate ) {
  173. if ( this.list.isEmpty() ) {
  174. // deactivate callback
  175. WorkingMemoryAction action = new DeactivateCallback( this );
  176. this.workingMemory.queueWorkingMemoryAction( action );
  177. }
  178. }
  179. }
  180. public void addRuleFlowGroupListener(RuleFlowGroupListener listener) {
  181. if ( listeners == null ) {
  182. listeners = new CopyOnWriteArrayList<RuleFlowGroupListener>();
  183. }
  184. listeners.add( listener );
  185. }
  186. public void removeRuleFlowGroupListener(RuleFlowGroupListener listener) {
  187. if (listeners != null) {
  188. listeners.remove(listener);
  189. }
  190. }
  191. public void notifyRuleFlowGroupListeners() {
  192. if ( listeners != null ) {
  193. for ( java.util.Iterator<RuleFlowGroupListener> iterator = listeners.iterator(); iterator.hasNext(); ) {
  194. iterator.next().ruleFlowGroupDeactivated();
  195. }
  196. }
  197. }
  198. public boolean isEmpty() {
  199. return this.list.isEmpty();
  200. }
  201. public java.util.Iterator iterator() {
  202. return this.list.javaUtilIterator();
  203. }
  204. public String toString() {
  205. return "RuleFlowGroup '" + this.name + "'";
  206. }
  207. public boolean equal(final Object object) {
  208. if ( (object == null) || !(object instanceof RuleFlowGroupImpl) ) {
  209. return false;
  210. }
  211. if ( ((RuleFlowGroupImpl) object).name.equals( this.name ) ) {
  212. return true;
  213. }
  214. return false;
  215. }
  216. public int hashCode() {
  217. return this.name.hashCode();
  218. }
  219. public static class DeactivateCallback
  220. implements
  221. WorkingMemoryAction {
  222. private static final long serialVersionUID = 400L;
  223. private InternalRuleFlowGroup ruleFlowGroup;
  224. public DeactivateCallback() {
  225. }
  226. public DeactivateCallback(InternalRuleFlowGroup ruleFlowGroup) {
  227. this.ruleFlowGroup = ruleFlowGroup;
  228. }
  229. public DeactivateCallback(MarshallerReaderContext context) throws IOException {
  230. }
  231. public void write(MarshallerWriteContext context) throws IOException {
  232. }
  233. public void readExternal(ObjectInput in) throws IOException,
  234. ClassNotFoundException {
  235. ruleFlowGroup = (InternalRuleFlowGroup) in.readObject();
  236. }
  237. public void writeExternal(ObjectOutput out) throws IOException {
  238. out.writeObject( ruleFlowGroup );
  239. }
  240. public void execute(InternalWorkingMemory workingMemory) {
  241. // check whether ruleflow group is still empty first
  242. if ( this.ruleFlowGroup.isEmpty() ) {
  243. // deactivate ruleflow group
  244. this.ruleFlowGroup.setActive( false );
  245. }
  246. }
  247. }
  248. }