Search in sources :

Example 86 with IPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSInOperatorFieldNameNotContainingInLiteral.

@Test
public void testLHSInOperatorFieldNameNotContainingInLiteral() {
    String drl = "package org.test\n" + "rule \"in\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( field1 in (1, 2) )\n" + "then\n" + "end";
    addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    IPattern p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    FactPattern fp = (FactPattern) p;
    assertEquals("Person", fp.getFactType());
    assertEquals(1, fp.getConstraintList().getConstraints().length);
    assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
    assertEquals("Person", sfp.getFactType());
    assertEquals("field1", sfp.getFieldName());
    assertEquals("in", sfp.getOperator());
    assertEquals("1, 2", sfp.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 87 with IPattern

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

the class GuidedDTDRLPersistenceTest method testLHSWithBRLColumn_ParseToDRL_FreeFormLine.

@Test
public // This test checks a Decision Table involving BRL columns is correctly converted into DRL
void testLHSWithBRLColumn_ParseToDRL_FreeFormLine() {
    GuidedDecisionTable52 dtable = new GuidedDecisionTable52();
    // Row 0 should become an IPattern in the resulting RuleModel as it contains values for all Template fields in the BRL Column
    // Row 1 should *NOT* become an IPattern in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column
    // Row 2 should *NOT* become an IPattern in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column
    // Row 3 should *NOT* become an IPattern in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column
    String[][] data = new String[][] { new String[] { "1", "desc", "Pupa", "50" }, new String[] { "2", "desc", "", "50" }, new String[] { "3", "desc", "Pupa", "" }, new String[] { "4", "desc", "", "" } };
    // Simple (mandatory) columns
    dtable.setRowNumberCol(new RowNumberCol52());
    dtable.setDescriptionCol(new DescriptionCol52());
    // BRL Column
    BRLConditionColumn brl1 = new BRLConditionColumn();
    // BRL Column definition
    List<IPattern> brl1Definition = new ArrayList<IPattern>();
    FreeFormLine brl1DefinitionFreeFormLine = new FreeFormLine();
    brl1DefinitionFreeFormLine.setText("Smurf( name == \"@{name}\", age == @{age} )");
    brl1Definition.add(brl1DefinitionFreeFormLine);
    brl1.setDefinition(brl1Definition);
    // Setup BRL column bindings
    BRLConditionVariableColumn brl1Variable1 = new BRLConditionVariableColumn("name", DataType.TYPE_STRING);
    BRLConditionVariableColumn brl1Variable2 = new BRLConditionVariableColumn("age", DataType.TYPE_NUMERIC_INTEGER);
    brl1.getChildColumns().add(brl1Variable1);
    brl1.getChildColumns().add(brl1Variable2);
    dtable.getConditions().add(brl1);
    dtable.setData(DataUtilities.makeDataLists(data));
    // Now to test conversion
    int ruleStartIndex;
    int pattern1StartIndex;
    GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance();
    String drl = p.marshal(dtable);
    // Row 0
    ruleStartIndex = drl.indexOf("//from row number: 1");
    assertFalse(ruleStartIndex == -1);
    pattern1StartIndex = drl.indexOf("Smurf( name == \"Pupa\", age == 50 )", ruleStartIndex);
    assertFalse(pattern1StartIndex == -1);
    // Row 1
    ruleStartIndex = drl.indexOf("//from row number: 2");
    assertFalse(ruleStartIndex == -1);
    pattern1StartIndex = drl.indexOf("Smurf(", ruleStartIndex);
    assertTrue(pattern1StartIndex == -1);
    // Row 2
    ruleStartIndex = drl.indexOf("//from row number: 3");
    assertFalse(ruleStartIndex == -1);
    pattern1StartIndex = drl.indexOf("Smurf(", ruleStartIndex);
    assertTrue(pattern1StartIndex == -1);
    // Row 3
    ruleStartIndex = drl.indexOf("//from row number: 4");
    assertFalse(ruleStartIndex == -1);
    pattern1StartIndex = drl.indexOf("Smurf(", ruleStartIndex);
    assertTrue(pattern1StartIndex == -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) ArrayList(java.util.ArrayList) BRLConditionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) DescriptionCol52(org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52) RowNumberCol52(org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52) Test(org.junit.Test)

Example 88 with IPattern

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

the class GuidedDTDRLPersistence method doCondition.

private void doCondition(List<BaseColumn> allColumns, Pattern52 pattern, List<IPattern> patterns, List<DTCellValue52> row, List<List<DTCellValue52>> data, RuleModel rm) {
    List<ConditionCol52> cols = pattern.getChildColumns();
    for (ConditionCol52 c : cols) {
        int index = allColumns.indexOf(c);
        DTCellValue52 dcv = row.get(index);
        String cell = "";
        if (c instanceof LimitedEntryCol) {
            if (Boolean.TRUE.equals(dcv.getBooleanValue())) {
                LimitedEntryCol lec = (LimitedEntryCol) c;
                DTCellValue52 value = lec.getValue();
                if (value != null) {
                    cell = GuidedDTDRLUtilities.convertDTCellValueToString(value);
                }
            }
        } else {
            cell = GuidedDTDRLUtilities.convertDTCellValueToString(dcv);
        }
        boolean isOtherwise = dcv.isOtherwise();
        boolean isValid = isOtherwise;
        // Otherwise values are automatically valid as they're constructed from the other rules
        if (!isOtherwise) {
            isValid = validCell(cell, dcv.getDataType());
        }
        // If operator is "== null" or "!= null" add constraint if table value is true
        if (c.getOperator() != null && (c.getOperator().equals("== null") || c.getOperator().equals("!= null"))) {
            isValid = Boolean.TRUE.equals(dcv.getBooleanValue());
        }
        if (isValid) {
            // get or create the pattern it belongs too
            IPattern ifp = findByFactPattern(patterns, pattern);
            // If the pattern does not exist create one suitable
            if (ifp == null) {
                FactPattern fp = new FactPattern(pattern.getFactType());
                fp.setBoundName(pattern.getBoundName());
                fp.setNegated(pattern.isNegated());
                fp.setWindow(pattern.getWindow());
                if (pattern.getEntryPointName() != null && pattern.getEntryPointName().length() > 0) {
                    FromEntryPointFactPattern fep = new FromEntryPointFactPattern();
                    fep.setEntryPointName(pattern.getEntryPointName());
                    fep.setFactPattern(fp);
                    patterns.add(fep);
                    ifp = fep;
                } else {
                    patterns.add(fp);
                    ifp = fp;
                }
            }
            // Extract the FactPattern from the IFactPattern
            FactPattern fp;
            if (ifp instanceof FactPattern) {
                fp = (FactPattern) ifp;
            } else if (ifp instanceof FromEntryPointFactPattern) {
                FromEntryPointFactPattern fep = (FromEntryPointFactPattern) ifp;
                fp = fep.getFactPattern();
            } else {
                throw new IllegalArgumentException("Inexpected IFactPattern implementation found.");
            }
            // Add the constraint from this cell
            switch(c.getConstraintValueType()) {
                case BaseSingleFieldConstraint.TYPE_LITERAL:
                case BaseSingleFieldConstraint.TYPE_RET_VALUE:
                    if (!isOtherwise) {
                        FieldConstraint fc = makeSingleFieldConstraint(c, cell);
                        fp.addConstraint(fc);
                    } else {
                        FieldConstraint fc = makeSingleFieldConstraint(c, allColumns, data);
                        fp.addConstraint(fc);
                    }
                    break;
                case BaseSingleFieldConstraint.TYPE_PREDICATE:
                    SingleFieldConstraint pred = new SingleFieldConstraint();
                    pred.setConstraintValueType(c.getConstraintValueType());
                    if (c.getFactField() != null && c.getFactField().indexOf("$param") > -1) {
                        // handle interpolation
                        pred.setValue(c.getFactField().replace("$param", cell));
                    } else {
                        pred.setValue(cell);
                    }
                    fp.addConstraint(pred);
                    break;
                default:
                    throw new IllegalArgumentException("Unknown constraintValueType: " + c.getConstraintValueType());
            }
        }
    }
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) 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) LimitedEntryCol(org.drools.workbench.models.guided.dtable.shared.model.LimitedEntryCol) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ConditionCol52(org.drools.workbench.models.guided.dtable.shared.model.ConditionCol52) 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 89 with IPattern

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

the class GuidedDTDRLPersistence method doCondition.

private void doCondition(List<BaseColumn> allColumns, LimitedEntryBRLConditionColumn column, List<IPattern> patterns, TemplateDataProvider rowDataProvider, List<DTCellValue52> row, RuleModel rm) {
    final int index = allColumns.indexOf(column);
    final DTCellValue52 dcv = row.get(index);
    if (dcv.getBooleanValue()) {
        for (IPattern pattern : column.getDefinition()) {
            patterns.add(pattern);
        }
    }
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) 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 90 with IPattern

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

the class GuidedDTDRLPersistence method doFindByNegatedFactPattern.

private IPattern doFindByNegatedFactPattern(final List<IPattern> patterns, final Pattern52 find) {
    final String factType = find.getFactType();
    for (IPattern ifp : patterns) {
        if (ifp instanceof FactPattern) {
            final FactPattern fp = (FactPattern) ifp;
            final Optional<IPattern> match = checkNegatedFactPattern(fp, factType);
            if (match.isPresent()) {
                return match.get();
            }
        } else if (ifp instanceof FromEntryPointFactPattern) {
            final FromEntryPointFactPattern fefp = (FromEntryPointFactPattern) ifp;
            final FactPattern fp = fefp.getFactPattern();
            final Optional<IPattern> match = checkNegatedFactPattern(fp, factType);
            if (match.isPresent()) {
                return match.get();
            }
        }
    }
    return null;
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) Optional(java.util.Optional) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern)

Aggregations

IPattern (org.drools.workbench.models.datamodel.rule.IPattern)118 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)95 Test (org.junit.Test)92 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)84 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)83 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)75 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)75 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)75 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)71 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)58 IAction (org.drools.workbench.models.datamodel.rule.IAction)34 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)30 ArrayList (java.util.ArrayList)23 BRLConditionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn)23 BRLConditionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn)22 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)20 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)16 DescriptionCol52 (org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52)16 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)16 RowNumberCol52 (org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52)16