Search in sources :

Example 21 with FreeFormLine

use of org.drools.workbench.models.datamodel.rule.FreeFormLine in project drools by kiegroup.

the class BRLRuleModelTest method testLHSNonEmptyStringValuesFreeFormLine.

@Test
public void testLHSNonEmptyStringValuesFreeFormLine() {
    GuidedDecisionTable52 dt = new GuidedDecisionTable52();
    dt.setTableFormat(GuidedDecisionTable52.TableFormat.EXTENDED_ENTRY);
    dt.setTableName("extended-entry");
    BRLConditionColumn brlCondition = new BRLConditionColumn();
    FreeFormLine ffl = new FreeFormLine();
    ffl.setText("p1 : Smurf( name ==\"@{$f1}\", age == @{$f2} )");
    brlCondition.getDefinition().add(ffl);
    brlCondition.getChildColumns().add(new BRLConditionVariableColumn("$f1", DataType.TYPE_STRING, "Smurf", "name"));
    brlCondition.getChildColumns().add(new BRLConditionVariableColumn("$f2", DataType.TYPE_NUMERIC_INTEGER, "Smurf", "age"));
    dt.getConditions().add(brlCondition);
    GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance();
    // Test 1
    dt.setData(DataUtilities.makeDataLists(new Object[][] { new Object[] { 1l, "desc-row1", "Pupa", null } }));
    String drl1 = p.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", null, 35l } }));
    String drl2 = p.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" + "end";
    assertEqualsIgnoreWhitespace(expected2, drl2);
    // Test 3
    dt.setData(DataUtilities.makeDataLists(new Object[][] { new Object[] { 3l, "desc-row3", "Pupa", 35l } }));
    String drl3 = p.marshal(dt);
    final String expected3 = "//from row number: 1\n" + "//desc-row3\n" + "rule \"Row 3 extended-entry\"\n" + "  dialect \"mvel\"\n" + "  when\n" + "    p1 : Smurf( name == \"Pupa\", age == 35 )\n" + "  then\n" + "end";
    assertEqualsIgnoreWhitespace(expected3, drl3);
    // Test 4
    dt.setData(DataUtilities.makeDataLists(new Object[][] { new Object[] { 4l, "desc-row4", null, null } }));
    String drl4 = p.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" + "end";
    assertEqualsIgnoreWhitespace(expected4, drl4);
}
Also used : BRLConditionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) BRLConditionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn) Test(org.junit.Test)

Example 22 with FreeFormLine

use of org.drools.workbench.models.datamodel.rule.FreeFormLine in project drools-wb by kiegroup.

the class GuidedDecisionTableGeneratorListenerTest method testMissingTemplateKeyValues_NonStringFields.

@Test
public // https://issues.jboss.org/browse/GUVNOR-2030
void testMissingTemplateKeyValues_NonStringFields() {
    final ConversionResult result = new ConversionResult();
    final List<DataListener> listeners = new ArrayList<DataListener>();
    addModelField("org.test.Client", "this", "org.test.Client", DataType.TYPE_THIS);
    addModelField("org.test.Client", "monthlyTransactions", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
    final GuidedDecisionTableGeneratorListener listener = new GuidedDecisionTableGeneratorListener(result, dmo);
    listeners.add(listener);
    // Convert
    final ExcelParser parser = new ExcelParser(listeners);
    final InputStream is = this.getClass().getResourceAsStream("GUVNOR-2030 (DecisionTable).xls");
    try {
        parser.parseFile(is);
    } finally {
        try {
            is.close();
        } catch (IOException ioe) {
            fail(ioe.getMessage());
        }
    }
    // Check conversion results
    assertEquals(0, result.getMessages().size());
    // Check basics
    final List<GuidedDecisionTable52> dtables = listener.getGuidedDecisionTables();
    assertNotNull(dtables);
    assertEquals(1, dtables.size());
    GuidedDecisionTable52 dtable = dtables.get(0);
    assertEquals("Steps", dtable.getTableName());
    assertEquals(GuidedDecisionTable52.TableFormat.EXTENDED_ENTRY, dtable.getTableFormat());
    // Check expanded columns
    List<BaseColumn> columns = dtable.getExpandedColumns();
    assertNotNull(columns);
    assertEquals(5, columns.size());
    assertTrue(columns.get(0) instanceof RowNumberCol52);
    assertTrue(columns.get(1) instanceof DescriptionCol52);
    assertTrue(columns.get(2) instanceof BRLConditionVariableColumn);
    assertTrue(columns.get(3) instanceof BRLConditionVariableColumn);
    assertTrue(columns.get(4) instanceof BRLActionVariableColumn);
    // Check individual condition columns
    assertEquals(1, dtable.getConditions().size());
    assertTrue(dtable.getConditions().get(0) instanceof BRLConditionColumn);
    // Column 1
    BRLConditionColumn conditionCol0 = ((BRLConditionColumn) dtable.getConditions().get(0));
    assertEquals("Converted from ['From', 'To']", conditionCol0.getHeader());
    assertEquals(2, conditionCol0.getChildColumns().size());
    List<IPattern> conditionCol0definition = conditionCol0.getDefinition();
    assertEquals(1, conditionCol0definition.size());
    assertTrue(conditionCol0definition.get(0) instanceof FactPattern);
    FactPattern conditionCol0fp = (FactPattern) conditionCol0definition.get(0);
    assertEquals("Client", conditionCol0fp.getFactType());
    assertEquals(2, conditionCol0fp.getNumberOfConstraints());
    assertTrue(conditionCol0fp.getConstraint(0) instanceof SingleFieldConstraint);
    final SingleFieldConstraint conditionCol0fpsfc0 = (SingleFieldConstraint) conditionCol0fp.getConstraint(0);
    assertEquals("monthlyTransactions", conditionCol0fpsfc0.getFieldName());
    assertEquals(">=", conditionCol0fpsfc0.getOperator());
    assertEquals("param1", conditionCol0fpsfc0.getValue());
    assertEquals(SingleFieldConstraint.TYPE_TEMPLATE, conditionCol0fpsfc0.getConstraintValueType());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, conditionCol0fpsfc0.getFieldType());
    assertTrue(conditionCol0fp.getConstraint(1) instanceof SingleFieldConstraint);
    final SingleFieldConstraint conditionCol0fpsfc1 = (SingleFieldConstraint) conditionCol0fp.getConstraint(1);
    assertEquals("monthlyTransactions", conditionCol0fpsfc1.getFieldName());
    assertEquals("<=", conditionCol0fpsfc1.getOperator());
    assertEquals("param2", conditionCol0fpsfc1.getValue());
    assertEquals(SingleFieldConstraint.TYPE_TEMPLATE, conditionCol0fpsfc1.getConstraintValueType());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, conditionCol0fpsfc1.getFieldType());
    // Column 1 - Variable 1
    BRLConditionVariableColumn conditionCol0param0 = conditionCol0.getChildColumns().get(0);
    assertEquals("param1", conditionCol0param0.getVarName());
    assertEquals("From", conditionCol0param0.getHeader());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, conditionCol0param0.getFieldType());
    assertEquals("Client", conditionCol0param0.getFactType());
    assertEquals("monthlyTransactions", conditionCol0param0.getFactField());
    // Column 1 - Variable 2
    BRLConditionVariableColumn conditionCol0param1 = conditionCol0.getChildColumns().get(1);
    assertEquals("param2", conditionCol0param1.getVarName());
    assertEquals("To", conditionCol0param1.getHeader());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, conditionCol0param1.getFieldType());
    assertEquals("Client", conditionCol0param1.getFactType());
    assertEquals("monthlyTransactions", conditionCol0param1.getFactField());
    // Check individual action columns
    assertEquals(1, dtable.getActionCols().size());
    assertTrue(dtable.getActionCols().get(0) instanceof BRLActionColumn);
    // Column 2
    BRLActionColumn actionCol0 = ((BRLActionColumn) dtable.getActionCols().get(0));
    assertEquals("Converted from ['Step']", actionCol0.getHeader());
    assertEquals(1, actionCol0.getChildColumns().size());
    List<IAction> actionCol0definition = actionCol0.getDefinition();
    assertEquals(1, actionCol0definition.size());
    assertTrue(actionCol0definition.get(0) instanceof FreeFormLine);
    FreeFormLine actionCol0ffl = (FreeFormLine) actionCol0definition.get(0);
    assertEquals("if (!$c.isPricingStepCustom() && ($c.getPricingStep() == null || $c.getPricingStep().compareTo(\"@{param3}\") < 0)) { modify($c) { setPricingStep(\"@{param3}\"); } };", actionCol0ffl.getText());
    // Check data
    assertEquals(1, dtable.getData().size());
    assertTrue(isRowEquivalent(new Object[] { 1, "Created from row 12", 0, 100, "A1" }, dtable.getData().get(0)));
}
Also used : BRLConditionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn) IPattern(org.drools.workbench.models.datamodel.rule.IPattern) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) ArrayList(java.util.ArrayList) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) BRLConditionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) DescriptionCol52(org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52) ExcelParser(org.drools.decisiontable.parser.xls.ExcelParser) BaseColumn(org.drools.workbench.models.guided.dtable.shared.model.BaseColumn) IAction(org.drools.workbench.models.datamodel.rule.IAction) InputStream(java.io.InputStream) IOException(java.io.IOException) BigInteger(java.math.BigInteger) ConversionResult(org.drools.workbench.models.guided.dtable.shared.conversion.ConversionResult) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) DataListener(org.drools.template.parser.DataListener) 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 23 with FreeFormLine

use of org.drools.workbench.models.datamodel.rule.FreeFormLine in project drools-wb by kiegroup.

the class GuidedDecisionTableGeneratorListenerTest method testMultipleSingleParameters.

@Test
public void testMultipleSingleParameters() {
    final ConversionResult result = new ConversionResult();
    final List<DataListener> listeners = new ArrayList<DataListener>();
    final GuidedDecisionTableGeneratorListener listener = new GuidedDecisionTableGeneratorListener(result, dmo);
    listeners.add(listener);
    // Convert
    final ExcelParser parser = new ExcelParser(listeners);
    final InputStream is = this.getClass().getResourceAsStream("MultipleSingleParameters.xls");
    try {
        parser.parseFile(is);
    } finally {
        try {
            is.close();
        } catch (IOException ioe) {
            fail(ioe.getMessage());
        }
    }
    // Check conversion results
    assertEquals(0, result.getMessages().size());
    // Check basics
    List<GuidedDecisionTable52> dtables = listener.getGuidedDecisionTables();
    assertNotNull(dtables);
    assertEquals(1, dtables.size());
    GuidedDecisionTable52 dtable = dtables.get(0);
    assertEquals("MultipleSingleParameters", dtable.getTableName());
    assertEquals(GuidedDecisionTable52.TableFormat.EXTENDED_ENTRY, dtable.getTableFormat());
    // Check expanded columns
    List<BaseColumn> columns = dtable.getExpandedColumns();
    assertNotNull(columns);
    assertEquals(3, columns.size());
    assertTrue(columns.get(0) instanceof RowNumberCol52);
    assertTrue(columns.get(1) instanceof DescriptionCol52);
    assertTrue(columns.get(2) instanceof BRLConditionVariableColumn);
    // Check individual condition columns
    assertEquals(1, dtable.getConditions().size());
    assertTrue(dtable.getConditions().get(0) instanceof BRLConditionColumn);
    // Column 1
    BRLConditionColumn conditionCol0 = ((BRLConditionColumn) dtable.getConditions().get(0));
    assertEquals("Converted from ['Re-using single parameter']", conditionCol0.getHeader());
    assertEquals(1, conditionCol0.getChildColumns().size());
    List<IPattern> conditionCol0definition = conditionCol0.getDefinition();
    assertEquals(1, conditionCol0definition.size());
    assertTrue(conditionCol0definition.get(0) instanceof FreeFormLine);
    FreeFormLine conditionCol0ffl = (FreeFormLine) conditionCol0definition.get(0);
    assertEquals("Driver(@{param1} != null, @{param1} == true)", conditionCol0ffl.getText());
    // Column 1 - Variable 1
    BRLConditionVariableColumn conditionCol0param0 = conditionCol0.getChildColumns().get(0);
    assertEquals("param1", conditionCol0param0.getVarName());
    assertEquals("Re-using single parameter", conditionCol0param0.getHeader());
    assertEquals(DataType.TYPE_OBJECT, conditionCol0param0.getFieldType());
    assertNull(conditionCol0param0.getFactType());
    assertNull(conditionCol0param0.getFactField());
    // Check data
    assertEquals(2, dtable.getData().size());
    assertTrue(isRowEquivalent(new Object[] { 1, "Row 1", "isQualified" }, dtable.getData().get(0)));
    assertTrue(isRowEquivalent(new Object[] { 2, "Row 2", "isLicensed" }, dtable.getData().get(1)));
}
Also used : BRLConditionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn) IPattern(org.drools.workbench.models.datamodel.rule.IPattern) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) BRLConditionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn) ConversionResult(org.drools.workbench.models.guided.dtable.shared.conversion.ConversionResult) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) DescriptionCol52(org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52) ExcelParser(org.drools.decisiontable.parser.xls.ExcelParser) DataListener(org.drools.template.parser.DataListener) BaseColumn(org.drools.workbench.models.guided.dtable.shared.model.BaseColumn) RowNumberCol52(org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52) Test(org.junit.Test)

Example 24 with FreeFormLine

use of org.drools.workbench.models.datamodel.rule.FreeFormLine in project drools-wb by kiegroup.

the class GuidedRuleEditorServiceImplTest method testLoadModifyScoreAction.

@Test
public void testLoadModifyScoreAction() throws Exception {
    final Path path = getPath("modifyScore.rdrl");
    final String ruleFileContent = getFileContent("modifyScore.rdrl");
    doReturn(ruleFileContent).when(ioService).readAllString(any());
    final RuleModel model = service.load(path);
    Assertions.assertThat(model.rhs).hasSize(1);
    final FreeFormLine action = (FreeFormLine) model.rhs[0];
    Assertions.assertThat(action.getText()).isEqualTo(MODIFY_SCORE_ACTION);
}
Also used : Path(org.uberfire.backend.vfs.Path) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 25 with FreeFormLine

use of org.drools.workbench.models.datamodel.rule.FreeFormLine in project drools-wb by kiegroup.

the class GuidedRuleEditorServiceImplTest method testValidateModifyScoreAction.

@Test
public void testValidateModifyScoreAction() throws Exception {
    final Path path = mock(Path.class);
    final FreeFormLine modifyAction = new FreeFormLine();
    modifyAction.setText(MODIFY_SCORE_ACTION);
    final RuleModel model = new RuleModel();
    model.addRhsItem(modifyAction);
    service.validate(path, model);
    final ArgumentCaptor<String> capturedFileContent = ArgumentCaptor.forClass(String.class);
    verify(genericValidator).validate(eq(path), capturedFileContent.capture());
    Assertions.assertThat(capturedFileContent.getValue()).contains(MODIFY_SCORE_ACTION);
}
Also used : Path(org.uberfire.backend.vfs.Path) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Aggregations

FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)64 Test (org.junit.Test)49 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)29 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)24 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)22 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)22 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)18 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)18 IAction (org.drools.workbench.models.datamodel.rule.IAction)17 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)14 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)14 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)12 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)11 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)11 ArrayList (java.util.ArrayList)10 BRLConditionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn)10 DescriptionCol52 (org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52)10 RowNumberCol52 (org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52)10 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)9 BRLConditionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn)9