Search in sources :

Example 66 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSInsertFactWithFieldAsVariable.

@Test
public void testRHSInsertFactWithFieldAsVariable() {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1077212
    String drl = "package org.mortgages\n" + "import org.test.Person\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( $f : field1 == 44 )\n" + "then\n" + "Person fact0 = new Person();\n" + "fact0.setField1( $f );\n" + "insert( fact0 );\n" + "end";
    addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
    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("field1", sfp.getFieldName());
    assertEquals("==", sfp.getOperator());
    assertEquals("44", sfp.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
    assertEquals("$f", sfp.getFieldBinding());
    assertEquals(1, m.rhs.length);
    IAction a = m.rhs[0];
    assertTrue(a instanceof ActionInsertFact);
    ActionInsertFact ap = (ActionInsertFact) a;
    assertEquals("Person", ap.getFactType());
    assertEquals("fact0", ap.getBoundName());
    assertEquals(1, ap.getFieldValues().length);
    ActionFieldValue afv = ap.getFieldValues()[0];
    assertEquals("field1", afv.getField());
    assertEquals(FieldNatureType.TYPE_VARIABLE, afv.getNature());
    assertEquals("=$f", afv.getValue());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
}
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) 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) Test(org.junit.Test)

Example 67 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintWithTwoFieldsBinding.

@Test
public void testSingleFieldConstraintWithTwoFieldsBinding() {
    String drl = "rule \"rule1\"\n" + "when\n" + "Applicant( $a : age, $n : name )\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(2, fp.getConstraintList().getConstraints().length);
    assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint sfp0 = (SingleFieldConstraint) fp.getConstraint(0);
    assertEquals("Applicant", sfp0.getFactType());
    assertEquals("age", sfp0.getFieldName());
    assertEquals(BaseSingleFieldConstraint.TYPE_UNDEFINED, sfp0.getConstraintValueType());
    assertEquals("$a", sfp0.getFieldBinding());
    assertTrue(fp.getConstraint(1) instanceof SingleFieldConstraint);
    SingleFieldConstraint sfp1 = (SingleFieldConstraint) fp.getConstraint(1);
    assertEquals("Applicant", sfp1.getFactType());
    assertEquals("name", sfp1.getFieldName());
    assertEquals(BaseSingleFieldConstraint.TYPE_UNDEFINED, sfp1.getConstraintValueType());
    assertEquals("$n", sfp1.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 68 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintCEPOperator1Parameter.

@Test
public void testSingleFieldConstraintCEPOperator1Parameter() {
    String drl = "rule \"rule1\"\n" + "when\n" + "$e : Event()\n" + "Event( this after[1d] $e )\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 p1 = m.lhs[0];
    assertTrue(p1 instanceof FactPattern);
    FactPattern fp1 = (FactPattern) p1;
    assertEquals("Event", fp1.getFactType());
    assertEquals("$e", fp1.getBoundName());
    assertEquals(0, fp1.getNumberOfConstraints());
    IPattern p2 = m.lhs[1];
    assertTrue(p2 instanceof FactPattern);
    FactPattern fp2 = (FactPattern) p2;
    assertEquals("Event", fp2.getFactType());
    assertNull(fp2.getBoundName());
    assertEquals(1, fp2.getNumberOfConstraints());
    SingleFieldConstraint sfp = (SingleFieldConstraint) fp2.getConstraint(0);
    assertEquals("Event", sfp.getFactType());
    assertEquals("this", sfp.getFieldName());
    assertEquals("after", sfp.getOperator());
    assertEquals("$e", sfp.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_VARIABLE, sfp.getConstraintValueType());
    assertEquals(3, sfp.getParameters().size());
    assertEquals("1d", sfp.getParameter("0"));
    assertEquals("1", sfp.getParameter("org.drools.workbench.models.commons.backend.rule.visibleParameterSet"));
    assertEquals("org.drools.workbench.models.commons.backend.rule.CEPOperatorParameterDRLBuilder", sfp.getParameter("org.drools.workbench.models.commons.backend.rule.operatorParameterGenerator"));
}
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 69 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintCEPOperator2Parameters.

@Test
public void testSingleFieldConstraintCEPOperator2Parameters() {
    String drl = "rule \"rule1\"\n" + "when\n" + "$e : Event()\n" + "Event( this after[1d, 2d] $e )\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 p1 = m.lhs[0];
    assertTrue(p1 instanceof FactPattern);
    FactPattern fp1 = (FactPattern) p1;
    assertEquals("Event", fp1.getFactType());
    assertEquals("$e", fp1.getBoundName());
    assertEquals(0, fp1.getNumberOfConstraints());
    IPattern p2 = m.lhs[1];
    assertTrue(p2 instanceof FactPattern);
    FactPattern fp2 = (FactPattern) p2;
    assertEquals("Event", fp2.getFactType());
    assertNull(fp2.getBoundName());
    assertEquals(1, fp2.getNumberOfConstraints());
    SingleFieldConstraint sfp = (SingleFieldConstraint) fp2.getConstraint(0);
    assertEquals("Event", sfp.getFactType());
    assertEquals("this", sfp.getFieldName());
    assertEquals("after", sfp.getOperator());
    assertEquals("$e", sfp.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_VARIABLE, sfp.getConstraintValueType());
    assertEquals(4, sfp.getParameters().size());
    assertEquals("1d", sfp.getParameter("0"));
    assertEquals("2d", sfp.getParameter("1"));
    assertEquals("2", sfp.getParameter("org.drools.workbench.models.commons.backend.rule.visibleParameterSet"));
    assertEquals("org.drools.workbench.models.commons.backend.rule.CEPOperatorParameterDRLBuilder", sfp.getParameter("org.drools.workbench.models.commons.backend.rule.operatorParameterGenerator"));
}
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 70 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testCompositeFactPatternWithOrAndCompositeFieldConstraint.

@Test
public void testCompositeFactPatternWithOrAndCompositeFieldConstraint() {
    String drl = "rule \"rule1\"\n" + "when\n" + "( Person( age == 42 ) or Person( age == 43 || age == 44) )\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 CompositeFactPattern);
    CompositeFactPattern cfp = (CompositeFactPattern) p;
    assertEquals(CompositeFactPattern.COMPOSITE_TYPE_OR, cfp.getType());
    // LHS sub-patterns
    assertEquals(2, cfp.getPatterns().length);
    IPattern cfp_p1 = cfp.getPatterns()[0];
    assertTrue(cfp_p1 instanceof FactPattern);
    FactPattern fp1 = (FactPattern) cfp_p1;
    assertEquals("Person", fp1.getFactType());
    assertEquals(1, fp1.getConstraintList().getConstraints().length);
    assertTrue(fp1.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint cfp_sfp1 = (SingleFieldConstraint) fp1.getConstraint(0);
    assertEquals("Person", cfp_sfp1.getFactType());
    assertEquals("age", cfp_sfp1.getFieldName());
    assertEquals("==", cfp_sfp1.getOperator());
    assertEquals("42", cfp_sfp1.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cfp_sfp1.getConstraintValueType());
    IPattern cfp_p2 = cfp.getPatterns()[1];
    assertTrue(cfp_p2 instanceof FactPattern);
    FactPattern fp2 = (FactPattern) cfp_p2;
    assertEquals(1, fp2.getConstraintList().getConstraints().length);
    assertTrue(fp2.getConstraint(0) instanceof CompositeFieldConstraint);
    CompositeFieldConstraint cfp_p2_cfp = (CompositeFieldConstraint) fp2.getConstraint(0);
    assertEquals("||", cfp_p2_cfp.getCompositeJunctionType());
    assertEquals(2, cfp_p2_cfp.getNumberOfConstraints());
    assertTrue(cfp_p2_cfp.getConstraint(0) instanceof SingleFieldConstraint);
    assertTrue(cfp_p2_cfp.getConstraint(1) instanceof SingleFieldConstraint);
    SingleFieldConstraint cfp_p2_sfp1 = (SingleFieldConstraint) cfp_p2_cfp.getConstraint(0);
    assertEquals("Person", cfp_p2_sfp1.getFactType());
    assertEquals("age", cfp_p2_sfp1.getFieldName());
    assertEquals("==", cfp_p2_sfp1.getOperator());
    assertEquals("43", cfp_p2_sfp1.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cfp_p2_sfp1.getConstraintValueType());
    SingleFieldConstraint cfp_p2_sfp2 = (SingleFieldConstraint) cfp_p2_cfp.getConstraint(1);
    assertEquals("Person", cfp_p2_sfp2.getFactType());
    assertEquals("age", cfp_p2_sfp2.getFieldName());
    assertEquals("==", cfp_p2_sfp2.getOperator());
    assertEquals("44", cfp_p2_sfp2.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cfp_p2_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) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) 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) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) 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