Search in sources :

Example 1 with ActionUpdateField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testNewKeywordVariableNamePrefix2.

@Test
public // https://issues.jboss.org/browse/GUVNOR-2143
void testNewKeywordVariableNamePrefix2() {
    String oldValue = System.getProperty("drools.dateformat");
    try {
        System.setProperty("drools.dateformat", "dd-MMM-yyyy");
        String drl = "package org.test;\n" + "rule \"rule1\"\n" + "  dialect \"java\"\n" + "  when\n" + "    $a : Applicant()\n" + "  then\n" + "    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + "    java.util.Date newStartDate = new java.util.Date();\n" + "    modify( $a ) {\n" + "      setApplicantDate( newStartDate )" + "    }\n" + "end\n";
        addModelField("org.test.Applicant", "this", "org.test.Applicant", DataType.TYPE_THIS);
        addModelField("org.test.Applicant", "applicantDate", Date.class.getName(), DataType.TYPE_DATE);
        when(dmo.getPackageName()).thenReturn("org.test");
        RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
        assertNotNull(m);
        assertEquals("rule1", m.name);
        assertEquals(1, m.lhs.length);
        IPattern p = m.lhs[0];
        assertTrue(p instanceof FactPattern);
        FactPattern fp = (FactPattern) p;
        assertEquals("Applicant", fp.getFactType());
        assertEquals("$a", fp.getBoundName());
        assertNull(fp.getConstraintList());
        assertEquals(2, m.rhs.length);
        assertTrue(m.rhs[0] instanceof FreeFormLine);
        FreeFormLine ffl = (FreeFormLine) m.rhs[0];
        assertEquals("java.util.Date newStartDate = new java.util.Date();", ffl.getText());
        assertTrue(m.rhs[1] instanceof ActionUpdateField);
        ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
        assertEquals("$a", auf.getVariable());
        assertEquals(1, auf.getFieldValues().length);
        ActionFieldValue afv = auf.getFieldValues()[0];
        assertEquals("applicantDate", afv.getField());
        assertEquals("newStartDate", afv.getValue());
        assertEquals(FieldNatureType.TYPE_FORMULA, afv.getNature());
        assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
    } finally {
        if (oldValue == null) {
            System.clearProperty("drools.dateformat");
        } else {
            System.setProperty("drools.dateformat", oldValue);
        }
    }
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) 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) Date(java.util.Date) Test(org.junit.Test)

Example 2 with ActionUpdateField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHS_DataTypeSuffixes.

@Test
public // See https://issues.jboss.org/browse/GUVNOR-2455
void testRHS_DataTypeSuffixes() throws Exception {
    String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"mvel\"\n" + "when\n" + "  $p : Person( )\n" + "then\n" + "  modify( $p ) { setDouble( 25.0d ), setFloat( 25.0f ), setLong( 25L ) }\n" + "end";
    addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
    addModelField("org.test.Person", "double", Double.class.getName(), DataType.TYPE_NUMERIC_DOUBLE);
    addModelField("org.test.Person", "float", Float.class.getName(), DataType.TYPE_NUMERIC_FLOAT);
    addModelField("org.test.Person", "long", Long.class.getName(), DataType.TYPE_NUMERIC_LONG);
    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("Person", fp0.getFactType());
    assertEquals(0, fp0.getNumberOfConstraints());
    assertEquals(1, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionUpdateField);
    ActionUpdateField auf = (ActionUpdateField) m.rhs[0];
    assertEquals("$p", auf.getVariable());
    assertEquals(3, auf.getFieldValues().length);
    ActionFieldValue afv0 = auf.getFieldValues()[0];
    assertEquals("double", afv0.getField());
    assertEquals("25.0", afv0.getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, afv0.getNature());
    ActionFieldValue afv1 = auf.getFieldValues()[1];
    assertEquals("float", afv1.getField());
    assertEquals("25.0", afv1.getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, afv1.getNature());
    ActionFieldValue afv2 = auf.getFieldValues()[2];
    assertEquals("long", afv2.getField());
    assertEquals("25", afv2.getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, afv2.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)

Example 3 with ActionUpdateField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSTemplateKeys.

@Test
public // https://issues.jboss.org/browse/GUVNOR-2030
void testRHSTemplateKeys() throws Exception {
    String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"mvel\"\n" + "when\n" + "  $p : Person( name == \"Fred\" )\n" + "then\n" + "  modify( $p ) { setName( \"@{k1}\" ) }\n" + "end";
    addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
    addModelField("org.test.Person", "name", String.class.getName(), 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("Person", fp0.getFactType());
    assertEquals(1, fp0.getNumberOfConstraints());
    assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraint);
    final SingleFieldConstraint sfc1 = (SingleFieldConstraint) fp0.getConstraint(0);
    assertEquals("Person", sfc1.getFactType());
    assertEquals("name", sfc1.getFieldName());
    assertEquals(DataType.TYPE_STRING, sfc1.getFieldType());
    assertEquals(SingleFieldConstraint.TYPE_LITERAL, sfc1.getConstraintValueType());
    assertEquals("Fred", sfc1.getValue());
    assertEquals(1, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionUpdateField);
    ActionUpdateField auf = (ActionUpdateField) m.rhs[0];
    assertEquals("$p", auf.getVariable());
    assertEquals(1, auf.getFieldValues().length);
    ActionFieldValue afv = auf.getFieldValues()[0];
    assertEquals("name", afv.getField());
    assertEquals("k1", afv.getValue());
    assertEquals(FieldNatureType.TYPE_TEMPLATE, afv.getNature());
}
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) 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)

Example 4 with ActionUpdateField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testNewKeywordVariableNamePrefix3.

@Test
public // https://issues.jboss.org/browse/GUVNOR-2143
void testNewKeywordVariableNamePrefix3() {
    String oldValue = System.getProperty("drools.dateformat");
    try {
        System.setProperty("drools.dateformat", "dd-MMM-yyyy");
        String drl = "package org.test;\n" + "rule \"rule1\"\n" + "  dialect \"java\"\n" + "  when\n" + "    $a : Applicant()\n" + "  then\n" + "    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + "    java.util.Date newStartDate = new java.util.Date();\n" + "    modify( $a ) \n" + "    { setApplicantDate( newStartDate ) }\n" + "end\n";
        addModelField("org.test.Applicant", "this", "org.test.Applicant", DataType.TYPE_THIS);
        addModelField("org.test.Applicant", "applicantDate", Date.class.getName(), DataType.TYPE_DATE);
        when(dmo.getPackageName()).thenReturn("org.test");
        RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
        assertNotNull(m);
        assertEquals("rule1", m.name);
        assertEquals(1, m.lhs.length);
        IPattern p = m.lhs[0];
        assertTrue(p instanceof FactPattern);
        FactPattern fp = (FactPattern) p;
        assertEquals("Applicant", fp.getFactType());
        assertEquals("$a", fp.getBoundName());
        assertNull(fp.getConstraintList());
        assertEquals(2, m.rhs.length);
        assertTrue(m.rhs[0] instanceof FreeFormLine);
        FreeFormLine ffl = (FreeFormLine) m.rhs[0];
        assertEquals("java.util.Date newStartDate = new java.util.Date();", ffl.getText());
        assertTrue(m.rhs[1] instanceof ActionUpdateField);
        ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
        assertEquals("$a", auf.getVariable());
        assertEquals(1, auf.getFieldValues().length);
        ActionFieldValue afv = auf.getFieldValues()[0];
        assertEquals("applicantDate", afv.getField());
        assertEquals("newStartDate", afv.getValue());
        assertEquals(FieldNatureType.TYPE_FORMULA, afv.getNature());
        assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
    } finally {
        if (oldValue == null) {
            System.clearProperty("drools.dateformat");
        } else {
            System.setProperty("drools.dateformat", oldValue);
        }
    }
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) 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) Date(java.util.Date) Test(org.junit.Test)

Example 5 with ActionUpdateField

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

the class RuleModelDRLPersistenceUnmarshallingTest method testNewKeywordVariableNamePrefix1.

@Test
public // https://issues.jboss.org/browse/GUVNOR-2143
void testNewKeywordVariableNamePrefix1() {
    String oldValue = System.getProperty("drools.dateformat");
    try {
        System.setProperty("drools.dateformat", "dd-MMM-yyyy");
        String drl = "package org.test;\n" + "rule \"rule1\"\n" + "  dialect \"java\"\n" + "  when\n" + "    $bundle : Bundle( $treatmentEffectiveDt : treatmentEffectiveDt )\n" + "  then\n" + "    java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + "    DateTime newStartDate = new DateTime();\n" + "    modify( $bundle ) {\n" + "      setTreatmentEffectiveDt( newStartDate.toDate() )" + "    }\n" + "end\n";
        addModelField("org.test.Bundle", "this", "org.test.Bundle", DataType.TYPE_THIS);
        addModelField("org.test.Bundle", "treatmentEffectiveDt", Date.class.getName(), DataType.TYPE_DATE);
        when(dmo.getPackageName()).thenReturn("org.test");
        RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
        assertNotNull(m);
        assertEquals("rule1", m.name);
        assertEquals(1, m.lhs.length);
        IPattern p = m.lhs[0];
        assertTrue(p instanceof FactPattern);
        FactPattern fp = (FactPattern) p;
        assertEquals("Bundle", fp.getFactType());
        assertEquals("$bundle", fp.getBoundName());
        assertEquals(1, fp.getConstraintList().getConstraints().length);
        assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
        SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
        assertEquals("Bundle", sfp.getFactType());
        assertEquals("treatmentEffectiveDt", sfp.getFieldName());
        assertEquals("$treatmentEffectiveDt", sfp.getFieldBinding());
        assertNull(sfp.getOperator());
        assertNull(sfp.getValue());
        assertEquals(BaseSingleFieldConstraint.TYPE_UNDEFINED, sfp.getConstraintValueType());
        assertEquals(2, m.rhs.length);
        assertTrue(m.rhs[0] instanceof FreeFormLine);
        FreeFormLine ffl = (FreeFormLine) m.rhs[0];
        assertEquals("DateTime newStartDate = new DateTime();", ffl.getText());
        assertTrue(m.rhs[1] instanceof ActionUpdateField);
        ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
        assertEquals("$bundle", auf.getVariable());
        assertEquals(1, auf.getFieldValues().length);
        ActionFieldValue afv = auf.getFieldValues()[0];
        assertEquals("treatmentEffectiveDt", afv.getField());
        assertEquals("newStartDate.toDate()", afv.getValue());
        assertEquals(FieldNatureType.TYPE_FORMULA, afv.getNature());
        assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
    } finally {
        if (oldValue == null) {
            System.clearProperty("drools.dateformat");
        } else {
            System.setProperty("drools.dateformat", oldValue);
        }
    }
}
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) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) 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) Date(java.util.Date) 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