Search in sources :

Example 41 with InternalReadAccessor

use of org.drools.core.spi.InternalReadAccessor in project drools by kiegroup.

the class FactTemplateFieldExtractorTest method testExtractor.

@Test
public void testExtractor() {
    InternalKnowledgePackage pkg = new KnowledgePackageImpl("org.store");
    final FieldTemplate cheeseName = new FieldTemplateImpl("name", 0, String.class);
    final FieldTemplate cheesePrice = new FieldTemplateImpl("price", 1, Integer.class);
    final FieldTemplate[] fields = new FieldTemplate[] { cheeseName, cheesePrice };
    final FactTemplate cheese = new FactTemplateImpl(pkg, "Cheese", fields);
    final InternalReadAccessor extractName = new FactTemplateFieldExtractor(cheese, 0);
    final InternalReadAccessor extractPrice = new FactTemplateFieldExtractor(cheese, 1);
    final Fact stilton = cheese.createFact(10);
    stilton.setFieldValue("name", "stilton");
    stilton.setFieldValue("price", new Integer(200));
    assertEquals("stilton", extractName.getValue(null, stilton));
    assertEquals(new Integer(200), extractPrice.getValue(null, stilton));
    assertFalse(extractName.isNullValue(null, stilton));
    stilton.setFieldValue("name", null);
    assertTrue(extractName.isNullValue(null, stilton));
    assertFalse(extractPrice.isNullValue(null, stilton));
    final Fact brie = cheese.createFact(12);
    brie.setFieldValue("name", "brie");
    brie.setFieldValue("price", new Integer(55));
    assertEquals("brie", extractName.getValue(null, brie));
    assertEquals(new Integer(55), extractPrice.getValue(null, brie));
    assertFalse(extractName.isNullValue(null, brie));
    brie.setFieldValue("name", null);
    assertTrue(extractName.isNullValue(null, brie));
    assertFalse(extractPrice.isNullValue(null, stilton));
}
Also used : InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) KnowledgePackageImpl(org.drools.core.definitions.impl.KnowledgePackageImpl) InternalKnowledgePackage(org.drools.core.definitions.InternalKnowledgePackage) Test(org.junit.Test)

Example 42 with InternalReadAccessor

use of org.drools.core.spi.InternalReadAccessor 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 43 with InternalReadAccessor

use of org.drools.core.spi.InternalReadAccessor 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 44 with InternalReadAccessor

use of org.drools.core.spi.InternalReadAccessor in project drools by kiegroup.

the class CompositeObjectSinkAdapterTest method testTripleAlpha.

@Test
public void testTripleAlpha() {
    final CompositeObjectSinkAdapter ad = new CompositeObjectSinkAdapter();
    InternalReadAccessor extractor = store.getReader(Cheese.class, "type");
    final MvelConstraint lit = new MvelConstraintTestUtil("type == \"stilton\"", new ObjectFieldImpl("stilton"), new MockExtractor());
    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("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.hashedSinkMap);
    assertEquals(2, ad.hashableSinks.size());
    final MvelConstraint lit3 = new MvelConstraintTestUtil("type == \"stinky\"", new ObjectFieldImpl("stinky"), new MockExtractor());
    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);
    // 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) ObjectFieldImpl(org.drools.core.base.field.ObjectFieldImpl) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil) Test(org.junit.Test)

Example 45 with InternalReadAccessor

use of org.drools.core.spi.InternalReadAccessor 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)

Aggregations

InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)58 Declaration (org.drools.core.rule.Declaration)27 Test (org.junit.Test)27 ClassObjectType (org.drools.core.base.ClassObjectType)26 Pattern (org.drools.core.rule.Pattern)23 Cheese (org.drools.core.test.model.Cheese)15 MvelConstraintTestUtil (org.drools.core.rule.MvelConstraintTestUtil)14 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)10 InternalFactHandle (org.drools.core.common.InternalFactHandle)9 FieldValue (org.drools.core.spi.FieldValue)9 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)8 TypeDeclaration (org.drools.core.rule.TypeDeclaration)7 FieldIndex (org.drools.core.util.AbstractHashTable.FieldIndex)7 TupleIndexHashTable (org.drools.core.util.index.TupleIndexHashTable)7 InternalKnowledgePackage (org.drools.core.definitions.InternalKnowledgePackage)6 KnowledgePackageImpl (org.drools.core.definitions.impl.KnowledgePackageImpl)6 LeftTupleImpl (org.drools.core.reteoo.LeftTupleImpl)6 RightTupleImpl (org.drools.core.reteoo.RightTupleImpl)6 BindingDescr (org.drools.compiler.lang.descr.BindingDescr)5 ClassFieldAccessorStore (org.drools.core.base.ClassFieldAccessorStore)5