use of org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52 in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testRHSActionWorkItemSetFields1.
@Test
public // Test all Actions setting fields are correctly converted to RuleModel
void testRHSActionWorkItemSetFields1() {
GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
String[] row = new String[] { "1", "desc", "true", "true", "true", "true", "true" };
List<BaseColumn> allColumns = new ArrayList<BaseColumn>();
allColumns.add(new RowNumberCol52());
allColumns.add(new DescriptionCol52());
List<ActionCol52> cols = new ArrayList<ActionCol52>();
ActionWorkItemCol52 awi = new ActionWorkItemCol52();
PortableWorkDefinition pwd = new PortableWorkDefinition();
pwd.setName("WorkItem");
awi.setWorkItemDefinition(pwd);
PortableBooleanParameterDefinition p1 = new PortableBooleanParameterDefinition();
p1.setName("BooleanResult");
pwd.addResult(p1);
PortableFloatParameterDefinition p2 = new PortableFloatParameterDefinition();
p2.setName("FloatResult");
pwd.addResult(p2);
PortableIntegerParameterDefinition p3 = new PortableIntegerParameterDefinition();
p3.setName("IntegerResult");
pwd.addResult(p3);
PortableStringParameterDefinition p4 = new PortableStringParameterDefinition();
p4.setName("StringResult");
pwd.addResult(p4);
cols.add(awi);
ActionWorkItemSetFieldCol52 asf1 = new ActionWorkItemSetFieldCol52();
asf1.setBoundName("$r");
asf1.setFactField("ResultBooleanField");
asf1.setType(DataType.TYPE_BOOLEAN);
asf1.setWorkItemName("WorkItem");
asf1.setWorkItemResultParameterName("BooleanResult");
asf1.setParameterClassName(Boolean.class.getName());
cols.add(asf1);
ActionWorkItemSetFieldCol52 asf2 = new ActionWorkItemSetFieldCol52();
asf2.setBoundName("$r");
asf2.setFactField("ResultFloatField");
asf2.setType(DataType.TYPE_NUMERIC_FLOAT);
asf2.setWorkItemName("WorkItem");
asf2.setWorkItemResultParameterName("FloatResult");
asf2.setParameterClassName(Float.class.getName());
cols.add(asf2);
ActionWorkItemSetFieldCol52 asf3 = new ActionWorkItemSetFieldCol52();
asf3.setBoundName("$r");
asf3.setFactField("ResultIntegerField");
asf3.setType(DataType.TYPE_NUMERIC_INTEGER);
asf3.setWorkItemName("WorkItem");
asf3.setWorkItemResultParameterName("IntegerResult");
asf3.setParameterClassName(Integer.class.getName());
cols.add(asf3);
ActionWorkItemSetFieldCol52 asf4 = new ActionWorkItemSetFieldCol52();
asf4.setBoundName("$r");
asf4.setFactField("ResultStringField");
asf4.setType(DataType.TYPE_STRING);
asf4.setWorkItemName("WorkItem");
asf4.setWorkItemResultParameterName("StringResult");
asf4.setParameterClassName(String.class.getName());
cols.add(asf4);
RuleModel rm = new RuleModel();
allColumns.addAll(cols);
List<DTCellValue52> rowData = DataUtilities.makeDataRowList(row);
TemplateDataProvider rowDataProvider = new GuidedDTTemplateDataProvider(allColumns, rowData);
p.doActions(allColumns, cols, rowDataProvider, rowData, rm);
assertEquals(2, rm.rhs.length);
// Examine RuleModel actions
ActionExecuteWorkItem aw = (ActionExecuteWorkItem) rm.rhs[0];
assertNotNull(aw);
ActionSetField asf = (ActionSetField) rm.rhs[1];
assertNotNull(asf);
// Check ActionExecuteWorkItem
PortableWorkDefinition mpwd = aw.getWorkDefinition();
assertNotNull(mpwd);
assertEquals(4, mpwd.getResults().size());
PortableBooleanParameterDefinition mp1 = (PortableBooleanParameterDefinition) mpwd.getResult("BooleanResult");
assertNotNull(mp1);
PortableFloatParameterDefinition mp2 = (PortableFloatParameterDefinition) mpwd.getResult("FloatResult");
assertNotNull(mp2);
PortableIntegerParameterDefinition mp3 = (PortableIntegerParameterDefinition) mpwd.getResult("IntegerResult");
assertNotNull(mp3);
PortableStringParameterDefinition mp4 = (PortableStringParameterDefinition) mpwd.getResult("StringResult");
assertNotNull(mp4);
// Check ActionSetField
assertEquals(asf.getVariable(), "$r");
assertEquals(4, asf.getFieldValues().length);
ActionFieldValue fv1 = asf.getFieldValues()[0];
assertNotNull(fv1);
assertTrue(fv1 instanceof ActionWorkItemFieldValue);
ActionWorkItemFieldValue wifv1 = (ActionWorkItemFieldValue) fv1;
assertEquals("ResultBooleanField", wifv1.getField());
assertEquals(DataType.TYPE_BOOLEAN, wifv1.getType());
assertEquals("WorkItem", wifv1.getWorkItemName());
assertEquals("BooleanResult", wifv1.getWorkItemParameterName());
assertEquals(Boolean.class.getName(), wifv1.getWorkItemParameterClassName());
ActionFieldValue fv2 = asf.getFieldValues()[1];
assertNotNull(fv2);
assertTrue(fv2 instanceof ActionWorkItemFieldValue);
ActionWorkItemFieldValue wifv2 = (ActionWorkItemFieldValue) fv2;
assertEquals("ResultFloatField", wifv2.getField());
assertEquals(DataType.TYPE_NUMERIC_FLOAT, wifv2.getType());
assertEquals("WorkItem", wifv2.getWorkItemName());
assertEquals("FloatResult", wifv2.getWorkItemParameterName());
assertEquals(Float.class.getName(), wifv2.getWorkItemParameterClassName());
ActionFieldValue fv3 = asf.getFieldValues()[2];
assertNotNull(fv3);
assertTrue(fv3 instanceof ActionWorkItemFieldValue);
ActionWorkItemFieldValue wifv3 = (ActionWorkItemFieldValue) fv3;
assertEquals("ResultIntegerField", wifv3.getField());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, wifv3.getType());
assertEquals("WorkItem", wifv3.getWorkItemName());
assertEquals("IntegerResult", wifv3.getWorkItemParameterName());
assertEquals(Integer.class.getName(), wifv3.getWorkItemParameterClassName());
ActionFieldValue fv4 = asf.getFieldValues()[3];
assertNotNull(fv4);
assertTrue(fv4 instanceof ActionWorkItemFieldValue);
ActionWorkItemFieldValue wifv4 = (ActionWorkItemFieldValue) fv4;
assertEquals("ResultStringField", wifv4.getField());
assertEquals(DataType.TYPE_STRING, wifv4.getType());
assertEquals("WorkItem", wifv4.getWorkItemName());
assertEquals("StringResult", wifv4.getWorkItemParameterName());
assertEquals(String.class.getName(), wifv4.getWorkItemParameterClassName());
}
use of org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52 in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testRHSActionWorkItemSetFields2.
@Test
public // Test only Actions set to "true" are correctly converted to RuleModel
void testRHSActionWorkItemSetFields2() {
GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
String[] row = new String[] { "1", "desc", "true", "true", "false" };
List<BaseColumn> allColumns = new ArrayList<BaseColumn>();
allColumns.add(new RowNumberCol52());
allColumns.add(new DescriptionCol52());
List<ActionCol52> cols = new ArrayList<ActionCol52>();
ActionWorkItemCol52 awi = new ActionWorkItemCol52();
PortableWorkDefinition pwd = new PortableWorkDefinition();
pwd.setName("WorkItem");
awi.setWorkItemDefinition(pwd);
PortableBooleanParameterDefinition p1 = new PortableBooleanParameterDefinition();
p1.setName("BooleanResult");
pwd.addResult(p1);
PortableFloatParameterDefinition p2 = new PortableFloatParameterDefinition();
p2.setName("FloatResult");
pwd.addResult(p2);
cols.add(awi);
ActionWorkItemSetFieldCol52 asf1 = new ActionWorkItemSetFieldCol52();
asf1.setBoundName("$r");
asf1.setFactField("ResultBooleanField");
asf1.setType(DataType.TYPE_BOOLEAN);
asf1.setWorkItemName("WorkItem");
asf1.setWorkItemResultParameterName("BooleanResult");
asf1.setParameterClassName(Boolean.class.getName());
cols.add(asf1);
ActionWorkItemSetFieldCol52 asf2 = new ActionWorkItemSetFieldCol52();
asf2.setBoundName("$r");
asf2.setFactField("ResultFloatField");
asf2.setType(DataType.TYPE_NUMERIC_FLOAT);
asf2.setWorkItemName("WorkItem");
asf2.setWorkItemResultParameterName("FloatResult");
asf2.setParameterClassName(Float.class.getName());
cols.add(asf2);
RuleModel rm = new RuleModel();
allColumns.addAll(cols);
List<DTCellValue52> rowData = DataUtilities.makeDataRowList(row);
TemplateDataProvider rowDataProvider = new GuidedDTTemplateDataProvider(allColumns, rowData);
p.doActions(allColumns, cols, rowDataProvider, rowData, rm);
assertEquals(2, rm.rhs.length);
// Examine RuleModel actions
ActionExecuteWorkItem aw = (ActionExecuteWorkItem) rm.rhs[0];
assertNotNull(aw);
ActionSetField asf = (ActionSetField) rm.rhs[1];
assertNotNull(asf);
// Check ActionExecuteWorkItem
PortableWorkDefinition mpwd = aw.getWorkDefinition();
assertNotNull(mpwd);
assertEquals(2, mpwd.getResults().size());
PortableBooleanParameterDefinition mp1 = (PortableBooleanParameterDefinition) mpwd.getResult("BooleanResult");
assertNotNull(mp1);
PortableFloatParameterDefinition mp2 = (PortableFloatParameterDefinition) mpwd.getResult("FloatResult");
assertNotNull(mp2);
// Check ActionSetField
assertEquals(asf.getVariable(), "$r");
assertEquals(1, asf.getFieldValues().length);
ActionFieldValue fv1 = asf.getFieldValues()[0];
assertNotNull(fv1);
assertTrue(fv1 instanceof ActionWorkItemFieldValue);
ActionWorkItemFieldValue wifv1 = (ActionWorkItemFieldValue) fv1;
assertEquals("ResultBooleanField", wifv1.getField());
assertEquals(DataType.TYPE_BOOLEAN, wifv1.getType());
assertEquals("WorkItem", wifv1.getWorkItemName());
assertEquals("BooleanResult", wifv1.getWorkItemParameterName());
assertEquals(Boolean.class.getName(), wifv1.getWorkItemParameterClassName());
}
use of org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52 in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testRHS.
@Test
public void testRHS() {
GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
String[] row = new String[] { "1", "desc", "a", "a condition", "actionsetfield1", "actionupdatefield2", "retract", "actioninsertfact1", "actioninsertfact2" };
List<BaseColumn> allColumns = new ArrayList<BaseColumn>();
allColumns.add(new RowNumberCol52());
allColumns.add(new DescriptionCol52());
allColumns.add(new MetadataCol52());
allColumns.add(new ConditionCol52());
List<ActionCol52> cols = new ArrayList<ActionCol52>();
ActionSetFieldCol52 asf1 = new ActionSetFieldCol52();
asf1.setBoundName("a");
asf1.setFactField("field1");
asf1.setType(DataType.TYPE_STRING);
cols.add(asf1);
ActionSetFieldCol52 asf2 = new ActionSetFieldCol52();
asf2.setBoundName("a");
asf2.setFactField("field2");
asf2.setUpdate(true);
asf2.setType(DataType.TYPE_NUMERIC_INTEGER);
cols.add(asf2);
ActionRetractFactCol52 ret = new ActionRetractFactCol52();
cols.add(ret);
ActionInsertFactCol52 ins1 = new ActionInsertFactCol52();
ins1.setBoundName("ins");
ins1.setFactType("Cheese");
ins1.setFactField("price");
ins1.setType(DataType.TYPE_NUMERIC_INTEGER);
cols.add(ins1);
ActionInsertFactCol52 ins2 = new ActionInsertFactCol52();
ins2.setBoundName("ins");
ins2.setFactType("Cheese");
ins2.setFactField("type");
ins2.setType(DataType.TYPE_NUMERIC_INTEGER);
cols.add(ins2);
RuleModel rm = new RuleModel();
allColumns.addAll(cols);
List<DTCellValue52> rowData = DataUtilities.makeDataRowList(row);
TemplateDataProvider rowDataProvider = new GuidedDTTemplateDataProvider(allColumns, rowData);
p.doActions(allColumns, cols, rowDataProvider, rowData, rm);
assertEquals(4, rm.rhs.length);
// examine the set field action that is produced
ActionSetField a1 = (ActionSetField) rm.rhs[0];
assertEquals("a", a1.getVariable());
assertEquals(1, a1.getFieldValues().length);
assertEquals("field1", a1.getFieldValues()[0].getField());
assertEquals("actionsetfield1", a1.getFieldValues()[0].getValue());
assertEquals(DataType.TYPE_STRING, a1.getFieldValues()[0].getType());
ActionSetField a2 = (ActionSetField) rm.rhs[1];
assertEquals("a", a2.getVariable());
assertEquals(1, a2.getFieldValues().length);
assertEquals("field2", a2.getFieldValues()[0].getField());
assertEquals("actionupdatefield2", a2.getFieldValues()[0].getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, a2.getFieldValues()[0].getType());
// examine the retract
ActionRetractFact a3 = (ActionRetractFact) rm.rhs[2];
assertEquals("retract", a3.getVariableName());
// examine the insert
ActionInsertFact a4 = (ActionInsertFact) rm.rhs[3];
assertEquals("Cheese", a4.getFactType());
assertEquals(2, a4.getFieldValues().length);
assertEquals("price", a4.getFieldValues()[0].getField());
assertEquals("actioninsertfact1", a4.getFieldValues()[0].getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, a4.getFieldValues()[0].getType());
assertEquals("type", a4.getFieldValues()[1].getField());
assertEquals("actioninsertfact2", a4.getFieldValues()[1].getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, a4.getFieldValues()[1].getType());
}
use of org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52 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;
}
use of org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52 in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testLHSWithBRLColumn_ParseToDRL_NoVariables.
@Test
public // This test checks a Decision Table involving BRL columns is correctly converted into DRL
void testLHSWithBRLColumn_ParseToDRL_NoVariables() {
GuidedDecisionTable52 dtable = new GuidedDecisionTable52();
// Row 0 should become an IPattern in the resulting RuleModel as it contains getValue()s for all Template fields in the BRL Column
// Row 1 should *NOT* become an IPattern in the resulting RuleModel as it does *NOT* contain getValue()s for all Template fields in the BRL Column
Object[][] data = new Object[][] { new Object[] { "1", "desc", Boolean.TRUE }, new Object[] { "2", "desc", Boolean.FALSE } };
// Simple (mandatory) columns
dtable.setRowNumberCol(new RowNumberCol52());
dtable.setDescriptionCol(new DescriptionCol52());
// BRL Column
BRLConditionColumn brl1 = new BRLConditionColumn();
// BRL Column definition
List<IPattern> brl1Definition = new ArrayList<IPattern>();
FactPattern brl1DefinitionFactPattern1 = new FactPattern("Baddie");
SingleFieldConstraint brl1DefinitionFactPattern1Constraint1 = new SingleFieldConstraint();
brl1DefinitionFactPattern1Constraint1.setFieldType(DataType.TYPE_STRING);
brl1DefinitionFactPattern1Constraint1.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
brl1DefinitionFactPattern1Constraint1.setFieldName("name");
brl1DefinitionFactPattern1Constraint1.setOperator("==");
brl1DefinitionFactPattern1Constraint1.setValue("Gargamel");
brl1DefinitionFactPattern1.addConstraint(brl1DefinitionFactPattern1Constraint1);
brl1Definition.add(brl1DefinitionFactPattern1);
brl1.setDefinition(brl1Definition);
// Setup BRL column bindings
BRLConditionVariableColumn brl1Variable1 = new BRLConditionVariableColumn("", DataType.TYPE_BOOLEAN);
brl1.getChildColumns().add(brl1Variable1);
dtable.getConditions().add(brl1);
dtable.setData(DataUtilities.makeDataLists(data));
// Now to test conversion
int ruleStartIndex;
int pattern1StartIndex;
GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance();
String drl = p.marshal(dtable);
// Row 0
ruleStartIndex = drl.indexOf("//from row number: 1");
assertFalse(ruleStartIndex == -1);
pattern1StartIndex = drl.indexOf("Baddie( name == \"Gargamel\" )", ruleStartIndex);
assertFalse(pattern1StartIndex == -1);
// Row 1
ruleStartIndex = drl.indexOf("//from row number: 2");
assertFalse(ruleStartIndex == -1);
pattern1StartIndex = drl.indexOf("Baddie( name == \"Gargamel\" )", ruleStartIndex);
assertTrue(pattern1StartIndex == -1);
}
Aggregations