use of org.drools.core.spi.FieldValue in project drools by kiegroup.
the class MVELTest method testNewConstructor.
@Test
public void testNewConstructor() {
final String str = "" + "package org.drools.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when \n" + " $m : Person( address == new Address('s1')) \n" + "then \n" + " list.add('r1'); \n" + "end \n";
final KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newByteArrayResource(str.getBytes()), ResourceType.DRL);
if (kbuilder.hasErrors()) {
fail(kbuilder.getErrors().toString());
}
final InternalKnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
kbase.addPackages(kbuilder.getKnowledgePackages());
final KieSession ksession = createKnowledgeSession(kbase);
final List list = new ArrayList();
ksession.setGlobal("list", list);
final Person p = new Person("yoda");
p.setAddress(new Address("s1"));
ksession.insert(p);
ksession.fireAllRules();
assertEquals("r1", list.get(0));
// Check it was built with MVELReturnValueExpression constraint
final List<ObjectTypeNode> nodes = ((InternalKnowledgeBase) kbase).getRete().getObjectTypeNodes();
ObjectTypeNode node = null;
for (final ObjectTypeNode n : nodes) {
if (((ClassObjectType) n.getObjectType()).getClassType() == Person.class) {
node = n;
break;
}
}
final AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
final AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
if (constraint instanceof MvelConstraint) {
assertTrue(((MvelConstraint) constraint).getFieldExtractor() instanceof ClassFieldReader);
final FieldValue r = ((MvelConstraint) constraint).getField();
assertEquals(p.getAddress(), r.getValue());
}
}
use of org.drools.core.spi.FieldValue in project drools by kiegroup.
the class FieldFactoryTest method testBigDecimal.
@Test
public void testBigDecimal() {
final FieldValue val = FieldFactory.getInstance().getFieldValue("42.42", ValueType.BIG_DECIMAL_TYPE);
assertEquals(BigDecimal.class, val.getValue().getClass());
assertTrue(val.getValue().equals(new BigDecimal("42.42")));
}
use of org.drools.core.spi.FieldValue in project drools by kiegroup.
the class TemporalEvaluatorFactoryTest method checkEvaluatorMethodWithFieldValue.
private void checkEvaluatorMethodWithFieldValue(final ValueType valueType, final InternalReadAccessor extractor, final Object[] row, final Evaluator evaluator) {
final FieldValue value = FieldFactory.getInstance().getFieldValue(row[2]);
RuntimeException exc = null;
try {
evaluator.evaluate(null, extractor, (EventFactHandle) row[0], value);
} catch (RuntimeException e) {
exc = e;
}
assertNotNull(exc);
}
use of org.drools.core.spi.FieldValue 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.spi.FieldValue in project drools by kiegroup.
the class BaseMannersTest method getLiteralConstraint.
private AlphaNodeFieldConstraint getLiteralConstraint(final Pattern pattern, final String fieldName, final boolean fieldValue) {
final Class clazz = ((ClassObjectType) pattern.getObjectType()).getClassType();
final InternalReadAccessor extractor = store.getReader(clazz, fieldName);
final FieldValue field = new BooleanFieldImpl(fieldValue);
return new MvelConstraintTestUtil(fieldName + " == " + fieldValue, new BooleanFieldImpl(fieldValue), extractor);
}
Aggregations