|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Rete.java | 100% | 100% | 100% | 100% |
|
1 | package org.drools.reteoo; | |
2 | ||
3 | /* | |
4 | * $Id: Rete.java,v 1.24 2005/02/02 00:23:22 mproctor Exp $ | |
5 | * | |
6 | * Copyright 2001-2003 (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 that the | |
10 | * following conditions are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain copyright statements and | |
13 | * notices. Redistributions must also contain a copy of this document. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright notice, | |
16 | * this list of conditions and the following disclaimer in the documentation | |
17 | * and/or other materials provided with the distribution. | |
18 | * | |
19 | * 3. The name "drools" must not be used to endorse or promote products derived | |
20 | * from this Software without prior written permission of The Werken Company. | |
21 | * For written permission, please contact bob@werken.com. | |
22 | * | |
23 | * 4. Products derived from this Software may not be called "drools" nor may | |
24 | * "drools" appear in their names without prior written permission of The Werken | |
25 | * Company. "drools" is a trademark of The Werken Company. | |
26 | * | |
27 | * 5. Due credit should be given to The Werken Company. (http://werken.com/) | |
28 | * | |
29 | * THIS SOFTWARE IS PROVIDED BY THE WERKEN COMPANY AND CONTRIBUTORS ``AS IS'' | |
30 | * AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
31 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
32 | * ARE DISCLAIMED. IN NO EVENT SHALL THE WERKEN COMPANY OR ITS CONTRIBUTORS BE | |
33 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | |
34 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | |
35 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | |
36 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | |
37 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
38 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE | |
39 | * POSSIBILITY OF SUCH DAMAGE. | |
40 | * | |
41 | */ | |
42 | ||
43 | import java.io.Serializable; | |
44 | import java.util.Collection; | |
45 | import java.util.HashMap; | |
46 | import java.util.Iterator; | |
47 | import java.util.Map; | |
48 | ||
49 | import org.drools.FactException; | |
50 | import org.drools.FactHandle; | |
51 | import org.drools.spi.ObjectType; | |
52 | ||
53 | /** | |
54 | * The Rete-OO network. | |
55 | * | |
56 | * This node accepts an <code>Object</code>, and simply propagates it to all | |
57 | * <code>ObjectTypeNode</code> s for type testings. | |
58 | * | |
59 | * @see ObjectTypeNode | |
60 | * | |
61 | * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a> | |
62 | */ | |
63 | class Rete | |
64 | implements | |
65 | Serializable | |
66 | { | |
67 | // ------------------------------------------------------------ | |
68 | // Instance members | |
69 | // ------------------------------------------------------------ | |
70 | ||
71 | /** The set of <code>ObjectTypeNodes</code>. */ | |
72 | private final Map objectTypeNodes = new HashMap( ); | |
73 | ||
74 | // ------------------------------------------------------------ | |
75 | // Constructors | |
76 | // ------------------------------------------------------------ | |
77 | ||
78 | /** | |
79 | * Construct. | |
80 | */ | |
81 | 163 | public Rete() |
82 | { | |
83 | } | |
84 | ||
85 | // ------------------------------------------------------------ | |
86 | // Instance methods | |
87 | // ------------------------------------------------------------ | |
88 | ||
89 | /** | |
90 | * Assert a new fact object into this <code>RuleBase</code> and the | |
91 | * specified <code>WorkingMemory</code>. | |
92 | * | |
93 | * @param handle | |
94 | * The fact handle. | |
95 | * @param object | |
96 | * The object to assert. | |
97 | * @param workingMemory | |
98 | * The working memory session. | |
99 | * | |
100 | * @throws FactException | |
101 | * if an error occurs during assertion. | |
102 | */ | |
103 | 1190 | void assertObject(FactHandle handle, |
104 | Object object, | |
105 | WorkingMemoryImpl workingMemory) throws FactException | |
106 | { | |
107 | 1190 | Iterator nodeIter = getObjectTypeNodeIterator( ); |
108 | ||
109 | 1190 | while ( nodeIter.hasNext( ) ) |
110 | { | |
111 | 4324 | ( (ObjectTypeNode) nodeIter.next( ) ).assertObject( handle, |
112 | object, | |
113 | workingMemory ); | |
114 | } | |
115 | } | |
116 | ||
117 | /** | |
118 | * Retract a fact object from this <code>RuleBase</code> and the specified | |
119 | * <code>WorkingMemory</code>. | |
120 | * | |
121 | * @param handle | |
122 | * The handle of the fact to retract. | |
123 | * @param workingMemory | |
124 | * The working memory session. | |
125 | * | |
126 | * @throws FactException | |
127 | * if an error occurs during retraction. | |
128 | */ | |
129 | 372 | void retractObject(FactHandle handle, |
130 | WorkingMemoryImpl workingMemory) throws FactException | |
131 | { | |
132 | 372 | Iterator nodeIter = getObjectTypeNodeIterator( ); |
133 | ||
134 | 372 | while ( nodeIter.hasNext( ) ) |
135 | { | |
136 | 1164 | ( (ObjectTypeNode) nodeIter.next( ) ).retractObject( handle, |
137 | workingMemory ); | |
138 | } | |
139 | } | |
140 | ||
141 | /** | |
142 | * Add an <code>ObjectTypeNode</code> child to this <code>Rete</code>. | |
143 | * | |
144 | * @param node | |
145 | * The node to add. | |
146 | */ | |
147 | 109 | void addObjectTypeNode(ObjectTypeNode node) |
148 | { | |
149 | 109 | this.objectTypeNodes.put( node.getObjectType( ), |
150 | node ); | |
151 | } | |
152 | ||
153 | /** | |
154 | * Retrieve all <code>ObjectTypeNode</code> children of this node. | |
155 | * | |
156 | * @return The <code>Set</code> of <code>ObjectTypeNodes</code>. | |
157 | */ | |
158 | 1 | Collection getObjectTypeNodes() |
159 | { | |
160 | 1 | return this.objectTypeNodes.values( ); |
161 | } | |
162 | ||
163 | /** | |
164 | * Retrieve an <code>Iterator</code> over the <code>ObjectTypeNode</code> | |
165 | * children of this node. | |
166 | * | |
167 | * @return An <code>Iterator</code> over <code>ObjectTypeNodes</code>. | |
168 | */ | |
169 | 1562 | Iterator getObjectTypeNodeIterator() |
170 | { | |
171 | 1562 | return this.objectTypeNodes.values( ).iterator( ); |
172 | } | |
173 | ||
174 | /** | |
175 | * Retrieve an <code>ObjectTypeNode</code> keyed by | |
176 | * <code>ObjectType</code>. | |
177 | * | |
178 | * @param objectType | |
179 | * The <code>ObjectType</code> key. | |
180 | * | |
181 | * @return The matching <code>ObjectTypeNode</code> if one has already | |
182 | * been created, else <code>null</code>. | |
183 | */ | |
184 | 191 | ObjectTypeNode getObjectTypeNode(ObjectType objectType) |
185 | { | |
186 | 191 | return (ObjectTypeNode) this.objectTypeNodes.get( objectType ); |
187 | } | |
188 | ||
189 | /** | |
190 | * Retrieve an <code>ObjectTypeNode</code> keyed by | |
191 | * <code>ObjectType</code>, creating one, if necessary. | |
192 | * | |
193 | * @param objectType | |
194 | * The <code>ObjectType</code> key. | |
195 | * | |
196 | * @return The matching <code>ObjectTypeNode</codeb>. | |
197 | */ | |
198 | 191 | ObjectTypeNode getOrCreateObjectTypeNode(ObjectType objectType) |
199 | { | |
200 | 191 | ObjectTypeNode node = getObjectTypeNode( objectType ); |
201 | ||
202 | 191 | if ( node == null ) |
203 | { | |
204 | 101 | node = new ObjectTypeNode( objectType ); |
205 | ||
206 | 101 | addObjectTypeNode( node ); |
207 | } | |
208 | ||
209 | 191 | return node; |
210 | } | |
211 | } |
|