use of org.drools.workbench.models.datamodel.rule.IAction 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.datamodel.rule.IAction 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.datamodel.rule.IAction in project drools by kiegroup.
the class GuidedDTDRLPersistence method hasVariables.
private boolean hasVariables(BRLActionColumn column) {
Map<InterpolationVariable, Integer> ivs = new HashMap<InterpolationVariable, Integer>();
RuleModel rm = new RuleModel();
for (IAction action : column.getDefinition()) {
rm.addRhsItem(action);
}
RuleModelVisitor rmv = new RuleModelVisitor(ivs);
rmv.visit(rm);
return ivs.size() > 0;
}
use of org.drools.workbench.models.datamodel.rule.IAction in project drools by kiegroup.
the class GuidedDTDRLPersistence method doAction.
private void doAction(List<BaseColumn> allColumns, LimitedEntryBRLActionColumn column, List<LabelledAction> actions, TemplateDataProvider rowDataProvider, List<DTCellValue52> row, RuleModel rm) {
final int index = allColumns.indexOf(column);
final DTCellValue52 dcv = row.get(index);
if (dcv.getBooleanValue()) {
for (IAction action : column.getDefinition()) {
addAction(action, actions);
}
}
}
use of org.drools.workbench.models.datamodel.rule.IAction 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);
}
}
}
}
Aggregations