/
FromNode.java
372 lines (308 loc) · 12.8 KB
/
FromNode.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.drools.core.reteoo;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import org.drools.base.base.ObjectType;
import org.drools.base.common.NetworkNode;
import org.drools.base.reteoo.NodeTypeEnums;
import org.drools.base.rule.From;
import org.drools.base.rule.Pattern;
import org.drools.base.rule.accessor.DataProvider;
import org.drools.base.rule.constraint.AlphaNodeFieldConstraint;
import org.drools.core.RuleBaseConfiguration;
import org.drools.core.common.BetaConstraints;
import org.drools.core.common.EmptyBetaConstraints;
import org.drools.core.common.InternalFactHandle;
import org.drools.core.common.Memory;
import org.drools.core.common.MemoryFactory;
import org.drools.core.common.PropagationContext;
import org.drools.core.common.ReteEvaluator;
import org.drools.core.common.UpdateContext;
import org.drools.core.reteoo.builder.BuildContext;
import org.drools.core.util.AbstractLinkedListNode;
import org.drools.core.util.index.TupleList;
import org.drools.util.bitmask.AllSetBitMask;
import org.drools.util.bitmask.BitMask;
import static org.drools.base.reteoo.PropertySpecificUtil.calculateNegativeMask;
import static org.drools.base.reteoo.PropertySpecificUtil.calculatePositiveMask;
import static org.drools.base.reteoo.PropertySpecificUtil.getAccessibleProperties;
import static org.drools.base.reteoo.PropertySpecificUtil.isPropertyReactive;
public class FromNode<T extends FromNode.FromMemory> extends LeftTupleSource
implements
LeftTupleSinkNode,
MemoryFactory<T> {
private static final long serialVersionUID = 510l;
protected DataProvider dataProvider;
protected AlphaNodeFieldConstraint[] alphaConstraints;
protected BetaConstraints betaConstraints;
protected LeftTupleSinkNode previousTupleSinkNode;
protected LeftTupleSinkNode nextTupleSinkNode;
protected From from;
protected boolean tupleMemoryEnabled;
protected transient ObjectTypeConf objectTypeConf;
public FromNode() {
}
public FromNode(final int id,
final DataProvider dataProvider,
final LeftTupleSource tupleSource,
final AlphaNodeFieldConstraint[] constraints,
final BetaConstraints binder,
final boolean tupleMemoryEnabled,
final BuildContext context,
final From from) {
super(id, context);
this.dataProvider = dataProvider;
setLeftTupleSource(tupleSource);
this.setObjectCount(leftInput.getObjectCount() + 1); // 'from' node increases the object count
this.alphaConstraints = constraints;
this.betaConstraints = (binder == null) ? EmptyBetaConstraints.getInstance() : binder;
this.betaConstraints.init(context, getType());
this.tupleMemoryEnabled = tupleMemoryEnabled;
this.from = from;
initMasks(context, tupleSource);
hashcode = calculateHashCode();
}
private int calculateHashCode() {
int hash = ( 23 * leftInput.hashCode() ) + ( 29 * dataProvider.hashCode() );
if (from.getResultPattern() != null) {
hash += 31 * from.getResultPattern().hashCode();
}
if (alphaConstraints != null) {
hash += 37 * Arrays.hashCode( alphaConstraints );
}
if (betaConstraints != null) {
hash += 41 * betaConstraints.hashCode();
}
return hash;
}
@Override
public boolean equals( Object object ) {
if (this == object) {
return true;
}
if (((NetworkNode)object).getType() != NodeTypeEnums.FromNode || this.hashCode() != object.hashCode()) {
return false;
}
FromNode other = (FromNode) object;
return this.leftInput.getId() == other.leftInput.getId() &&
dataProvider.equals( other.dataProvider ) &&
Objects.equals(from.getResultPattern(), other.from.getResultPattern() ) &&
Arrays.equals( alphaConstraints, other.alphaConstraints ) &&
betaConstraints.equals( other.betaConstraints );
}
public DataProvider getDataProvider() {
return dataProvider;
}
public AlphaNodeFieldConstraint[] getAlphaConstraints() {
return alphaConstraints;
}
public BetaConstraints getBetaConstraints() {
return betaConstraints;
}
@Override
protected void initDeclaredMask(BuildContext context,
LeftTupleSource leftInput) {
super.initDeclaredMask(context, leftInput);
if ( leftDeclaredMask.isAllSet() ) {
return;
}
if ( context == null || context.getLastBuiltPatterns() == null ) {
// only happens during unit tests
leftDeclaredMask = AllSetBitMask.get();
return;
}
Pattern pattern = context.getLastBuiltPatterns()[1];
if ( pattern == null ) {
return;
}
ObjectType objectType = pattern.getObjectType();
// if pattern is null (e.g. for eval or query nodes) we cannot calculate the mask, so we set it all
if ( isPropertyReactive( context.getRuleBase(), objectType ) ) {
Collection<String> leftListenedProperties = pattern.getListenedProperties();
List<String> accessibleProperties = getAccessibleProperties( context.getRuleBase(), objectType );
leftDeclaredMask = leftDeclaredMask.setAll( calculatePositiveMask( objectType, leftListenedProperties, accessibleProperties ) );
leftNegativeMask = leftNegativeMask.setAll( calculateNegativeMask( objectType, leftListenedProperties, accessibleProperties ) );
}
}
@Override
protected Pattern getLeftInputPattern( BuildContext context ) {
return context.getLastBuiltPatterns()[0];
}
@Override
protected BitMask setNodeConstraintsPropertyReactiveMask( BitMask mask, ObjectType objectType, List<String> accessibleProperties) {
for (int i = 0; i < alphaConstraints.length; i++) {
mask = mask.setAll(alphaConstraints[i].getListenedPropertyMask(objectType, accessibleProperties));
}
return mask;
}
public Class< ? > getResultClass() {
return from.getResultClass();
}
public void networkUpdated(UpdateContext updateContext) {
this.leftInput.networkUpdated(updateContext);
}
@SuppressWarnings("unchecked")
public RightTuple createRightTuple(final TupleImpl leftTuple,
final PropagationContext context,
final ReteEvaluator reteEvaluator,
final Object object) {
return new RightTuple(createFactHandle(reteEvaluator, object) );
}
public InternalFactHandle createFactHandle( ReteEvaluator reteEvaluator, Object object ) {
InternalFactHandle handle = reteEvaluator.getFactHandle(object);
if (handle != null && handle.getObject() == object) {
return handle;
}
if ( objectTypeConf == null ) {
// use default entry point and object class. Notice that at this point object is assignable to resultClass
objectTypeConf = new ClassObjectTypeConf( reteEvaluator.getDefaultEntryPointId(), getResultClass(), reteEvaluator.getKnowledgeBase() );
}
return reteEvaluator.createFactHandle(object, objectTypeConf, null );
}
public void addToCreatedHandlesMap(final Map<Object, RightTuple> matches,
final RightTuple rightTuple) {
if ( rightTuple.getFactHandle().isValid() ) {
Object object = rightTuple.getFactHandle().getObject();
// keeping a list of matches
RightTuple existingMatch = matches.get(object);
if ( existingMatch != null ) {
// this is for the obscene case where two or more objects returned by "from"
// have the same hash code and evaluate equals() to true, so we need to preserve
// all of them to avoid leaks
rightTuple.setNext(existingMatch);
}
matches.put( object,
rightTuple );
}
}
public T createMemory(final RuleBaseConfiguration config, ReteEvaluator reteEvaluator) {
BetaMemory beta = new BetaMemory(new TupleList(),
null,
this.betaConstraints.createContext(),
NodeTypeEnums.FromNode );
return (T) new FromMemory( beta,
this.dataProvider );
}
public boolean isLeftTupleMemoryEnabled() {
return tupleMemoryEnabled;
}
/**
* Returns the next node
* @return
* The next TupleSinkNode
*/
public LeftTupleSinkNode getNextLeftTupleSinkNode() {
return this.nextTupleSinkNode;
}
/**
* Sets the next node
* @param next
* The next TupleSinkNode
*/
public void setNextLeftTupleSinkNode(final LeftTupleSinkNode next) {
this.nextTupleSinkNode = next;
}
/**
* Returns the previous node
* @return
* The previous TupleSinkNode
*/
public LeftTupleSinkNode getPreviousLeftTupleSinkNode() {
return this.previousTupleSinkNode;
}
/**
* Sets the previous node
* @param previous
* The previous TupleSinkNode
*/
public void setPreviousLeftTupleSinkNode(final LeftTupleSinkNode previous) {
this.previousTupleSinkNode = previous;
}
public int getType() {
return NodeTypeEnums.FromNode;
}
public static class FromMemory extends AbstractLinkedListNode<Memory>
implements
Serializable,
SegmentNodeMemory {
private static final long serialVersionUID = 510l;
private DataProvider dataProvider;
private final BetaMemory betaMemory;
public Object providerContext;
public FromMemory(BetaMemory betaMemory,
DataProvider dataProvider) {
this.betaMemory = betaMemory;
this.dataProvider = dataProvider;
this.providerContext = dataProvider.createContext();
}
public int getNodeType() {
return NodeTypeEnums.FromNode;
}
public SegmentMemory getSegmentMemory() {
return betaMemory.getSegmentMemory();
}
public void setSegmentMemory(SegmentMemory segmentMemory) {
betaMemory.setSegmentMemory(segmentMemory);
}
public BetaMemory<Object> getBetaMemory() {
return betaMemory;
}
public void reset() {
this.betaMemory.reset();
this.providerContext = dataProvider.createContext();
}
@Override
public long getNodePosMaskBit() {
return betaMemory.getNodePosMaskBit();
}
@Override
public void setNodePosMaskBit( long segmentPos ) {
betaMemory.setNodePosMaskBit( segmentPos );
}
@Override
public void setNodeDirtyWithoutNotify() {
betaMemory.setNodeDirtyWithoutNotify();
}
@Override
public void setNodeCleanWithoutNotify() {
betaMemory.setNodeCleanWithoutNotify();
}
}
@Override
public ObjectTypeNode getObjectTypeNode() {
return leftInput.getObjectTypeNode();
}
public void doAttach( BuildContext context ) {
super.doAttach(context);
this.leftInput.addTupleSink( this, context );
}
protected boolean doRemove(final RuleRemovalContext context,
final ReteooBuilder builder) {
if ( !this.isInUse() ) {
getLeftTupleSource().removeTupleSink( this );
return true;
}
return false;
}
}