Search in sources :

Example 1 with MVELConstraint

use of org.drools.mvel.MVELConstraint in project drools by kiegroup.

the class RuleBuilderTest method testBuildBigDecimalLiteralConstraint.

@Test
public void testBuildBigDecimalLiteralConstraint() throws Exception {
    final PackageDescr pkgDescr = new PackageDescr("org.drools");
    final RuleDescr ruleDescr = new RuleDescr("Test Rule");
    AndDescr andDescr = new AndDescr();
    PatternDescr patDescr = new PatternDescr("java.math.BigDecimal", "$bd");
    ExprConstraintDescr fcd = new ExprConstraintDescr("this == 10");
    patDescr.addConstraint(fcd);
    andDescr.addDescr(patDescr);
    ruleDescr.setLhs(andDescr);
    ruleDescr.setConsequence("");
    pkgDescr.addRule(ruleDescr);
    final KnowledgeBuilderImpl kBuilder = new KnowledgeBuilderImpl();
    kBuilder.addPackage(pkgDescr);
    assertTrue(kBuilder.getErrors().toString(), kBuilder.getErrors().isEmpty());
    final RuleImpl rule = kBuilder.getPackages()[0].getRule("Test Rule");
    final GroupElement and = rule.getLhs();
    final Pattern pat = (Pattern) and.getChildren().get(0);
    if (pat.getConstraints().get(0) instanceof MVELConstraint) {
        final MVELConstraint fc = (MVELConstraint) pat.getConstraints().get(0);
        assertTrue("Wrong class. Expected java.math.BigDecimal. Found: " + fc.getField().getValue().getClass(), fc.getField().getValue() instanceof BigDecimal);
    }
}
Also used : Pattern(org.drools.core.rule.Pattern) PatternDescr(org.drools.drl.ast.descr.PatternDescr) MVELConstraint(org.drools.mvel.MVELConstraint) AndDescr(org.drools.drl.ast.descr.AndDescr) KnowledgeBuilderImpl(org.drools.compiler.builder.impl.KnowledgeBuilderImpl) GroupElement(org.drools.core.rule.GroupElement) RuleDescr(org.drools.drl.ast.descr.RuleDescr) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) PackageDescr(org.drools.drl.ast.descr.PackageDescr) ExprConstraintDescr(org.drools.drl.ast.descr.ExprConstraintDescr) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 2 with MVELConstraint

use of org.drools.mvel.MVELConstraint in project drools by kiegroup.

the class MVELTest method testNewConstructor.

@Test
public void testNewConstructor() {
    final String str = "" + "package org.drools.mvel.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";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    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);
    }
}
Also used : Address(org.drools.mvel.compiler.Address) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) AlphaNode(org.drools.core.reteoo.AlphaNode) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) MVELConstraint(org.drools.mvel.MVELConstraint) MVELObjectClassFieldReader(org.drools.mvel.extractors.MVELObjectClassFieldReader) ClassFieldReader(org.drools.mvel.accessors.ClassFieldReader) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 3 with MVELConstraint

use of org.drools.mvel.MVELConstraint in project drools by kiegroup.

the class MVELTest method testArrayAccessorWithGenerics.

@Test
public void testArrayAccessorWithGenerics() {
    final String str = "" + "package org.drools.mvel.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( addresses[0] == new Address('s1'), addresses[0].street == new Address('s1').street ) \n" + "then \n" + "    list.add('r1'); \n" + "end \n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Person p = new Person("yoda");
    p.addAddress(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;
        }
    }
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MVELConstraint) {
        assertTrue(((MVELConstraint) constraint).getFieldExtractor() instanceof MVELObjectClassFieldReader);
    }
    alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    constraint = alphanode.getConstraint();
    if (constraint instanceof MVELConstraint) {
        assertTrue(((MVELConstraint) constraint).getFieldExtractor() instanceof MVELObjectClassFieldReader);
    }
}
Also used : Address(org.drools.mvel.compiler.Address) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) MVELObjectClassFieldReader(org.drools.mvel.extractors.MVELObjectClassFieldReader) AlphaNode(org.drools.core.reteoo.AlphaNode) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) MVELConstraint(org.drools.mvel.MVELConstraint) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 4 with MVELConstraint

use of org.drools.mvel.MVELConstraint in project drools by kiegroup.

the class MVELTest method testMapAccessorWithStaticFieldAccess.

@Test
public void testMapAccessorWithStaticFieldAccess() {
    final String str = "" + "package org.drools.mvel.compiler.test \n" + "import " + Person.class.getCanonicalName() + "\n" + "import " + Address.class.getCanonicalName() + "\n" + "import " + TestEnum.class.getCanonicalName() + "\n" + "global java.util.List list \n" + "rule \"show\" \n" + "when  \n" + "    $m : Person( namedAddresses[TestEnum.ONE] == new Address('s1'), namedAddresses[TestEnum.ONE].street == new Address('s1').street ) \n" + "then \n" + "    list.add('r1'); \n" + "end \n";
    KieBase kbase = KieBaseUtil.getKieBaseFromKieModuleFromDrl("test", kieBaseTestConfiguration, str);
    KieSession ksession = kbase.newKieSession();
    final List list = new ArrayList();
    ksession.setGlobal("list", list);
    final Person p = new Person("yoda");
    p.getNamedAddresses().put(TestEnum.ONE, 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;
        }
    }
    AlphaNode alphanode = (AlphaNode) node.getObjectSinkPropagator().getSinks()[0];
    AlphaNodeFieldConstraint constraint = alphanode.getConstraint();
    if (constraint instanceof MVELConstraint) {
        assertTrue(((MVELConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
    }
    alphanode = (AlphaNode) alphanode.getObjectSinkPropagator().getSinks()[0];
    constraint = alphanode.getConstraint();
    if (constraint instanceof MVELConstraint) {
        assertTrue(((MVELConstraint) alphanode.getConstraint()).getFieldExtractor() instanceof MVELObjectClassFieldReader);
    }
}
Also used : Address(org.drools.mvel.compiler.Address) ArrayList(java.util.ArrayList) ObjectTypeNode(org.drools.core.reteoo.ObjectTypeNode) TestEnum(org.drools.mvel.compiler.TestEnum) MVELObjectClassFieldReader(org.drools.mvel.extractors.MVELObjectClassFieldReader) AlphaNode(org.drools.core.reteoo.AlphaNode) AlphaNodeFieldConstraint(org.drools.core.spi.AlphaNodeFieldConstraint) MVELConstraint(org.drools.mvel.MVELConstraint) KieBase(org.kie.api.KieBase) StatelessKieSession(org.kie.api.runtime.StatelessKieSession) KieSession(org.kie.api.runtime.KieSession) ArrayList(java.util.ArrayList) List(java.util.List) Person(org.drools.mvel.compiler.Person) Test(org.junit.Test)

Example 5 with MVELConstraint

use of org.drools.mvel.MVELConstraint in project drools by kiegroup.

the class ConstraintEvaluationExceptionTest method initConstraintTestField.

private void initConstraintTestField(String constraint, String ruleName, String ruleFileName) {
    if (testRunType.isExecutableModel()) {
        predicateInformation = new PredicateInformation(constraint, ruleName, ruleFileName);
    } else {
        mvelConstraint = new MVELConstraint("com.example", constraint, null, null, null, null, null);
        InternalKnowledgeBase kBase = KnowledgeBaseFactory.newKnowledgeBase();
        BuildContext buildContext = new BuildContext(kBase, Collections.emptyList());
        RuleImpl ruleImpl = new RuleImpl(ruleName);
        Resource resource = new ByteArrayResource();
        resource.setSourcePath(ruleFileName);
        ruleImpl.setResource(resource);
        buildContext.setRule(ruleImpl);
        mvelConstraint.registerEvaluationContext(buildContext);
    }
}
Also used : MVELConstraint(org.drools.mvel.MVELConstraint) BuildContext(org.drools.core.reteoo.builder.BuildContext) Resource(org.kie.api.io.Resource) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) RuleImpl(org.drools.core.definitions.rule.impl.RuleImpl) ByteArrayResource(org.drools.core.io.impl.ByteArrayResource) PredicateInformation(org.drools.model.functions.PredicateInformation) InternalKnowledgeBase(org.drools.kiesession.rulebase.InternalKnowledgeBase)

Aggregations

MVELConstraint (org.drools.mvel.MVELConstraint)11 Test (org.junit.Test)6 AlphaNode (org.drools.core.reteoo.AlphaNode)5 AlphaNodeFieldConstraint (org.drools.core.spi.AlphaNodeFieldConstraint)5 ArrayList (java.util.ArrayList)4 List (java.util.List)4 RuleImpl (org.drools.core.definitions.rule.impl.RuleImpl)4 ObjectTypeNode (org.drools.core.reteoo.ObjectTypeNode)4 Address (org.drools.mvel.compiler.Address)4 Person (org.drools.mvel.compiler.Person)4 MVELObjectClassFieldReader (org.drools.mvel.extractors.MVELObjectClassFieldReader)4 KieBase (org.kie.api.KieBase)4 Pattern (org.drools.core.rule.Pattern)3 KieSession (org.kie.api.runtime.KieSession)3 StatelessKieSession (org.kie.api.runtime.StatelessKieSession)3 KnowledgeBuilderImpl (org.drools.compiler.builder.impl.KnowledgeBuilderImpl)2 DescrBuildError (org.drools.compiler.compiler.DescrBuildError)2 ArrayElementReader (org.drools.core.base.extractors.ArrayElementReader)2 ByteArrayResource (org.drools.core.io.impl.ByteArrayResource)2 BuildContext (org.drools.core.reteoo.builder.BuildContext)2