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);
}
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;
}
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);
}
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());
}
}
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;
}
Aggregations