Search in sources :

Example 51 with InternalReadAccessor

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

the class BaseClassFieldAccessorFactoryTest method testInterface.

@Test
public void testInterface() throws Exception {
    final InternalReadAccessor ex = store.getReader(TestInterface.class, "something");
    assertEquals(1, ex.getIndex());
    assertEquals("foo", ex.getValue(null, new TestInterfaceImpl()));
}
Also used : TestInterfaceImpl(org.drools.core.util.asm.TestInterfaceImpl) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Test(org.junit.Test)

Example 52 with InternalReadAccessor

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

the class BaseClassFieldAccessorFactoryTest method testIt.

@Test
public void testIt() throws Exception {
    ClassFieldAccessorFactory factory = new ClassFieldAccessorFactory();
    ClassFieldAccessorCache.CacheEntry cachEntry = new ClassFieldAccessorCache.CacheEntry(Thread.currentThread().getContextClassLoader());
    InternalReadAccessor ex = factory.getClassFieldReader(TestBean.class, "name", cachEntry);
    assertEquals("michael", ex.getValue(null, new TestBean()));
    ex = factory.getClassFieldReader(TestBean.class, "age", cachEntry);
    assertEquals(42, ((Number) ex.getValue(null, new TestBean())).intValue());
}
Also used : InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) Test(org.junit.Test)

Example 53 with InternalReadAccessor

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

the class BaseClassFieldAccessorFactoryTest method testInherited.

@Test
public void testInherited() throws Exception {
    final InternalReadAccessor ex = store.getReader(BeanInherit.class, "text");
    assertEquals("hola", ex.getValue(null, new BeanInherit()));
}
Also used : InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) BeanInherit(org.drools.core.util.asm.BeanInherit) Test(org.junit.Test)

Example 54 with InternalReadAccessor

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

the class CompositeObjectSinkAdapter method removeObjectSink.

public ObjectSinkPropagator removeObjectSink(final ObjectSink sink) {
    // dirty it, so it'll rebuild on next get
    this.sinks = null;
    if (this.sinksMap != null) {
        this.sinksMap.remove(sink);
    }
    if (sink.getType() == NodeTypeEnums.AlphaNode) {
        final AlphaNode alphaNode = (AlphaNode) sink;
        final AlphaNodeFieldConstraint fieldConstraint = alphaNode.getConstraint();
        if (fieldConstraint instanceof IndexableConstraint) {
            final IndexableConstraint indexableConstraint = (IndexableConstraint) fieldConstraint;
            final FieldValue value = indexableConstraint.getField();
            if (isHashable(indexableConstraint)) {
                final InternalReadAccessor fieldAccessor = indexableConstraint.getFieldExtractor();
                final int index = fieldAccessor.getIndex();
                final FieldIndex fieldIndex = unregisterFieldIndex(index);
                if (fieldIndex.isHashed()) {
                    HashKey hashKey = new HashKey(index, value, fieldAccessor);
                    this.hashedSinkMap.remove(hashKey);
                    if (fieldIndex.getCount() <= this.alphaNodeHashingThreshold - 1) {
                        // we have less than three so unhash
                        unHashSinks(fieldIndex);
                    }
                } else {
                    this.hashableSinks.remove(alphaNode);
                }
                if (this.hashableSinks != null && this.hashableSinks.isEmpty()) {
                    this.hashableSinks = null;
                }
                return size() == 1 ? new SingleObjectSinkAdapter(getSinks()[0]) : this;
            }
        }
    }
    this.otherSinks.remove((ObjectSinkNode) sink);
    if (this.otherSinks.isEmpty()) {
        this.otherSinks = null;
    }
    return size() == 1 ? new SingleObjectSinkAdapter(getSinks()[0]) : this;
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FieldValue(org.drools.core.spi.FieldValue) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint)

Example 55 with InternalReadAccessor

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

the class CompositeObjectSinkAdapter method hashSinks.

void hashSinks(final FieldIndex fieldIndex) {
    if (this.hashedSinkMap == null) {
        this.hashedSinkMap = new ObjectHashMap();
    }
    final int index = fieldIndex.getIndex();
    final InternalReadAccessor fieldReader = fieldIndex.getFieldExtractor();
    ObjectSinkNode currentSink = this.hashableSinks.getFirst();
    while (currentSink != null) {
        final AlphaNode alphaNode = (AlphaNode) currentSink;
        final AlphaNodeFieldConstraint fieldConstraint = alphaNode.getConstraint();
        final IndexableConstraint indexableConstraint = (IndexableConstraint) fieldConstraint;
        // position to the next sink now because alphaNode may be removed if the index is equal. If we were to do this
        // afterwards, currentSink.nextNode would be null
        currentSink = currentSink.getNextObjectSinkNode();
        // the right field index
        if (index == indexableConstraint.getFieldExtractor().getIndex()) {
            final FieldValue value = indexableConstraint.getField();
            this.hashedSinkMap.put(new HashKey(index, value, fieldReader), alphaNode);
            // remove the alpha from the possible candidates of hashable sinks since it is now hashed
            hashableSinks.remove(alphaNode);
        }
    }
    if (this.hashableSinks.isEmpty()) {
        this.hashableSinks = null;
    }
    fieldIndex.setHashed(true);
}
Also used : AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) ObjectHashMap(org.drools.core.util.ObjectHashMap) FieldValue(org.drools.core.spi.FieldValue) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint)

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