Search in sources :

Example 26 with Cheese

use of org.drools.core.test.model.Cheese 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 27 with Cheese

use of org.drools.core.test.model.Cheese 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 28 with Cheese

use of org.drools.core.test.model.Cheese in project drools by kiegroup.

the class CompositeObjectSinkAdapterTest method testTripleAlphaObjectCharacterConstraint.

@Test
public void testTripleAlphaObjectCharacterConstraint() {
    final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
    InternalReadAccessor extractor = store.getReader(Cheese.class, "charObjectType");
    final MvelConstraint lit = new MvelConstraintTestUtil("charObjectType == 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("charObjectType == 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("charObjectType == 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.setCharObjectType('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.setCharObjectType('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 29 with Cheese

use of org.drools.core.test.model.Cheese in project drools by kiegroup.

the class CompositeObjectSinkAdapterTest method testPropagationWithNullValue.

@Test
public void testPropagationWithNullValue() {
    final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
    InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final MvelConstraint lit1 = new MvelConstraintTestUtil("type == \"stilton\"", new ObjectFieldImpl("stilton"), new MockExtractor());
    final AlphaNode al1 = new AlphaNode(buildContext.getNextId(), lit1, new MockObjectSource(buildContext.getNextId()), buildContext);
    final MvelConstraint lit2 = new MvelConstraintTestUtil("type == \"brie\"", new ObjectFieldImpl("brie"), new MockExtractor());
    final AlphaNode al2 = new AlphaNode(buildContext.getNextId(), lit2, new MockObjectSource(buildContext.getNextId()), buildContext);
    final MvelConstraint lit3 = new MvelConstraintTestUtil("type == \"muzzarela\"", new ObjectFieldImpl("muzzarela"), new MockExtractor());
    final AlphaNode al3 = new AlphaNode(buildContext.getNextId(), lit3, new MockObjectSource(buildContext.getNextId()), buildContext);
    ad.addObjectSink(al1);
    ad.addObjectSink(al2);
    ad.addObjectSink(al3);
    InternalFactHandle handle = new ReteooFactHandleFactory().newFactHandle(new Cheese(), null, null, new DisconnectedWorkingMemoryEntryPoint("DEFAULT"));
    try {
        ad.propagateAssertObject(handle, null, null);
    } catch (RuntimeException e) {
        fail("Not supposed to throw any exception: " + e.getMessage());
    }
}
Also used : DisconnectedWorkingMemoryEntryPoint(org.drools.core.common.DisconnectedWorkingMemoryEntryPoint) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) Cheese(org.drools.core.test.model.Cheese) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ObjectFieldImpl(org.drools.core.base.field.ObjectFieldImpl) InternalFactHandle(org.drools.core.common.InternalFactHandle) Test(org.junit.Test)

Example 30 with Cheese

use of org.drools.core.test.model.Cheese in project drools by kiegroup.

the class DeclarationTest method testGetFieldValue.

@Test
public void testGetFieldValue() {
    final InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final Pattern pattern = new Pattern(5, new ClassObjectType(Cheese.class));
    // Bind the extractor to a decleration
    // Declarations know the pattern they derive their value from
    final Declaration declaration = new Declaration("typeOfCheese", extractor, pattern);
    // Create some facts
    final Cheese cheddar = new Cheese("cheddar", 5);
    // Check we can extract Declarations correctly
    assertEquals("cheddar", declaration.getValue(null, cheddar));
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Cheese(org.drools.core.test.model.Cheese) Test(org.junit.Test)

Aggregations

Cheese (org.drools.core.test.model.Cheese)35 Test (org.junit.Test)35 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)14 InternalFactHandle (org.drools.core.common.InternalFactHandle)14 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)14 ClassObjectType (org.drools.core.base.ClassObjectType)12 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)10 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)9 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)9 StatefulKnowledgeSessionImpl (org.drools.core.impl.StatefulKnowledgeSessionImpl)8 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)8 FactHandle (org.kie.api.runtime.rule.FactHandle)8 ClassFieldReader (org.drools.core.base.ClassFieldReader)7 RightTuple (org.drools.core.reteoo.RightTuple)7 MvelConstraintTestUtil (org.drools.core.rule.MvelConstraintTestUtil)7 Pattern (org.drools.core.rule.Pattern)7 Tuple (org.drools.core.spi.Tuple)7 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)6 Declaration (org.drools.core.rule.Declaration)6 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)6