Search in sources :

Example 1 with MethodInfo

use of org.kie.soup.project.datamodel.oracle.MethodInfo 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));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) List(java.util.List) ArrayList(java.util.ArrayList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 2 with MethodInfo

use of org.kie.soup.project.datamodel.oracle.MethodInfo 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));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) List(java.util.List) ArrayList(java.util.ArrayList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 3 with MethodInfo

use of org.kie.soup.project.datamodel.oracle.MethodInfo 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));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) List(java.util.List) ArrayList(java.util.ArrayList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 4 with MethodInfo

use of org.kie.soup.project.datamodel.oracle.MethodInfo 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));
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) List(java.util.List) ArrayList(java.util.ArrayList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 5 with MethodInfo

use of org.kie.soup.project.datamodel.oracle.MethodInfo in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallCheckParameterDataTypes3.

@Test
public void testMethodCallCheckParameterDataTypes3() {
    // BZ-1045423
    String drl = "" + "package org.mortgages;\n" + "import org.mortgages.MyType;\n" + "rule \"my rule\"\n" + "  dialect \"mvel\"\n" + "  when\n" + "    i : Integer( )\n" + "    t : MyType( )\n" + "  then\n" + "    t.doSomething( i );\n" + "end\n";
    Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
    List<MethodInfo> mapMethodInformation = new ArrayList<>();
    mapMethodInformation.add(new MethodInfo("doSomething", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "void", "void", "org.mortgages.MyType"));
    methodInformation.put("org.mortgages.MyType", mapMethodInformation);
    when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertTrue(m.rhs[0] instanceof ActionCallMethod);
    ActionCallMethod mc = (ActionCallMethod) m.rhs[0];
    assertEquals("doSomething", mc.getMethodName());
    assertEquals("t", mc.getVariable());
    assertEquals(1, mc.getState());
    assertEquals(1, mc.getFieldValues().length);
    ActionFieldValue f1 = mc.getFieldValue(0);
    assertEquals("i", f1.getValue());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, f1.getType());
    assertEquals(FieldNatureType.TYPE_VARIABLE, f1.getNature());
    String marshalled = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
    logger.debug(marshalled);
    assertEqualsIgnoreWhitespace(drl, marshalled);
}
Also used : HashMap(java.util.HashMap) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ArrayList(java.util.ArrayList) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) List(java.util.List) ArrayList(java.util.ArrayList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Aggregations

MethodInfo (org.kie.soup.project.datamodel.oracle.MethodInfo)16 ArrayList (java.util.ArrayList)12 List (java.util.List)12 HashMap (java.util.HashMap)11 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)11 Test (org.junit.Test)11 ActionCallMethod (org.drools.workbench.models.datamodel.rule.ActionCallMethod)9 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)3 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)3 ExpressionMethod (org.drools.workbench.models.datamodel.rule.ExpressionMethod)3 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)3 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)3 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)3 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)3 ExpressionField (org.drools.workbench.models.datamodel.rule.ExpressionField)2 ExpressionUnboundFact (org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact)2 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)2 SingleFieldConstraintEBLeftSide (org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide)2 AccumulateDescr (org.drools.compiler.lang.descr.AccumulateDescr)1 CollectDescr (org.drools.compiler.lang.descr.CollectDescr)1