Search in sources :

Example 41 with RuleModel

use of org.drools.workbench.models.datamodel.rule.RuleModel 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)

Example 42 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSChainedMethodCalls4.

@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1127303
void testRHSChainedMethodCalls4() throws Exception {
    String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"mvel\"\n" + "when\n" + "  Person( $n : name )\n" + "then\n" + "  $n.toUpperCase().indexOf(\"\\\").\", 1);\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(1, m.rhs.length);
    final IAction a0 = m.rhs[0];
    assertTrue(a0 instanceof FreeFormLine);
    final FreeFormLine ffl1 = (FreeFormLine) a0;
    assertEquals("$n.toUpperCase().indexOf(\"\\\").\", 1);", ffl1.getText());
    // Check round-trip
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
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) IAction(org.drools.workbench.models.datamodel.rule.IAction) 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 43 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSChainedMethodCalls1.

@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1127303
void testRHSChainedMethodCalls1() throws Exception {
    String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"mvel\"\n" + "when\n" + "  Person( $n : name )\n" + "then\n" + "  $n.toUpperCase().indexOf(\"S\", 1);\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(1, m.rhs.length);
    final IAction a0 = m.rhs[0];
    assertTrue(a0 instanceof FreeFormLine);
    final FreeFormLine ffl1 = (FreeFormLine) a0;
    assertEquals("$n.toUpperCase().indexOf(\"S\", 1);", ffl1.getText());
    // Check round-trip
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
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) IAction(org.drools.workbench.models.datamodel.rule.IAction) 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 44 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSInOperatorFieldNameContainingInLiteral.

@Test
public void testLHSInOperatorFieldNameContainingInLiteral() {
    String drl = "package org.test\n" + "rule \"in\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( rating in (1, 2) )\n" + "then\n" + "end";
    addModelField("org.test.Person", "rating", "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("rating", sfp.getFieldName());
    assertEquals("in", sfp.getOperator());
    assertEquals("1, 2", sfp.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
}
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) 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 45 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testForAll.

@Test
public void testForAll() throws Exception {
    // RHBPMS-4666
    String drl = "package org.test;\n" + "import java.util.List;\n" + "rule \"MyRule\" dialect \"mvel\" when\n" + "  $myList : List( empty == false )\n" + "  forall( String( this.startsWith( \"n\" ) ) from $myList )\n" + "then\n" + "end";
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    // Check round-trip
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Also used : RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Aggregations

RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)384 Test (org.junit.Test)352 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)223 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)216 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)215 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)201 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)201 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)145 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)134 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)91 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)85 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)63 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)47 ArrayList (java.util.ArrayList)44 IAction (org.drools.workbench.models.datamodel.rule.IAction)31 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)29 SingleFieldConstraintEBLeftSide (org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide)29 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)27 RuleAttribute (org.drools.workbench.models.datamodel.rule.RuleAttribute)25 List (java.util.List)24