Search in sources :

Example 31 with SingleFieldConstraint

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

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

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

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

Example 35 with SingleFieldConstraint

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

the class RuleModelDRLPersistenceUnmarshallingTest method testFullyQualifiedClassNameEnumeration.

@Test
public void testFullyQualifiedClassNameEnumeration() throws Exception {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1047879
    String drl = "import org.drools.workbench.models.commons.backend.rule.TestEnum;\n" + "import org.drools.workbench.models.commons.backend.rule.OuterClassWithEnums;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "OuterClassWithEnums( outerField == TestEnum.VALUE1 )\n" + "then\n" + "end";
    addModelField("org.drools.workbench.models.commons.backend.rule.OuterClassWithEnums", "outerField", TestEnum.class.getSimpleName(), DataType.TYPE_COMPARABLE);
    addJavaEnumDefinition("org.drools.workbench.models.commons.backend.rule.OuterClassWithEnums", "outerField", new String[] { "TestEnum.VALUE1=TestEnum.VALUE1", "TestEnum.VALUE2=TestEnum.VALUE2" });
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    FactPattern pattern = (FactPattern) m.lhs[0];
    assertEquals(1, pattern.getNumberOfConstraints());
    assertTrue(pattern.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint constraint = (SingleFieldConstraint) pattern.getConstraint(0);
    assertEquals("OuterClassWithEnums", constraint.getFactType());
    assertEquals("outerField", constraint.getFieldName());
    assertEquals(DataType.TYPE_COMPARABLE, constraint.getFieldType());
    assertEquals("==", constraint.getOperator());
    assertEquals("TestEnum.VALUE1", constraint.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_ENUM, constraint.getConstraintValueType());
    final String drl2 = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
    assertEqualsIgnoreWhitespace(drl, drl2);
}
Also used : 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)

Aggregations

SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)278 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)250 Test (org.junit.Test)242 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)236 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)218 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)199 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)148 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)142 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)138 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)73 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)65 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)64 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)62 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)28 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)27 BRLConditionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn)25 ConnectiveConstraint (org.drools.workbench.models.datamodel.rule.ConnectiveConstraint)23 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)23 BRLConditionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn)21 ArrayList (java.util.ArrayList)20