use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class ReteEvaluatorTest method testPropertyReactivity.
@Test
public void testPropertyReactivity() {
String str = "import " + Person.class.getCanonicalName() + ";" + "rule R when\n" + " $s : String()\n" + " $p : Person(name == $s)\n" + "then\n" + " modify($p) { setAge($p.getAge()+1) }\n" + "end";
ReteEvaluator reteEvaluator = new StatefulKnowledgeSessionImpl(1L, getKBase(str));
Person me = new Person("Mario", 40);
reteEvaluator.insert("Mario");
reteEvaluator.insert(me);
assertEquals(1, reteEvaluator.fireAllRules());
assertEquals(41, me.getAge());
}
use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class NotNodeLeftTuple method getAccumulatedObjects.
@Override
public Collection<Object> getAccumulatedObjects() {
if (NodeTypeEnums.ExistsNode != getTupleSink().getType()) {
return Collections.emptyList();
}
BetaNode betaNode = ((BetaNode) getTupleSink());
BetaConstraints constraints = betaNode.getRawConstraints();
ReteEvaluator reteEvaluator = getFactHandle().getReteEvaluator();
BetaMemory bm = (BetaMemory) reteEvaluator.getNodeMemory((MemoryFactory) getTupleSink());
TupleMemory rtm = bm.getRightTupleMemory();
FastIterator it = betaNode.getRightIterator(rtm);
ContextEntry[] contextEntry = bm.getContext();
constraints.updateFromTuple(contextEntry, reteEvaluator, this);
Collection<Object> result = new ArrayList<>();
for (RightTuple rightTuple = betaNode.getFirstRightTuple(this, rtm, it); rightTuple != null; ) {
RightTuple nextRight = (RightTuple) it.next(rightTuple);
if (!(rightTuple instanceof SubnetworkTuple)) {
InternalFactHandle fh = rightTuple.getFactHandleForEvaluation();
if (constraints.isAllowedCachedLeft(contextEntry, fh)) {
result.add(fh.getObject());
}
}
rightTuple = nextRight;
}
return result;
}
use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class CrossProductTest method setUp.
@Before
public void setUp() throws Exception {
final ObjectType list1ObjectType = new ClassObjectType(String.class);
final ObjectType list2ObjectType = new ClassObjectType(String.class);
final RuleImpl rule = new RuleImpl("rule-1");
final Pattern list1Pattern = new Pattern(0, list1ObjectType, "s1");
final Pattern list2Pattern = new Pattern(1, list2ObjectType, "s2");
rule.addPattern(list1Pattern);
rule.addPattern(list2Pattern);
final Declaration s1Declaration = rule.getDeclaration("s1");
final Declaration s2Declaration = rule.getDeclaration("s2");
this.values = new ArrayList();
rule.setConsequence(new Consequence() {
private static final long serialVersionUID = 510l;
public void evaluate(final KnowledgeHelper knowledgeHelper, final ReteEvaluator reteEvaluator) throws Exception {
final String s1 = (String) knowledgeHelper.get(s1Declaration);
final String s2 = (String) knowledgeHelper.get(s2Declaration);
CrossProductTest.this.values.add(new String[] { s1, s2 });
}
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
}
public void writeExternal(ObjectOutput out) throws IOException {
}
public String getName() {
return "default";
}
});
this.pkg = CoreComponentFactory.get().createKnowledgePackage("org.drools");
this.pkg.addRule(rule);
}
use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class ReturnValueGenerator method generate.
public static void generate(final ReturnValueStub stub, final Tuple tuple, final Declaration[] previousDeclarations, final Declaration[] localDeclarations, final ReteEvaluator reteEvaluator) {
final String[] globals = stub.getGlobals();
final String[] globalTypes = stub.getGlobalTypes();
// Sort declarations based on their offset, so it can ascend the tuple's parents stack only once
final List<DeclarationMatcher> declarationMatchers = matchDeclarationsToTuple(previousDeclarations);
final ClassGenerator generator = createInvokerClassGenerator(stub, reteEvaluator).setInterfaces(ReturnValueExpression.class, CompiledInvoker.class);
generator.addMethod(ACC_PUBLIC, "createContext", generator.methodDescr(Object.class), new ClassGenerator.MethodBody() {
public void body(MethodVisitor mv) {
mv.visitInsn(ACONST_NULL);
mv.visitInsn(ARETURN);
}
}).addMethod(ACC_PUBLIC, "replaceDeclaration", generator.methodDescr(null, Declaration.class, Declaration.class)).addMethod(ACC_PUBLIC, "evaluate", generator.methodDescr(FieldValue.class, Object.class, Tuple.class, Declaration[].class, Declaration[].class, ReteEvaluator.class, Object.class), new String[] { "java/lang/Exception" }, new GeneratorHelper.EvaluateMethod() {
public void body(MethodVisitor mv) {
objAstorePos = 9;
int[] previousDeclarationsParamsPos = new int[previousDeclarations.length];
mv.visitVarInsn(ALOAD, 2);
cast(LeftTuple.class);
// LeftTuple
mv.visitVarInsn(ASTORE, 7);
Tuple currentTuple = tuple;
for (DeclarationMatcher matcher : declarationMatchers) {
int i = matcher.getMatcherIndex();
previousDeclarationsParamsPos[i] = objAstorePos;
currentTuple = traverseTuplesUntilDeclaration(currentTuple, matcher.getTupleIndex(), 7);
mv.visitVarInsn(ALOAD, 3);
push(i);
// declarations[i]
mv.visitInsn(AALOAD);
// workingMemory
mv.visitVarInsn(ALOAD, 5);
mv.visitVarInsn(ALOAD, 7);
invokeInterface(LeftTuple.class, "getFactHandle", InternalFactHandle.class);
// tuple.getFactHandle().getObject()
invokeInterface(InternalFactHandle.class, "getObject", Object.class);
storeObjectFromDeclaration(previousDeclarations[i], previousDeclarations[i].getTypeName());
}
int[] localDeclarationsParamsPos = parseDeclarations(localDeclarations, 4, 2, 5, false);
// @{ruleClassName}.@{methodName}(@foreach{previousDeclarations}, @foreach{localDeclarations}, @foreach{globals})
StringBuilder returnValueMethodDescr = new StringBuilder("(");
for (int i = 0; i < previousDeclarations.length; i++) {
// previousDeclarations[i]
load(previousDeclarationsParamsPos[i]);
returnValueMethodDescr.append(typeDescr(previousDeclarations[i].getTypeName()));
}
for (int i = 0; i < localDeclarations.length; i++) {
// localDeclarations[i]
load(localDeclarationsParamsPos[i]);
returnValueMethodDescr.append(typeDescr(localDeclarations[i].getTypeName()));
}
// @foreach{type : globalTypes, identifier : globals} @{type} @{identifier} = ( @{type} ) workingMemory.getGlobal( "@{identifier}" );
parseGlobals(globals, globalTypes, 5, returnValueMethodDescr);
returnValueMethodDescr.append(")Lorg/drools/core/spi/FieldValue;");
mv.visitMethodInsn(INVOKESTATIC, stub.getInternalRuleClassName(), stub.getMethodName(), returnValueMethodDescr.toString());
mv.visitInsn(ARETURN);
}
});
stub.setReturnValue(generator.<ReturnValueExpression>newInstance());
}
use of org.drools.core.common.ReteEvaluator in project drools by kiegroup.
the class PhreakTimerNode method scheduleLeftTuple.
private void scheduleLeftTuple(final TimerNode timerNode, final TimerNodeMemory tm, final PathMemory pmem, final SegmentMemory smem, final LeftTupleSink sink, final ActivationsManager activationsManager, final Timer timer, final TimerService timerService, final long timestamp, final String[] calendarNames, final Calendars calendars, final LeftTuple leftTuple, final TupleSets<LeftTuple> trgLeftTuples, final TupleSets<LeftTuple> stagedLeftTuples) {
ReteEvaluator reteEvaluator = activationsManager.getReteEvaluator();
if (leftTuple.getPropagationContext().getReaderContext() == null) {
final Trigger trigger = createTrigger(timerNode, reteEvaluator, timer, timestamp, calendarNames, calendars, leftTuple);
// regular propagation
scheduleTimer(timerNode, tm, smem, sink, reteEvaluator, timerService, timestamp, leftTuple, trgLeftTuples, stagedLeftTuples, trigger);
} else {
// de-serializing, so we need to correlate timers before scheduling them
Scheduler scheduler = new Scheduler() {
@Override
public void schedule(Trigger t) {
scheduleTimer(timerNode, tm, smem, sink, reteEvaluator, timerService, timestamp, leftTuple, trgLeftTuples, stagedLeftTuples, t);
evaluate(pmem, activationsManager, sink, tm, trgLeftTuples);
}
@Override
public Trigger getTrigger() {
return createTrigger(timerNode, reteEvaluator, timer, timestamp, calendarNames, calendars, leftTuple);
}
};
TupleKey key = TupleKey.createTupleKey(leftTuple);
leftTuple.getPropagationContext().getReaderContext().addTimerNodeScheduler(timerNode.getId(), key, scheduler);
leftTuple.setContextObject(key);
}
}
Aggregations