Search in sources :

Example 1 with ConditionCol

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

the class GuidedDecisionTableUpgradeHelper1 method assertConditionColumnPatternGrouping.

// Ensure Condition columns are grouped by pattern (as we merge equal
// patterns in the UI). This operates on the original Model data and
// therefore should be called before the Decision Table's internal data
// representation (i.e. DynamicData, DynamicDataRow and CellValue) is
// populated
private void assertConditionColumnPatternGrouping(GuidedDecisionTable model) {
    class ConditionColData {

        ConditionCol col;

        String[] data;
    }
    // Offset into Model's data array
    final int metaDataColCount = (model.metadataCols == null ? 0 : model.metadataCols.size());
    final int attributeColCount = (model.attributeCols == null ? 0 : model.attributeCols.size());
    final int DATA_COLUMN_OFFSET = metaDataColCount + attributeColCount + GuidedDecisionTable.INTERNAL_ELEMENTS;
    Map<String, List<ConditionColData>> uniqueGroups = new TreeMap<String, List<ConditionColData>>();
    List<List<ConditionColData>> groups = new ArrayList<List<ConditionColData>>();
    final int DATA_ROWS = model.data.length;
    // Copy conditions and related data into temporary groups
    for (int iCol = 0; iCol < model.conditionCols.size(); iCol++) {
        ConditionCol col = model.conditionCols.get(iCol);
        String pattern = col.boundName + "";
        List<ConditionColData> groupCols = uniqueGroups.get(pattern);
        if (groupCols == null) {
            groupCols = new ArrayList<ConditionColData>();
            groups.add(groupCols);
            uniqueGroups.put(pattern, groupCols);
        }
        // Make a ConditionColData object
        ConditionColData ccd = new ConditionColData();
        int colIndex = DATA_COLUMN_OFFSET + iCol;
        ccd.data = new String[DATA_ROWS];
        for (int iRow = 0; iRow < DATA_ROWS; iRow++) {
            String[] row = model.data[iRow];
            ccd.data[iRow] = row[colIndex];
        }
        ccd.col = col;
        groupCols.add(ccd);
    }
    // Copy temporary groups back into the model
    int iCol = 0;
    model.conditionCols.clear();
    for (List<ConditionColData> me : groups) {
        for (ConditionColData ccd : me) {
            model.conditionCols.add(ccd.col);
            int colIndex = DATA_COLUMN_OFFSET + iCol;
            for (int iRow = 0; iRow < DATA_ROWS; iRow++) {
                String[] row = model.data[iRow];
                row[colIndex] = ccd.data[iRow];
            }
            iCol++;
        }
    }
}
Also used : ArrayList(java.util.ArrayList) ConditionCol(org.drools.workbench.models.guided.dtable.shared.model.legacy.ConditionCol) ArrayList(java.util.ArrayList) List(java.util.List) TreeMap(java.util.TreeMap)

Example 2 with ConditionCol

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

the class GuidedDTModelConversionTest method testConversionPatternGrouping2.

@Test
public void testConversionPatternGrouping2() {
    GuidedDecisionTable dt = new GuidedDecisionTable();
    dt.tableName = "michael";
    ConditionCol con = new ConditionCol();
    con.boundName = "z1";
    con.constraintValueType = BaseSingleFieldConstraint.TYPE_LITERAL;
    con.factField = "age";
    con.factType = "Driver";
    con.header = "Driver z1 age";
    con.operator = "==";
    dt.conditionCols.add(con);
    ConditionCol con2 = new ConditionCol();
    con2.boundName = "f1";
    con2.constraintValueType = BaseSingleFieldConstraint.TYPE_LITERAL;
    con2.factField = "name";
    con2.factType = "Person";
    con2.header = "Person f1 name";
    con2.operator = "==";
    dt.conditionCols.add(con2);
    ConditionCol con3 = new ConditionCol();
    con3.boundName = "z1";
    con3.constraintValueType = BaseSingleFieldConstraint.TYPE_RET_VALUE;
    con3.factField = "rating";
    con3.factType = "Driver";
    con3.header = "Driver rating";
    con3.operator = "==";
    dt.conditionCols.add(con3);
    ConditionCol con4 = new ConditionCol();
    con4.boundName = "f2";
    con4.constraintValueType = BaseSingleFieldConstraint.TYPE_PREDICATE;
    con4.factType = "Person2";
    con4.header = "Person2 f2 not needed";
    con4.factField = "(not needed)";
    dt.conditionCols.add(con4);
    dt.data = new String[][] { new String[] { "1", "desc", "z1c1r1", "f1c1r1", "z1c2r1", "f2c1r1" }, new String[] { "2", "desc", "z1c1r2", "f1c1r2", "z1c2r2", "f2c1r2" } };
    String[][] expected = new String[][] { new String[] { "1", "desc", "z1c1r1", "z1c2r1", "f1c1r1", "f2c1r1" }, new String[] { "2", "desc", "z1c1r2", "z1c2r2", "f1c1r2", "f2c1r2" } };
    GuidedDecisionTable52 tsdt = upgrader.upgrade(dt);
    assertEquals("michael", tsdt.getTableName());
    assertEquals(0, tsdt.getMetadataCols().size());
    assertEquals(0, tsdt.getAttributeCols().size());
    assertEquals(3, tsdt.getConditions().size());
    assertEquals("z1", tsdt.getConditionPattern("z1").getBoundName());
    assertEquals("Driver", tsdt.getConditionPattern("z1").getFactType());
    assertEquals("f1", tsdt.getConditionPattern("f1").getBoundName());
    assertEquals("Person", tsdt.getConditionPattern("f1").getFactType());
    assertEquals("f2", tsdt.getConditionPattern("f2").getBoundName());
    assertEquals("Person2", tsdt.getConditionPattern("f2").getFactType());
    assertEquals(2, tsdt.getConditionPattern("z1").getChildColumns().size());
    assertEquals(1, tsdt.getConditionPattern("f1").getChildColumns().size());
    assertEquals(1, tsdt.getConditionPattern("f2").getChildColumns().size());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, tsdt.getConditionPattern("z1").getChildColumns().get(0).getConstraintValueType());
    assertEquals("age", tsdt.getConditionPattern("z1").getChildColumns().get(0).getFactField());
    assertEquals("Driver", tsdt.getPattern(tsdt.getConditionPattern("z1").getChildColumns().get(0)).getFactType());
    assertEquals("Driver z1 age", tsdt.getConditionPattern("z1").getChildColumns().get(0).getHeader());
    assertEquals("==", tsdt.getConditionPattern("z1").getChildColumns().get(0).getOperator());
    assertEquals(BaseSingleFieldConstraint.TYPE_RET_VALUE, tsdt.getConditionPattern("z1").getChildColumns().get(1).getConstraintValueType());
    assertEquals("rating", tsdt.getConditionPattern("z1").getChildColumns().get(1).getFactField());
    assertEquals("Driver", tsdt.getPattern(tsdt.getConditionPattern("z1").getChildColumns().get(1)).getFactType());
    assertEquals("Driver rating", tsdt.getConditionPattern("z1").getChildColumns().get(1).getHeader());
    assertEquals("==", tsdt.getConditionPattern("z1").getChildColumns().get(1).getOperator());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, tsdt.getConditionPattern("f1").getChildColumns().get(0).getConstraintValueType());
    assertEquals("name", tsdt.getConditionPattern("f1").getChildColumns().get(0).getFactField());
    assertEquals("Person", tsdt.getPattern(tsdt.getConditionPattern("f1").getChildColumns().get(0)).getFactType());
    assertEquals("Person f1 name", tsdt.getConditionPattern("f1").getChildColumns().get(0).getHeader());
    assertEquals("==", tsdt.getConditionPattern("f1").getChildColumns().get(0).getOperator());
    assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, tsdt.getConditionPattern("f2").getChildColumns().get(0).getConstraintValueType());
    assertEquals("(not needed)", tsdt.getConditionPattern("f2").getChildColumns().get(0).getFactField());
    assertEquals("Person2", tsdt.getPattern(tsdt.getConditionPattern("f2").getChildColumns().get(0)).getFactType());
    assertEquals("Person2 f2 not needed", tsdt.getConditionPattern("f2").getChildColumns().get(0).getHeader());
    assertEquals(null, tsdt.getConditionPattern("f2").getChildColumns().get(0).getOperator());
    assertEquals(2, tsdt.getData().size());
    for (int i = 0; i < 2; i++) {
        System.out.println("Row-" + i);
        StringBuilder sb = new StringBuilder();
        for (DTCellValue52 c : tsdt.getData().get(i)) {
            sb.append(c.getStringValue() + ", ");
        }
        sb.delete(sb.lastIndexOf(","), sb.length());
        System.out.println(sb.toString());
    }
    assertEquals(new Integer(1), (Integer) tsdt.getData().get(0).get(0).getNumericValue());
    assertEquals("desc", tsdt.getData().get(0).get(1).getStringValue());
    assertEquals("z1c1r1", tsdt.getData().get(0).get(2).getStringValue());
    assertEquals("z1c2r1", tsdt.getData().get(0).get(3).getStringValue());
    assertEquals("f1c1r1", tsdt.getData().get(0).get(4).getStringValue());
    assertEquals("f2c1r1", tsdt.getData().get(0).get(5).getStringValue());
    assertEquals(new Integer(2), (Integer) tsdt.getData().get(1).get(0).getNumericValue());
    assertEquals("desc", tsdt.getData().get(1).get(1).getStringValue());
    assertEquals("z1c1r2", tsdt.getData().get(1).get(2).getStringValue());
    assertEquals("z1c2r2", tsdt.getData().get(1).get(3).getStringValue());
    assertEquals("f1c1r2", tsdt.getData().get(1).get(4).getStringValue());
    assertEquals("f2c1r2", tsdt.getData().get(1).get(5).getStringValue());
    isRowEquivalent(tsdt.getData().get(0), expected[0]);
    isRowEquivalent(tsdt.getData().get(1), expected[1]);
}
Also used : GuidedDecisionTable(org.drools.workbench.models.guided.dtable.shared.model.legacy.GuidedDecisionTable) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) ConditionCol(org.drools.workbench.models.guided.dtable.shared.model.legacy.ConditionCol) DTCellValue52(org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) Test(org.junit.Test)

Example 3 with ConditionCol

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

the class GuidedDTModelConversionTest method testConversionPatternGrouping.

@Test
public void testConversionPatternGrouping() {
    GuidedDecisionTable dt = new GuidedDecisionTable();
    dt.tableName = "michael";
    MetadataCol md = new MetadataCol();
    md.attr = "legacy";
    md.defaultValue = "yes";
    dt.metadataCols.add(md);
    AttributeCol attr = new AttributeCol();
    attr.attr = "salience";
    attr.defaultValue = "66";
    dt.attributeCols.add(attr);
    ConditionCol con = new ConditionCol();
    con.boundName = "f1";
    con.constraintValueType = BaseSingleFieldConstraint.TYPE_LITERAL;
    con.factField = "age";
    con.factType = "Driver";
    con.header = "Driver f1 age";
    con.operator = "==";
    dt.conditionCols.add(con);
    ConditionCol con2 = new ConditionCol();
    con2.boundName = "f2";
    con2.constraintValueType = BaseSingleFieldConstraint.TYPE_LITERAL;
    con2.factField = "name";
    con2.factType = "Person";
    con2.header = "Person f2 name";
    con2.operator = "==";
    dt.conditionCols.add(con2);
    ConditionCol con3 = new ConditionCol();
    con3.boundName = "f1";
    con3.constraintValueType = BaseSingleFieldConstraint.TYPE_RET_VALUE;
    con3.factField = "rating";
    con3.factType = "Driver";
    con3.header = "Driver rating";
    con3.operator = "==";
    dt.conditionCols.add(con3);
    ConditionCol con4 = new ConditionCol();
    con4.boundName = "f2";
    con4.constraintValueType = BaseSingleFieldConstraint.TYPE_PREDICATE;
    con4.factType = "Person";
    con4.header = "Person f2 not needed";
    con4.factField = "(not needed)";
    dt.conditionCols.add(con4);
    ActionInsertFactCol ins = new ActionInsertFactCol();
    ins.boundName = "ins";
    ins.factType = "Cheese";
    ins.factField = "price";
    ins.type = DataType.TYPE_NUMERIC_INTEGER;
    dt.actionCols.add(ins);
    ActionRetractFactCol ret = new ActionRetractFactCol();
    ret.boundName = "ret1";
    dt.actionCols.add(ret);
    ActionSetFieldCol set = new ActionSetFieldCol();
    set.boundName = "f1";
    set.factField = "goo1";
    set.type = DataType.TYPE_STRING;
    dt.actionCols.add(set);
    ActionSetFieldCol set2 = new ActionSetFieldCol();
    set2.boundName = "f1";
    set2.factField = "goo2";
    set2.defaultValue = "whee";
    set2.type = DataType.TYPE_STRING;
    dt.actionCols.add(set2);
    dt.data = new String[][] { new String[] { "1", "desc", "metar1", "saliencer1", "f1c1r1", "f2c1r1", "f1c2r1", "f2c2r1", "a1r1", "a2r1", "a3r1", "a4r1" }, new String[] { "2", "desc", "metar2", "saliencer2", "f1c1r2", "f2c1r2", "f1c2r2", "f2c2r2", "a1r2", "a2r2", "a3r2", "a4r2" } };
    String[][] expected = new String[][] { new String[] { "1", "desc", "metar1", "saliencer1", "f1c1r1", "f1c2r1", "f2c1r1", "f2c2r1", "a1r1", "ret1", "a3r1", "a4r1" }, new String[] { "2", "desc", "metar2", "saliencer2", "f1c1r2", "f1c2r2", "f2c1r2", "f2c2r2", "a1r2", "ret1", "a3r2", "a4r2" } };
    GuidedDecisionTable52 tsdt = upgrader.upgrade(dt);
    assertEquals("michael", tsdt.getTableName());
    assertEquals(1, tsdt.getMetadataCols().size());
    assertEquals("legacy", tsdt.getMetadataCols().get(0).getMetadata());
    assertEquals("yes", tsdt.getMetadataCols().get(0).getDefaultValue().getStringValue());
    assertEquals(1, tsdt.getAttributeCols().size());
    assertEquals("salience", tsdt.getAttributeCols().get(0).getAttribute());
    assertEquals("66", tsdt.getAttributeCols().get(0).getDefaultValue().getStringValue());
    assertEquals(2, tsdt.getConditions().size());
    assertEquals("f1", tsdt.getConditionPattern("f1").getBoundName());
    assertEquals("Driver", tsdt.getConditionPattern("f1").getFactType());
    assertEquals("f2", tsdt.getConditionPattern("f2").getBoundName());
    assertEquals("Person", tsdt.getConditionPattern("f2").getFactType());
    assertEquals(2, tsdt.getConditionPattern("f1").getChildColumns().size());
    assertEquals(2, tsdt.getConditionPattern("f2").getChildColumns().size());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, tsdt.getConditionPattern("f1").getChildColumns().get(0).getConstraintValueType());
    assertEquals("age", tsdt.getConditionPattern("f1").getChildColumns().get(0).getFactField());
    assertEquals("Driver", tsdt.getPattern(tsdt.getConditionPattern("f1").getChildColumns().get(0)).getFactType());
    assertEquals("Driver f1 age", tsdt.getConditionPattern("f1").getChildColumns().get(0).getHeader());
    assertEquals("==", tsdt.getConditionPattern("f1").getChildColumns().get(0).getOperator());
    assertEquals(BaseSingleFieldConstraint.TYPE_RET_VALUE, tsdt.getConditionPattern("f1").getChildColumns().get(1).getConstraintValueType());
    assertEquals("rating", tsdt.getConditionPattern("f1").getChildColumns().get(1).getFactField());
    assertEquals("Driver", tsdt.getPattern(tsdt.getConditionPattern("f1").getChildColumns().get(1)).getFactType());
    assertEquals("Driver rating", tsdt.getConditionPattern("f1").getChildColumns().get(1).getHeader());
    assertEquals("==", tsdt.getConditionPattern("f1").getChildColumns().get(1).getOperator());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, tsdt.getConditionPattern("f2").getChildColumns().get(0).getConstraintValueType());
    assertEquals("name", tsdt.getConditionPattern("f2").getChildColumns().get(0).getFactField());
    assertEquals("Person", tsdt.getPattern(tsdt.getConditionPattern("f2").getChildColumns().get(0)).getFactType());
    assertEquals("Person f2 name", tsdt.getConditionPattern("f2").getChildColumns().get(0).getHeader());
    assertEquals("==", tsdt.getConditionPattern("f2").getChildColumns().get(0).getOperator());
    assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, tsdt.getConditionPattern("f2").getChildColumns().get(1).getConstraintValueType());
    assertEquals("(not needed)", tsdt.getConditionPattern("f2").getChildColumns().get(1).getFactField());
    assertEquals("Person", tsdt.getPattern(tsdt.getConditionPattern("f2").getChildColumns().get(1)).getFactType());
    assertEquals("Person f2 not needed", tsdt.getConditionPattern("f2").getChildColumns().get(1).getHeader());
    assertEquals(null, tsdt.getConditionPattern("f2").getChildColumns().get(1).getOperator());
    assertEquals(4, tsdt.getActionCols().size());
    ActionInsertFactCol52 a1 = (ActionInsertFactCol52) tsdt.getActionCols().get(0);
    assertEquals("ins", a1.getBoundName());
    assertEquals("Cheese", a1.getFactType());
    assertEquals("price", a1.getFactField());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, a1.getType());
    ActionRetractFactCol52 a2 = (ActionRetractFactCol52) tsdt.getActionCols().get(1);
    assertNotNull(a2);
    ActionSetFieldCol52 a3 = (ActionSetFieldCol52) tsdt.getActionCols().get(2);
    assertEquals("f1", a3.getBoundName());
    assertEquals("goo1", a3.getFactField());
    assertEquals(DataType.TYPE_STRING, a3.getType());
    ActionSetFieldCol52 a4 = (ActionSetFieldCol52) tsdt.getActionCols().get(3);
    assertEquals("f1", a4.getBoundName());
    assertEquals("goo2", a4.getFactField());
    assertEquals("whee", a4.getDefaultValue().getStringValue());
    assertEquals(DataType.TYPE_STRING, a4.getType());
    assertEquals(2, tsdt.getData().size());
    for (int i = 0; i < 2; i++) {
        System.out.println("Row-" + i);
        StringBuilder sb = new StringBuilder();
        for (DTCellValue52 c : tsdt.getData().get(i)) {
            sb.append(c.getStringValue() + ", ");
        }
        sb.delete(sb.lastIndexOf(","), sb.length());
        System.out.println(sb.toString());
    }
    assertEquals(new Integer(1), (Integer) tsdt.getData().get(0).get(0).getNumericValue());
    assertEquals("desc", tsdt.getData().get(0).get(1).getStringValue());
    assertEquals("metar1", tsdt.getData().get(0).get(2).getStringValue());
    assertEquals("saliencer1", tsdt.getData().get(0).get(3).getStringValue());
    assertEquals("f1c1r1", tsdt.getData().get(0).get(4).getStringValue());
    assertEquals("f1c2r1", tsdt.getData().get(0).get(5).getStringValue());
    assertEquals("f2c1r1", tsdt.getData().get(0).get(6).getStringValue());
    assertEquals("f2c2r1", tsdt.getData().get(0).get(7).getStringValue());
    assertEquals("a1r1", tsdt.getData().get(0).get(8).getStringValue());
    assertEquals("ret1", tsdt.getData().get(0).get(9).getStringValue());
    assertEquals("a3r1", tsdt.getData().get(0).get(10).getStringValue());
    assertEquals("a4r1", tsdt.getData().get(0).get(11).getStringValue());
    assertEquals(new Integer(2), (Integer) tsdt.getData().get(1).get(0).getNumericValue());
    assertEquals("desc", tsdt.getData().get(1).get(1).getStringValue());
    assertEquals("metar2", tsdt.getData().get(1).get(2).getStringValue());
    assertEquals("saliencer2", tsdt.getData().get(1).get(3).getStringValue());
    assertEquals("f1c1r2", tsdt.getData().get(1).get(4).getStringValue());
    assertEquals("f1c2r2", tsdt.getData().get(1).get(5).getStringValue());
    assertEquals("f2c1r2", tsdt.getData().get(1).get(6).getStringValue());
    assertEquals("f2c2r2", tsdt.getData().get(1).get(7).getStringValue());
    assertEquals("a1r2", tsdt.getData().get(1).get(8).getStringValue());
    assertEquals("ret1", tsdt.getData().get(1).get(9).getStringValue());
    assertEquals("a3r2", tsdt.getData().get(1).get(10).getStringValue());
    assertEquals("a4r2", tsdt.getData().get(1).get(11).getStringValue());
    isRowEquivalent(tsdt.getData().get(0), expected[0]);
    isRowEquivalent(tsdt.getData().get(1), expected[1]);
}
Also used : GuidedDecisionTable(org.drools.workbench.models.guided.dtable.shared.model.legacy.GuidedDecisionTable) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) ActionInsertFactCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52) ActionRetractFactCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionRetractFactCol52) DTCellValue52(org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52) MetadataCol(org.drools.workbench.models.guided.dtable.shared.model.legacy.MetadataCol) ActionSetFieldCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionSetFieldCol52) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionInsertFactCol(org.drools.workbench.models.guided.dtable.shared.model.legacy.ActionInsertFactCol) 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) ActionSetFieldCol(org.drools.workbench.models.guided.dtable.shared.model.legacy.ActionSetFieldCol) Test(org.junit.Test)

Example 4 with ConditionCol

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

the class GuidedDTModelConversionTest method testConversion.

@Test
public void testConversion() {
    GuidedDecisionTable dt = new GuidedDecisionTable();
    dt.tableName = "michael";
    MetadataCol md = new MetadataCol();
    md.attr = "legacy";
    md.defaultValue = "yes";
    dt.metadataCols.add(md);
    AttributeCol attr = new AttributeCol();
    attr.attr = "salience";
    attr.defaultValue = "66";
    dt.attributeCols.add(attr);
    ConditionCol con = new ConditionCol();
    con.boundName = "f1";
    con.constraintValueType = BaseSingleFieldConstraint.TYPE_LITERAL;
    con.factField = "age";
    con.factType = "Driver";
    con.header = "Driver f1 age";
    con.operator = "==";
    dt.conditionCols.add(con);
    ConditionCol con2 = new ConditionCol();
    con2.boundName = "f1";
    con2.constraintValueType = BaseSingleFieldConstraint.TYPE_LITERAL;
    con2.factField = "name";
    con2.factType = "Driver";
    con2.header = "Driver f1 name";
    con2.operator = "==";
    dt.conditionCols.add(con2);
    ConditionCol con3 = new ConditionCol();
    con3.boundName = "f1";
    con3.constraintValueType = BaseSingleFieldConstraint.TYPE_RET_VALUE;
    con3.factField = "rating";
    con3.factType = "Driver";
    con3.header = "Driver rating";
    con3.operator = "==";
    dt.conditionCols.add(con3);
    ConditionCol con4 = new ConditionCol();
    con4.boundName = "f2";
    con4.constraintValueType = BaseSingleFieldConstraint.TYPE_PREDICATE;
    con4.factType = "Driver";
    con4.header = "Driver 2 pimp";
    con4.factField = "(not needed)";
    dt.conditionCols.add(con4);
    ActionInsertFactCol ins = new ActionInsertFactCol();
    ins.boundName = "ins";
    ins.factType = "Cheese";
    ins.factField = "price";
    ins.type = DataType.TYPE_NUMERIC_INTEGER;
    dt.actionCols.add(ins);
    ActionRetractFactCol ret = new ActionRetractFactCol();
    ret.boundName = "ret1";
    dt.actionCols.add(ret);
    ActionSetFieldCol set = new ActionSetFieldCol();
    set.boundName = "f1";
    set.factField = "goo1";
    set.type = DataType.TYPE_STRING;
    dt.actionCols.add(set);
    ActionSetFieldCol set2 = new ActionSetFieldCol();
    set2.boundName = "f1";
    set2.factField = "goo2";
    set2.defaultValue = "whee";
    set2.type = DataType.TYPE_STRING;
    dt.actionCols.add(set2);
    dt.data = new String[][] { new String[] { "1", "desc", "metar1", "saliencer1", "c1r1", "c2r1", "c3r1", "c4r1", "a1r1", "a2r1", "a3r1", "a4r1" }, new String[] { "2", "desc", "metar2", "saliencer2", "c1r2", "c2r2", "c3r2", "c4r2", "a1r2", "a2r2", "a3r2", "a4r2" } };
    String[][] expected = new String[][] { new String[] { "1", "desc", "metar1", "saliencer1", "c1r1", "c2r1", "c3r1", "c4r1", "a1r1", "ret1", "a3r1", "a4r1" }, new String[] { "2", "desc", "metar2", "saliencer2", "c1r2", "c2r2", "c3r2", "c4r2", "a1r2", "ret1", "a3r2", "a4r2" } };
    GuidedDecisionTable52 tsdt = upgrader.upgrade(dt);
    assertEquals("michael", tsdt.getTableName());
    assertEquals(GuidedDecisionTable52.TableFormat.EXTENDED_ENTRY, tsdt.getTableFormat());
    assertEquals(1, tsdt.getMetadataCols().size());
    assertEquals("legacy", tsdt.getMetadataCols().get(0).getMetadata());
    assertEquals("yes", tsdt.getMetadataCols().get(0).getDefaultValue().getStringValue());
    assertEquals(1, tsdt.getAttributeCols().size());
    assertEquals("salience", tsdt.getAttributeCols().get(0).getAttribute());
    assertEquals("66", tsdt.getAttributeCols().get(0).getDefaultValue().getStringValue());
    assertEquals(2, tsdt.getConditions().size());
    assertEquals("f1", tsdt.getConditionPattern("f1").getBoundName());
    assertEquals("Driver", tsdt.getConditionPattern("f1").getFactType());
    assertEquals("f2", tsdt.getConditionPattern("f2").getBoundName());
    assertEquals("Driver", tsdt.getConditionPattern("f2").getFactType());
    assertEquals(3, tsdt.getConditionPattern("f1").getChildColumns().size());
    assertEquals(1, tsdt.getConditionPattern("f2").getChildColumns().size());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, tsdt.getConditionPattern("f1").getChildColumns().get(0).getConstraintValueType());
    assertEquals("age", tsdt.getConditionPattern("f1").getChildColumns().get(0).getFactField());
    assertEquals("Driver", tsdt.getPattern(tsdt.getConditionPattern("f1").getChildColumns().get(0)).getFactType());
    assertEquals("Driver f1 age", tsdt.getConditionPattern("f1").getChildColumns().get(0).getHeader());
    assertEquals("==", tsdt.getConditionPattern("f1").getChildColumns().get(0).getOperator());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, tsdt.getConditionPattern("f1").getChildColumns().get(1).getConstraintValueType());
    assertEquals("name", tsdt.getConditionPattern("f1").getChildColumns().get(1).getFactField());
    assertEquals("Driver", tsdt.getPattern(tsdt.getConditionPattern("f1").getChildColumns().get(1)).getFactType());
    assertEquals("Driver f1 name", tsdt.getConditionPattern("f1").getChildColumns().get(1).getHeader());
    assertEquals("==", tsdt.getConditionPattern("f1").getChildColumns().get(1).getOperator());
    assertEquals(BaseSingleFieldConstraint.TYPE_RET_VALUE, tsdt.getConditionPattern("f1").getChildColumns().get(2).getConstraintValueType());
    assertEquals("rating", tsdt.getConditionPattern("f1").getChildColumns().get(2).getFactField());
    assertEquals("Driver", tsdt.getPattern(tsdt.getConditionPattern("f1").getChildColumns().get(2)).getFactType());
    assertEquals("Driver rating", tsdt.getConditionPattern("f1").getChildColumns().get(2).getHeader());
    assertEquals("==", tsdt.getConditionPattern("f1").getChildColumns().get(2).getOperator());
    assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, tsdt.getConditionPattern("f2").getChildColumns().get(0).getConstraintValueType());
    assertEquals("(not needed)", tsdt.getConditionPattern("f2").getChildColumns().get(0).getFactField());
    assertEquals("Driver", tsdt.getPattern(tsdt.getConditionPattern("f2").getChildColumns().get(0)).getFactType());
    assertEquals("Driver 2 pimp", tsdt.getConditionPattern("f2").getChildColumns().get(0).getHeader());
    assertEquals(4, tsdt.getActionCols().size());
    ActionInsertFactCol52 a1 = (ActionInsertFactCol52) tsdt.getActionCols().get(0);
    assertEquals("ins", a1.getBoundName());
    assertEquals("Cheese", a1.getFactType());
    assertEquals("price", a1.getFactField());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, a1.getType());
    ActionRetractFactCol52 a2 = (ActionRetractFactCol52) tsdt.getActionCols().get(1);
    assertNotNull(a2);
    ActionSetFieldCol52 a3 = (ActionSetFieldCol52) tsdt.getActionCols().get(2);
    assertEquals("f1", a3.getBoundName());
    assertEquals("goo1", a3.getFactField());
    assertEquals(DataType.TYPE_STRING, a3.getType());
    ActionSetFieldCol52 a4 = (ActionSetFieldCol52) tsdt.getActionCols().get(3);
    assertEquals("f1", a4.getBoundName());
    assertEquals("goo2", a4.getFactField());
    assertEquals("whee", a4.getDefaultValue().getStringValue());
    assertEquals(DataType.TYPE_STRING, a4.getType());
    assertEquals(2, tsdt.getData().size());
    isRowEquivalent(tsdt.getData().get(0), expected[0]);
    isRowEquivalent(tsdt.getData().get(1), expected[1]);
}
Also used : GuidedDecisionTable(org.drools.workbench.models.guided.dtable.shared.model.legacy.GuidedDecisionTable) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) ActionInsertFactCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52) ActionRetractFactCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionRetractFactCol52) MetadataCol(org.drools.workbench.models.guided.dtable.shared.model.legacy.MetadataCol) ActionSetFieldCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionSetFieldCol52) ActionInsertFactCol(org.drools.workbench.models.guided.dtable.shared.model.legacy.ActionInsertFactCol) 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) ActionSetFieldCol(org.drools.workbench.models.guided.dtable.shared.model.legacy.ActionSetFieldCol) Test(org.junit.Test)

Example 5 with ConditionCol

use of org.drools.workbench.models.guided.dtable.shared.model.legacy.ConditionCol 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

ConditionCol (org.drools.workbench.models.guided.dtable.shared.model.legacy.ConditionCol)5 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)4 DTCellValue52 (org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52)3 ActionRetractFactCol (org.drools.workbench.models.guided.dtable.shared.model.legacy.ActionRetractFactCol)3 AttributeCol (org.drools.workbench.models.guided.dtable.shared.model.legacy.AttributeCol)3 GuidedDecisionTable (org.drools.workbench.models.guided.dtable.shared.model.legacy.GuidedDecisionTable)3 MetadataCol (org.drools.workbench.models.guided.dtable.shared.model.legacy.MetadataCol)3 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)2 ActionInsertFactCol52 (org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52)2 ActionRetractFactCol52 (org.drools.workbench.models.guided.dtable.shared.model.ActionRetractFactCol52)2 ActionSetFieldCol52 (org.drools.workbench.models.guided.dtable.shared.model.ActionSetFieldCol52)2 ActionInsertFactCol (org.drools.workbench.models.guided.dtable.shared.model.legacy.ActionInsertFactCol)2 ActionSetFieldCol (org.drools.workbench.models.guided.dtable.shared.model.legacy.ActionSetFieldCol)2 HashMap (java.util.HashMap)1 List (java.util.List)1 TreeMap (java.util.TreeMap)1 DescriptionCol52 (org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52)1 Pattern52 (org.drools.workbench.models.guided.dtable.shared.model.Pattern52)1