use of org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testRHSWithBRLColumn_ParseToDRL_MultipleActions.
@Test
public // This test checks a Decision Table involving BRL columns is correctly converted into DRL
void testRHSWithBRLColumn_ParseToDRL_MultipleActions() {
GuidedDecisionTable52 dtable = new GuidedDecisionTable52();
// All three rows are entered, some columns with optional data
String[][] data = new String[][] { new String[] { "1", "desc", "Pupa", "50" }, new String[] { "2", "desc", "", "50" }, new String[] { "3", "desc", "Pupa", "" } };
// Simple (mandatory) columns
dtable.setRowNumberCol(new RowNumberCol52());
dtable.setDescriptionCol(new DescriptionCol52());
// BRL Column
BRLActionColumn brl1 = new BRLActionColumn();
// BRL Column definition
List<IAction> brl1Definition = new ArrayList<IAction>();
ActionInsertFact brl1DefinitionAction1 = new ActionInsertFact("Baddie");
ActionFieldValue brl1DefinitionAction1FieldValue1 = new ActionFieldValue("name", "Gargamel", DataType.TYPE_STRING);
brl1DefinitionAction1FieldValue1.setNature(BaseSingleFieldConstraint.TYPE_LITERAL);
brl1DefinitionAction1.addFieldValue(brl1DefinitionAction1FieldValue1);
brl1Definition.add(brl1DefinitionAction1);
ActionInsertFact brl1DefinitionAction2 = new ActionInsertFact("Smurf");
ActionFieldValue brl1DefinitionAction2FieldValue1 = new ActionFieldValue("name", "$name", DataType.TYPE_STRING);
brl1DefinitionAction2FieldValue1.setNature(BaseSingleFieldConstraint.TYPE_TEMPLATE);
brl1DefinitionAction2.addFieldValue(brl1DefinitionAction2FieldValue1);
ActionFieldValue brl1DefinitionAction2FieldValue2 = new ActionFieldValue("age", "$age", DataType.TYPE_NUMERIC_INTEGER);
brl1DefinitionAction2FieldValue2.setNature(BaseSingleFieldConstraint.TYPE_TEMPLATE);
brl1DefinitionAction2.addFieldValue(brl1DefinitionAction2FieldValue2);
brl1Definition.add(brl1DefinitionAction2);
brl1.setDefinition(brl1Definition);
// Setup BRL column bindings
BRLActionVariableColumn brl1Variable1 = new BRLActionVariableColumn("$name", DataType.TYPE_STRING, "Person", "name");
brl1.getChildColumns().add(brl1Variable1);
BRLActionVariableColumn brl1Variable2 = new BRLActionVariableColumn("$age", DataType.TYPE_NUMERIC_INTEGER, "Person", "age");
brl1.getChildColumns().add(brl1Variable2);
dtable.getActionCols().add(brl1);
dtable.setData(DataUtilities.makeDataLists(data));
// Now to test conversion
int ruleStartIndex;
int action1StartIndex;
int action2StartIndex;
GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance();
String drl = p.marshal(dtable);
// Row 0
ruleStartIndex = drl.indexOf("//from row number: 1");
assertFalse(ruleStartIndex == -1);
action1StartIndex = drl.indexOf("Baddie fact0 = new Baddie();", ruleStartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("fact0.setName( \"Gargamel\" );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("insert( fact0 );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action2StartIndex = drl.indexOf("Smurf fact1 = new Smurf();", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact1.setName( \"Pupa\" );", action2StartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact1.setAge( 50 );", action2StartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("insert( fact1 );", action2StartIndex);
assertFalse(action2StartIndex == -1);
// Row 1
ruleStartIndex = drl.indexOf("//from row number: 2");
int ruleEndIndex = drl.indexOf("//from row number: 3");
assertFalse(ruleStartIndex == -1);
action1StartIndex = drl.indexOf("Baddie fact0 = new Baddie();", ruleStartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("fact0.setName( \"Gargamel\" );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("insert( fact0 );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action2StartIndex = drl.indexOf("Smurf fact1 = new Smurf();", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact1.setName( \"Pupa\" );", ruleStartIndex);
assertFalse(action2StartIndex < ruleEndIndex);
action2StartIndex = drl.indexOf("fact1.setAge( 50 );", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("insert( fact1 );", ruleStartIndex);
assertFalse(action2StartIndex == -1);
// Row 2
ruleStartIndex = drl.indexOf("//from row number: 3");
assertFalse(ruleStartIndex == -1);
action1StartIndex = drl.indexOf("Baddie fact0 = new Baddie();", ruleStartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("fact0.setName( \"Gargamel\" );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("insert( fact0 );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action2StartIndex = drl.indexOf("Smurf fact1 = new Smurf();", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact1.setName( \"Pupa\" );", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact1.setAge( 50 );", ruleStartIndex);
assertTrue(action2StartIndex == -1);
action2StartIndex = drl.indexOf("insert( fact1 );", ruleStartIndex);
assertFalse(action2StartIndex == -1);
}
use of org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testRHSWithBRLColumn_ParseToDRL_NoVariables.
@Test
public // This test checks a Decision Table involving BRL columns is correctly converted into DRL
void testRHSWithBRLColumn_ParseToDRL_NoVariables() {
GuidedDecisionTable52 dtable = new GuidedDecisionTable52();
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
BRLActionColumn brl1 = new BRLActionColumn();
// BRL Column definition
List<IAction> brl1Definition = new ArrayList<IAction>();
ActionInsertFact brl1DefinitionAction1 = new ActionInsertFact("Baddie");
ActionFieldValue brl1DefinitionAction1FieldValue1 = new ActionFieldValue("name", "Gargamel", DataType.TYPE_STRING);
brl1DefinitionAction1FieldValue1.setNature(BaseSingleFieldConstraint.TYPE_LITERAL);
brl1DefinitionAction1.addFieldValue(brl1DefinitionAction1FieldValue1);
brl1Definition.add(brl1DefinitionAction1);
brl1.setDefinition(brl1Definition);
// Setup BRL column bindings
BRLActionVariableColumn brl1Variable1 = new BRLActionVariableColumn("", DataType.TYPE_BOOLEAN);
brl1.getChildColumns().add(brl1Variable1);
dtable.getActionCols().add(brl1);
dtable.setData(DataUtilities.makeDataLists(data));
// Now to test conversion
int ruleStartIndex;
int action1StartIndex;
GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance();
String drl = p.marshal(dtable);
// Row 0
ruleStartIndex = drl.indexOf("//from row number: 1");
assertFalse(ruleStartIndex == -1);
action1StartIndex = drl.indexOf("Baddie fact0 = new Baddie();", ruleStartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("fact0.setName( \"Gargamel\" );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("insert( fact0 );", action1StartIndex);
assertFalse(action1StartIndex == -1);
// Row 1
ruleStartIndex = drl.indexOf("//from row number: 2");
assertFalse(ruleStartIndex == -1);
action1StartIndex = drl.indexOf("Baddie fact0 = new Baddie();", ruleStartIndex);
assertTrue(action1StartIndex == -1);
action1StartIndex = drl.indexOf("fact0.setName( \"Gargamel\" );", ruleStartIndex);
assertTrue(action1StartIndex == -1);
action1StartIndex = drl.indexOf("insert( fact0 );", ruleStartIndex);
assertTrue(action1StartIndex == -1);
}
use of org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn in project drools by kiegroup.
the class GuidedDTDRLPersistence method doAction.
private void doAction(List<BaseColumn> allColumns, BRLActionColumn column, List<LabelledAction> actions, TemplateDataProvider rowDataProvider, List<DTCellValue52> row, RuleModel rm) {
// Check whether the parameter-less BRL fragment needs inclusion
if (!hasVariables(column)) {
final BRLActionVariableColumn variableColumn = column.getChildColumns().get(0);
final int index = allColumns.indexOf(variableColumn);
final DTCellValue52 dcv = row.get(index);
if (dcv.getBooleanValue()) {
for (IAction action : column.getDefinition()) {
addAction(action, actions);
}
}
} else {
for (IAction action : column.getDefinition()) {
boolean addAction = false;
// Get interpolation variables used by the Action
Map<InterpolationVariable, Integer> ivs = new HashMap<InterpolationVariable, Integer>();
RuleModelVisitor rmv = new RuleModelVisitor(action, ivs);
rmv.visit(action);
if (ivs.size() == 0) {
addAction = true;
} else if (ivs.size() > 0) {
// Ensure every key has a value and substitute keys for values
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 (action instanceof FreeFormLine) {
addAction = templateKeyCount == ivs.size();
} else if (templateKeyCount > 0) {
addAction = true;
}
}
if (addAction) {
addAction(action, actions);
}
}
}
}
use of org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn in project drools by kiegroup.
the class BRLRuleModelTest method testRHSNonEmptyStringValues.
@Test
public void testRHSNonEmptyStringValues() {
GuidedDecisionTable52 dt = new GuidedDecisionTable52();
dt.setTableFormat(GuidedDecisionTable52.TableFormat.EXTENDED_ENTRY);
dt.setTableName("extended-entry");
Pattern52 p1 = new Pattern52();
p1.setBoundName("p1");
p1.setFactType("Smurf");
BRLActionColumn brlAction = new BRLActionColumn();
ActionUpdateField auf1 = new ActionUpdateField("p1");
auf1.addFieldValue(new ActionFieldValue("name", "$name", DataType.TYPE_STRING));
auf1.getFieldValues()[0].setNature(BaseSingleFieldConstraint.TYPE_TEMPLATE);
ActionUpdateField auf2 = new ActionUpdateField("p1");
auf2.addFieldValue(new ActionFieldValue("age", "$age", DataType.TYPE_NUMERIC_INTEGER));
auf2.getFieldValues()[0].setNature(BaseSingleFieldConstraint.TYPE_TEMPLATE);
brlAction.getDefinition().add(auf1);
brlAction.getDefinition().add(auf2);
brlAction.getChildColumns().add(new BRLActionVariableColumn("$name", DataType.TYPE_STRING, "Smurf", "name"));
brlAction.getChildColumns().add(new BRLActionVariableColumn("$age", DataType.TYPE_NUMERIC_INTEGER, "Smurf", "age"));
dt.getActionCols().add(brlAction);
// Test 1
dt.setData(DataUtilities.makeDataLists(new Object[][] { new Object[] { 1l, "desc-row1", null, null } }));
String drl1 = GuidedDTDRLPersistence.getInstance().marshal(dt);
final String expected1 = "//from row number: 1\n" + "//desc-row1\n" + "rule \"Row 1 extended-entry\"\n" + " dialect \"mvel\"\n" + " when\n" + " then\n" + "end";
assertEqualsIgnoreWhitespace(expected1, drl1);
// Test 2
dt.setData(DataUtilities.makeDataLists(new Object[][] { new Object[] { 2l, "desc-row2", " ", 35l } }));
String drl2 = GuidedDTDRLPersistence.getInstance().marshal(dt);
final String expected2 = "//from row number: 1\n" + "//desc-row2\n" + "rule \"Row 2 extended-entry\"\n" + " dialect \"mvel\"\n" + " when\n" + " then\n" + " modify( p1 ) {\n" + " setAge( 35 )\n" + " }\n" + "end";
assertEqualsIgnoreWhitespace(expected2, drl2);
// Test 3
dt.setData(DataUtilities.makeDataLists(new Object[][] { new Object[] { 3l, "desc-row3", "", null } }));
String drl3 = GuidedDTDRLPersistence.getInstance().marshal(dt);
final String expected3 = "//from row number: 1\n" + "//desc-row3\n" + "rule \"Row 3 extended-entry\"\n" + " dialect \"mvel\"\n" + " when\n" + " then\n" + "end";
assertEqualsIgnoreWhitespace(expected3, drl3);
// Test 4
dt.setData(DataUtilities.makeDataLists(new Object[][] { new Object[] { 4l, "desc-row4", "", 35l } }));
String drl4 = GuidedDTDRLPersistence.getInstance().marshal(dt);
final String expected4 = "//from row number: 1\n" + "//desc-row4\n" + "rule \"Row 4 extended-entry\"\n" + " dialect \"mvel\"\n" + " when\n" + " then\n" + " modify( p1 ) {\n" + " setAge( 35 )\n" + " }\n" + "end";
assertEqualsIgnoreWhitespace(expected4, drl4);
}
use of org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn in project drools-wb by kiegroup.
the class DecisionTableAnalyzerTest method testInsertBRLActionColumn.
@Test
public void testInsertBRLActionColumn() throws Exception {
BRLActionColumn brlActionOne = new BRLActionColumn();
BRLActionVariableColumn a = new BRLActionVariableColumn();
BRLActionVariableColumn b = new BRLActionVariableColumn();
BRLActionColumn brlActionTwo = new BRLActionColumn();
BRLActionVariableColumn c = new BRLActionVariableColumn();
BRLActionVariableColumn d = new BRLActionVariableColumn();
brlActionOne.setChildColumns(Arrays.asList(a, b));
brlActionTwo.setChildColumns(Arrays.asList(c, d));
model.setActionCols(Arrays.asList(brlActionOne, brlActionTwo));
decisionTableAnalyzer.insertColumn(brlActionOne);
verify(updateManager).newColumn(model, 2);
}
Aggregations