View Javadoc
1 package org.drools.reteoo.impl; 2 3 /* 4 $Id: ReteImpl.java,v 1.2 2002/08/13 04:12:26 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 org.drools.WorkingMemory; 50 import org.drools.FactException; 51 import org.drools.AssertionException; 52 import org.drools.RetractionException; 53 import org.drools.reteoo.Rete; 54 import org.drools.reteoo.ObjectTypeNode; 55 import org.drools.spi.ObjectType; 56 57 import java.util.Map; 58 import java.util.HashMap; 59 import java.util.Collection; 60 import java.util.Collections; 61 import java.util.Iterator; 62 63 /*** The Rete-OO network. 64 * 65 * This node accepts an <code>Object</code>, and simply 66 * propagates it to all <code>ObjectTypeNode</code>s for 67 * type testings. 68 * 69 * @see ObjectTypeNodeImpl 70 * 71 * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a> 72 */ 73 public class ReteImpl implements Rete 74 { 75 // ------------------------------------------------------------ 76 // Instance members 77 // ------------------------------------------------------------ 78 79 /*** The set of <code>ObjectTypeNodes</code>. */ 80 private Map objectTypeNodes; 81 82 // ------------------------------------------------------------ 83 // Constructors 84 // ------------------------------------------------------------ 85 86 /*** Construct. 87 */ 88 public ReteImpl() 89 { 90 this.objectTypeNodes = Collections.EMPTY_MAP; 91 } 92 93 // ------------------------------------------------------------ 94 // Instance methods 95 // ------------------------------------------------------------ 96 97 /*** Assert a new fact object into this <code>RuleBase</code> 98 * and the specified <code>WorkingMemory</code>. 99 * 100 * @param object The object to assert. 101 * @param workingMemory The working memory session. 102 * 103 * @throws AssertionException if an error occurs during assertion. 104 */ 105 public void assertObject(Object object, 106 WorkingMemory workingMemory) throws AssertionException 107 { 108 Iterator nodeIter = getObjectTypeNodeIterator(); 109 ObjectTypeNodeImpl eachNode = null; 110 111 while ( nodeIter.hasNext() ) 112 { 113 eachNode = (ObjectTypeNodeImpl) nodeIter.next(); 114 115 eachNode.assertObject( object, 116 workingMemory ); 117 } 118 } 119 120 /*** Retract a fact object from this <code>RuleBase</code> 121 * and the specified <code>WorkingMemory</code>. 122 * 123 * @param object The object to retract. 124 * @param workingMemory The working memory session. 125 * 126 * @throws RetractionException if an error occurs during retraction. 127 */ 128 public void retractObject(Object object, 129 WorkingMemory workingMemory) throws RetractionException 130 { 131 Iterator nodeIter = getObjectTypeNodeIterator(); 132 ObjectTypeNodeImpl eachNode = null; 133 134 while ( nodeIter.hasNext() ) 135 { 136 eachNode = (ObjectTypeNodeImpl) nodeIter.next(); 137 138 eachNode.retractObject( object, 139 workingMemory ); 140 } 141 } 142 143 /*** Modify a fact object in this <code>RuleBase</code> 144 * and the specified <code>WorkingMemory</code>. 145 * 146 * With the exception of time-based nodes, modification of 147 * a fact object is semantically equivelent to retracting and 148 * re-asserting it. 149 * 150 * @param object The object to modify. 151 * @param workingMemory The working memory session. 152 * 153 * @throws FactException if an error occurs during modification. 154 */ 155 public void modifyObject(Object object, 156 WorkingMemory workingMemory) throws FactException 157 { 158 Iterator nodeIter = getObjectTypeNodeIterator(); 159 ObjectTypeNodeImpl eachNode = null; 160 161 while ( nodeIter.hasNext() ) 162 { 163 eachNode = (ObjectTypeNodeImpl) nodeIter.next(); 164 165 eachNode.modifyObject( object, 166 workingMemory ); 167 } 168 } 169 170 /*** Add an <code>ObjectTypeNode</code> child to 171 * this <code>Rete</code>. 172 * 173 * @param node The node to add. 174 */ 175 void addObjectTypeNode(ObjectTypeNode node) 176 { 177 if ( this.objectTypeNodes == Collections.EMPTY_MAP ) 178 { 179 this.objectTypeNodes = new HashMap(); 180 } 181 182 this.objectTypeNodes.put( node.getObjectType(), 183 node ); 184 } 185 186 /*** Retrieve all <code>ObjectTypeNode</code> children 187 * of this node. 188 * 189 * @return The <code>Set</code> of <code>ObjectTypeNodes</code>. 190 */ 191 public Collection getObjectTypeNodes() 192 { 193 return this.objectTypeNodes.values(); 194 } 195 196 /*** Retrieve an <code>Iterator</code> over the 197 * <code>ObjectTypeNode</code> children of this 198 * node. 199 * 200 * @return An <code>Iterator</code> over <code>ObjectTypeNodes</code>. 201 */ 202 Iterator getObjectTypeNodeIterator() 203 { 204 return this.objectTypeNodes.values().iterator(); 205 } 206 207 /*** Retrieve an <code>ObjectTypeNode</code> keyed by <code>ObjectType</code>. 208 * 209 * @param objectType The <code>ObjectType</code> key. 210 * 211 * @return The matching <code>ObjectTypeNode</code> if one has already 212 * been created, else <code>null</code>. 213 */ 214 public ObjectTypeNode getObjectTypeNode(ObjectType objectType) 215 { 216 return (ObjectTypeNode) this.objectTypeNodes.get( objectType ); 217 } 218 219 /*** Retrieve an <code>ObjectTypeNode</code> keyed by <code>ObjectType</code>, 220 * creating one, if necessary. 221 * 222 * @param objectType The <code>ObjectType</code> key. 223 * 224 * @return The matching <code>ObjectTypeNode</code>. 225 */ 226 public ObjectTypeNodeImpl getOrCreateObjectTypeNode(ObjectType objectType) 227 { 228 ObjectTypeNodeImpl node = (ObjectTypeNodeImpl) getObjectTypeNode( objectType ); 229 230 if ( node == null ) 231 { 232 node = new ObjectTypeNodeImpl( objectType ); 233 234 addObjectTypeNode( node ); 235 } 236 237 return node; 238 } 239 }

This page was automatically generated by Maven