use of org.drools.core.spi.FieldValue in project drools by kiegroup.
the class AlphaNodeTest method testReturnValueConstraintAssertObject.
/*
* This just test AlphaNode With a different Constraint type.
*/
@Test
public void testReturnValueConstraintAssertObject() throws Exception {
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
BuildContext buildContext = new BuildContext(kBase);
buildContext.setRule(new RuleImpl("test"));
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final RuleImpl rule = new RuleImpl("test-rule");
PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
final MockObjectSource source = new MockObjectSource(buildContext.getNextId());
final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, buildContext);
final MockObjectSink sink = new MockObjectSink();
alphaNode.addObjectSink(sink);
final Cheese cheddar = new Cheese("cheddar", 5);
final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
assertLength(0, sink.getAsserted());
// object should assert as it passes text
alphaNode.assertObject(f0, context, ksession);
assertLength(1, sink.getAsserted());
final Object[] list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
final Cheese stilton = new Cheese("stilton", 6);
f0.setObject(stilton);
sink.getAsserted().clear();
// object should not assert as it does not pass text
alphaNode.assertObject(f0, context, ksession);
assertLength(0, sink.getAsserted());
}
use of org.drools.core.spi.FieldValue in project drools by kiegroup.
the class AlphaNodeTest method testUpdateSinkWithoutMemory.
@Test
public void testUpdateSinkWithoutMemory() {
// An AlphaNode should try and repropagate from its source
InternalKnowledgeBase kBase = (InternalKnowledgeBase) KnowledgeBaseFactory.newKnowledgeBase();
BuildContext buildContext = new BuildContext(kBase);
buildContext.setRule(new RuleImpl("test"));
StatefulKnowledgeSessionImpl ksession = (StatefulKnowledgeSessionImpl) kBase.newKieSession();
final RuleImpl rule = new RuleImpl("test-rule");
PropagationContextFactory pctxFactory = kBase.getConfiguration().getComponentFactory().getPropagationContextFactory();
final PropagationContext context = pctxFactory.createPropagationContext(0, PropagationContext.Type.INSERTION, null, null, null);
final MockObjectSource source = new MockObjectSource(buildContext.getNextId());
final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, // no memory
buildContext);
alphaNode.attach(buildContext);
final MockObjectSink sink1 = new MockObjectSink();
alphaNode.addObjectSink(sink1);
// Assert a single fact which should be in the AlphaNode memory and also
// propagated to the
// the tuple sink
final Cheese cheese = new Cheese("cheddar", 0);
final DefaultFactHandle handle1 = new DefaultFactHandle(1, cheese);
// adding handle to the mock source
source.addFact(handle1);
alphaNode.assertObject(handle1, context, ksession);
// Create a fact that should not be propagated, since the alpha node restriction will filter it out
final Cheese stilton = new Cheese("stilton", 10);
final DefaultFactHandle handle2 = new DefaultFactHandle(2, stilton);
// adding handle to the mock source
source.addFact(handle2);
alphaNode.assertObject(handle2, context, ksession);
assertLength(1, sink1.getAsserted());
// Attach a new tuple sink
final MockObjectSink sink2 = new MockObjectSink();
// Tell the alphanode to update the new node. Make sure the first sink1
// is not updated
// likewise the source should not do anything
alphaNode.updateSink(sink2, context, ksession);
assertLength(1, sink1.getAsserted());
assertLength(1, sink2.getAsserted());
assertEquals(1, source.getUdated());
}
use of org.drools.core.spi.FieldValue 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 WorkingMemory workingMemory) {
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, workingMemory).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, WorkingMemory.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.getOriginalIndex();
previousDeclarationsParamsPos[i] = objAstorePos;
currentTuple = traverseTuplesUntilDeclaration(currentTuple, matcher.getRootDistance(), 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.spi.FieldValue in project drools by kiegroup.
the class ReturnValueRestriction method isAllowedCachedLeft.
public boolean isAllowedCachedLeft(final ContextEntry context, final InternalFactHandle handle) {
try {
ReturnValueContextEntry ctx = (ReturnValueContextEntry) context;
FieldValue value = this.expression.evaluate(handle, ctx.tuple, this.previousDeclarations, this.localDeclarations, ctx.workingMemory, ctx.dialectContext);
return this.evaluator.evaluate(ctx.workingMemory, this.readAccessor, handle, value);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
use of org.drools.core.spi.FieldValue in project drools by kiegroup.
the class ReturnValueRestriction method isAllowedCachedRight.
public boolean isAllowedCachedRight(final Tuple tuple, final ContextEntry context) {
try {
ReturnValueContextEntry ctx = (ReturnValueContextEntry) context;
FieldValue value = this.expression.evaluate(ctx.handle, tuple, this.previousDeclarations, this.localDeclarations, ctx.workingMemory, ctx.dialectContext);
return this.evaluator.evaluate(ctx.workingMemory, this.readAccessor, ctx.handle, value);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
Aggregations