View Javadoc
1 package org.drools; 2 3 /* 4 $Id: TransactionalWorkingMemory.java,v 1.7 2003/01/14 03:09:25 bob Exp $ 5 6 Copyright 2002 (C) The Werken Company. All Rights Reserved. 7 8 Redistribution and use of this software and associated documentation 9 ("Software"), with or without modification, are permitted provided 10 that the following conditions are met: 11 12 1. Redistributions of source code must retain copyright 13 statements and notices. Redistributions must also contain a 14 copy of this document. 15 16 2. Redistributions in binary form must reproduce the 17 above copyright notice, this list of conditions and the 18 following disclaimer in the documentation and/or other 19 materials provided with the distribution. 20 21 3. The name "drools" must not be used to endorse or promote 22 products derived from this Software without prior written 23 permission of The Werken Company. For written permission, 24 please contact bob@werken.com. 25 26 4. Products derived from this Software may not be called "drools" 27 nor may "drools" appear in their names without prior written 28 permission of The Werken Company. "drools" is a registered 29 trademark of The Werken Company. 30 31 5. Due credit should be given to The Werken Company. 32 (http://drools.werken.com/). 33 34 THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS 35 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 36 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 37 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 38 THE WERKEN COMPANY OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 39 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 40 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 41 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 42 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 43 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 44 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 45 OF THE POSSIBILITY OF SUCH DAMAGE. 46 47 */ 48 49 import java.util.Set; 50 import java.util.HashSet; 51 import java.util.Iterator; 52 53 /*** A transactional knowledge session for a <code>RuleBase</code>. 54 * 55 * <p> 56 * A <code>WorkingMemory</code> which caches all assertion, retractions 57 * and modifications, and only performs the fact propagation and 58 * rule action invokation upon {@link #commit}. 59 * </p> 60 * 61 * @see WorkingMemory 62 * @see RuleBase 63 * 64 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a> 65 * @author <a href="mailto:tomv@aic.net.au">Tom Vasak</a> 66 */ 67 public class TransactionalWorkingMemory extends WorkingMemory 68 { 69 // ------------------------------------------------------------ 70 // Instance members 71 // ------------------------------------------------------------ 72 73 /*** Object to assert upon commit. */ 74 private Set assertedObjects; 75 76 /*** Objects retracted during commit. */ 77 private Set retractedObjects; 78 79 /*** Performing commit. */ 80 private boolean committing; 81 82 // ------------------------------------------------------------ 83 // Constructors 84 // ------------------------------------------------------------ 85 86 /*** Construct. 87 * 88 * @param ruleBase The <code>RuleBase</code> for this <code>WorkingMemory</code>. 89 */ 90 public TransactionalWorkingMemory(RuleBase ruleBase) 91 { 92 super( ruleBase ); 93 94 this.assertedObjects = new HashSet(); 95 this.retractedObjects = new HashSet(); 96 this.committing = false; 97 } 98 99 // ------------------------------------------------------------ 100 // Instance methods 101 // ------------------------------------------------------------ 102 103 /*** Abort this <code>TransactionalWorkingMemory</code>, 104 * by tossing out all asserted objects, and reseting 105 * this <code>TransactionalWorkingMemory</code> to a 106 * clean state. 107 */ 108 public synchronized void abort() 109 { 110 this.assertedObjects.clear(); 111 this.retractedObjects.clear(); 112 } 113 114 /*** Commit all asserted objects into the logic engine, 115 * and reset this <code>TransactionalWorkingMemory</code> 116 * to a clean state. 117 * 118 * @throws DroolsException If an 119 */ 120 public synchronized void commit() throws DroolsException 121 { 122 try 123 { 124 this.committing = true; 125 126 Iterator objIter = this.assertedObjects.iterator(); 127 Object eachObj = null; 128 129 while ( objIter.hasNext() ) 130 { 131 eachObj = objIter.next(); 132 133 super.assertObject( eachObj ); 134 135 objIter.remove(); 136 } 137 138 objIter = this.retractedObjects.iterator(); 139 eachObj = null; 140 141 while ( objIter.hasNext() ) 142 { 143 eachObj = objIter.next(); 144 145 super.retractObject( eachObj ); 146 147 objIter.remove(); 148 } 149 } 150 finally 151 { 152 this.assertedObjects.clear(); 153 this.retractedObjects.clear(); 154 this.committing = false; 155 } 156 } 157 158 /*** Assert a new fact object into this working memory. 159 * 160 * @param object The object to assert. 161 * 162 * @throws AssertionException if an error occurs during assertion. 163 */ 164 public synchronized void assertObject(Object object) throws AssertionException 165 { 166 if ( this.committing ) 167 { 168 super.assertObject( object ); 169 } 170 else 171 { 172 if ( this.retractedObjects.contains( object ) ) 173 { 174 this.retractedObjects.remove( object ); 175 } 176 this.assertedObjects.add( object ); 177 } 178 } 179 180 /*** Modify a fact object in this working memory. 181 * 182 * With the exception of time-based nodes, modification of 183 * a fact object is semantically equivelent to retracting and 184 * re-asserting it. 185 * 186 * @param object The object to modify. 187 * 188 * @throws FactException if an error occurs during modification. 189 */ 190 public synchronized void modifyObject(Object object) throws FactException 191 { 192 if ( this.committing ) 193 { 194 super.modifyObject( object ); 195 } 196 else 197 { 198 if ( this.retractedObjects.contains( object ) ) 199 { 200 // Should throw a FactException since you are modifying 201 // after retracting. Let it go for now. 202 this.retractedObjects.remove( object ); 203 } 204 this.assertedObjects.add( object ); 205 } 206 } 207 208 /*** Retract a fact object from this working memory. 209 * 210 * @param object The object to retract. 211 * 212 * @throws RetractionException if an error occurs during retraction. 213 */ 214 public synchronized void retractObject(Object object) throws RetractionException 215 { 216 if ( this.committing ) 217 { 218 super.retractObject( object ); 219 this.retractedObjects.add( object ); 220 } 221 else 222 { 223 if ( this.retractedObjects.contains( object ) ) 224 { 225 // Maybe should throw a RetractionException since you are 226 // retracting again after a previous retraction. 227 } 228 else if ( this.assertedObjects.contains( object ) ) 229 { 230 this.assertedObjects.remove( object ); 231 232 // Object may still be currently asserted as a fact so 233 // still need to retract. 234 this.retractedObjects.add( object ); 235 } 236 else 237 { 238 this.retractedObjects.add( object ); 239 } 240 } 241 } 242 }

This page was automatically generated by Maven