use of org.drools.core.rule.constraint.MvelConstraint in project drools by kiegroup.
the class JavaAccumulateBuilder method bindReaderToDeclaration.
private void bindReaderToDeclaration(RuleBuildContext context, AccumulateDescr accumDescr, Pattern pattern, AccumulateFunctionCallDescr fc, InternalReadAccessor readAccessor, Class<?> resultType, int index) {
if (fc.getBind() != null) {
if (context.getDeclarationResolver().isDuplicated(context.getRule(), fc.getBind(), resultType.getName())) {
if (!fc.isUnification()) {
context.addError(new DescrBuildError(context.getParentDescr(), accumDescr, null, "Duplicate declaration for variable '" + fc.getBind() + "' in the rule '" + context.getRule().getName() + "'"));
} else {
Declaration inner = context.getDeclarationResolver().getDeclaration(fc.getBind());
Constraint c = new MvelConstraint(Collections.singletonList(context.getPkg().getName()), index >= 0 ? "this[ " + index + " ] == " + fc.getBind() : "this == " + fc.getBind(), new Declaration[] { inner }, null, null, IndexUtil.ConstraintType.EQUAL, context.getDeclarationResolver().getDeclaration(fc.getBind()), index >= 0 ? new ArrayElementReader(readAccessor, index, resultType) : readAccessor, true);
((MutableTypeConstraint) c).setType(Constraint.ConstraintType.BETA);
pattern.addConstraint(c);
}
} else {
Declaration declr = pattern.addDeclaration(fc.getBind());
declr.setReadAccessor(readAccessor);
}
}
}
use of org.drools.core.rule.constraint.MvelConstraint in project drools by kiegroup.
the class AlphaNodeTest method testLiteralConstraintAssertObjectWithoutMemory.
@Test
public void testLiteralConstraintAssertObjectWithoutMemory() 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 ClassFieldReader extractor = store.getReader(Cheese.class, "type");
final FieldValue field = FieldFactory.getInstance().getFieldValue("cheddar");
final MvelConstraint constraint = new MvelConstraintTestUtil("type == \"cheddar\"", field, extractor);
// With Memory
final AlphaNode alphaNode = new AlphaNode(buildContext.getNextId(), constraint, source, // no memory
buildContext);
final MockObjectSink sink = new MockObjectSink();
alphaNode.addObjectSink(sink);
final Cheese cheddar = new Cheese("cheddar", 5);
final DefaultFactHandle f0 = (DefaultFactHandle) ksession.insert(cheddar);
// check sink is empty
assertLength(0, sink.getAsserted());
// object should assert as it passes text
alphaNode.assertObject(f0, context, ksession);
assertEquals(1, sink.getAsserted().size());
Object[] list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
final Cheese stilton = new Cheese("stilton", 6);
final DefaultFactHandle f1 = new DefaultFactHandle(1, stilton);
// object should NOT assert as it does not pass test
alphaNode.assertObject(f1, context, ksession);
assertLength(1, sink.getAsserted());
list = (Object[]) sink.getAsserted().get(0);
assertSame(cheddar, ksession.getObject((DefaultFactHandle) list[0]));
}
use of org.drools.core.rule.constraint.MvelConstraint in project drools by kiegroup.
the class CompositeObjectSinkAdapterTest method testDoubleAlphaWithBeta.
@Test
public void testDoubleAlphaWithBeta() {
final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
final MvelConstraint lit = new MvelConstraintTestUtil("type == \"stilton\"", new ObjectFieldImpl("stilton"), new MockExtractor());
final AlphaNode al = new AlphaNode(buildContext.getNextId(), lit, new MockObjectSource(0), buildContext);
ad.addObjectSink(al);
assertNull(ad.otherSinks);
assertNotNull(ad.hashedFieldIndexes);
assertEquals(1, ad.hashableSinks.size());
assertEquals(al, ad.getSinks()[0]);
final MvelConstraint lit2 = new MvelConstraintTestUtil("type == \"cheddar\"", new ObjectFieldImpl("cheddar"), new MockExtractor());
final AlphaNode al2 = new AlphaNode(buildContext.getNextId(), lit2, new MockObjectSource(buildContext.getNextId()), buildContext);
ad.addObjectSink(al2);
assertNull(ad.otherSinks);
assertEquals(2, ad.hashableSinks.size());
assertEquals(al, ad.getSinks()[0]);
assertEquals(al2, ad.getSinks()[1]);
// add a beta, just for good measure, make sure it leaves others alone
final MockBetaNode beta = new MockBetaNode(buildContext.getNextId(), new MockBetaNode(), new MockObjectSource(), buildContext);
ad.addObjectSink(beta);
assertNotNull(ad.otherSinks);
assertEquals(2, ad.hashableSinks.size());
assertEquals(1, ad.otherSinks.size());
assertEquals(beta, ad.otherSinks.getFirst());
ad.removeObjectSink(beta);
assertNull(ad.otherSinks);
assertEquals(2, ad.hashableSinks.size());
}
use of org.drools.core.rule.constraint.MvelConstraint in project drools by kiegroup.
the class CompositeObjectSinkAdapterTest method testTripleAlphaCharacterConstraint.
@Test
public void testTripleAlphaCharacterConstraint() {
final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
InternalReadAccessor extractor = store.getReader(Cheese.class, "charType");
final MvelConstraint lit = new MvelConstraintTestUtil("charType == 65", new LongFieldImpl(65), extractor);
final AlphaNode al = new AlphaNode(buildContext.getNextId(), lit, new MockObjectSource(buildContext.getNextId()), buildContext);
ad.addObjectSink(al);
assertNull(ad.otherSinks);
assertNotNull(ad.hashedFieldIndexes);
assertEquals(1, ad.hashableSinks.size());
assertEquals(al, ad.getSinks()[0]);
final MvelConstraint lit2 = new MvelConstraintTestUtil("charType == 66", new LongFieldImpl(66), extractor);
final AlphaNode al2 = new AlphaNode(buildContext.getNextId(), lit2, new MockObjectSource(buildContext.getNextId()), buildContext);
ad.addObjectSink(al2);
assertNull(ad.hashedSinkMap);
assertEquals(2, ad.hashableSinks.size());
final MvelConstraint lit3 = new MvelConstraintTestUtil("charType == 67", new LongFieldImpl(67), extractor);
final AlphaNode al3 = new AlphaNode(buildContext.getNextId(), lit3, new MockObjectSource(buildContext.getNextId()), buildContext);
ad.addObjectSink(al3);
// this should now be nicely hashed.
assertNotNull(ad.hashedSinkMap);
assertNull(ad.hashableSinks);
// test propagation
Cheese cheese = new Cheese();
cheese.setCharType('B');
CompositeObjectSinkAdapter.HashKey hashKey = new CompositeObjectSinkAdapter.HashKey();
// should find this
hashKey.setValue(extractor.getIndex(), cheese, extractor);
ObjectSink sink = (ObjectSink) ad.hashedSinkMap.get(hashKey);
assertSame(al2, sink);
// should not find this one
cheese.setCharType('X');
hashKey.setValue(extractor.getIndex(), cheese, extractor);
sink = (ObjectSink) ad.hashedSinkMap.get(hashKey);
assertNull(sink);
// now remove one, check the hashing is undone
ad.removeObjectSink(al2);
assertNotNull(ad.hashableSinks);
assertEquals(2, ad.hashableSinks.size());
assertNull(ad.hashedSinkMap);
}
use of org.drools.core.rule.constraint.MvelConstraint in project drools by kiegroup.
the class CompositeObjectSinkAdapterTest method testSingleAlpha.
@Test
public void testSingleAlpha() {
final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
final MvelConstraint lit = new MvelConstraintTestUtil("type == \"stilton\"", new ObjectFieldImpl("stilton"), new MockExtractor());
final AlphaNode al = new AlphaNode(buildContext.getNextId(), lit, new MockObjectSource(0), buildContext);
ad.addObjectSink(al);
assertNull(ad.otherSinks);
assertNotNull(ad.hashedFieldIndexes);
assertEquals(1, ad.hashableSinks.size());
assertEquals(al, ad.getSinks()[0]);
ad.removeObjectSink(al);
assertNull(ad.otherSinks);
assertNull(ad.hashableSinks);
}
Aggregations