Search in sources :

Example 1 with BRLRuleModel

use of org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel in project drools by kiegroup.

the class GuidedDTDRLPersistence method marshal.

public String marshal(final GuidedDecisionTable52 originalDTable) {
    final GuidedDecisionTable52 dt = DecisionTableHitPolicyEnhancer.enhance(originalDTable);
    StringBuilder sb = new StringBuilder();
    List<List<DTCellValue52>> data = dt.getData();
    List<BaseColumn> allColumns = dt.getExpandedColumns();
    // Append package name and imports to DRL
    PackageNameWriter.write(sb, dt);
    ImportsWriter.write(sb, dt);
    // Build rules
    for (int i = 0; i < data.size(); i++) {
        List<DTCellValue52> row = data.get(i);
        // Specialised BRDRLPersistence provider than can handle template key expansion
        TemplateDataProvider rowDataProvider = new GuidedDTTemplateDataProvider(allColumns, row);
        Integer num = (Integer) row.get(0).getNumericValue();
        String desc = row.get(1).getStringValue();
        BRLRuleModel rm = new BRLRuleModel(dt);
        rm.name = getName(dt.getTableName(), num);
        doMetadata(allColumns, dt.getMetadataCols(), row, rm);
        doAttribs(allColumns, dt.getAttributeCols(), row, rm);
        doConditions(allColumns, dt.getConditions(), rowDataProvider, row, data, rm);
        doActions(allColumns, dt.getActionCols(), rowDataProvider, row, rm);
        if (dt.getParentName() != null) {
            rm.parentName = dt.getParentName();
        }
        sb.append("//from row number: " + (i + 1) + "\n");
        if (desc != null && desc.length() > 0) {
            sb.append("//" + desc + "\n");
        }
        GuidedDTBRDRLPersistence drlMarshaller = new GuidedDTBRDRLPersistence(rowDataProvider);
        String rule = drlMarshaller.marshal(rm);
        sb.append(rule);
        sb.append("\n");
    }
    return sb.toString();
}
Also used : GuidedDTBRDRLPersistence(org.drools.workbench.models.guided.dtable.backend.util.GuidedDTBRDRLPersistence) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) GuidedDTTemplateDataProvider(org.drools.workbench.models.guided.dtable.backend.util.GuidedDTTemplateDataProvider) DTCellValue52(org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52) BRLRuleModel(org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) List(java.util.List) ActionFieldList(org.drools.workbench.models.datamodel.rule.ActionFieldList) ArrayList(java.util.ArrayList) BaseColumn(org.drools.workbench.models.guided.dtable.shared.model.BaseColumn) GuidedDTTemplateDataProvider(org.drools.workbench.models.guided.dtable.backend.util.GuidedDTTemplateDataProvider) TemplateDataProvider(org.drools.workbench.models.guided.dtable.backend.util.TemplateDataProvider)

Example 2 with BRLRuleModel

use of org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel in project drools by kiegroup.

the class BRLRuleModelTest method testRuleModelRHSBoundFacts_NoDuplicates.

@Test
public void testRuleModelRHSBoundFacts_NoDuplicates() {
    GuidedDecisionTable52 dt = new GuidedDecisionTable52();
    // Setup Decision Table columns (with existing BRLActionColumn)
    ActionInsertFactCol52 ins = new ActionInsertFactCol52();
    ins.setBoundName("$ins");
    ins.setFactField("rating");
    ins.setFactType("Person");
    ins.setType(DataType.TYPE_STRING);
    dt.getActionCols().add(ins);
    BRLActionColumn brlAction = new BRLActionColumn();
    ActionInsertFact aif1 = new ActionInsertFact("Person");
    aif1.setBoundName("$aif");
    aif1.addFieldValue(new ActionFieldValue("rating", null, DataType.TYPE_STRING));
    aif1.getFieldValues()[0].setNature(BaseSingleFieldConstraint.TYPE_LITERAL);
    brlAction.getDefinition().add(aif1);
    dt.getActionCols().add(brlAction);
    // Setup RuleModel columns (existing BRLActionColumn being edited)
    BRLRuleModel model = new BRLRuleModel(dt);
    ActionInsertFact aif2 = new ActionInsertFact("Person");
    aif2.setBoundName("$aif");
    aif2.addFieldValue(new ActionFieldValue("rating", null, DataType.TYPE_STRING));
    aif2.getFieldValues()[0].setNature(BaseSingleFieldConstraint.TYPE_LITERAL);
    model.addRhsItem(aif2);
    // Checks
    assertNotNull(model.getRHSBoundFacts());
    assertEquals(2, model.getRHSBoundFacts().size());
    assertTrue(model.getRHSBoundFacts().contains("$ins"));
    assertTrue(model.getRHSBoundFacts().contains("$aif"));
    ActionInsertFact r1 = model.getRHSBoundFact("$ins");
    assertNotNull(r1);
    assertTrue(r1 instanceof ActionInsertFactCol52ActionInsertFactAdaptor);
    ActionInsertFactCol52ActionInsertFactAdaptor raif1 = (ActionInsertFactCol52ActionInsertFactAdaptor) r1;
    assertEquals("Person", raif1.getFactType());
    assertEquals("rating", raif1.getFieldValues()[0].getField());
    assertEquals(DataType.TYPE_STRING, raif1.getFieldValues()[0].getType());
    assertNull(raif1.getFieldValues()[0].getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, raif1.getFieldValues()[0].getNature());
    ActionInsertFact r2 = model.getRHSBoundFact("$aif");
    assertNotNull(r2);
    assertTrue(r2 instanceof ActionInsertFact);
    ActionInsertFact raif2 = (ActionInsertFact) r2;
    assertEquals("Person", raif2.getFactType());
    assertEquals("rating", raif2.getFieldValues()[0].getField());
    assertEquals(DataType.TYPE_STRING, raif2.getFieldValues()[0].getType());
    assertNull(raif2.getFieldValues()[0].getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, raif2.getFieldValues()[0].getNature());
}
Also used : BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) ActionInsertFactCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) ActionInsertFactCol52ActionInsertFactAdaptor(org.drools.workbench.models.guided.dtable.shared.model.adaptors.ActionInsertFactCol52ActionInsertFactAdaptor) BRLRuleModel(org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel) Test(org.junit.Test)

Example 3 with BRLRuleModel

use of org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel in project drools by kiegroup.

the class BRLRuleModelTest method testRuleModelWithRHSBoundFactsUsageWithBRLActionColumn.

@Test
public void testRuleModelWithRHSBoundFactsUsageWithBRLActionColumn() {
    final GuidedDecisionTable52 dt = new GuidedDecisionTable52();
    // Setup Decision Table columns
    final Pattern52 p1 = new Pattern52();
    p1.setFactType("Driver");
    p1.setBoundName("$d");
    dt.getConditions().add(p1);
    final BRLActionColumn brl = new BRLActionColumn();
    brl.setDefinition(Collections.singletonList(new ActionSetField() {

        {
            setVariable("$d");
        }
    }));
    dt.getActionCols().add(brl);
    final BRLRuleModel model = new BRLRuleModel(dt);
    // Checks
    assertTrue(model.isBoundFactUsed("$d"));
    assertFalse(model.isBoundFactUsed("$cheese"));
}
Also used : BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) Pattern52(org.drools.workbench.models.guided.dtable.shared.model.Pattern52) BRLRuleModel(org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel) Test(org.junit.Test)

Example 4 with BRLRuleModel

use of org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel in project drools by kiegroup.

the class BRLRuleModelTest method testRuleModelWithRHSBoundFactsUsageWithActionInsertFact.

@Test
public void testRuleModelWithRHSBoundFactsUsageWithActionInsertFact() {
    final GuidedDecisionTable52 dt = new GuidedDecisionTable52();
    // Setup Decision Table columns
    final Pattern52 p1 = new Pattern52();
    p1.setFactType("Driver");
    p1.setBoundName("$d");
    dt.getConditions().add(p1);
    final ActionInsertFactCol52 ins = new ActionInsertFactCol52();
    ins.setBoundName("$ins");
    ins.setFactField("rating");
    ins.setFactType("Person");
    ins.setType(DataType.TYPE_STRING);
    dt.getActionCols().add(ins);
    final BRLRuleModel model = new BRLRuleModel(dt);
    // Checks
    assertTrue(model.isBoundFactUsed("$ins"));
    assertFalse(model.isBoundFactUsed("$cheese"));
}
Also used : GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) Pattern52(org.drools.workbench.models.guided.dtable.shared.model.Pattern52) ActionInsertFactCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52) BRLRuleModel(org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel) Test(org.junit.Test)

Example 5 with BRLRuleModel

use of org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel in project drools by kiegroup.

the class BRLRuleModelTest method testRuleModelWithRHSBoundFactsUsageWithActionRetractFact.

@Test
public void testRuleModelWithRHSBoundFactsUsageWithActionRetractFact() {
    final GuidedDecisionTable52 dt = new GuidedDecisionTable52();
    // Setup Decision Table columns
    final Pattern52 p1 = new Pattern52();
    p1.setFactType("Driver");
    p1.setBoundName("$d");
    dt.getConditions().add(p1);
    final ActionRetractFactCol52 del = new ActionRetractFactCol52();
    dt.getActionCols().add(del);
    dt.getData().add(Arrays.asList(new DTCellValue52(1), new DTCellValue52("description"), new DTCellValue52("$d")));
    final BRLRuleModel model = new BRLRuleModel(dt);
    // Checks
    assertTrue(model.isBoundFactUsed("$d"));
    assertFalse(model.isBoundFactUsed("$cheese"));
}
Also used : GuidedDecisionTable52(org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52) Pattern52(org.drools.workbench.models.guided.dtable.shared.model.Pattern52) LimitedEntryActionRetractFactCol52(org.drools.workbench.models.guided.dtable.shared.model.LimitedEntryActionRetractFactCol52) ActionRetractFactCol52(org.drools.workbench.models.guided.dtable.shared.model.ActionRetractFactCol52) DTCellValue52(org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52) BRLRuleModel(org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel) Test(org.junit.Test)

Aggregations

BRLRuleModel (org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel)29 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)22 Pattern52 (org.drools.workbench.models.guided.dtable.shared.model.Pattern52)18 Test (org.junit.Test)17 ActionInsertFactCol52 (org.drools.workbench.models.guided.dtable.shared.model.ActionInsertFactCol52)10 ConditionCol52 (org.drools.workbench.models.guided.dtable.shared.model.ConditionCol52)10 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)9 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)7 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)7 PatternWrapper (org.drools.workbench.screens.guided.dtable.client.wizard.column.plugins.commons.PatternWrapper)6 HashSet (java.util.HashSet)5 BRLConditionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn)5 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)4 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)4 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)4 ActionInsertFactCol52ActionInsertFactAdaptor (org.drools.workbench.models.guided.dtable.shared.model.adaptors.ActionInsertFactCol52ActionInsertFactAdaptor)4 ArrayList (java.util.ArrayList)3 List (java.util.List)3 FieldConstraint (org.drools.workbench.models.datamodel.rule.FieldConstraint)3 ActionCol52 (org.drools.workbench.models.guided.dtable.shared.model.ActionCol52)3