use of org.drools.core.base.field.LongFieldImpl in project drools by kiegroup.
the class BaseMannersTest method getLiteralConstraint.
private AlphaNodeFieldConstraint getLiteralConstraint(final Pattern pattern, final String fieldName, final int fieldValue) {
final Class clazz = ((ClassObjectType) pattern.getObjectType()).getClassType();
final InternalReadAccessor extractor = store.getReader(clazz, fieldName);
final FieldValue field = new LongFieldImpl(fieldValue);
return new MvelConstraintTestUtil(fieldName + " == " + fieldValue, new LongFieldImpl(fieldValue), extractor);
}
use of org.drools.core.base.field.LongFieldImpl 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);
}
use of org.drools.core.base.field.LongFieldImpl 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);
}
use of org.drools.core.base.field.LongFieldImpl 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