Search in sources :

Example 6 with ExpressionMethod

use of org.drools.workbench.models.datamodel.rule.ExpressionMethod in project drools by kiegroup.

the class RuleModelDRLPersistenceTest method testLHSReturnType.

@Test
public void testLHSReturnType() {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1087690
    RuleModel m = new RuleModel();
    m.name = "test";
    FactPattern p = new FactPattern("Number");
    m.addLhsItem(p);
    SingleFieldConstraint con1 = new SingleFieldConstraint();
    con1.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
    con1.setFieldName("this");
    con1.setOperator("!= null");
    con1.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
    p.addConstraint(con1);
    SingleFieldConstraintEBLeftSide con2 = new SingleFieldConstraintEBLeftSide();
    con2.getExpressionLeftSide().appendPart(new ExpressionUnboundFact(p.getFactType()));
    con2.getExpressionLeftSide().appendPart(new ExpressionMethod("intValue", "int", DataType.TYPE_NUMERIC_INTEGER));
    con2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    con2.setOperator("==");
    con2.setValue("0");
    p.addConstraint(con2);
    String expected = "rule \"test\"\n" + "dialect \"mvel\"\n" + "when\n" + "Number( this != null, intValue() == 0 )\n" + "then\n" + "end";
    checkMarshalling(expected, m);
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) Test(org.junit.Test)

Example 7 with ExpressionMethod

use of org.drools.workbench.models.datamodel.rule.ExpressionMethod in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSNumberExpressionWithThisPrefix.

@Test
public void testLHSNumberExpressionWithThisPrefix() throws Exception {
    String drl = "package org.mortgages;\n" + "import java.lang.Number\n" + "rule \"test\"\n" + " dialect \"mvel\"\n" + " when\n" + "  Number( this.intValue() > 5 )\n" + " then\n" + "end";
    final HashMap<String, List<MethodInfo>> map = new HashMap<>();
    final ArrayList<MethodInfo> methodInfos = new ArrayList<>();
    methodInfos.add(new MethodInfo("intValue", Collections.emptyList(), "int", null, DataType.TYPE_NUMERIC_INTEGER));
    map.put("java.lang.Number", methodInfos);
    when(dmo.getModuleMethodInformation()).thenReturn(map);
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    assertTrue(m.lhs[0] instanceof FactPattern);
    final FactPattern fp = (FactPattern) m.lhs[0];
    assertEquals("Number", fp.getFactType());
    assertEquals(1, fp.getNumberOfConstraints());
    assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraintEBLeftSide);
    final SingleFieldConstraintEBLeftSide exp = (SingleFieldConstraintEBLeftSide) fp.getConstraint(0);
    assertEquals("int", exp.getFieldType());
    assertEquals(">", exp.getOperator());
    assertEquals("5", exp.getValue());
    assertEquals(3, exp.getExpressionLeftSide().getParts().size());
    assertTrue(exp.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
    final ExpressionUnboundFact expPart0 = (ExpressionUnboundFact) exp.getExpressionLeftSide().getParts().get(0);
    assertEquals("Number", expPart0.getFactType());
    assertTrue(exp.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
    final ExpressionField expPart1 = (ExpressionField) exp.getExpressionLeftSide().getParts().get(1);
    assertEquals("this", expPart1.getName());
    assertTrue(exp.getExpressionLeftSide().getParts().get(2) instanceof ExpressionMethod);
    final ExpressionMethod expPart2 = (ExpressionMethod) exp.getExpressionLeftSide().getParts().get(2);
    assertEquals("intValue", expPart2.getName());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod) List(java.util.List) ArrayList(java.util.ArrayList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) Test(org.junit.Test)

Example 8 with ExpressionMethod

use of org.drools.workbench.models.datamodel.rule.ExpressionMethod in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSNumberExpressionWithoutThisPrefix.

@Test
public void testLHSNumberExpressionWithoutThisPrefix() throws Exception {
    String drl = "package org.mortgages;\n" + "import java.lang.Number\n" + "rule \"test\"\n" + " dialect \"mvel\"\n" + " when\n" + "  Number( intValue() > 5 )\n" + " then\n" + "end";
    final HashMap<String, List<MethodInfo>> map = new HashMap<>();
    final ArrayList<MethodInfo> methodInfos = new ArrayList<>();
    methodInfos.add(new MethodInfo("intValue", Collections.emptyList(), "int", null, DataType.TYPE_NUMERIC_INTEGER));
    map.put("java.lang.Number", methodInfos);
    when(dmo.getModuleMethodInformation()).thenReturn(map);
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    assertTrue(m.lhs[0] instanceof FactPattern);
    final FactPattern fp = (FactPattern) m.lhs[0];
    assertEquals("Number", fp.getFactType());
    assertEquals(1, fp.getNumberOfConstraints());
    assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraintEBLeftSide);
    final SingleFieldConstraintEBLeftSide exp = (SingleFieldConstraintEBLeftSide) fp.getConstraint(0);
    assertEquals("int", exp.getFieldType());
    assertEquals(">", exp.getOperator());
    assertEquals("5", exp.getValue());
    assertEquals(2, exp.getExpressionLeftSide().getParts().size());
    assertTrue(exp.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
    final ExpressionUnboundFact expPart0 = (ExpressionUnboundFact) exp.getExpressionLeftSide().getParts().get(0);
    assertEquals("Number", expPart0.getFactType());
    assertTrue(exp.getExpressionLeftSide().getParts().get(1) instanceof ExpressionMethod);
    final ExpressionMethod expPart1 = (ExpressionMethod) exp.getExpressionLeftSide().getParts().get(1);
    assertEquals("intValue", expPart1.getName());
}
Also used : SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) List(java.util.List) ArrayList(java.util.ArrayList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) Test(org.junit.Test)

Example 9 with ExpressionMethod

use of org.drools.workbench.models.datamodel.rule.ExpressionMethod in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testStringReplaceExpression.

@Test
public void testStringReplaceExpression() throws Exception {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1264321
    String drl = "rule \"Replace_condition_Issue\"\n" + "dialect \"mvel\"\n" + "when\n" + "  MyType( myString.replace(\"a\",\"b\"))\n" + "then\n" + "end";
    addModelField("org.test.MyType", "this", "org.test.MyType", DataType.TYPE_THIS);
    addModelField("org.test.MyType", "myString", String.class.getName(), DataType.TYPE_STRING);
    addMethodInformation("java.lang.String", "replace", new ArrayList<String>() {

        {
            add("String");
            add("String");
        }
    }, "java.lang.String", null, DataType.TYPE_STRING);
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    final IPattern p0 = m.lhs[0];
    assertTrue(p0 instanceof FactPattern);
    final FactPattern fp0 = (FactPattern) p0;
    assertEquals("MyType", fp0.getFactType());
    assertEquals(1, fp0.getNumberOfConstraints());
    assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraintEBLeftSide);
    final SingleFieldConstraintEBLeftSide fp0sfc0 = (SingleFieldConstraintEBLeftSide) fp0.getConstraint(0);
    assertEquals(3, fp0sfc0.getExpressionLeftSide().getParts().size());
    assertTrue(fp0sfc0.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
    final ExpressionUnboundFact ep0 = (ExpressionUnboundFact) fp0sfc0.getExpressionLeftSide().getParts().get(0);
    assertEquals("MyType", ep0.getFactType());
    assertTrue(fp0sfc0.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
    final ExpressionField ep1 = (ExpressionField) fp0sfc0.getExpressionLeftSide().getParts().get(1);
    assertEquals("myString", ep1.getName());
    assertTrue(fp0sfc0.getExpressionLeftSide().getParts().get(2) instanceof ExpressionMethod);
    final ExpressionMethod ep2 = (ExpressionMethod) fp0sfc0.getExpressionLeftSide().getParts().get(2);
    assertEquals("replace", ep2.getName());
    assertEquals(2, ep2.getParams().size());
    final ExpressionFormLine param0 = ep2.getParams().get(new ExpressionMethodParameterDefinition(0, "String"));
    assertNotNull(param0);
    assertEquals(1, param0.getParts().size());
    assertNotNull(param0.getParts().get(0));
    assertEquals("a", param0.getParts().get(0).getName());
    assertEquals("String", param0.getParts().get(0).getClassType());
    final ExpressionFormLine param1 = ep2.getParams().get(new ExpressionMethodParameterDefinition(1, "String"));
    assertNotNull(param1);
    assertEquals(1, param1.getParts().size());
    assertNotNull(param1.getParts().get(0));
    assertEquals("b", param1.getParts().get(0).getName());
    assertEquals("String", param1.getParts().get(0).getClassType());
    assertEquals(0, m.rhs.length);
    // Check round-trip
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod) ExpressionFormLine(org.drools.workbench.models.datamodel.rule.ExpressionFormLine) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) ExpressionMethodParameterDefinition(org.drools.workbench.models.datamodel.rule.ExpressionMethodParameterDefinition) Test(org.junit.Test)

Example 10 with ExpressionMethod

use of org.drools.workbench.models.datamodel.rule.ExpressionMethod in project drools by kiegroup.

the class CopyExpressionVisitor method visit.

public void visit(ExpressionMethod part) {
    ExpressionMethod method = new ExpressionMethod(part.getName(), part.getClassType(), part.getGenericType(), part.getParametricType());
    copyMethodParams(part, method);
    add(method);
    moveNext(part);
}
Also used : ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod)

Aggregations

ExpressionMethod (org.drools.workbench.models.datamodel.rule.ExpressionMethod)12 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)9 ExpressionUnboundFact (org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact)9 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)9 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)9 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)9 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)9 Test (org.junit.Test)9 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)8 SingleFieldConstraintEBLeftSide (org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide)7 ExpressionField (org.drools.workbench.models.datamodel.rule.ExpressionField)5 ExpressionFormLine (org.drools.workbench.models.datamodel.rule.ExpressionFormLine)5 ExpressionText (org.drools.workbench.models.datamodel.rule.ExpressionText)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 List (java.util.List)3 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)3 ExpressionVariable (org.drools.workbench.models.datamodel.rule.ExpressionVariable)3 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)3 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)3