Search in sources :

Example 1 with GuidedDTBRDRLPersistence

use of org.drools.workbench.models.guided.dtable.backend.util.GuidedDTBRDRLPersistence 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 GuidedDTBRDRLPersistence

use of org.drools.workbench.models.guided.dtable.backend.util.GuidedDTBRDRLPersistence in project drools by kiegroup.

the class GuidedDTBRDRLPersistenceTest method testRHSWithTemplateAwareIAction.

@Test
public void testRHSWithTemplateAwareIAction() {
    GuidedDTBRDRLPersistence persistence = new GuidedDTBRDRLPersistence((key) -> "value");
    RuleModel ruleModel = new RuleModel();
    ruleModel.name = "Template aware";
    ruleModel.addRhsItem(new TemplateAwareIAction("initialValue"));
    String result = persistence.marshal(ruleModel);
    String expected = "rule \"Template aware\"\n" + "\tdialect \"mvel\"\n" + "\twhen\n" + "\tthen\n" + "\t\tsubstitutedValue;\n" + "end\n";
    assertEquals(expected, result);
}
Also used : GuidedDTBRDRLPersistence(org.drools.workbench.models.guided.dtable.backend.util.GuidedDTBRDRLPersistence) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Aggregations

GuidedDTBRDRLPersistence (org.drools.workbench.models.guided.dtable.backend.util.GuidedDTBRDRLPersistence)2 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ActionFieldList (org.drools.workbench.models.datamodel.rule.ActionFieldList)1 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)1 FieldConstraint (org.drools.workbench.models.datamodel.rule.FieldConstraint)1 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)1 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)1 GuidedDTTemplateDataProvider (org.drools.workbench.models.guided.dtable.backend.util.GuidedDTTemplateDataProvider)1 TemplateDataProvider (org.drools.workbench.models.guided.dtable.backend.util.TemplateDataProvider)1 BRLRuleModel (org.drools.workbench.models.guided.dtable.shared.model.BRLRuleModel)1 BaseColumn (org.drools.workbench.models.guided.dtable.shared.model.BaseColumn)1 DTCellValue52 (org.drools.workbench.models.guided.dtable.shared.model.DTCellValue52)1 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)1 Test (org.junit.Test)1