Search in sources :

Example 6 with MvelConstraintTestUtil

use of org.drools.core.rule.MvelConstraintTestUtil in project drools by kiegroup.

the class RightTupleIndexHashTableIteratorTest method getConstraint.

protected BetaNodeFieldConstraint getConstraint(String identifier, Operator operator, String fieldName, Class clazz) {
    ClassFieldAccessorStore store = new ClassFieldAccessorStore();
    store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    store.setEagerWire(true);
    InternalReadAccessor extractor = store.getReader(clazz, fieldName);
    Declaration declaration = new Declaration(identifier, extractor, new Pattern(0, new ClassObjectType(clazz)));
    String expression = fieldName + " " + operator.getOperatorString() + " " + declaration.getIdentifier();
    return new MvelConstraintTestUtil(expression, declaration, extractor);
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ClassFieldAccessorStore(org.drools.core.base.ClassFieldAccessorStore) Declaration(org.drools.core.rule.Declaration) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil)

Example 7 with MvelConstraintTestUtil

use of org.drools.core.rule.MvelConstraintTestUtil in project drools by kiegroup.

the class LeftLeftTupleIndexHashTableIteratorTest method getConstraint.

protected BetaNodeFieldConstraint getConstraint(String identifier, Operator operator, String fieldName, Class clazz) {
    ClassFieldAccessorStore store = new ClassFieldAccessorStore();
    store.setClassFieldAccessorCache(new ClassFieldAccessorCache(Thread.currentThread().getContextClassLoader()));
    store.setEagerWire(true);
    InternalReadAccessor extractor = store.getReader(clazz, fieldName);
    Declaration declaration = new Declaration(identifier, extractor, new Pattern(0, new ClassObjectType(clazz)));
    String expression = fieldName + " " + operator.getOperatorString() + " " + declaration.getIdentifier();
    return new MvelConstraintTestUtil(expression, declaration, extractor);
}
Also used : ClassFieldAccessorCache(org.drools.core.base.ClassFieldAccessorCache) Pattern(org.drools.core.rule.Pattern) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ClassFieldAccessorStore(org.drools.core.base.ClassFieldAccessorStore) Declaration(org.drools.core.rule.Declaration) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil)

Example 8 with MvelConstraintTestUtil

use of org.drools.core.rule.MvelConstraintTestUtil 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 9 with MvelConstraintTestUtil

use of org.drools.core.rule.MvelConstraintTestUtil 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 10 with MvelConstraintTestUtil

use of org.drools.core.rule.MvelConstraintTestUtil 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)

Aggregations

MvelConstraintTestUtil (org.drools.core.rule.MvelConstraintTestUtil)20 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)14 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)12 ClassObjectType (org.drools.core.base.ClassObjectType)10 Test (org.junit.Test)10 FieldValue (org.drools.core.spi.FieldValue)9 Cheese (org.drools.core.test.model.Cheese)7 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)6 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)6 Pattern (org.drools.core.rule.Pattern)6 ClassFieldAccessorStore (org.drools.core.base.ClassFieldAccessorStore)5 ClassFieldReader (org.drools.core.base.ClassFieldReader)4 ObjectFieldImpl (org.drools.core.base.field.ObjectFieldImpl)4 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)4 IOException (java.io.IOException)3 ObjectInput (java.io.ObjectInput)3 ObjectOutput (java.io.ObjectOutput)3 WorkingMemory (org.drools.core.WorkingMemory)3 LongFieldImpl (org.drools.core.base.field.LongFieldImpl)3 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)3