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());
}
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"));
}
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);
}
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);
}
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;
}
Aggregations