use of org.drools.core.spi.Tuple in project drools by kiegroup.
the class TupleIndexRBTree method iterator.
public Iterator<Tuple> iterator() {
TupleList list = tree.first();
Tuple firstTuple = list != null ? list.getFirst() : null;
return new FastIterator.IteratorAdapter(fastIterator(), firstTuple);
}
use of org.drools.core.spi.Tuple in project drools by kiegroup.
the class ReactiveObjectUtil method notifyModification.
public static void notifyModification(Object object, Collection<Tuple> leftTuples, ModificationType type) {
for (Tuple leftTuple : leftTuples) {
if (!((ReactiveFromNodeLeftTuple) leftTuple).updateModificationState(object, type)) {
continue;
}
PropagationContext propagationContext = leftTuple.getPropagationContext();
ReactiveFromNode node = (ReactiveFromNode) leftTuple.getTupleSink();
LeftTupleSinkNode sink = node.getSinkPropagator().getFirstLeftTupleSink();
InternalWorkingMemory wm = getInternalWorkingMemory(propagationContext);
wm.addPropagation(new ReactivePropagation(object, (ReactiveFromNodeLeftTuple) leftTuple, propagationContext, node, sink, type));
}
}
use of org.drools.core.spi.Tuple in project drools by kiegroup.
the class RuleExecutor method getNextTuple.
private Tuple getNextTuple() {
if (tupleList.isEmpty()) {
return null;
}
Tuple leftTuple;
if (queue != null) {
leftTuple = (Tuple) queue.dequeue();
tupleList.remove(leftTuple);
} else {
leftTuple = tupleList.removeFirst();
((Activation) leftTuple).setQueued(false);
}
return leftTuple;
}
use of org.drools.core.spi.Tuple in project drools by kiegroup.
the class RuleExecutor method fireActivation.
public void fireActivation(InternalWorkingMemory wm, InternalAgenda agenda, Activation activation) throws ConsequenceException {
// We do this first as if a node modifies a fact that causes a recursion
// on an empty pattern
// we need to make sure it re-activates
wm.startOperation();
try {
wm.getAgendaEventSupport().fireBeforeActivationFired(activation, wm);
if (activation.getActivationGroupNode() != null) {
// We know that this rule will cancel all other activations in the group
// so lets remove the information now, before the consequence fires
final InternalActivationGroup activationGroup = activation.getActivationGroupNode().getActivationGroup();
activationGroup.removeActivation(activation);
agenda.clearAndCancelActivationGroup(activationGroup);
}
activation.setQueued(false);
try {
innerFireActivation(wm, agenda, activation, activation.getConsequence());
} finally {
// if the tuple contains expired events
for (Tuple tuple = activation.getTuple(); tuple != null; tuple = tuple.getParent()) {
if (tuple.getFactHandle() != null && tuple.getFactHandle().isEvent()) {
// can be null for eval, not and exists that have no right input
EventFactHandle handle = (EventFactHandle) tuple.getFactHandle();
// decrease the activation count for the event
handle.decreaseActivationsCount();
// handles "expire" only in stream mode.
if (handle.expirePartition() && handle.isExpired()) {
if (handle.getActivationsCount() <= 0) {
// and if no more activations, retract the handle
handle.getEntryPoint().delete(handle);
}
}
}
}
}
wm.getAgendaEventSupport().fireAfterActivationFired(activation, wm);
} finally {
wm.endOperation();
}
}
use of org.drools.core.spi.Tuple in project drools by kiegroup.
the class ConsequenceGenerator method generate.
public static void generate(final ConsequenceStub stub, KnowledgeHelper knowledgeHelper, WorkingMemory workingMemory) {
RuleTerminalNode rtn = (RuleTerminalNode) knowledgeHelper.getMatch().getTuple().getTupleSink();
final Declaration[] declarations = rtn.getRequiredDeclarations();
final Tuple tuple = knowledgeHelper.getTuple();
// Sort declarations based on their offset, so it can ascend the tuple's parents stack only once
final List<DeclarationMatcher> declarationMatchers = matchDeclarationsToTuple(declarations);
final ClassGenerator generator = createInvokerClassGenerator(stub, workingMemory).setInterfaces(Consequence.class, CompiledInvoker.class);
generator.addMethod(ACC_PUBLIC, "getName", generator.methodDescr(String.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
push(stub.getGeneratedInvokerClassName());
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(null, KnowledgeHelper.class, WorkingMemory.class), new String[] { "java/lang/Exception" }, new GeneratorHelper.EvaluateMethod() {
public void body(MethodVisitor mv) {
// Tuple tuple = knowledgeHelper.getTuple();
mv.visitVarInsn(ALOAD, 1);
invokeInterface(KnowledgeHelper.class, "getTuple", Tuple.class);
cast(LeftTuple.class);
// LeftTuple
mv.visitVarInsn(ASTORE, 3);
// Declaration[] declarations = ((RuleTerminalNode)knowledgeHelper.getMatch().getTuple().getTupleSink()).getDeclarations();
mv.visitVarInsn(ALOAD, 1);
invokeInterface(KnowledgeHelper.class, "getMatch", Activation.class);
invokeInterface(Activation.class, "getTuple", Tuple.class);
invokeInterface(Tuple.class, "getTupleSink", Sink.class);
cast(RuleTerminalNode.class);
invokeVirtual(RuleTerminalNode.class, "getRequiredDeclarations", Declaration[].class);
mv.visitVarInsn(ASTORE, 4);
Tuple currentTuple = tuple;
// astore start position for objects to store in loop
objAstorePos = 6;
int[] paramsPos = new int[declarations.length];
// declarationMatchers is already sorted by offset with tip declarations now first
for (DeclarationMatcher matcher : declarationMatchers) {
// original index refers to the array position with RuleTerminalNode.getDeclarations()
int i = matcher.getOriginalIndex();
int handlePos = objAstorePos;
int objPos = ++objAstorePos;
paramsPos[i] = handlePos;
currentTuple = traverseTuplesUntilDeclaration(currentTuple, matcher.getRootDistance(), 3);
// handle = tuple.getFactHandle()
mv.visitVarInsn(ALOAD, 3);
invokeInterface(Tuple.class, "getOriginalFactHandle", InternalFactHandle.class);
mv.visitVarInsn(ASTORE, handlePos);
String declarationType = declarations[i].getTypeName();
if (stub.getNotPatterns()[i]) {
// notPattern indexes field declarations
// declarations[i].getValue((InternalWorkingMemory)workingMemory, fact[i].getObject());
// org.kie.rule.Declaration[]
mv.visitVarInsn(ALOAD, 4);
// i
push(i);
// declarations[i]
mv.visitInsn(AALOAD);
// WorkingMemory
mv.visitVarInsn(ALOAD, 2);
cast(InternalWorkingMemory.class);
// handle[i]
mv.visitVarInsn(ALOAD, handlePos);
invokeInterface(InternalFactHandle.class, "getObject", Object.class);
storeObjectFromDeclaration(declarations[i], declarationType);
// The facthandle should be set to that of the field, if it's an object, otherwise this will return null
// fact[i] = (InternalFactHandle)workingMemory.getFactHandle(obj);
mv.visitVarInsn(ALOAD, 2);
loadAsObject(objPos);
invokeInterface(WorkingMemory.class, "getFactHandle", FactHandle.class, Object.class);
cast(InternalFactHandle.class);
mv.visitVarInsn(ASTORE, handlePos);
} else {
// handle[i]
mv.visitVarInsn(ALOAD, handlePos);
invokeInterface(InternalFactHandle.class, "getObject", Object.class);
mv.visitTypeInsn(CHECKCAST, internalName(declarationType));
// obj[i]
objAstorePos += store(objPos, declarationType);
}
}
// @{ruleClassName}.@{methodName}(KnowledgeHelper, @foreach{declr : declarations} Object, FactHandle @end)
StringBuilder consequenceMethodDescr = new StringBuilder("(L" + KnowledgeHelper.class.getName().replace('.', '/') + ";");
// KnowledgeHelper
mv.visitVarInsn(ALOAD, 1);
for (int i = 0; i < declarations.length; i++) {
// obj[i]
load(paramsPos[i] + 1);
// handle[i]
mv.visitVarInsn(ALOAD, paramsPos[i]);
consequenceMethodDescr.append(typeDescr(declarations[i].getTypeName())).append("L" + FactHandle.class.getName().replace('.', '/') + ";");
}
// @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
parseGlobals(stub.getGlobals(), stub.getGlobalTypes(), 2, consequenceMethodDescr);
consequenceMethodDescr.append(")V");
mv.visitMethodInsn(INVOKESTATIC, stub.getInternalRuleClassName(), stub.getMethodName(), consequenceMethodDescr.toString());
mv.visitInsn(RETURN);
}
});
stub.setConsequence(generator.<Consequence>newInstance());
}
Aggregations