use of org.drools.workbench.models.guided.dtable.shared.model.BaseColumn 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.BaseColumn in project drools by kiegroup.
the class GuidedDecisionTableUpgradeHelper3 method upgrade.
/**
* Convert the Default Values in the Decision Table model
*
* @param source
* @return The new DTModel
*/
public GuidedDecisionTable52 upgrade(GuidedDecisionTable52 source) {
final GuidedDecisionTable52 destination = source;
for (BaseColumn column : source.getExpandedColumns()) {
DTColumnConfig52 dtColumn = null;
if (column instanceof MetadataCol52) {
dtColumn = (DTColumnConfig52) column;
} else if (column instanceof AttributeCol52) {
dtColumn = (DTColumnConfig52) column;
} else if (column instanceof ConditionCol52) {
dtColumn = (DTColumnConfig52) column;
} else if (column instanceof ActionCol52) {
dtColumn = (DTColumnConfig52) column;
}
if (dtColumn instanceof LimitedEntryCol) {
dtColumn = null;
}
if (dtColumn instanceof BRLVariableColumn) {
dtColumn = null;
}
if (dtColumn != null) {
final String legacyDefaultValue = dtColumn.defaultValue;
if (legacyDefaultValue != null) {
dtColumn.setDefaultValue(new DTCellValue52(legacyDefaultValue));
dtColumn.defaultValue = null;
}
}
}
return destination;
}
use of org.drools.workbench.models.guided.dtable.shared.model.BaseColumn in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testLHSNotPattern.
@Test
public void testLHSNotPattern() {
GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
String[] row = new String[] { "1", "desc", "a", "mike", "33 + 1", "age > 6", "stilton" };
String[][] data = new String[1][];
data[0] = row;
List<BaseColumn> allColumns = new ArrayList<BaseColumn>();
List<CompositeColumn<? extends BaseColumn>> allPatterns = new ArrayList<CompositeColumn<? extends BaseColumn>>();
allColumns.add(new RowNumberCol52());
allColumns.add(new DescriptionCol52());
allColumns.add(new MetadataCol52());
Pattern52 p1 = new Pattern52();
p1.setNegated(true);
p1.setBoundName("p1");
p1.setFactType("Person");
allPatterns.add(p1);
ConditionCol52 col = new ConditionCol52();
col.setFactField("name");
col.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
col.setOperator("==");
p1.getChildColumns().add(col);
allColumns.add(col);
ConditionCol52 col2 = new ConditionCol52();
col2.setFactField("age");
col2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_RET_VALUE);
col2.setOperator("<");
p1.getChildColumns().add(col2);
allColumns.add(col2);
ConditionCol52 col3 = new ConditionCol52();
col3.setConstraintValueType(BaseSingleFieldConstraint.TYPE_PREDICATE);
p1.getChildColumns().add(col3);
allColumns.add(col3);
Pattern52 p2 = new Pattern52();
p2.setBoundName("c");
p2.setFactType("Cheese");
allPatterns.add(p2);
ConditionCol52 col4 = new ConditionCol52();
col4.setFactField("type");
col4.setOperator("==");
col4.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
p2.getChildColumns().add(col4);
allColumns.add(col4);
List<DTCellValue52> rowData = DataUtilities.makeDataRowList(row);
TemplateDataProvider rowDataProvider = new GuidedDTTemplateDataProvider(allColumns, rowData);
RuleModel rm = new RuleModel();
p.doConditions(allColumns, allPatterns, rowDataProvider, rowData, DataUtilities.makeDataLists(data), rm);
String drl = RuleModelDRLPersistenceImpl.getInstance().marshal(rm);
assertEquals(2, rm.lhs.length);
assertEquals("Person", ((FactPattern) rm.lhs[0]).getFactType());
assertEquals("p1", ((FactPattern) rm.lhs[0]).getBoundName());
assertEquals("Cheese", ((FactPattern) rm.lhs[1]).getFactType());
assertEquals("c", ((FactPattern) rm.lhs[1]).getBoundName());
// examine the first pattern
FactPattern person = (FactPattern) rm.lhs[0];
assertEquals(3, person.getConstraintList().getConstraints().length);
SingleFieldConstraint cons = (SingleFieldConstraint) person.getConstraint(0);
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cons.getConstraintValueType());
assertEquals("name", cons.getFieldName());
assertEquals("==", cons.getOperator());
assertEquals("mike", cons.getValue());
cons = (SingleFieldConstraint) person.getConstraint(1);
assertEquals(BaseSingleFieldConstraint.TYPE_RET_VALUE, cons.getConstraintValueType());
assertEquals("age", cons.getFieldName());
assertEquals("<", cons.getOperator());
assertEquals("33 + 1", cons.getValue());
assertNull(cons.getFieldBinding());
cons = (SingleFieldConstraint) person.getConstraint(2);
assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, cons.getConstraintValueType());
assertEquals("age > 6", cons.getValue());
assertEquals(person.isNegated(), true);
assertTrue(drl.indexOf("not Person(") > 0);
// examine the second pattern
FactPattern cheese = (FactPattern) rm.lhs[1];
assertEquals(1, cheese.getConstraintList().getConstraints().length);
cons = (SingleFieldConstraint) cheese.getConstraint(0);
assertEquals("type", cons.getFieldName());
assertEquals("==", cons.getOperator());
assertEquals("stilton", cons.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cons.getConstraintValueType());
assertEquals(cheese.isNegated(), false);
assertTrue(drl.indexOf("c : Cheese(") > 0);
}
use of org.drools.workbench.models.guided.dtable.shared.model.BaseColumn in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testLHSOtherwisePatternNumeric.
@Test
public void testLHSOtherwisePatternNumeric() {
GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
String[][] row = new String[3][];
String[][] data = new String[3][];
row[0] = new String[] { "1", "desc1", "1", "1" };
List<DTCellValue52> rowDTModel0 = DataUtilities.makeDataRowList(row[0]);
data[0] = row[0];
row[1] = new String[] { "2", "desc2", "2", "2" };
List<DTCellValue52> rowDTModel1 = DataUtilities.makeDataRowList(row[1]);
data[1] = row[1];
row[2] = new String[] { "3", "desc3", null, null };
List<DTCellValue52> rowDTModel2 = DataUtilities.makeDataRowList(row[2]);
rowDTModel2.get(2).setOtherwise(true);
rowDTModel2.get(3).setOtherwise(true);
data[2] = row[2];
final List<List<DTCellValue52>> allDTData = new ArrayList<List<DTCellValue52>>() {
{
add(rowDTModel0);
add(rowDTModel1);
add(rowDTModel2);
}
};
List<BaseColumn> allColumns = new ArrayList<BaseColumn>();
List<CompositeColumn<? extends BaseColumn>> allPatterns = new ArrayList<CompositeColumn<? extends BaseColumn>>();
allColumns.add(new RowNumberCol52());
allColumns.add(new DescriptionCol52());
Pattern52 p1 = new Pattern52();
p1.setBoundName("p1");
p1.setFactType("Person");
allPatterns.add(p1);
ConditionCol52 col = new ConditionCol52();
col.setFactField("age");
col.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
col.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
col.setOperator("==");
p1.getChildColumns().add(col);
allColumns.add(col);
Pattern52 p2 = new Pattern52();
p2.setBoundName("p2");
p2.setFactType("Person");
allPatterns.add(p2);
ConditionCol52 col2 = new ConditionCol52();
col2.setFactField("age");
col2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
col2.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
col2.setOperator("!=");
p2.getChildColumns().add(col2);
allColumns.add(col2);
RuleModel rm = new RuleModel();
TemplateDataProvider rowDataProvider0 = new GuidedDTTemplateDataProvider(allColumns, rowDTModel0);
p.doConditions(allColumns, allPatterns, rowDataProvider0, rowDTModel0, allDTData, rm);
String drl0 = RuleModelDRLPersistenceImpl.getInstance().marshal(rm);
assertEquals(2, rm.lhs.length);
assertEquals("Person", ((FactPattern) rm.lhs[0]).getFactType());
assertEquals("p1", ((FactPattern) rm.lhs[0]).getBoundName());
assertEquals("Person", ((FactPattern) rm.lhs[1]).getFactType());
assertEquals("p2", ((FactPattern) rm.lhs[1]).getBoundName());
assertTrue(drl0.indexOf("p1 : Person( age == 1 )") > 0);
assertTrue(drl0.indexOf("p2 : Person( age != 1 )") > 0);
TemplateDataProvider rowDataProvider1 = new GuidedDTTemplateDataProvider(allColumns, rowDTModel1);
p.doConditions(allColumns, allPatterns, rowDataProvider1, rowDTModel1, allDTData, rm);
String drl1 = RuleModelDRLPersistenceImpl.getInstance().marshal(rm);
assertEquals(2, rm.lhs.length);
assertEquals("Person", ((FactPattern) rm.lhs[0]).getFactType());
assertEquals("p1", ((FactPattern) rm.lhs[0]).getBoundName());
assertEquals("Person", ((FactPattern) rm.lhs[1]).getFactType());
assertEquals("p2", ((FactPattern) rm.lhs[1]).getBoundName());
assertTrue(drl1.indexOf("p1 : Person( age == 2 )") > 0);
assertTrue(drl1.indexOf("p2 : Person( age != 2 )") > 0);
TemplateDataProvider rowDataProvider2 = new GuidedDTTemplateDataProvider(allColumns, rowDTModel2);
p.doConditions(allColumns, allPatterns, rowDataProvider2, rowDTModel2, allDTData, rm);
String drl2 = RuleModelDRLPersistenceImpl.getInstance().marshal(rm);
assertEquals(2, rm.lhs.length);
assertEquals("Person", ((FactPattern) rm.lhs[0]).getFactType());
assertEquals("p1", ((FactPattern) rm.lhs[0]).getBoundName());
assertEquals("Person", ((FactPattern) rm.lhs[1]).getFactType());
assertEquals("p2", ((FactPattern) rm.lhs[1]).getBoundName());
assertTrue(drl2.indexOf("p1 : Person( age not in ( 1, 2 )") > 0);
assertTrue(drl2.indexOf("p2 : Person( age in ( 1, 2 )") > 0);
}
use of org.drools.workbench.models.guided.dtable.shared.model.BaseColumn in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method multipleLHSNotPatternInclusion.
@Test
public void multipleLHSNotPatternInclusion() {
GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
Object[] row = new Object[] { "1", "desc", "mike", true, true };
Object[][] data = new Object[1][];
data[0] = row;
List<BaseColumn> allColumns = new ArrayList<>();
List<CompositeColumn<? extends BaseColumn>> allPatterns = new ArrayList<>();
allColumns.add(new RowNumberCol52());
allColumns.add(new DescriptionCol52());
Pattern52 p1 = new Pattern52();
p1.setBoundName("p1");
p1.setFactType("Person");
allPatterns.add(p1);
ConditionCol52 p1col = new ConditionCol52();
p1col.setFactField("name");
p1col.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
p1col.setOperator("==");
p1.getChildColumns().add(p1col);
allColumns.add(p1col);
Pattern52 p2 = new Pattern52();
p2.setBoundName("");
p2.setNegated(true);
p2.setFactType("Cheese");
allPatterns.add(p2);
ConditionCol52 p2col = new ConditionCol52();
p2col.setFactField("this");
p2col.setOperator("");
p2col.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
p2.getChildColumns().add(p2col);
allColumns.add(p2col);
Pattern52 p3 = new Pattern52();
p3.setBoundName("");
p3.setNegated(true);
p3.setFactType("Smurf");
allPatterns.add(p3);
ConditionCol52 p3col = new ConditionCol52();
p3col.setFactField("this");
p3col.setOperator("!= null");
p3col.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
p3.getChildColumns().add(p3col);
allColumns.add(p3col);
List<DTCellValue52> rowData = DataUtilities.makeDataRowList(row);
TemplateDataProvider rowDataProvider = new GuidedDTTemplateDataProvider(allColumns, rowData);
RuleModel rm = new RuleModel();
rm.name = "r0";
p.doConditions(allColumns, allPatterns, rowDataProvider, rowData, DataUtilities.makeDataLists(data), rm);
final String actualRuleModelDrl = RuleModelDRLPersistenceImpl.getInstance().marshal(rm);
final String expectedRuleModelDrl = "rule \"r0\"\n" + " dialect \"mvel\"\n" + " when\n" + " p1 : Person( name == \"mike\" )\n" + " not Cheese()\n" + " not Smurf( this != null )\n" + " then\n" + "end\n";
assertEqualsIgnoreWhitespace(expectedRuleModelDrl, actualRuleModelDrl);
final GuidedDecisionTable52 dt = new GuidedDecisionTable52();
dt.setTableName("dt");
dt.getConditions().add(p1);
dt.getConditions().add(p2);
dt.getConditions().add(p3);
dt.getData().addAll(DataUtilities.makeDataLists(data));
final String actualDecisionTableDrl = GuidedDTDRLPersistence.getInstance().marshal(dt);
final String expectedDecisionTableDrl = "//from row number: 1\n" + "//desc\n" + "rule \"Row1dt\"\n" + " dialect \"mvel\"\n" + " when\n" + " p1 : Person( name == \"mike\" )\n" + " not Cheese()\n" + " not Smurf( this != null )\n" + " then\n" + "end\n";
assertEqualsIgnoreWhitespace(expectedDecisionTableDrl, actualDecisionTableDrl);
}
Aggregations