Search in sources :

Example 21 with FieldValue

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

Example 22 with FieldValue

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

the class CompositeObjectSinkAdapter method addObjectSink.

public ObjectSinkPropagator addObjectSink(ObjectSink sink, int alphaNodeHashingThreshold) {
    // dirty it, so it'll rebuild on next get
    this.sinks = null;
    if (this.sinksMap != null) {
        this.sinksMap.put(sink, sink);
    }
    if (sink.getType() == NodeTypeEnums.AlphaNode) {
        final AlphaNode alphaNode = (AlphaNode) sink;
        final InternalReadAccessor readAccessor = getHashableAccessor(alphaNode);
        if (readAccessor != null) {
            final int index = readAccessor.getIndex();
            final FieldIndex fieldIndex = registerFieldIndex(index, readAccessor);
            // DROOLS-678 : prevent null values from being hashed as 0s
            final FieldValue value = ((IndexableConstraint) alphaNode.getConstraint()).getField();
            if (fieldIndex.getCount() >= this.alphaNodeHashingThreshold && this.alphaNodeHashingThreshold != 0 && !value.isNull()) {
                if (!fieldIndex.isHashed()) {
                    hashSinks(fieldIndex);
                }
                // no need to check, we know  the sink  does not exist
                this.hashedSinkMap.put(new HashKey(index, value, fieldIndex.getFieldExtractor()), alphaNode, false);
            } else {
                if (this.hashableSinks == null) {
                    this.hashableSinks = new ObjectSinkNodeList();
                }
                this.hashableSinks.add(alphaNode);
            }
            return this;
        }
    }
    if (this.otherSinks == null) {
        this.otherSinks = new ObjectSinkNodeList();
    }
    this.otherSinks.add((ObjectSinkNode) sink);
    return this;
}
Also used : 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 23 with FieldValue

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

the class CompositeObjectSinkAdapter method unHashSinks.

void unHashSinks(final FieldIndex fieldIndex) {
    final int index = fieldIndex.getIndex();
    // this is the list of sinks that need to be removed from the hashedSinkMap
    final List<HashKey> unhashedSinks = new ArrayList<HashKey>();
    final Iterator iter = this.hashedSinkMap.newIterator();
    ObjectHashMap.ObjectEntry entry = (ObjectHashMap.ObjectEntry) iter.next();
    while (entry != null) {
        final AlphaNode alphaNode = (AlphaNode) entry.getValue();
        final IndexableConstraint indexableConstraint = (IndexableConstraint) alphaNode.getConstraint();
        // the right field index
        if (index == indexableConstraint.getFieldExtractor().getIndex()) {
            final FieldValue value = indexableConstraint.getField();
            if (this.hashableSinks == null) {
                this.hashableSinks = new ObjectSinkNodeList();
            }
            this.hashableSinks.add(alphaNode);
            unhashedSinks.add(new HashKey(index, value, fieldIndex.getFieldExtractor()));
        }
        entry = (ObjectHashMap.ObjectEntry) iter.next();
    }
    for (HashKey hashKey : unhashedSinks) {
        this.hashedSinkMap.remove(hashKey);
    }
    if (this.hashedSinkMap.isEmpty()) {
        this.hashedSinkMap = null;
    }
    fieldIndex.setHashed(false);
}
Also used : IndexableConstraint(org.drools.core.rule.IndexableConstraint) ArrayList(java.util.ArrayList) Iterator(org.drools.core.util.Iterator) ObjectHashMap(org.drools.core.util.ObjectHashMap) ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry) FieldValue(org.drools.core.spi.FieldValue) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) IndexableConstraint(org.drools.core.rule.IndexableConstraint) ObjectEntry(org.drools.core.util.ObjectHashMap.ObjectEntry)

Example 24 with FieldValue

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

the class JavaDialectTest method testEvalDetectionInAlphaNode.

@Test
public void testEvalDetectionInAlphaNode() {
    // Tests evals are generated and executed with Java dialect
    String drl = "";
    drl += "package org.drools.compiler.test\n";
    drl += "import org.drools.compiler.Person\n";
    drl += "global java.util.List list\n";
    drl += "rule test1\n";
    drl += "when\n";
    drl += "   $p1 : Person( eval( name \n != null ), name == ( new String(\"xxx\") ) )\n";
    drl += "then\n";
    drl += "end\n";
    KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
    kbuilder.add(ResourceFactory.newReaderResource(new StringReader(drl)), ResourceType.DRL);
    KnowledgeBuilderErrors errors = kbuilder.getErrors();
    if (kbuilder.hasErrors()) {
        fail(kbuilder.getErrors().toString());
    }
    assertFalse(kbuilder.hasErrors());
    InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
    kbase.addPackages(kbuilder.getKnowledgePackages());
    List<ObjectTypeNode> nodes = ((KnowledgeBaseImpl) kbase).getRete().getObjectTypeNodes();
    ObjectTypeNode node = null;
    for (ObjectTypeNode n : nodes) {
        if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
            node = n;
            break;
        }
    }
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    PredicateConstraint c = (PredicateConstraint) alphanode.getConstraint();
    assertTrue(c.getPredicateExpression() instanceof PredicateExpression);
    assertTrue(c.getPredicateExpression() instanceof CompiledInvoker);
    alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MvelConstraint) {
        FieldValue fieldVal = ((MvelConstraint) constraint).getField();
        assertEquals("xxx", fieldVal.getValue());
    }
}
Also used : KnowledgeBuilderErrors(org.kie.internal.builder.KnowledgeBuilderErrors) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) PredicateExpression(org.drools.core.spi.PredicateExpression) AlphaNode(org.drools.core.reteoo.AlphaNode) CompiledInvoker(org.drools.core.spi.CompiledInvoker) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) StringReader(java.io.StringReader) PredicateConstraint(org.drools.core.rule.PredicateConstraint) FieldValue(org.drools.core.spi.FieldValue) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 25 with FieldValue

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

the class FieldFactory method getFieldValue.

public FieldValue getFieldValue(Object value, ValueType valueType) {
    FieldValue field = null;
    if (value == null) {
        valueType = ValueType.NULL_TYPE;
    }
    if (valueType == ValueType.NULL_TYPE) {
        field = new ObjectFieldImpl(null);
    } else if (valueType == ValueType.PCHAR_TYPE || valueType == ValueType.CHAR_TYPE) {
        if (value instanceof String && ((String) value).length() == 1) {
            field = new LongFieldImpl(((String) value).charAt(0));
        } else {
            field = new LongFieldImpl(((Character) value).charValue());
        }
    } else if (valueType == ValueType.PBYTE_TYPE || valueType == ValueType.BYTE_TYPE) {
        if (value instanceof String) {
            field = new LongFieldImpl(Byte.parseByte((String) value));
        } else {
            field = new LongFieldImpl(((Number) value).byteValue());
        }
    } else if (valueType == ValueType.PSHORT_TYPE || valueType == ValueType.SHORT_TYPE) {
        if (value instanceof String) {
            try {
                field = new LongFieldImpl(NumberFormat.getInstance().parse((String) value).shortValue());
            } catch (ParseException e) {
                throw new NumberFormatException("Error parsing number '" + value + "'");
            }
        } else {
            field = new LongFieldImpl(((Number) value).shortValue());
        }
    } else if (valueType == ValueType.PINTEGER_TYPE || valueType == ValueType.INTEGER_TYPE) {
        if (value instanceof String) {
            try {
                field = new LongFieldImpl(NumberFormat.getInstance().parse((String) value).intValue());
            } catch (ParseException e) {
                throw new NumberFormatException("Error parsing number '" + value + "'");
            }
        } else {
            field = new LongFieldImpl(((Number) value).intValue());
        }
    } else if (valueType == ValueType.PLONG_TYPE || valueType == ValueType.LONG_TYPE) {
        if (value instanceof String) {
            try {
                field = new LongFieldImpl(NumberFormat.getInstance().parse((String) value).longValue());
            } catch (ParseException e) {
                throw new NumberFormatException("Error parsing number '" + value + "'");
            }
        } else {
            field = new LongFieldImpl(((Number) value).longValue());
        }
    } else if (valueType == ValueType.PFLOAT_TYPE || valueType == ValueType.FLOAT_TYPE) {
        if (value instanceof String) {
            try {
                field = new DoubleFieldImpl(NumberFormat.getInstance().parse((String) value).floatValue());
            } catch (ParseException e) {
                throw new NumberFormatException("Error parsing number '" + value + "'");
            }
        } else {
            field = new DoubleFieldImpl(((Number) value).floatValue());
        }
    } else if (valueType == ValueType.PDOUBLE_TYPE || valueType == ValueType.DOUBLE_TYPE) {
        if (value instanceof String) {
            try {
                field = new DoubleFieldImpl(NumberFormat.getInstance().parse((String) value).doubleValue());
            } catch (ParseException e) {
                throw new NumberFormatException("Error parsing number '" + value + "'");
            }
        } else {
            field = new DoubleFieldImpl(((Number) value).doubleValue());
        }
    } else if (valueType == ValueType.PBOOLEAN_TYPE || valueType == ValueType.BOOLEAN_TYPE) {
        if (value instanceof String) {
            field = new BooleanFieldImpl(Boolean.valueOf((String) value));
        } else {
            field = new BooleanFieldImpl((Boolean) value);
        }
    } else if (valueType == ValueType.STRING_TYPE) {
        field = new ObjectFieldImpl(value.toString());
    } else if (valueType == ValueType.DATE_TYPE) {
        // MN: I think its fine like this, seems to work !
        if (value instanceof String) {
            Date date = DateUtils.parseDate((String) value);
            field = new ObjectFieldImpl(date);
        } else {
            field = new ObjectFieldImpl(value);
        }
    } else if (valueType == ValueType.ARRAY_TYPE) {
        // MN: I think its fine like this.
        field = new ObjectFieldImpl(value);
    } else if (valueType == ValueType.OBJECT_TYPE) {
        field = new ObjectFieldImpl(value);
    } else if (valueType == ValueType.TRAIT_TYPE) {
        field = new ObjectFieldImpl(value);
    } else if (valueType == ValueType.BIG_DECIMAL_TYPE) {
        field = new ObjectFieldImpl(MathUtils.getBigDecimal(value));
    } else if (valueType == ValueType.BIG_INTEGER_TYPE) {
        field = new ObjectFieldImpl(MathUtils.getBigInteger(value));
    } else if (valueType == ValueType.CLASS_TYPE) {
        field = new ClassFieldImpl((Class) value);
    }
    return field;
}
Also used : BooleanFieldImpl(org.drools.core.base.field.BooleanFieldImpl) DoubleFieldImpl(org.drools.core.base.field.DoubleFieldImpl) LongFieldImpl(org.drools.core.base.field.LongFieldImpl) Date(java.util.Date) ClassFieldImpl(org.drools.core.base.field.ClassFieldImpl) ObjectFieldImpl(org.drools.core.base.field.ObjectFieldImpl) FieldValue(org.drools.core.spi.FieldValue) ParseException(java.text.ParseException)

Aggregations

FieldValue (org.drools.core.spi.FieldValue)26 MvelConstraintTestUtil (org.drools.core.rule.MvelConstraintTestUtil)9 MvelConstraint (org.drools.core.rule.constraint.MvelConstraint)9 InternalReadAccessor (org.drools.core.spi.InternalReadAccessor)9 Test (org.junit.Test)9 IOException (java.io.IOException)6 ClassObjectType (org.drools.core.base.ClassObjectType)6 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)6 InternalKnowledgeBase (org.drools.core.impl.InternalKnowledgeBase)6 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)6 ClassFieldReader (org.drools.core.base.ClassFieldReader)5 WorkingMemory (org.drools.core.WorkingMemory)4 IndexableConstraint (org.drools.core.rule.IndexableConstraint)4 Pattern (org.drools.core.rule.Pattern)4 Cheese (org.drools.core.test.model.Cheese)4 ObjectInput (java.io.ObjectInput)3 ObjectOutput (java.io.ObjectOutput)3 ArrayList (java.util.ArrayList)3 ClassFieldAccessorCache (org.drools.core.base.ClassFieldAccessorCache)3 DefaultFactHandle (org.drools.core.common.DefaultFactHandle)3