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));
}
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);
}
Aggregations