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();
}
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());
}
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"));
}
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"));
}
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"));
}
Aggregations