View Javadoc
1 package org.drools.reteoo.impl; 2 3 /* 4 $Id: ObjectTypeNodeImpl.java,v 1.4 2002/08/25 21:15:06 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.ObjectTypeNode; 54 import org.drools.spi.ObjectType; 55 56 import java.util.Set; 57 import java.util.HashSet; 58 import java.util.Collections; 59 import java.util.Iterator; 60 61 /*** Filters <code>Objects</code> coming from the <code>Rete</code> 62 * using a <code>ObjectType</code> semantic module. 63 * 64 * <p> 65 * It receives <code>Objects</code> from the <code>Rete</code>, 66 * uses a <code>ObjectType</code> instance to determine membership, 67 * and propagates matching <code>Objects</code> further to all 68 * matching <code>ParameterNode</code>s. 69 * </p> 70 * 71 * @see ObjectTypeNode 72 * @see ObjectType 73 * @see ParameterNodeImpl 74 * @see ReteImpl 75 * 76 * @author <a href="mailto:bob@eng.werken.com">bob@eng.werken.com</a> 77 */ 78 public class ObjectTypeNodeImpl implements ObjectTypeNode 79 { 80 // ------------------------------------------------------------ 81 // Instance members 82 // ------------------------------------------------------------ 83 84 /*** The <code>ObjectType</code> semantic module. */ 85 private ObjectType objectType; 86 87 /*** The <code>ParameterNode</code> children. */ 88 private Set parameterNodes; 89 90 // ------------------------------------------------------------ 91 // Constructors 92 // ------------------------------------------------------------ 93 94 /*** Construct given a semantic <code>ObjectType</code>. 95 * 96 * @param objectType The semantic object-type differentiator. 97 */ 98 public ObjectTypeNodeImpl(ObjectType objectType) 99 { 100 this.objectType = objectType; 101 102 this.parameterNodes = Collections.EMPTY_SET; 103 } 104 105 // ------------------------------------------------------------ 106 // Instance methods 107 // ------------------------------------------------------------ 108 109 /*** Retrieve the semantic <code>ObjectType</code> differentiator. 110 * 111 * @return The semantic <code>ObjectType</code> differentiator. 112 */ 113 public ObjectType getObjectType() 114 { 115 return this.objectType; 116 } 117 118 /*** Add a <code>ParameterNode</code> child to this node. 119 * 120 * @param node The <code>ParameterNode</code> child to add. 121 */ 122 void addParameterNode(ParameterNodeImpl node) 123 { 124 if ( this.parameterNodes == Collections.EMPTY_SET ) 125 { 126 this.parameterNodes = new HashSet(); 127 } 128 129 this.parameterNodes.add( node ); 130 } 131 132 /*** Retrieve the <code>Set</code> of <code>ParameterNodes/code> 133 * children of this node. 134 * 135 * @return The <code>Set</code> of <code>ParameterNode</code> 136 * children. 137 */ 138 Set getParameterNodes() 139 { 140 return this.parameterNodes; 141 } 142 143 /*** Retreive an <code>Iterator</code> over <code>ParameterNode</code> 144 * children of this node. 145 * 146 * @return An <code>Iterator</code> over <code>ParameterNode</code> 147 * children of this node. 148 */ 149 Iterator getParameterNodeIterator() 150 { 151 return this.parameterNodes.iterator(); 152 } 153 154 /*** Assert a new fact object into this <code>RuleBase</code> 155 * and the specified <code>WorkingMemory</code>. 156 * 157 * @param object The object to assert. 158 * @param workingMemory The working memory session. 159 * 160 * @throws AssertionException if an error occurs during assertion. 161 */ 162 void assertObject(Object object, 163 WorkingMemory workingMemory) throws AssertionException 164 { 165 ObjectType objectType = getObjectType(); 166 167 if ( ! objectType.matches( object ) ) 168 { 169 return; 170 } 171 172 Iterator nodeIter = getParameterNodeIterator(); 173 ParameterNodeImpl eachNode = null; 174 175 while ( nodeIter.hasNext() ) 176 { 177 eachNode = (ParameterNodeImpl) nodeIter.next(); 178 179 eachNode.assertObject( object, 180 workingMemory ); 181 } 182 } 183 184 /*** Retract a fact object from this <code>RuleBase</code> 185 * and the specified <code>WorkingMemory</code>. 186 * 187 * @param object The object to retract. 188 * @param workingMemory The working memory session. 189 * 190 * @throws RetractionException if an error occurs during assertion. 191 */ 192 void retractObject(Object object, 193 WorkingMemory workingMemory) throws RetractionException 194 { 195 ObjectType objectType = getObjectType(); 196 197 if ( ! objectType.matches( object ) ) 198 { 199 return; 200 } 201 202 Iterator nodeIter = getParameterNodeIterator(); 203 ParameterNodeImpl eachNode = null; 204 205 while ( nodeIter.hasNext() ) 206 { 207 eachNode = (ParameterNodeImpl) nodeIter.next(); 208 209 eachNode.retractObject( object, 210 workingMemory ); 211 } 212 } 213 214 /*** Modify a fact object in this <code>RuleBase</code> 215 * and the specified <code>WorkingMemory</code>. 216 * 217 * With the exception of time-based nodes, modification of 218 * a fact object is semantically equivelent to retracting and 219 * re-asserting it. 220 * 221 * @param object The object to modify. 222 * @param workingMemory The working memory session. 223 * 224 * @throws FactException if an error occurs during assertion. 225 */ 226 void modifyObject(Object object, 227 WorkingMemory workingMemory) throws FactException 228 { 229 ObjectType objectType = getObjectType(); 230 231 if ( ! objectType.matches( object ) ) 232 { 233 return; 234 } 235 236 Iterator nodeIter = getParameterNodeIterator(); 237 ParameterNodeImpl eachNode = null; 238 239 while ( nodeIter.hasNext() ) 240 { 241 eachNode = (ParameterNodeImpl) nodeIter.next(); 242 243 eachNode.modifyObject( object, 244 workingMemory ); 245 } 246 } 247 }

This page was automatically generated by Maven