Search in sources :

Example 1 with FieldValue

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());
    }
}
Also used : Address(org.drools.compiler.Address) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MvelConstraint(org.drools.core.rule.constraint.MvelConstraint) AlphaNode(org.drools.core.reteoo.AlphaNode) KnowledgeBuilder(org.kie.internal.builder.KnowledgeBuilder) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) ClassFieldReader(org.drools.core.base.ClassFieldReader) MVELObjectClassFieldReader(org.drools.core.base.extractors.MVELObjectClassFieldReader) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) FieldValue(org.drools.core.spi.FieldValue) Person(org.drools.compiler.Person) InternalKnowledgeBase(org.drools.core.impl.InternalKnowledgeBase) Test(org.junit.Test)

Example 2 with FieldValue

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")));
}
Also used : FieldValue(org.drools.core.spi.FieldValue) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 3 with FieldValue

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);
}
Also used : FieldValue(org.drools.core.spi.FieldValue)

Example 4 with FieldValue

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);
}
Also used : ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) LongFieldImpl(org.drools.core.base.field.LongFieldImpl) FieldValue(org.drools.core.spi.FieldValue) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil)

Example 5 with FieldValue

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);
}
Also used : BooleanFieldImpl(org.drools.core.base.field.BooleanFieldImpl) ClassObjectType(org.drools.core.base.ClassObjectType) InternalReadAccessor(org.drools.core.spi.InternalReadAccessor) FieldValue(org.drools.core.spi.FieldValue) MvelConstraintTestUtil(org.drools.core.rule.MvelConstraintTestUtil)

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