Search in sources :

Example 11 with ActionFieldValue

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSModifyBlockSingleFieldSingleLine.

@Test
public void testRHSModifyBlockSingleFieldSingleLine() throws Exception {
    // The value used in the "set" is intentionally yucky to catch extraction of the field's value errors!
    String drl = "rule \"modify1\"\n" + "  dialect \"mvel\"\n" + "  when\n" + "    $p : Person( )\n" + "  then\n" + "  modify( $p ) { setFirstName( \",)\" ) }\n" + "end";
    addModelField("Person", "firstName", "java.lang.String", DataType.TYPE_STRING);
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertEquals(1, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionUpdateField);
    ActionUpdateField field = (ActionUpdateField) m.rhs[0];
    assertEquals("$p", field.getVariable());
    assertNotNull(field.getFieldValues()[0]);
    assertEquals(1, field.getFieldValues().length);
    ActionFieldValue value = field.getFieldValues()[0];
    assertEquals("firstName", value.getField());
    assertEquals(",)", value.getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, value.getNature());
    assertEquals(DataType.TYPE_STRING, value.getType());
}
Also used : ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 12 with ActionFieldValue

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

the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallCheckParameterDataTypes1.

@Test
public void testMethodCallCheckParameterDataTypes1() {
    // BZ-1045423
    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";
    Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
    List<MethodInfo> mapMethodInformation = new ArrayList<>();
    mapMethodInformation.add(new MethodInfo("put", Arrays.asList("java.lang.Object", "java.lang.Object"), "void", "void", "java.util.Map"));
    methodInformation.put("java.util.Map", 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("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());
    assertEquals("java.lang.Object", f1.getType());
    assertEquals(FieldNatureType.TYPE_LITERAL, f1.getNature());
    ActionFieldValue f2 = mc.getFieldValue(1);
    assertEquals("a", f2.getValue());
    assertEquals("java.lang.Object", f2.getType());
    assertEquals(FieldNatureType.TYPE_VARIABLE, f2.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)

Example 13 with ActionFieldValue

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

the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallCheckParameterDataTypes2.

@Test
public void testMethodCallCheckParameterDataTypes2() {
    // BZ-1045423
    String drl = "" + "package org.mortgages;\n" + "import org.mortgages.MyType;\n" + "rule \"my rule\"\n" + "  dialect \"mvel\"\n" + "  when\n" + "    t : MyType( )\n" + "  then\n" + "    t.doSomething( 1 * 2 );\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("1 * 2", f1.getValue());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, f1.getType());
    assertEquals(FieldNatureType.TYPE_FORMULA, 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)

Example 14 with ActionFieldValue

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSInsertFactWithFieldAsVariableSamePackage.

@Test
public void testRHSInsertFactWithFieldAsVariableSamePackage() {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1077212
    String drl = "package org.test\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( $f : field1 == 44 )\n" + "then\n" + "Person fact0 = new Person();\n" + "fact0.setField1( $f );\n" + "insert( fact0 );\n" + "end";
    addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    IPattern p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    FactPattern fp = (FactPattern) p;
    assertEquals("Person", fp.getFactType());
    assertEquals(1, fp.getConstraintList().getConstraints().length);
    assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
    assertEquals("Person", sfp.getFactType());
    assertEquals("field1", sfp.getFieldName());
    assertEquals("==", sfp.getOperator());
    assertEquals("44", sfp.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
    assertEquals("$f", sfp.getFieldBinding());
    assertEquals(1, m.rhs.length);
    IAction a = m.rhs[0];
    assertTrue(a instanceof ActionInsertFact);
    ActionInsertFact ap = (ActionInsertFact) a;
    assertEquals("Person", ap.getFactType());
    assertEquals("fact0", ap.getBoundName());
    assertEquals(1, ap.getFieldValues().length);
    ActionFieldValue afv = ap.getFieldValues()[0];
    assertEquals("field1", afv.getField());
    assertEquals(FieldNatureType.TYPE_VARIABLE, afv.getNature());
    assertEquals("=$f", afv.getValue());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) IAction(org.drools.workbench.models.datamodel.rule.IAction) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) 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) Test(org.junit.Test)

Example 15 with ActionFieldValue

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

the class RuleModelDRLPersistenceImpl method buildFieldValue.

private ActionFieldValue buildFieldValue(final boolean isJavaDialect, String field, String value, final String dataType, final Map<String, String> boundParams) {
    if (value.contains("wiWorkItem.getResult")) {
        field = field.substring(0, 1).toUpperCase() + field.substring(1);
        String wiParam = field.substring("Results".length());
        if (wiParam.equals("BooleanResult")) {
            return new ActionWorkItemFieldValue(field, DataType.TYPE_BOOLEAN, "WorkItem", wiParam, Boolean.class.getName());
        } else if (wiParam.equals("StringResult")) {
            return new ActionWorkItemFieldValue(field, DataType.TYPE_STRING, "WorkItem", wiParam, String.class.getName());
        } else if (wiParam.equals("IntegerResult")) {
            return new ActionWorkItemFieldValue(field, DataType.TYPE_NUMERIC_INTEGER, "WorkItem", wiParam, Integer.class.getName());
        } else if (wiParam.equals("FloatResult")) {
            return new ActionWorkItemFieldValue(field, DataType.TYPE_NUMERIC_FLOAT, "WorkItem", wiParam, Float.class.getName());
        }
    }
    value = removeNumericSuffix(value, dataType);
    final int fieldNature = inferFieldNature(dataType, value, boundParams, isJavaDialect);
    // If the field is a formula don't adjust the param value
    String paramValue = value;
    switch(fieldNature) {
        case FieldNatureType.TYPE_FORMULA:
            break;
        case FieldNatureType.TYPE_VARIABLE:
            paramValue = "=" + paramValue;
            break;
        case FieldNatureType.TYPE_TEMPLATE:
            paramValue = unwrapTemplateKey(value);
            break;
        default:
            paramValue = adjustParam(dataType, value, boundParams, isJavaDialect);
    }
    ActionFieldValue fieldValue = new ActionFieldValue(field, paramValue, dataType);
    fieldValue.setNature(fieldNature);
    return fieldValue;
}
Also used : ActionWorkItemFieldValue(org.drools.workbench.models.datamodel.rule.ActionWorkItemFieldValue) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Aggregations

ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)136 Test (org.junit.Test)113 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)71 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)71 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)65 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)63 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)53 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)48 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)41 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)41 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)33 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)30 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)28 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)26 IAction (org.drools.workbench.models.datamodel.rule.IAction)25 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)24 ActionSetField (org.drools.workbench.models.datamodel.rule.ActionSetField)23 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)23 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)21 ArrayList (java.util.ArrayList)19