1 package org.drools.jsr94.rules;
2
3 /*
4 $Id: JSR94TransactionalWorkingMemory.java,v 1.5 2003/06/19 09:28:35 tdiesler 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.apache.commons.collections.SequencedHashMap;
50 import org.drools.AssertionException;
51 import org.drools.RetractionException;
52 import org.drools.RuleBase;
53 import org.drools.TransactionalWorkingMemory;
54
55 import javax.rules.Handle;
56 import javax.rules.InvalidRuleSessionException;
57 import java.math.BigInteger;
58 import java.util.Collection;
59 import java.util.Iterator;
60 import java.util.Map;
61
62 /***
63 * Provide access to the list of objects currently asserted to the working memory.
64 *
65 * @author <a href="mailto:thomas.diesler@softcon-itec.de">thomas diesler</a>
66 */
67 public class JSR94TransactionalWorkingMemory extends TransactionalWorkingMemory
68 {
69
70 // list of Handles
71 private Map objectMap = new SequencedHashMap();
72
73 // the next handle id
74 private static BigInteger nextHandleid = new BigInteger( "1" );
75
76 /***
77 * Construct a new transactional working memory for a ruleBase.
78 *
79 * @param ruleBase The rule base with which this memory is associated.
80 */
81 JSR94TransactionalWorkingMemory( RuleBase ruleBase )
82 {
83 super( ruleBase );
84 }
85
86 /***
87 * Gets the next <code>Handle</code> for this <code>RuleRuntime</code>.
88 */
89 Handle getNextHandle()
90 {
91 Handle handle = new HandleImpl( nextHandleid );
92 nextHandleid = nextHandleid.add( new BigInteger( "1" ) );
93 return handle;
94 }
95
96 /***
97 * Get a collection of handles currently asserted to the working memory.
98 */
99 Collection getObjectHandles()
100 {
101 return objectMap.keySet();
102 }
103
104 /***
105 * Get a collection of handles currently asserted to the working memory.
106 */
107 Collection getObjects()
108 {
109 return objectMap.values();
110 }
111
112
113 Object getObject( Handle handle )
114 {
115 return objectMap.get( handle );
116 }
117
118 /***
119 * Assert a new object with the given handle into this working memory.
120 *
121 * @throws AssertionException if an error occurs during assertion.
122 */
123 void assertObjectForHandle( Handle handle, Object object ) throws AssertionException
124 {
125 objectMap.put( handle, object );
126 super.assertObject( object );
127 }
128
129 /***
130 * Retract an object with the given handle from this working memory.
131 *
132 * @param handle The handle to retract.
133 *
134 * @throws RetractionException if an error occurs during retraction.
135 */
136 void removeObjectForHandle( Handle handle ) throws RetractionException, InvalidRuleSessionException
137 {
138 Object object = objectMap.get( handle );
139 if ( object == null ) throw new InvalidRuleSessionException( "invalid handle: " + handle );
140 super.retractObject( object );
141 objectMap.remove( handle );
142 }
143
144 /***
145 * Assert a new fact object into this working memory.
146 * <p>
147 * When a fact is asserted during <code>StatefulRuleSession.executeRules</code> we commit immediately.
148 *
149 * @param object The object to assert.
150 *
151 * @throws AssertionException if an error occurs during assertion.
152 */
153 public void assertObject( Object object ) throws AssertionException
154 {
155 Handle handle = getNextHandle();
156 objectMap.put( handle, object );
157 super.assertObject( object );
158 }
159
160 /*** Retract a fact object from this working memory.
161 *
162 * @param object The object to retract.
163 *
164 * @throws RetractionException if an error occurs during retraction.
165 */
166 public void retractObject( Object object ) throws RetractionException
167 {
168 super.retractObject( object );
169
170 // Note: this is really bad, for each removal we have to scan the entire map.
171 // Any other ways?
172 Iterator itKeys = objectMap.keySet().iterator();
173 while ( itKeys.hasNext() )
174 {
175 Handle handle = (Handle) itKeys.next();
176 if ( objectMap.get( handle ) == object )
177 {
178 objectMap.remove( handle );
179 break;
180 }
181 }
182 }
183 }
This page was automatically generated by Maven