use of org.drools.workbench.models.datamodel.rule.FieldConstraint in project drools by kiegroup.
the class GuidedDTDRLPersistence method doCondition.
private void doCondition(List<BaseColumn> allColumns, Pattern52 pattern, List<IPattern> patterns, List<DTCellValue52> row, List<List<DTCellValue52>> data, RuleModel rm) {
List<ConditionCol52> cols = pattern.getChildColumns();
for (ConditionCol52 c : cols) {
int index = allColumns.indexOf(c);
DTCellValue52 dcv = row.get(index);
String cell = "";
if (c instanceof LimitedEntryCol) {
if (Boolean.TRUE.equals(dcv.getBooleanValue())) {
LimitedEntryCol lec = (LimitedEntryCol) c;
DTCellValue52 value = lec.getValue();
if (value != null) {
cell = GuidedDTDRLUtilities.convertDTCellValueToString(value);
}
}
} else {
cell = GuidedDTDRLUtilities.convertDTCellValueToString(dcv);
}
boolean isOtherwise = dcv.isOtherwise();
boolean isValid = isOtherwise;
// Otherwise values are automatically valid as they're constructed from the other rules
if (!isOtherwise) {
isValid = validCell(cell, dcv.getDataType());
}
// If operator is "== null" or "!= null" add constraint if table value is true
if (c.getOperator() != null && (c.getOperator().equals("== null") || c.getOperator().equals("!= null"))) {
isValid = Boolean.TRUE.equals(dcv.getBooleanValue());
}
if (isValid) {
// get or create the pattern it belongs too
IPattern ifp = findByFactPattern(patterns, pattern);
// If the pattern does not exist create one suitable
if (ifp == null) {
FactPattern fp = new FactPattern(pattern.getFactType());
fp.setBoundName(pattern.getBoundName());
fp.setNegated(pattern.isNegated());
fp.setWindow(pattern.getWindow());
if (pattern.getEntryPointName() != null && pattern.getEntryPointName().length() > 0) {
FromEntryPointFactPattern fep = new FromEntryPointFactPattern();
fep.setEntryPointName(pattern.getEntryPointName());
fep.setFactPattern(fp);
patterns.add(fep);
ifp = fep;
} else {
patterns.add(fp);
ifp = fp;
}
}
// Extract the FactPattern from the IFactPattern
FactPattern fp;
if (ifp instanceof FactPattern) {
fp = (FactPattern) ifp;
} else if (ifp instanceof FromEntryPointFactPattern) {
FromEntryPointFactPattern fep = (FromEntryPointFactPattern) ifp;
fp = fep.getFactPattern();
} else {
throw new IllegalArgumentException("Inexpected IFactPattern implementation found.");
}
// Add the constraint from this cell
switch(c.getConstraintValueType()) {
case BaseSingleFieldConstraint.TYPE_LITERAL:
case BaseSingleFieldConstraint.TYPE_RET_VALUE:
if (!isOtherwise) {
FieldConstraint fc = makeSingleFieldConstraint(c, cell);
fp.addConstraint(fc);
} else {
FieldConstraint fc = makeSingleFieldConstraint(c, allColumns, data);
fp.addConstraint(fc);
}
break;
case BaseSingleFieldConstraint.TYPE_PREDICATE:
SingleFieldConstraint pred = new SingleFieldConstraint();
pred.setConstraintValueType(c.getConstraintValueType());
if (c.getFactField() != null && c.getFactField().indexOf("$param") > -1) {
// handle interpolation
pred.setValue(c.getFactField().replace("$param", cell));
} else {
pred.setValue(cell);
}
fp.addConstraint(pred);
break;
default:
throw new IllegalArgumentException("Unknown constraintValueType: " + c.getConstraintValueType());
}
}
}
}
use of org.drools.workbench.models.datamodel.rule.FieldConstraint in project drools by kiegroup.
the class GuidedDTDRLPersistence method makeSingleFieldConstraint.
// Build a normal SingleFieldConstraint for a non-otherwise cell value
private FieldConstraint makeSingleFieldConstraint(ConditionCol52 c, String cell) {
SingleFieldConstraint sfc = new SingleFieldConstraint(c.getFactField());
// expansion and contraction of decision table columns.... this might have to go.
if (no(c.getOperator())) {
String[] a = cell.split("\\s");
if (a.length > 1) {
// Operator might be 1 part (e.g. "==") or two parts (e.g. "not in")
StringBuilder operator = new StringBuilder(a[0]);
for (int i = 1; i < a.length - 1; i++) {
operator.append(a[i]);
}
sfc.setOperator(operator.toString());
sfc.setValue(a[a.length - 1]);
} else {
sfc.setValue(cell);
}
} else {
sfc.setOperator(c.getOperator());
if (OperatorsOracle.operatorRequiresList(c.getOperator())) {
sfc.setValue(makeInList(cell));
} else {
if (!c.getOperator().equals("== null") && !c.getOperator().equals("!= null")) {
sfc.setValue(cell);
}
}
}
final int constraintValueType = c.getConstraintValueType();
if ((constraintValueType == BaseSingleFieldConstraint.TYPE_LITERAL || constraintValueType == BaseSingleFieldConstraint.TYPE_RET_VALUE) && c.isBound()) {
sfc.setFieldBinding(c.getBinding());
}
sfc.setParameters(c.getParameters());
sfc.setConstraintValueType(c.getConstraintValueType());
sfc.setFieldType(c.getFieldType());
return sfc;
}
use of org.drools.workbench.models.datamodel.rule.FieldConstraint in project drools by kiegroup.
the class BRLRuleModelTest method testDecisionTableColumnsWithLHSBoundFieldsInConditionCol52.
@Test
public void testDecisionTableColumnsWithLHSBoundFieldsInConditionCol52() {
GuidedDecisionTable52 dt = new GuidedDecisionTable52();
Pattern52 p1 = new Pattern52();
p1.setFactType("Driver");
p1.setBoundName("$p1");
ConditionCol52 c1 = new ConditionCol52();
c1.setFactField("name");
c1.setFieldType(DataType.TYPE_STRING);
c1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
c1.setBinding("$c1");
p1.getChildColumns().add(c1);
dt.getConditions().add(p1);
BRLRuleModel model = new BRLRuleModel(dt);
FieldConstraint fcr1 = model.getLHSBoundField("$c1");
assertNotNull(fcr1);
assertTrue(fcr1 instanceof ConditionCol52FieldConstraintAdaptor);
ConditionCol52FieldConstraintAdaptor fcr1sfc = (ConditionCol52FieldConstraintAdaptor) fcr1;
assertEquals("Driver", fcr1sfc.getFactType());
assertEquals("name", fcr1sfc.getFieldName());
assertEquals(DataType.TYPE_STRING, fcr1sfc.getFieldType());
}
use of org.drools.workbench.models.datamodel.rule.FieldConstraint in project drools-wb by kiegroup.
the class RuleModelCloneVisitor method visitSingleFieldConstraint.
private SingleFieldConstraint visitSingleFieldConstraint(SingleFieldConstraint sfc) {
SingleFieldConstraint clone = new SingleFieldConstraint();
clone.setConstraintValueType(sfc.getConstraintValueType());
clone.setExpressionValue((ExpressionFormLine) visit(sfc.getExpressionValue()));
clone.setFieldBinding(sfc.getFieldBinding());
clone.setFactType(sfc.getFactType());
clone.setFieldName(sfc.getFieldName());
clone.setFieldType(sfc.getFieldType());
clone.setId(sfc.getId());
clone.setOperator(sfc.getOperator());
clone.setParent((FieldConstraint) visit(sfc.getParent()));
for (Map.Entry<String, String> entry : sfc.getParameters().entrySet()) {
clone.setParameter(entry.getKey(), entry.getValue());
}
clone.setValue(sfc.getValue());
if (sfc.getConnectives() != null) {
clone.setConnectives(new ConnectiveConstraint[sfc.getConnectives().length]);
for (int i = 0; i < sfc.getConnectives().length; i++) {
clone.getConnectives()[i] = (ConnectiveConstraint) visit(sfc.getConnectives()[i]);
}
}
return clone;
}
use of org.drools.workbench.models.datamodel.rule.FieldConstraint in project drools-wb by kiegroup.
the class RuleModelCloneVisitor method visitFactPattern.
private FactPattern visitFactPattern(FactPattern pattern) {
FactPattern clone = new FactPattern();
clone.setBoundName(pattern.getBoundName());
clone.setFactType(pattern.getFactType());
clone.setNegated(pattern.isNegated());
CEPWindow cloneCEPWindow = new CEPWindow();
cloneCEPWindow.setOperator(pattern.getWindow().getOperator());
cloneCEPWindow.setParameters(cloneCEPWindowParameters(pattern.getWindow()));
clone.setWindow(cloneCEPWindow);
for (FieldConstraint fc : pattern.getFieldConstraints()) {
clone.addConstraint((FieldConstraint) visit(fc));
}
return clone;
}
Aggregations