Search in sources :

Example 1 with FreeFormLine

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

the class RuleTemplateModelDRLPersistenceTest method testActionFreeFormLineFirstValue.

@Test
public void testActionFreeFormLineFirstValue() {
    TemplateModel m = new TemplateModel();
    m.name = "r1";
    FreeFormLine ffl = new FreeFormLine();
    ffl.setText("System.println( \"@{f1}\" + \"@{f2}\" );");
    m.addRhsItem(ffl);
    String expected = "rule \"r1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "then\n" + "end";
    m.addRow(new String[] { "foo", null });
    checkMarshall(expected, m);
}
Also used : FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) TemplateModel(org.drools.workbench.models.guided.template.shared.TemplateModel) Test(org.junit.Test)

Example 2 with FreeFormLine

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

the class RuleTemplateModelDRLPersistenceTest method testFromCollectFreeFormLineBothValues.

@Test
public void testFromCollectFreeFormLineBothValues() {
    TemplateModel m = new TemplateModel();
    m.name = "r1";
    FreeFormLine ffl = new FreeFormLine();
    ffl.setText("Person( field1 == \"@{f1}\", field2 == \"@{f2}\" )");
    FactPattern fp = new FactPattern("java.util.List");
    FromCollectCompositeFactPattern fac = new FromCollectCompositeFactPattern();
    fac.setRightPattern(ffl);
    fac.setFactPattern(fp);
    m.addLhsItem(fac);
    String expected = "rule \"r1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "java.util.List() from collect ( Person( field1 == \"foo\", field2 == \"bar\" ) ) \n" + "then\n" + "end";
    m.addRow(new String[] { "foo", "bar" });
    checkMarshall(expected, m);
}
Also used : FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) TemplateModel(org.drools.workbench.models.guided.template.shared.TemplateModel) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) Test(org.junit.Test)

Example 3 with FreeFormLine

use of org.drools.workbench.models.datamodel.rule.FreeFormLine 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 4 with FreeFormLine

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

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSFormula.

@Test
public // https://issues.jboss.org/browse/DROOLS-713
void testLHSFormula() throws Exception {
    String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"java\"\n" + "agenda-group \"MyGroup\"\n" + "salience 900\n" + "when\n" + "  $bundle : MyClass( $protocolSequence : protocolSequence )\n" + "  eval( $protocolSequence != null )\n" + "  $followupBundle : MyClass( protocolSequence == ( $protocolSequence + 1 ) )\n" + "then\n" + "end";
    addModelField("org.test.MyClass", "this", "org.test.MyClass", DataType.TYPE_THIS);
    addModelField("org.test.MyClass", "protocolSequence", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(3, m.lhs.length);
    final IPattern p0 = m.lhs[0];
    assertTrue(p0 instanceof FactPattern);
    final FactPattern fp0 = (FactPattern) p0;
    assertEquals("$bundle", fp0.getBoundName());
    assertEquals("MyClass", fp0.getFactType());
    final IPattern p1 = m.lhs[1];
    assertTrue(p1 instanceof FreeFormLine);
    final FreeFormLine ffl1 = (FreeFormLine) p1;
    assertEquals("eval( $protocolSequence != null )", ffl1.getText());
    final IPattern p2 = m.lhs[2];
    assertTrue(p2 instanceof FactPattern);
    final FactPattern fp2 = (FactPattern) p2;
    assertEquals("$followupBundle", fp2.getBoundName());
    assertEquals("MyClass", fp2.getFactType());
    assertEquals(1, fp2.getNumberOfConstraints());
    assertTrue(fp2.getConstraint(0) instanceof SingleFieldConstraint);
    final SingleFieldConstraint sfc1 = (SingleFieldConstraint) fp2.getConstraint(0);
    assertEquals("MyClass", sfc1.getFactType());
    assertEquals("protocolSequence", sfc1.getFieldName());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, sfc1.getFieldType());
    assertEquals("==", sfc1.getOperator());
    assertEquals("$protocolSequence + 1", sfc1.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_RET_VALUE, sfc1.getConstraintValueType());
    // 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) 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 5 with FreeFormLine

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSChainedMethodCalls2.

@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1127303
void testRHSChainedMethodCalls2() 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)

Aggregations

FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)64 Test (org.junit.Test)49 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)29 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)24 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)22 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)22 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)18 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)18 IAction (org.drools.workbench.models.datamodel.rule.IAction)17 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)14 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)14 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)12 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)11 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)11 ArrayList (java.util.ArrayList)10 BRLConditionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn)10 DescriptionCol52 (org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52)10 RowNumberCol52 (org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52)10 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)9 BRLConditionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn)9