use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeConstraints.
@Test
public void testCompositeConstraints() {
RuleModel m = new RuleModel();
m.name = "with composite";
FactPattern p1 = new FactPattern("Person");
p1.setBoundName("p1");
m.addLhsItem(p1);
FactPattern p = new FactPattern("Goober");
m.addLhsItem(p);
CompositeFieldConstraint comp = new CompositeFieldConstraint();
comp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_OR);
p.addConstraint(comp);
final SingleFieldConstraint X = new SingleFieldConstraint();
X.setFieldName("goo");
X.setFieldType(DataType.TYPE_STRING);
X.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
X.setValue("foo");
X.setOperator("==");
X.setConnectives(new ConnectiveConstraint[1]);
X.getConnectives()[0] = new ConnectiveConstraint();
X.getConnectives()[0].setConstraintValueType(ConnectiveConstraint.TYPE_LITERAL);
X.getConnectives()[0].setFieldType(DataType.TYPE_STRING);
X.getConnectives()[0].setOperator("|| ==");
X.getConnectives()[0].setValue("bar");
comp.addConstraint(X);
final SingleFieldConstraint Y = new SingleFieldConstraint();
Y.setFieldName("goo2");
Y.setFieldType(DataType.TYPE_STRING);
Y.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
Y.setValue("foo");
Y.setOperator("==");
comp.addConstraint(Y);
CompositeFieldConstraint comp2 = new CompositeFieldConstraint();
comp2.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_AND);
final SingleFieldConstraint Q1 = new SingleFieldConstraint();
Q1.setFieldType(DataType.TYPE_STRING);
Q1.setFieldName("goo");
Q1.setOperator("==");
Q1.setValue("whee");
Q1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
comp2.addConstraint(Q1);
final SingleFieldConstraint Q2 = new SingleFieldConstraint();
Q2.setFieldType(DataType.TYPE_STRING);
Q2.setFieldName("gabba");
Q2.setOperator("==");
Q2.setValue("whee");
Q2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
comp2.addConstraint(Q2);
// now nest it
comp.addConstraint(comp2);
final SingleFieldConstraint Z = new SingleFieldConstraint();
Z.setFieldName("goo3");
Z.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
Z.setFieldType(DataType.TYPE_STRING);
Z.setValue("foo");
Z.setOperator("==");
p.addConstraint(Z);
ActionInsertFact ass = new ActionInsertFact("Whee");
m.addRhsItem(ass);
String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
String expected = "rule \"with composite\"\n" + "\tdialect \"mvel\"\n" + "\twhen\n" + "\t\tp1 : Person( )\n" + "\t\tGoober( goo == \"foo\" || == \"bar\" || goo2 == \"foo\" || ( goo == \"whee\" && gabba == \"whee\" ), goo3 == \"foo\" )\n" + "\tthen\n" + "\t\tinsert( new Whee() );\n" + "end\n";
assertEqualsIgnoreWhitespace(expected, actual);
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.
the class RuleModelVisitor method visitSingleFieldConstraint.
private void visitSingleFieldConstraint(SingleFieldConstraint sfc) {
InterpolationVariable var = new InterpolationVariable(sfc.getValue(), sfc.getFieldType(), (factPattern == null ? "" : factPattern.getFactType()), sfc.getFieldName());
if (BaseSingleFieldConstraint.TYPE_TEMPLATE == sfc.getConstraintValueType() && !vars.containsKey(var)) {
vars.put(var, vars.size());
}
// Visit Connection constraints
if (sfc.getConnectives() != null) {
for (int i = 0; i < sfc.getConnectives().length; i++) {
final ConnectiveConstraint cc = sfc.getConnectives()[i];
InterpolationVariable ccVar = new InterpolationVariable(cc.getValue(), cc.getFieldType(), (factPattern == null ? "" : factPattern.getFactType()), cc.getFieldName());
if (BaseSingleFieldConstraint.TYPE_TEMPLATE == cc.getConstraintValueType() && !vars.containsKey(ccVar)) {
vars.put(ccVar, vars.size());
}
}
}
}
use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.
the class RuleModelVisitor method visitSingleFieldConstraint.
private void visitSingleFieldConstraint(SingleFieldConstraintEBLeftSide sfexp) {
String genericType = sfexp.getExpressionLeftSide().getGenericType();
String factType = sfexp.getExpressionLeftSide().getPreviousClassType();
if (factType == null) {
factType = sfexp.getExpressionLeftSide().getClassType();
}
InterpolationVariable var = new InterpolationVariable(sfexp.getValue(), genericType, factType, sfexp.getFieldName());
if (BaseSingleFieldConstraint.TYPE_TEMPLATE == sfexp.getConstraintValueType() && !vars.containsKey(var)) {
vars.put(var, vars.size());
}
// Visit Connection constraints
if (sfexp.getConnectives() != null) {
for (int i = 0; i < sfexp.getConnectives().length; i++) {
final ConnectiveConstraint cc = sfexp.getConnectives()[i];
InterpolationVariable ccVar = new InterpolationVariable(cc.getValue(), genericType, factType, cc.getFieldName());
if (BaseSingleFieldConstraint.TYPE_TEMPLATE == cc.getConstraintValueType() && !vars.containsKey(ccVar)) {
vars.put(ccVar, vars.size());
}
}
}
}
use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.
the class RuleModelUpgradeHelper2 method fixConnectiveConstraints.
private void fixConnectiveConstraints(SingleFieldConstraint sfc) {
if (sfc.getConnectives() == null) {
return;
}
for (ConnectiveConstraint cc : sfc.getConnectives()) {
if (cc.getFieldName() == null) {
cc.setFieldName(sfc.getFieldName());
cc.setFieldType(sfc.getFieldType());
}
}
}
use of org.drools.workbench.models.datamodel.rule.ConnectiveConstraint in project drools by kiegroup.
the class RuleModelUpgradeHelper3 method fixConstraints.
private void fixConstraints(FactPattern fp, SingleFieldConstraint sfc) {
final FieldConstraint parent = sfc.getParent();
if (parent == null) {
sfc.setFactType(fp.getFactType());
} else if (parent instanceof SingleFieldConstraint) {
sfc.setFactType(((SingleFieldConstraint) parent).getFieldType());
}
sfc.setFieldName(fixFieldName(sfc.getFieldName()));
if (sfc.getConnectives() == null) {
return;
}
for (ConnectiveConstraint cc : sfc.getConnectives()) {
cc.setFactType(fp.getFactType());
cc.setFieldName(fixFieldName(cc.getFieldName()));
}
}
Aggregations