Search in sources :

Example 51 with ActionUpdateField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSModifyBlockSingleFieldMultipleLines.

@Test
public void testRHSModifyBlockSingleFieldMultipleLines() 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 ) {\n" + "    setFirstName( \",)\" )\n" + "  }\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 52 with ActionUpdateField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSModifyBlockMultipleFieldsMultipleLines.

@Test
public void testRHSModifyBlockMultipleFieldsMultipleLines() 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 ) { \n" + "    setFirstName( \",)\" ), \n" + "    setLastName( \",)\" )\n" + "  }\n" + "end";
    addModelField("Person", "firstName", "java.lang.String", DataType.TYPE_STRING);
    addModelField("Person", "lastName", "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(2, field.getFieldValues().length);
    ActionFieldValue value1 = field.getFieldValues()[0];
    assertEquals("firstName", value1.getField());
    assertEquals(",)", value1.getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, value1.getNature());
    assertEquals(DataType.TYPE_STRING, value1.getType());
    ActionFieldValue value2 = field.getFieldValues()[1];
    assertEquals("lastName", value2.getField());
    assertEquals(",)", value2.getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, value2.getNature());
    assertEquals(DataType.TYPE_STRING, value2.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 53 with ActionUpdateField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSUpdateFactWithFieldAsLiteral.

@Test
public void testRHSUpdateFactWithFieldAsLiteral() {
    String drl = "package org.mortgages\n" + "import org.test.Person\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "$p : Person()\n" + "then\n" + "$p.setField1( 55 );\n" + "update( $p );\n" + "end";
    addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
    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(0, fp.getNumberOfConstraints());
    assertEquals(1, m.rhs.length);
    IAction a = m.rhs[0];
    assertTrue(a instanceof ActionUpdateField);
    ActionUpdateField ap = (ActionUpdateField) a;
    assertEquals("$p", ap.getVariable());
    assertEquals(1, ap.getFieldValues().length);
    ActionFieldValue afv = ap.getFieldValues()[0];
    assertEquals("field1", afv.getField());
    assertEquals(FieldNatureType.TYPE_LITERAL, afv.getNature());
    assertEquals("55", afv.getValue());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) IAction(org.drools.workbench.models.datamodel.rule.IAction) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) 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 54 with ActionUpdateField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSUpdateFactWithFormula.

@Test
public void testRHSUpdateFactWithFormula() {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1079253
    String drl = "package org.mortgages;\n" + "import org.test.ShoppingCart\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$sc : ShoppingCart( )\n" + "then\n" + "$sc.setCartItemPromoSavings( ($sc.cartItemPromoSavings == 0.0) ? 0.0 : $sc.cartItemPromoSavings * -1 );\n" + "update( $sc );\n" + "end\n";
    addModelField("org.test.ShoppingCart", "cartItemPromoSavings", "java.lang.Double", DataType.TYPE_NUMERIC_DOUBLE);
    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("ShoppingCart", fp.getFactType());
    assertEquals("$sc", fp.getBoundName());
    assertEquals(0, fp.getNumberOfConstraints());
    assertEquals(1, m.rhs.length);
    IAction a = m.rhs[0];
    assertTrue(a instanceof ActionUpdateField);
    ActionUpdateField ap = (ActionUpdateField) a;
    assertEquals("$sc", ap.getVariable());
    assertEquals(1, ap.getFieldValues().length);
    ActionFieldValue afv = ap.getFieldValues()[0];
    assertEquals("cartItemPromoSavings", afv.getField());
    assertEquals("($sc.cartItemPromoSavings == 0.0) ? 0.0 : $sc.cartItemPromoSavings * -1", afv.getValue());
    assertEquals(FieldNatureType.TYPE_FORMULA, afv.getNature());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) IAction(org.drools.workbench.models.datamodel.rule.IAction) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) 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 55 with ActionUpdateField

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

the class RuleModelDRLPersistenceUnmarshallingTest method actionUpdateFieldWithFormulaWithEscapedQuote.

@Test
public void actionUpdateFieldWithFormulaWithEscapedQuote() throws Exception {
    String drl = "package org.mortgages;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Application()\n" + "then\n" + "modify( $a ) {\n" + "  setName( \"Pupa \\\"\" + 20 + \"\\\" Smurf\" )" + "}\n" + "end";
    addModelField("org.mortgages.Applicant", "this", "org.mortgages.Applicant", DataType.TYPE_THIS);
    addModelField("org.mortgages.Applicant", "name", String.class.getName(), DataType.TYPE_STRING);
    when(dmo.getPackageName()).thenReturn("org.mortgages");
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals("r1", m.name);
    // LHS Pattern
    assertEquals(1, m.lhs.length);
    IPattern p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    FactPattern fp = (FactPattern) p;
    assertEquals("Application", fp.getFactType());
    assertEquals(0, fp.getNumberOfConstraints());
    assertEquals(1, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionUpdateField);
    ActionUpdateField auf = (ActionUpdateField) m.rhs[0];
    assertEquals("$a", auf.getVariable());
    assertEquals(1, auf.getFieldValues().length);
    ActionFieldValue afv0 = auf.getFieldValues()[0];
    assertEquals("name", afv0.getField());
    assertEquals("\"Pupa \\\"\" + 20 + \"\\\" Smurf\"", afv0.getValue());
    assertEquals(FieldNatureType.TYPE_FORMULA, afv0.getNature());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) 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)

Aggregations

ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)58 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)53 Test (org.junit.Test)47 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)37 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)37 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)36 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)27 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)22 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)22 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)16 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)16 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)11 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)11 ActionSetField (org.drools.workbench.models.datamodel.rule.ActionSetField)10 IAction (org.drools.workbench.models.datamodel.rule.IAction)8 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)8 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)8 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)8 Pattern52 (org.drools.workbench.models.guided.dtable.shared.model.Pattern52)7 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)6