|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
TupleKey.java | 50% | 65.8% | 91.7% | 66.7% |
|
1 | package org.drools.reteoo; | |
2 | ||
3 | /* | |
4 | * $Id: TupleKey.java,v 1.35 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.Collections; | |
45 | import java.util.Set; | |
46 | ||
47 | import org.drools.FactHandle; | |
48 | import org.drools.rule.Declaration; | |
49 | import org.drools.spi.Tuple; | |
50 | ||
51 | /** | |
52 | * A composite key to match tuples. | |
53 | * | |
54 | * @see Tuple | |
55 | * | |
56 | * @author <a href="mailto:bob@eng.werken.com">bob mcwhirter </a> | |
57 | */ | |
58 | class TupleKey | |
59 | implements | |
60 | Serializable | |
61 | { | |
62 | public static final TupleKey EMPTY_KEY = new TupleKey( ); | |
63 | ||
64 | // ------------------------------------------------------------ | |
65 | // Instance members | |
66 | // ------------------------------------------------------------ | |
67 | ||
68 | /** Columns. */ | |
69 | private final FactHandleList handles; | |
70 | ||
71 | // ------------------------------------------------------------ | |
72 | // Constructors | |
73 | // ------------------------------------------------------------ | |
74 | ||
75 | 14 | private TupleKey() |
76 | { | |
77 | 14 | this.handles = FactHandleList.EMPTY_LIST; |
78 | } | |
79 | ||
80 | 24797 | public TupleKey(TupleKey left, |
81 | TupleKey right) | |
82 | { | |
83 | 24797 | this.handles = new FactHandleList( left.handles, |
84 | right.handles ); | |
85 | } | |
86 | ||
87 | 1917 | public TupleKey(Declaration declaration, |
88 | FactHandle handle) | |
89 | { | |
90 | 1917 | this.handles = new FactHandleList( declaration.getIndex( ), |
91 | handle ); | |
92 | } | |
93 | ||
94 | 3 | public String toString() |
95 | { | |
96 | 3 | return "[TupleKey: handles=" + this.handles + "]"; |
97 | } | |
98 | ||
99 | // ------------------------------------------------------------ | |
100 | // | |
101 | // ------------------------------------------------------------ | |
102 | ||
103 | /** | |
104 | * Retrieve a <code>FactHandle</code> by declaration. | |
105 | * | |
106 | * @param declaration The declaration. | |
107 | * | |
108 | * @return The fact handle. | |
109 | */ | |
110 | 83444 | public FactHandle get(Declaration declaration) |
111 | { | |
112 | 83444 | return this.handles.get( declaration.getIndex( ) ); |
113 | } | |
114 | ||
115 | /** | |
116 | * Determine if this key contains the specified root fact object. | |
117 | * | |
118 | * @param handle The fact-handle to test. | |
119 | * | |
120 | * @return <code>true</code> if this key contains the specified root | |
121 | * fact-handle, otherwise <code>false</code>. | |
122 | */ | |
123 | 6 | public boolean containsFactHandle(FactHandle handle) |
124 | { | |
125 | 6 | return this.handles.contains( handle ); |
126 | } | |
127 | ||
128 | /** | |
129 | * Determine if the specified key is a subset of this key. | |
130 | * | |
131 | * @param that The key to compare. | |
132 | * | |
133 | * @return <code>true</code> if the specified key is a subset of this key. | |
134 | */ | |
135 | 53392 | public boolean containsAll(TupleKey that) |
136 | { | |
137 | 53392 | return this.handles.containsAll( that.handles ); |
138 | } | |
139 | ||
140 | 5868 | public FactHandleImpl getMostRecentFact() |
141 | { | |
142 | 5868 | FactHandleImpl mostRecent = null; |
143 | 5868 | long currentRecency = Long.MIN_VALUE; |
144 | 5868 | FactHandleImpl eachHandle; |
145 | 5868 | long recency; |
146 | ||
147 | 5868 | for ( int i = this.handles.size() - 1; i >= 0; i-- ) |
148 | { | |
149 | 17334 | eachHandle = ( FactHandleImpl ) this.handles.get( i ); |
150 | 17334 | if ( eachHandle != null ) |
151 | { | |
152 | 17334 | recency = eachHandle.getRecency( ); |
153 | 17334 | if ( recency > currentRecency ) |
154 | { | |
155 | 6129 | currentRecency = recency; |
156 | 6129 | mostRecent = eachHandle; |
157 | } | |
158 | } | |
159 | } | |
160 | ||
161 | 5868 | return mostRecent; |
162 | } | |
163 | ||
164 | 0 | public FactHandleImpl getLeastRecentFact() |
165 | { | |
166 | 0 | FactHandleImpl leastRecent = null; |
167 | 0 | long currentRecency = Long.MAX_VALUE; |
168 | 0 | FactHandleImpl eachHandle; |
169 | 0 | long recency; |
170 | ||
171 | ||
172 | 0 | for ( int i = this.handles.size() - 1; i >= 0; i-- ) |
173 | { | |
174 | 0 | eachHandle = ( FactHandleImpl ) this.handles.get( i ); |
175 | 0 | if ( eachHandle != null ) |
176 | { | |
177 | 0 | recency = eachHandle.getRecency( ); |
178 | 0 | if ( recency < currentRecency ) |
179 | { | |
180 | 0 | currentRecency = recency; |
181 | 0 | leastRecent = eachHandle; |
182 | } | |
183 | } | |
184 | } | |
185 | ||
186 | 0 | return leastRecent; |
187 | } | |
188 | ||
189 | // TODO: Remove this at some stage when ReteTuple no longer needs it | |
190 | 3 | public Set getDeclarations() |
191 | { | |
192 | 3 | return Collections.EMPTY_SET; |
193 | // return this.handles.keySet(); | |
194 | } | |
195 | ||
196 | // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - | |
197 | ||
198 | /** | |
199 | * @see Object | |
200 | */ | |
201 | 11 | public boolean equals( Object object ) |
202 | { | |
203 | 11 | if ( this == object ) |
204 | { | |
205 | 1 | return true; |
206 | } | |
207 | ||
208 | 10 | if ( object == null || getClass( ) != object.getClass( ) ) |
209 | { | |
210 | 0 | return false; |
211 | } | |
212 | ||
213 | 10 | return this.handles.equals( ( ( TupleKey ) object ).handles ); |
214 | } | |
215 | ||
216 | /** | |
217 | * @see Object | |
218 | */ | |
219 | 28504 | public int hashCode() |
220 | { | |
221 | 28504 | return this.handles.hashCode( ); |
222 | } | |
223 | } |
|