Search in sources :

Example 1 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testBoundListConstraint.

@Test
public void testBoundListConstraint() throws Exception {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1264339
    String drl = "package org.test;\n" + "rule \"List_Of_Values_Issue\"\n" + "dialect \"mvel\"\n" + "when\n" + "  MyTransactionVO( $myData : myData )\n" + "  MyDataList( myDataList contains $myData )\n" + "     then\n" + "end";
    addModelField("org.test.MyTransactionVO", "this", "org.test.MyTransactionVO", DataType.TYPE_THIS);
    addModelField("org.test.MyTransactionVO", "myData", "org.test.MyDataList", "org.test.MyDataList");
    addModelField("org.test.MyDataList", "this", "org.test.MyDataList", DataType.TYPE_THIS);
    addModelField("org.test.MyDataList", "myDataList", List.class.getName(), DataType.TYPE_COLLECTION);
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(2, m.lhs.length);
    final IPattern p0 = m.lhs[0];
    assertTrue(p0 instanceof FactPattern);
    final FactPattern fp0 = (FactPattern) p0;
    assertEquals("MyTransactionVO", fp0.getFactType());
    assertEquals(1, fp0.getNumberOfConstraints());
    final IPattern p1 = m.lhs[1];
    assertTrue(p1 instanceof FactPattern);
    final FactPattern fp1 = (FactPattern) p1;
    assertEquals("MyDataList", fp1.getFactType());
    assertEquals(1, fp1.getNumberOfConstraints());
    assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraint);
    final SingleFieldConstraint fp0sfc0 = (SingleFieldConstraint) fp0.getConstraint(0);
    assertEquals("MyTransactionVO", fp0sfc0.getFactType());
    assertEquals("myData", fp0sfc0.getFieldName());
    assertEquals("$myData", fp0sfc0.getFieldBinding());
    assertEquals("MyDataList", fp0sfc0.getFieldType());
    assertNull(fp0sfc0.getOperator());
    assertNull(fp0sfc0.getValue());
    assertTrue(fp1.getConstraint(0) instanceof SingleFieldConstraint);
    final SingleFieldConstraint fp1sfc0 = (SingleFieldConstraint) fp1.getConstraint(0);
    assertEquals("MyDataList", fp1sfc0.getFactType());
    assertEquals("myDataList", fp1sfc0.getFieldName());
    assertEquals("contains", fp1sfc0.getOperator());
    assertEquals("$myData", fp1sfc0.getValue());
    assertEquals(SingleFieldConstraint.TYPE_VARIABLE, fp1sfc0.getConstraintValueType());
    assertEquals(0, m.rhs.length);
    // 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) 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) List(java.util.List) ArrayList(java.util.ArrayList) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 2 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintCEPOperatorTimeWindow.

@Test
public void testSingleFieldConstraintCEPOperatorTimeWindow() {
    String drl = "rule \"rule1\"\n" + "when\n" + "Event() over window:time (1d)\n" + "then\n" + "end";
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals("rule1", m.name);
    assertEquals(1, m.lhs.length);
    IPattern p1 = m.lhs[0];
    assertTrue(p1 instanceof FactPattern);
    FactPattern fp1 = (FactPattern) p1;
    assertEquals("Event", fp1.getFactType());
    assertNull(fp1.getBoundName());
    assertEquals(0, fp1.getNumberOfConstraints());
    assertNotNull(fp1.getWindow());
    CEPWindow window = fp1.getWindow();
    assertEquals("over window:time", window.getOperator());
    assertEquals(2, window.getParameters().size());
    assertEquals("1d", window.getParameter("1"));
    assertEquals("org.drools.workbench.models.commons.backend.rule.CEPWindowOperatorParameterDRLBuilder", window.getParameter("org.drools.workbench.models.commons.backend.rule.operatorParameterGenerator"));
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) CEPWindow(org.drools.workbench.models.datamodel.rule.CEPWindow) 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 IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testFactPatternWithBinding.

@Test
public void testFactPatternWithBinding() {
    String drl = "rule \"rule1\"\n" + "when\n" + "$a : Applicant()\n" + "then\n" + "end";
    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());
    assertEquals(0, fp.getNumberOfConstraints());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) 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 IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintContainsOperator.

@Test
public void testSingleFieldConstraintContainsOperator() {
    String drl = "rule \"rule1\"\n" + "when\n" + "$is : IncomeSource( )\n" + "Applicant( incomes contains $is )\n" + "then\n" + "end";
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals("rule1", m.name);
    assertEquals(2, m.lhs.length);
    IPattern p0 = m.lhs[0];
    assertTrue(p0 instanceof FactPattern);
    FactPattern fp0 = (FactPattern) p0;
    assertEquals("IncomeSource", fp0.getFactType());
    assertEquals("$is", fp0.getBoundName());
    IPattern p1 = m.lhs[1];
    assertTrue(p1 instanceof FactPattern);
    FactPattern fp1 = (FactPattern) p1;
    assertEquals("Applicant", fp1.getFactType());
    assertEquals(0, fp0.getNumberOfConstraints());
    assertEquals(1, fp1.getNumberOfConstraints());
    assertTrue(fp1.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint sfp = (SingleFieldConstraint) fp1.getConstraint(0);
    assertEquals("Applicant", sfp.getFactType());
    assertEquals("incomes", sfp.getFieldName());
    assertEquals("contains", sfp.getOperator());
    assertEquals("$is", sfp.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_VARIABLE, 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 5 with IPattern

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

Aggregations

IPattern (org.drools.workbench.models.datamodel.rule.IPattern)122 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)98 Test (org.junit.Test)92 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)84 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)84 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)75 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)75 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)75 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)72 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)58 IAction (org.drools.workbench.models.datamodel.rule.IAction)34 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)30 BRLConditionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn)26 ArrayList (java.util.ArrayList)24 BRLConditionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn)23 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)21 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)16 DescriptionCol52 (org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52)16 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)16 RowNumberCol52 (org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52)16