Search in sources :

Example 21 with CompositeFactPattern

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

Example 22 with CompositeFactPattern

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

the class RuleModelTest method testLHSBindingTypeFromOR.

@Test
public void testLHSBindingTypeFromOR() throws Exception {
    final RuleModel model = new RuleModel();
    model.lhs = new IPattern[1];
    CompositeFactPattern compositeFactPattern = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_OR);
    model.lhs[0] = compositeFactPattern;
    FactPattern pattern1 = new FactPattern();
    SingleFieldConstraint constraint1 = new SingleFieldConstraint();
    pattern1.addConstraint(constraint1);
    constraint1.setFieldType("String");
    constraint1.setFieldBinding("test");
    compositeFactPattern.addFactPattern(pattern1);
    FactPattern pattern2 = new FactPattern();
    SingleFieldConstraint constraint2 = new SingleFieldConstraint();
    pattern2.addConstraint(constraint2);
    compositeFactPattern.addFactPattern(pattern2);
    assertEquals("String", model.getLHSBindingType("test"));
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 23 with CompositeFactPattern

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

the class RuleModelTest method testBoundFieldVariableFromOR.

@Test
public void testBoundFieldVariableFromOR() throws Exception {
    final RuleModel model = new RuleModel();
    model.lhs = new IPattern[1];
    CompositeFactPattern compositeFactPattern = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_OR);
    model.lhs[0] = compositeFactPattern;
    FactPattern pattern1 = new FactPattern();
    SingleFieldConstraint constraint1 = new SingleFieldConstraint();
    pattern1.addConstraint(constraint1);
    constraint1.setFieldBinding("test");
    compositeFactPattern.addFactPattern(pattern1);
    FactPattern pattern2 = new FactPattern();
    SingleFieldConstraint constraint2 = new SingleFieldConstraint();
    pattern2.addConstraint(constraint2);
    compositeFactPattern.addFactPattern(pattern2);
    SingleFieldConstraint lhsBoundField = model.getLHSBoundField("test");
    assertEquals(constraint1, lhsBoundField);
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 24 with CompositeFactPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testReciprocal_CompositeFactPatternWithOrAndCompositeFieldConstraint.

@Test
public void testReciprocal_CompositeFactPatternWithOrAndCompositeFieldConstraint() {
    // This is the inverse of "CompositeFactPatternWithOrAndCompositeFieldConstraint"
    String drl = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "( Person( age == 42 ) or Person( age == 43 || age == 44) )\n" + "then\n" + "end";
    RuleModel m = new RuleModel();
    m.name = "rule1";
    // LHS Pattern
    CompositeFactPattern cfp = new CompositeFactPattern();
    cfp.setType(CompositeFactPattern.COMPOSITE_TYPE_OR);
    // LHS sub-patterns
    FactPattern fp1 = new FactPattern();
    fp1.setFactType("Person");
    SingleFieldConstraint fp1_sfp1 = new SingleFieldConstraint();
    fp1_sfp1.setFactType("Person");
    fp1_sfp1.setFieldName("age");
    fp1_sfp1.setOperator("==");
    fp1_sfp1.setValue("42");
    fp1_sfp1.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
    fp1_sfp1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    fp1.addConstraint(fp1_sfp1);
    FactPattern fp2 = new FactPattern();
    fp2.setFactType("Person");
    CompositeFieldConstraint fp2_cfp = new CompositeFieldConstraint();
    fp2_cfp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_OR);
    fp2.addConstraint(fp2_cfp);
    SingleFieldConstraint fp2_sfp1 = new SingleFieldConstraint();
    fp2_sfp1.setFactType("Person");
    fp2_sfp1.setFieldName("age");
    fp2_sfp1.setOperator("==");
    fp2_sfp1.setValue("43");
    fp2_sfp1.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
    fp2_sfp1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    fp2_cfp.addConstraint(fp2_sfp1);
    SingleFieldConstraint fp2_sfp2 = new SingleFieldConstraint();
    fp2_sfp2.setFactType("Person");
    fp2_sfp2.setFieldName("age");
    fp2_sfp2.setOperator("==");
    fp2_sfp2.setValue("44");
    fp2_sfp2.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
    fp2_sfp2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    fp2_cfp.addConstraint(fp2_sfp2);
    cfp.addFactPattern(fp1);
    cfp.addFactPattern(fp2);
    m.addLhsItem(cfp);
    String actualDrl = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
    assertEqualsIgnoreWhitespace(drl, actualDrl);
}
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) 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)

Example 25 with CompositeFactPattern

use of org.drools.workbench.models.datamodel.rule.CompositeFactPattern in project drools-wb by kiegroup.

the class RuleModelCloneVisitor method visitCompositeFactPattern.

private CompositeFactPattern visitCompositeFactPattern(CompositeFactPattern pattern) {
    CompositeFactPattern clone = new CompositeFactPattern();
    clone.setType(pattern.getType());
    if (pattern.getPatterns() != null) {
        for (IFactPattern fp : pattern.getPatterns()) {
            clone.addFactPattern((IFactPattern) visit(fp));
        }
    }
    return clone;
}
Also used : FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) IFactPattern(org.drools.workbench.models.datamodel.rule.IFactPattern)

Aggregations

CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)27 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)23 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)22 Test (org.junit.Test)22 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)21 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)21 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)18 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)16 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)15 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)9 ActionRetractFact (org.drools.workbench.models.datamodel.rule.ActionRetractFact)4 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)4 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)4 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)3 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)3 DSLSentence (org.drools.workbench.models.datamodel.rule.DSLSentence)3 IFactPattern (org.drools.workbench.models.datamodel.rule.IFactPattern)3 RuleAttribute (org.drools.workbench.models.datamodel.rule.RuleAttribute)3 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)2 IAction (org.drools.workbench.models.datamodel.rule.IAction)2