Search in sources :

Example 26 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method compositeFieldConstraintWithOnePredicateAndOneLiteral.

@Test
public void compositeFieldConstraintWithOnePredicateAndOneLiteral() {
    String drl = "rule \"rule1\"\n" + "when\n" + "Person( eval( age > 18 ) && age < 45 )\n" + "then\n" + "end";
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals("rule1", m.name);
    // LHS Pattern
    assertEquals(1, m.lhs.length);
    IPattern p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    FactPattern fp = (FactPattern) p;
    // LHS sub-patterns
    assertEquals(1, fp.getNumberOfConstraints());
    assertTrue(fp.getConstraint(0) instanceof CompositeFieldConstraint);
    CompositeFieldConstraint fp_cfp = (CompositeFieldConstraint) fp.getConstraint(0);
    assertEquals("&&", fp_cfp.getCompositeJunctionType());
    assertEquals(2, fp_cfp.getNumberOfConstraints());
    assertTrue(fp_cfp.getConstraint(0) instanceof SingleFieldConstraint);
    assertTrue(fp_cfp.getConstraint(1) instanceof SingleFieldConstraint);
    SingleFieldConstraint fp_cfp_sfp1 = (SingleFieldConstraint) fp_cfp.getConstraint(0);
    assertEquals("age > 18", fp_cfp_sfp1.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, fp_cfp_sfp1.getConstraintValueType());
    SingleFieldConstraint fp_cfp_sfp2 = (SingleFieldConstraint) fp_cfp.getConstraint(1);
    assertEquals("Person", fp_cfp_sfp2.getFactType());
    assertEquals("age", fp_cfp_sfp2.getFieldName());
    assertEquals("<", fp_cfp_sfp2.getOperator());
    assertEquals("45", fp_cfp_sfp2.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, fp_cfp_sfp2.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) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) Test(org.junit.Test)

Example 27 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testFromBoundVariable.

@Test
public void testFromBoundVariable() {
    String drl = "import java.lang.Number;\n" + "import org.drools.workbench.models.commons.backend.rule.Counter;\n" + "rule \"rule1\"\n" + "when\n" + "cc : Counter()\n" + "Number() from cc.number\n" + "then\n" + "end";
    addModelField("org.drools.workbench.models.commons.backend.rule.Counter", "number", "java.lang.Number", DataType.TYPE_NUMERIC);
    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("Counter", fp0.getFactType());
    assertEquals("cc", fp0.getBoundName());
    assertEquals(0, fp0.getNumberOfConstraints());
    IPattern p1 = m.lhs[1];
    assertTrue(p1 instanceof FromCompositeFactPattern);
    FromCompositeFactPattern fcfp1 = (FromCompositeFactPattern) p1;
    FactPattern fp1 = fcfp1.getFactPattern();
    ExpressionFormLine efl1 = fcfp1.getExpression();
    assertNotNull(fp1);
    assertNotNull(efl1);
    assertEquals("Number", fp1.getFactType());
    assertEquals(0, fp1.getNumberOfConstraints());
    assertEquals(2, efl1.getParts().size());
    assertTrue(efl1.getParts().get(0) instanceof ExpressionVariable);
    assertTrue(efl1.getParts().get(1) instanceof ExpressionField);
    ExpressionVariable eflv1 = (ExpressionVariable) efl1.getParts().get(0);
    assertEquals("cc", eflv1.getName());
    assertEquals("Counter", eflv1.getClassType());
    assertEquals(DataType.TYPE_NUMERIC, eflv1.getGenericType());
    ExpressionField eflf1 = (ExpressionField) efl1.getParts().get(1);
    assertEquals("number", eflf1.getName());
    assertEquals("java.lang.Number", eflf1.getClassType());
    assertEquals(DataType.TYPE_NUMERIC, eflf1.getGenericType());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) ExpressionFormLine(org.drools.workbench.models.datamodel.rule.ExpressionFormLine) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) 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) ExpressionVariable(org.drools.workbench.models.datamodel.rule.ExpressionVariable) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 28 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testBigDecimal2.

@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1189930
void testBigDecimal2() throws Exception {
    String drl = "package org.test;\n" + "import java.lang.Number;\n" + "import java.math.BigDecimal;\n" + "import java.util.Calendar;\n" + "rule \"BigDecimalRule\"\n" + "  dialect \"java\"\n" + "when\n" + "  $bd : BigDecimal( )\n" + "then\n" + "  LastRunInformation lastRun = new LastRunInformation();\n" + "  lastRun.setLastNumber($bd);\n" + "  insert(lastRun);\n" + "end";
    addModelField("org.test.LastRunInformation", "this", "org.test.LastRunInformation", DataType.TYPE_THIS);
    addModelField("org.test.LastRunInformation", "lastNumber", BigDecimal.class.getName(), DataType.TYPE_NUMERIC_BIGDECIMAL);
    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 p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    final FactPattern fp = (FactPattern) p;
    assertEquals("$bd", fp.getBoundName());
    assertEquals("BigDecimal", fp.getFactType());
    assertEquals(1, m.rhs.length);
    final IAction a = m.rhs[0];
    assertTrue(a instanceof ActionInsertFact);
    final ActionInsertFact aif = (ActionInsertFact) a;
    assertEquals("lastRun", aif.getBoundName());
    assertEquals("LastRunInformation", aif.getFactType());
    assertEquals(1, aif.getFieldValues().length);
    final ActionFieldValue afv0 = aif.getFieldValues()[0];
    assertEquals("lastNumber", afv0.getField());
    assertEquals(FieldNatureType.TYPE_VARIABLE, afv0.getNature());
    assertEquals("=$bd", afv0.getValue());
    // Check round-trip
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) IAction(org.drools.workbench.models.datamodel.rule.IAction) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) 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) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 29 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintWithBinding.

@Test
public void testSingleFieldConstraintWithBinding() {
    String drl = "rule \"rule1\"\n" + "when\n" + "Applicant( $a : age < 55 )\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(1, fp.getConstraintList().getConstraints().length);
    assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
    assertEquals("Applicant", sfp.getFactType());
    assertEquals("age", sfp.getFieldName());
    assertEquals("<", sfp.getOperator());
    assertEquals("55", sfp.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
    assertEquals("$a", sfp.getFieldBinding());
}
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 30 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintConnectives2.

@Test
public // https://issues.jboss.org/browse/GUVNOR-2141
void testSingleFieldConstraintConnectives2() {
    String drl = "package org.test;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "  when\n" + "    Applicant( age == 55 || == 75 )\n" + "  then\n" + "end";
    addModelField("org.test.Applicant", "this", "org.test.Applicant", DataType.TYPE_THIS);
    addModelField("org.test.Applicant", "age", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
    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(1, fp.getConstraintList().getConstraints().length);
    assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
    assertEquals("Applicant", sfp.getFactType());
    assertEquals("age", sfp.getFieldName());
    assertEquals("==", sfp.getOperator());
    assertEquals("55", sfp.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
    assertEquals(1, sfp.getConnectives().length);
    ConnectiveConstraint cc = sfp.getConnectives()[0];
    assertEquals("Applicant", cc.getFactType());
    assertEquals("age", cc.getFieldName());
    assertEquals("|| ==", cc.getOperator());
    assertEquals("75", cc.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cc.getConstraintValueType());
    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) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Aggregations

IPattern (org.drools.workbench.models.datamodel.rule.IPattern)118 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)95 Test (org.junit.Test)92 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)84 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)83 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)71 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 ArrayList (java.util.ArrayList)23 BRLConditionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn)23 BRLConditionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn)22 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)20 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