use of org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52 in project drools by kiegroup.
the class GuidedDTDRLPersistence method marshal.
public String marshal(final GuidedDecisionTable52 originalDTable) {
final GuidedDecisionTable52 dt = DecisionTableHitPolicyEnhancer.enhance(originalDTable);
StringBuilder sb = new StringBuilder();
List<List<DTCellValue52>> data = dt.getData();
List<BaseColumn> allColumns = dt.getExpandedColumns();
// Append package name and imports to DRL
PackageNameWriter.write(sb, dt);
ImportsWriter.write(sb, dt);
// Build rules
for (int i = 0; i < data.size(); i++) {
List<DTCellValue52> row = data.get(i);
// Specialised BRDRLPersistence provider than can handle template key expansion
TemplateDataProvider rowDataProvider = new GuidedDTTemplateDataProvider(allColumns, row);
Integer num = (Integer) row.get(0).getNumericValue();
String desc = row.get(1).getStringValue();
BRLRuleModel rm = new BRLRuleModel(dt);
rm.name = getName(dt.getTableName(), num);
doMetadata(allColumns, dt.getMetadataCols(), row, rm);
doAttribs(allColumns, dt.getAttributeCols(), row, rm);
doConditions(allColumns, dt.getConditions(), rowDataProvider, row, data, rm);
doActions(allColumns, dt.getActionCols(), rowDataProvider, row, rm);
if (dt.getParentName() != null) {
rm.parentName = dt.getParentName();
}
sb.append("//from row number: " + (i + 1) + "\n");
if (desc != null && desc.length() > 0) {
sb.append("//" + desc + "\n");
}
GuidedDTBRDRLPersistence drlMarshaller = new GuidedDTBRDRLPersistence(rowDataProvider);
String rule = drlMarshaller.marshal(rm);
sb.append(rule);
sb.append("\n");
}
return sb.toString();
}
use of org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52 in project drools by kiegroup.
the class GuidedDTDRLPersistence method doActions.
void doActions(List<BaseColumn> allColumns, List<ActionCol52> actionCols, TemplateDataProvider rowDataProvider, List<DTCellValue52> row, RuleModel rm) {
List<LabelledAction> actions = new ArrayList<LabelledAction>();
for (ActionCol52 c : actionCols) {
if (c instanceof LimitedEntryBRLActionColumn) {
doAction(allColumns, (LimitedEntryBRLActionColumn) c, actions, rowDataProvider, row, rm);
} else if (c instanceof BRLActionColumn) {
doAction(allColumns, (BRLActionColumn) c, actions, rowDataProvider, row, rm);
} else {
int index = allColumns.indexOf(c);
DTCellValue52 dcv = row.get(index);
String cell = "";
if (c instanceof LimitedEntryCol) {
if (dcv.getBooleanValue() == true) {
LimitedEntryCol lec = (LimitedEntryCol) c;
cell = GuidedDTDRLUtilities.convertDTCellValueToString(lec.getValue());
}
} else {
cell = GuidedDTDRLUtilities.convertDTCellValueToString(dcv);
}
if (validCell(cell, dcv.getDataType())) {
if (c instanceof ActionWorkItemInsertFactCol52) {
doAction(actions, (ActionWorkItemInsertFactCol52) c, cell);
} else if (c instanceof ActionInsertFactCol52) {
doAction(actions, (ActionInsertFactCol52) c, cell);
} else if (c instanceof ActionWorkItemSetFieldCol52) {
doAction(actions, (ActionWorkItemSetFieldCol52) c, cell);
} else if (c instanceof ActionSetFieldCol52) {
doAction(actions, (ActionSetFieldCol52) c, cell);
} else if (c instanceof ActionRetractFactCol52) {
doAction(actions, cell);
} else if (c instanceof ActionWorkItemCol52) {
doAction(actions, (ActionWorkItemCol52) c, cell);
}
}
}
}
rm.rhs = new IAction[actions.size()];
for (int i = 0; i < rm.rhs.length; i++) {
rm.rhs[i] = actions.get(i).action;
}
}
use of org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52 in project drools by kiegroup.
the class GuidedDTDRLPersistence method doMetadata.
void doMetadata(List<BaseColumn> allColumns, List<MetadataCol52> metadataCols, List<DTCellValue52> row, RuleModel rm) {
// setup temp list
List<RuleMetadata> metadataList = new ArrayList<RuleMetadata>();
for (int j = 0; j < metadataCols.size(); j++) {
MetadataCol52 meta = metadataCols.get(j);
int index = allColumns.indexOf(meta);
final DTCellValue52 dcv = row.get(index);
String cell = GuidedDTDRLUtilities.convertDTCellValueToString(dcv);
if (validateMetadataCell(cell)) {
metadataList.add(new RuleMetadata(meta.getMetadata(), cell));
}
}
if (metadataList.size() > 0) {
rm.metadataList = metadataList.toArray(new RuleMetadata[metadataList.size()]);
}
}
use of org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52 in project drools by kiegroup.
the class GuidedDTDRLPersistence method doCondition.
private void doCondition(List<BaseColumn> allColumns, BRLConditionColumn column, List<IPattern> patterns, TemplateDataProvider rowDataProvider, List<DTCellValue52> row, RuleModel rm) {
// Check whether the parameter-less BRL fragment needs inclusion
if (!hasVariables(column)) {
final BRLConditionVariableColumn variableColumn = column.getChildColumns().get(0);
final int index = allColumns.indexOf(variableColumn);
final DTCellValue52 dcv = row.get(index);
if (dcv != null && dcv.getBooleanValue()) {
for (IPattern pattern : column.getDefinition()) {
patterns.add(pattern);
}
}
} else {
for (IPattern pattern : column.getDefinition()) {
boolean addPattern = false;
// Get interpolation variables used by the Pattern
Map<InterpolationVariable, Integer> ivs = new HashMap<InterpolationVariable, Integer>();
RuleModelVisitor rmv = new RuleModelVisitor(pattern, ivs);
rmv.visit(pattern);
if (ivs.size() == 0) {
addPattern = true;
} else if (ivs.size() > 0) {
int templateKeyCount = 0;
for (InterpolationVariable variable : ivs.keySet()) {
String value = rowDataProvider.getTemplateKeyValue(variable.getVarName());
if (!"".equals(value)) {
templateKeyCount++;
}
}
// Ensure at least one key has a value (FreeFormLines need all values to be provided)
if (pattern instanceof FreeFormLine) {
addPattern = templateKeyCount == ivs.size();
} else if (templateKeyCount > 0) {
addPattern = true;
}
}
if (addPattern) {
patterns.add(pattern);
}
}
}
}
use of org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52 in project drools by kiegroup.
the class GuidedDTDRLPersistence method doAttribs.
void doAttribs(List<BaseColumn> allColumns, List<AttributeCol52> attributeCols, List<DTCellValue52> row, RuleModel rm) {
List<RuleAttribute> attribs = new ArrayList<RuleAttribute>();
for (int j = 0; j < attributeCols.size(); j++) {
AttributeCol52 at = attributeCols.get(j);
int index = allColumns.indexOf(at);
final DTCellValue52 dcv = row.get(index);
String cell = GuidedDTDRLUtilities.convertDTCellValueToString(dcv);
if (validateAttributeCell(cell)) {
// If instance of "otherwise" column then flag RuleModel as being negated
if (at.getAttribute().equals(GuidedDecisionTable52.NEGATE_RULE_ATTR)) {
rm.setNegated(Boolean.valueOf(cell));
} else {
attribs.add(new RuleAttribute(at.getAttribute(), cell));
}
}
}
if (attribs.size() > 0) {
rm.attributes = attribs.toArray(new RuleAttribute[attribs.size()]);
}
}
Aggregations