Search in sources :

Example 1 with ExpressionMethodParameterDefinition

use of org.drools.workbench.models.datamodel.rule.ExpressionMethodParameterDefinition 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 2 with ExpressionMethodParameterDefinition

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

the class CopyExpressionVisitor method copyMethodParams.

private void copyMethodParams(ExpressionMethod part, ExpressionMethod method) {
    Map<ExpressionMethodParameterDefinition, ExpressionFormLine> params = new HashMap<ExpressionMethodParameterDefinition, ExpressionFormLine>();
    for (Map.Entry<ExpressionMethodParameterDefinition, ExpressionFormLine> entry : part.getParams().entrySet()) {
        params.put(new ExpressionMethodParameterDefinition(entry.getKey().getIndex(), entry.getKey().getDataType()), new ExpressionFormLine(entry.getValue()));
    }
    method.setParams(params);
}
Also used : ExpressionFormLine(org.drools.workbench.models.datamodel.rule.ExpressionFormLine) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) ExpressionMethodParameterDefinition(org.drools.workbench.models.datamodel.rule.ExpressionMethodParameterDefinition)

Aggregations

ExpressionFormLine (org.drools.workbench.models.datamodel.rule.ExpressionFormLine)2 ExpressionMethodParameterDefinition (org.drools.workbench.models.datamodel.rule.ExpressionMethodParameterDefinition)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)1 ExpressionField (org.drools.workbench.models.datamodel.rule.ExpressionField)1 ExpressionMethod (org.drools.workbench.models.datamodel.rule.ExpressionMethod)1 ExpressionUnboundFact (org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact)1 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)1 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)1 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)1 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)1 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)1 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)1 SingleFieldConstraintEBLeftSide (org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide)1 Test (org.junit.Test)1