Search in sources :

Example 16 with MvelConstraint

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);
        }
    }
}
Also used : DescrBuildError(org.drools.compiler.compiler.DescrBuildError) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Constraint(org.drools.core.spi.Constraint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) MutableTypeConstraint(org.drools.core.rule.MutableTypeConstraint) ArrayElementReader(org.drools.core.base.extractors.ArrayElementReader) Declaration(org.drools.core.rule.Declaration)

Example 17 with MvelConstraint

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]));
}
Also used : PropagationContextFactory(org.drools.core.common.PropagationContextFactory) PropagationContext(org.drools.core.spi.PropagationContext) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) Cheese(org.drools.core.test.model.Cheese) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) DefaultFactHandle(org.drools.core.common.DefaultFactHandle) BuildContext(org.drools.core.reteoo.builder.BuildContext) ClassFieldReader(org.drools.core.base.ClassFieldReader) StatefulKnowledgeSessionImpl(org.drools.core.impl.StatefulKnowledgeSessionImpl) FieldValue(org.drools.core.spi.FieldValue) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 18 with MvelConstraint

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());
}
Also used : MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) ObjectFieldImpl(org.drools.core.base.field.ObjectFieldImpl) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) Test(org.junit.Test)

Example 19 with MvelConstraint

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);
}
Also used : InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) LongFieldImpl(org.drools.core.base.field.LongFieldImpl) Cheese(org.drools.core.test.model.Cheese) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) Test(org.junit.Test)

Example 20 with MvelConstraint

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);
}
Also used : MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) ObjectFieldImpl(org.drools.core.base.field.ObjectFieldImpl) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) Test(org.junit.Test)

Aggregations

MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)28 Test (org.junit.Test)20 MvelConstraintTestUtil (org.drools.core.rule.MvelConstraintTestUtil)12 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)11 Cheese (org.drools.core.test.model.Cheese)9 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)8 FieldValue (org.drools.core.spi.FieldValue)8 ClassFieldReader (org.drools.core.base.ClassFieldReader)7 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)7 ArrayList (java.util.ArrayList)6 Pattern (org.drools.core.rule.Pattern)6 KieSession (org.kie.api.runtime.KieSession)6 List (java.util.List)5 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)5 AlphaNode (org.drools.core.reteoo.AlphaNode)5 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)5 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)5 KnowledgeBuilder (org.kie.internal.builder.KnowledgeBuilder)5 Address (org.drools.compiler.Address)4 Person (org.drools.compiler.Person)4