Search in sources :

Example 1 with ActionCol

use of org.drools.workbench.models.guided.dtable.shared.model.legacy.ActionCol in project drools by kiegroup.

the class GuidedDecisionTableUpgradeHelper1 method upgrade.

/**
 * Convert the legacy Decision Table model to the new
 *
 * @param legacyDTModel
 * @return The new DTModel
 */
public GuidedDecisionTable52 upgrade(GuidedDecisionTable legacyDTModel) {
    assertConditionColumnPatternGrouping(legacyDTModel);
    GuidedDecisionTable52 newDTModel = new GuidedDecisionTable52();
    newDTModel.setTableFormat(GuidedDecisionTable52.TableFormat.EXTENDED_ENTRY);
    newDTModel.setTableName(legacyDTModel.tableName);
    newDTModel.setParentName(legacyDTModel.parentName);
    newDTModel.setRowNumberCol(new RowNumberCol52());
    newDTModel.setDescriptionCol(new DescriptionCol52());
    // a String metadata attribute is: "value", a numerical: 1. No conversion action required
    if (legacyDTModel.metadataCols != null) {
        for (MetadataCol c : legacyDTModel.metadataCols) {
            newDTModel.getMetadataCols().add(makeNewColumn(c));
        }
    }
    // Attribute columns' data-type is based upon the attribute name
    if (legacyDTModel.attributeCols != null) {
        for (AttributeCol c : legacyDTModel.attributeCols) {
            newDTModel.getAttributeCols().add(makeNewColumn(c));
        }
    }
    // Legacy decision tables did not have Condition field data-types. Set all Condition
    // fields to a *sensible* default of String (as this matches legacy behaviour).
    List<Pattern52> patterns = new ArrayList<Pattern52>();
    Map<String, Pattern52> uniquePatterns = new HashMap<String, Pattern52>();
    if (legacyDTModel.conditionCols != null) {
        for (int i = 0; i < legacyDTModel.conditionCols.size(); i++) {
            ConditionCol c = legacyDTModel.conditionCols.get(i);
            String boundName = c.boundName;
            Pattern52 p = uniquePatterns.get(boundName);
            if (p == null) {
                p = new Pattern52();
                p.setBoundName(boundName);
                p.setFactType(c.factType);
                patterns.add(p);
                uniquePatterns.put(boundName, p);
            }
            if (p.getFactType() != null && !p.getFactType().equals(c.factType)) {
                throw new IllegalArgumentException("Inconsistent FactTypes for ConditionCols bound to '" + boundName + "' detected.");
            }
            p.getChildColumns().add(makeNewColumn(c));
        }
        for (Pattern52 p : patterns) {
            newDTModel.getConditions().add(p);
        }
    }
    // Action columns have a discrete data-type. No conversion action required.
    if (legacyDTModel.actionCols != null) {
        for (ActionCol c : legacyDTModel.actionCols) {
            newDTModel.getActionCols().add(makeNewColumn(c));
        }
    }
    // Copy across data
    newDTModel.setData(DataUtilities.makeDataLists(legacyDTModel.data));
    // Copy the boundName for ActionRetractFactCol into the data of the new Guided Decision Table model
    if (legacyDTModel.actionCols != null) {
        final int metaDataColCount = (legacyDTModel.metadataCols == null ? 0 : legacyDTModel.metadataCols.size());
        final int attributeColCount = (legacyDTModel.attributeCols == null ? 0 : legacyDTModel.attributeCols.size());
        final int conditionColCount = (legacyDTModel.conditionCols == null ? 0 : legacyDTModel.conditionCols.size());
        final int DATA_COLUMN_OFFSET = metaDataColCount + attributeColCount + conditionColCount + GuidedDecisionTable.INTERNAL_ELEMENTS;
        for (int iCol = 0; iCol < legacyDTModel.actionCols.size(); iCol++) {
            ActionCol lc = legacyDTModel.actionCols.get(iCol);
            if (lc instanceof ActionRetractFactCol) {
                String boundName = ((ActionRetractFactCol) lc).boundName;
                for (List<DTCellValue52> row : newDTModel.getData()) {
                    row.get(DATA_COLUMN_OFFSET + iCol).setStringValue(boundName);
                }
            }
        }
    }
    return newDTModel;
}
Also used : GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ActionCol(org.drools.workbench.models.guided.dtable.shared.model.legacy.ActionCol) DTCellValue52(org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52) MetadataCol(org.drools.workbench.models.guided.dtable.shared.model.legacy.MetadataCol) DescriptionCol52(org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52) Pattern52(org.drools.workbench.models.guided.dtable.shared.model.Pattern52) AttributeCol(org.drools.workbench.models.guided.dtable.shared.model.legacy.AttributeCol) ActionRetractFactCol(org.drools.workbench.models.guided.dtable.shared.model.legacy.ActionRetractFactCol) ConditionCol(org.drools.workbench.models.guided.dtable.shared.model.legacy.ConditionCol) RowNumberCol52(org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 DTCellValue52 (org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52)1 DescriptionCol52 (org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52)1 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)1 Pattern52 (org.drools.workbench.models.guided.dtable.shared.model.Pattern52)1 RowNumberCol52 (org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52)1 ActionCol (org.drools.workbench.models.guided.dtable.shared.model.legacy.ActionCol)1 ActionRetractFactCol (org.drools.workbench.models.guided.dtable.shared.model.legacy.ActionRetractFactCol)1 AttributeCol (org.drools.workbench.models.guided.dtable.shared.model.legacy.AttributeCol)1 ConditionCol (org.drools.workbench.models.guided.dtable.shared.model.legacy.ConditionCol)1 MetadataCol (org.drools.workbench.models.guided.dtable.shared.model.legacy.MetadataCol)1