Search in sources :

Example 36 with BRLActionVariableColumn

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);
}
Also used : GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) IAction(org.drools.workbench.models.datamodel.rule.IAction) ArrayList(java.util.ArrayList) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) DescriptionCol52(org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn) RowNumberCol52(org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52) Test(org.junit.Test)

Example 37 with BRLActionVariableColumn

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);
}
Also used : GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) IAction(org.drools.workbench.models.datamodel.rule.IAction) ArrayList(java.util.ArrayList) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) DescriptionCol52(org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn) RowNumberCol52(org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52) Test(org.junit.Test)

Example 38 with BRLActionVariableColumn

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);
            }
        }
    }
}
Also used : FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) IAction(org.drools.workbench.models.datamodel.rule.IAction) RuleModelVisitor(org.drools.workbench.models.datamodel.rule.visitors.RuleModelVisitor) InterpolationVariable(org.drools.workbench.models.datamodel.rule.InterpolationVariable) HashMap(java.util.HashMap) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn) DTCellValue52(org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Example 39 with BRLActionVariableColumn

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);
}
Also used : BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) Pattern52(org.drools.workbench.models.guided.dtable.shared.model.Pattern52) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn) Test(org.junit.Test)

Example 40 with BRLActionVariableColumn

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);
}
Also used : BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn) Test(org.junit.Test)

Aggregations

BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)63 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)51 Test (org.junit.Test)38 ArrayList (java.util.ArrayList)27 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)27 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)21 IAction (org.drools.workbench.models.datamodel.rule.IAction)19 DescriptionCol52 (org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52)18 RowNumberCol52 (org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52)18 BaseColumn (org.drools.workbench.models.guided.dtable.shared.model.BaseColumn)17 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)16 BRLConditionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn)15 BRLConditionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn)14 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)11 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)11 Pattern52 (org.drools.workbench.models.guided.dtable.shared.model.Pattern52)11 IOException (java.io.IOException)10 InputStream (java.io.InputStream)10 ExcelParser (org.drools.decisiontable.parser.xls.ExcelParser)10 DataListener (org.drools.template.parser.DataListener)10