use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFunctionCalls3.
@Test
public void testFunctionCalls3() {
String drl = "package org.mortgages;\n" + "import java.lang.Number;\n" + "import java.lang.String;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + " $var : String()\n" + "then\n" + " $var.indexOf( $var );\n" + " $var.endsWith( \".\" );\n" + " $var.substring( 0, 1 );\n" + "end\n";
Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
List<MethodInfo> mapMethodInformation = new ArrayList<>();
mapMethodInformation.add(new MethodInfo("indexOf", Collections.singletonList(DataType.TYPE_STRING), "int", null, DataType.TYPE_STRING));
mapMethodInformation.add(new MethodInfo("indexOf", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "int", null, DataType.TYPE_STRING));
mapMethodInformation.add(new MethodInfo("endsWith", Collections.singletonList(DataType.TYPE_STRING), "boolean", null, DataType.TYPE_BOOLEAN));
mapMethodInformation.add(new MethodInfo("substring", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "String", null, DataType.TYPE_STRING));
mapMethodInformation.add(new MethodInfo("substring", Arrays.asList(DataType.TYPE_NUMERIC_INTEGER, DataType.TYPE_NUMERIC_INTEGER), "String", null, DataType.TYPE_STRING));
methodInformation.put("java.lang.String", mapMethodInformation);
when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(3, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
assertTrue(m.rhs[1] instanceof ActionCallMethod);
assertTrue(m.rhs[2] instanceof ActionCallMethod);
ActionCallMethod actionCallMethod1 = (ActionCallMethod) m.rhs[0];
assertEquals(1, actionCallMethod1.getState());
assertEquals("indexOf", actionCallMethod1.getMethodName());
assertEquals("$var", actionCallMethod1.getVariable());
assertEquals(1, actionCallMethod1.getFieldValues().length);
assertEquals("indexOf", actionCallMethod1.getFieldValues()[0].getField());
assertEquals("$var", actionCallMethod1.getFieldValues()[0].getValue());
assertEquals(FieldNatureType.TYPE_VARIABLE, actionCallMethod1.getFieldValues()[0].getNature());
assertEquals(DataType.TYPE_STRING, actionCallMethod1.getFieldValues()[0].getType());
ActionCallMethod actionCallMethod2 = (ActionCallMethod) m.rhs[1];
assertEquals(1, actionCallMethod2.getState());
assertEquals("endsWith", actionCallMethod2.getMethodName());
assertEquals("$var", actionCallMethod2.getVariable());
assertEquals(1, actionCallMethod2.getFieldValues().length);
assertEquals("endsWith", actionCallMethod2.getFieldValues()[0].getField());
assertEquals(".", actionCallMethod2.getFieldValues()[0].getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod2.getFieldValues()[0].getNature());
assertEquals(DataType.TYPE_STRING, actionCallMethod2.getFieldValues()[0].getType());
ActionCallMethod actionCallMethod3 = (ActionCallMethod) m.rhs[2];
assertEquals(1, actionCallMethod3.getState());
assertEquals("substring", actionCallMethod3.getMethodName());
assertEquals("$var", actionCallMethod3.getVariable());
assertEquals(2, actionCallMethod3.getFieldValues().length);
assertEquals("substring", actionCallMethod3.getFieldValues()[0].getField());
assertEquals("0", actionCallMethod3.getFieldValues()[0].getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod3.getFieldValues()[0].getNature());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, actionCallMethod3.getFieldValues()[0].getType());
assertEquals("substring", actionCallMethod3.getFieldValues()[1].getField());
assertEquals("1", actionCallMethod3.getFieldValues()[1].getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod3.getFieldValues()[1].getNature());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, actionCallMethod3.getFieldValues()[1].getType());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFunctionCalls2.
@Test
public void testFunctionCalls2() {
String drl = "package org.mortgages;\n" + "import java.lang.Number;\n" + "import java.lang.String;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + " s : String()\n" + "then\n" + " s.indexOf( 0 );\n" + "end\n";
Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
List<MethodInfo> mapMethodInformation = new ArrayList<>();
mapMethodInformation.add(new MethodInfo("indexOf", Collections.singletonList(DataType.TYPE_STRING), "int", null, DataType.TYPE_STRING));
mapMethodInformation.add(new MethodInfo("indexOf", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "int", null, DataType.TYPE_STRING));
methodInformation.put("java.lang.String", mapMethodInformation);
when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod actionCallMethod1 = (ActionCallMethod) m.rhs[0];
assertEquals(1, actionCallMethod1.getState());
assertEquals("indexOf", actionCallMethod1.getMethodName());
assertEquals("s", actionCallMethod1.getVariable());
assertEquals(1, actionCallMethod1.getFieldValues().length);
assertEquals("indexOf", actionCallMethod1.getFieldValues()[0].getField());
assertEquals("0", actionCallMethod1.getFieldValues()[0].getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod1.getFieldValues()[0].getNature());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, actionCallMethod1.getFieldValues()[0].getType());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallWithTwoParametersIntegerAndString.
@Test
public void testMethodCallWithTwoParametersIntegerAndString() throws Exception {
String drl = "package org.mortgages;\n" + "rule \"test\"\n" + " dialect \"mvel\"\n" + " when\n" + " $t : TestClass()\n" + " then\n" + " $t.testFunction( 123, \"hello\" );\n" + "end";
final HashMap<String, List<MethodInfo>> map = new HashMap<>();
final ArrayList<MethodInfo> methodInfos = new ArrayList<>();
final ArrayList<String> params = new ArrayList<>();
params.add("Integer");
params.add("String");
methodInfos.add(new MethodInfo("testFunction", params, "java.lang.Void", null, "TestClass"));
map.put("TestClass", methodInfos);
when(dmo.getModuleMethodInformation()).thenReturn(map);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
assertEquals(1, m.rhs.length);
ActionCallMethod actionCallMethod = (ActionCallMethod) m.rhs[0];
assertEquals("testFunction", actionCallMethod.getMethodName());
assertEquals("$t", actionCallMethod.getVariable());
assertEquals(2, actionCallMethod.getFieldValues().length);
assertEquals("testFunction", actionCallMethod.getFieldValue(0).getField());
assertEquals("123", actionCallMethod.getFieldValue(0).getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod.getFieldValue(0).getNature());
assertEquals("Integer", actionCallMethod.getFieldValue(0).getType());
assertEquals("testFunction", actionCallMethod.getFieldValue(1).getField());
assertEquals("hello", actionCallMethod.getFieldValue(1).getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod.getFieldValue(1).getNature());
assertEquals("String", actionCallMethod.getFieldValue(1).getType());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFunctionCalls.
@Test
public void testFunctionCalls() {
String drl = "package org.mortgages;\n" + "import java.lang.Number;\n" + "import java.lang.String;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + " s : String()\n" + "then\n" + " s.indexOf( s );\n" + " s.indexOf( 0 );\n" + "end\n";
Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
List<MethodInfo> mapMethodInformation = new ArrayList<>();
mapMethodInformation.add(new MethodInfo("indexOf", Collections.singletonList(DataType.TYPE_STRING), "int", null, DataType.TYPE_STRING));
mapMethodInformation.add(new MethodInfo("indexOf", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "int", null, DataType.TYPE_STRING));
methodInformation.put("java.lang.String", mapMethodInformation);
when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(2, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
assertTrue(m.rhs[1] instanceof ActionCallMethod);
ActionCallMethod actionCallMethod1 = (ActionCallMethod) m.rhs[0];
assertEquals(1, actionCallMethod1.getState());
assertEquals("indexOf", actionCallMethod1.getMethodName());
assertEquals("s", actionCallMethod1.getVariable());
assertEquals(1, actionCallMethod1.getFieldValues().length);
assertEquals("indexOf", actionCallMethod1.getFieldValues()[0].getField());
assertEquals("s", actionCallMethod1.getFieldValues()[0].getValue());
assertEquals(FieldNatureType.TYPE_VARIABLE, actionCallMethod1.getFieldValues()[0].getNature());
assertEquals("String", actionCallMethod1.getFieldValues()[0].getType());
ActionCallMethod actionCallMethod2 = (ActionCallMethod) m.rhs[1];
assertEquals(1, actionCallMethod2.getState());
assertEquals("indexOf", actionCallMethod2.getMethodName());
assertEquals("s", actionCallMethod2.getVariable());
assertEquals(1, actionCallMethod2.getFieldValues().length);
assertEquals("indexOf", actionCallMethod2.getFieldValues()[0].getField());
assertEquals("0", actionCallMethod2.getFieldValues()[0].getValue());
assertEquals(FieldNatureType.TYPE_LITERAL, actionCallMethod2.getFieldValues()[0].getNature());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, actionCallMethod2.getFieldValues()[0].getType());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCall.
@Test
public void testMethodCall() {
// BZ-1042511
String drl = "" + "package org.mortgages;\n" + "import org.mortgages.LoanApplication;\n" + "import java.util.Map;\n" + "rule \"my rule\"\n" + " dialect \"mvel\"\n" + " when\n" + " a : LoanApplication( )\n" + " m : Map()\n" + " then\n" + " m.put(\"key\", a );\n" + "end\n";
HashMap<String, String> globals = new HashMap<>();
when(dmo.getPackageGlobals()).thenReturn(globals);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertEquals(2, m.getImports().getImports().size());
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod mc = (ActionCallMethod) m.rhs[0];
assertEquals("put", mc.getMethodName());
assertEquals("m", mc.getVariable());
assertEquals(1, mc.getState());
assertEquals(2, mc.getFieldValues().length);
ActionFieldValue f1 = mc.getFieldValue(0);
assertEquals("key", f1.getValue());
ActionFieldValue f2 = mc.getFieldValue(1);
assertEquals("a", f2.getValue());
String marshalled = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
logger.debug(marshalled);
assertEqualsIgnoreWhitespace(drl, marshalled);
}
Aggregations