use of org.drools.workbench.models.datamodel.rule.FreeFormLine in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testFreeFormLine.
@Test
public void testFreeFormLine() {
RuleModel m = new RuleModel();
m.name = "with composite";
m.lhs = new IPattern[1];
m.rhs = new IAction[1];
FreeFormLine fl = new FreeFormLine();
fl.setText("Person()");
m.lhs[0] = fl;
FreeFormLine fr = new FreeFormLine();
fr.setText("fun()");
m.rhs[0] = fr;
String drl = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertNotNull(drl);
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
RuleModel m_ = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
assertEquals(1, m_.lhs.length);
assertEquals(1, m_.rhs.length);
assertEquals("Person", ((FactPattern) m_.lhs[0]).getFactType());
assertEquals("fun()", ((FreeFormLine) m_.rhs[0]).getText());
}
use of org.drools.workbench.models.datamodel.rule.FreeFormLine in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testFreeFormatDRLAction.
@Test
public void testFreeFormatDRLAction() {
final String drl = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Applicant( )\n" + "then\n" + "$a.setName( \"Michael\" );\n" + "Here's something typed by the user as free-format DRL\n" + "$a.setAge( 40 );\n" + "end\n";
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
assertNotNull(m);
// LHS
assertEquals(1, m.lhs.length);
assertTrue(m.lhs[0] instanceof FreeFormLine);
final FreeFormLine ffl1 = (FreeFormLine) m.lhs[0];
assertEquals("$a : Applicant( )", ffl1.getText());
// RHS
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof FreeFormLine);
final FreeFormLine ffl2 = (FreeFormLine) m.rhs[0];
assertEquals("$a.setName( \"Michael\" );\n" + "Here's something typed by the user as free-format DRL\n" + "$a.setAge( 40 );", ffl2.getText());
}
use of org.drools.workbench.models.datamodel.rule.FreeFormLine in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testFreeFormatDRLCondition.
@Test
public void testFreeFormatDRLCondition() {
final String drl = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Applicant( )\n" + "Here's something typed by the user as free-format DRL\n" + "$b : Bananna( )\n" + "then\n" + "end\n";
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
assertNotNull(m);
// LHS
assertEquals(3, m.lhs.length);
// Condition line 1
assertTrue(m.lhs[0] instanceof FactPattern);
final FactPattern fp1 = (FactPattern) m.lhs[0];
assertEquals("$a", fp1.getBoundName());
assertEquals("Applicant", fp1.getFactType());
// Condition line 2
assertTrue(m.lhs[1] instanceof FreeFormLine);
final FreeFormLine ffl = (FreeFormLine) m.lhs[1];
assertEquals("Here's something typed by the user as free-format DRL", ffl.getText());
// Condition line 3
assertTrue(m.lhs[2] instanceof FactPattern);
final FactPattern fp2 = (FactPattern) m.lhs[2];
assertEquals("$b", fp2.getBoundName());
assertEquals("Bananna", fp2.getFactType());
}
use of org.drools.workbench.models.datamodel.rule.FreeFormLine in project drools by kiegroup.
the class GuidedDTDRLPersistence method doCondition.
private void doCondition(List<BaseColumn> allColumns, BRLConditionColumn column, List<IPattern> patterns, TemplateDataProvider rowDataProvider, List<DTCellValue52> row, RuleModel rm) {
// Check whether the parameter-less BRL fragment needs inclusion
if (!hasVariables(column)) {
final BRLConditionVariableColumn variableColumn = column.getChildColumns().get(0);
final int index = allColumns.indexOf(variableColumn);
final DTCellValue52 dcv = row.get(index);
if (dcv != null && dcv.getBooleanValue()) {
for (IPattern pattern : column.getDefinition()) {
patterns.add(pattern);
}
}
} else {
for (IPattern pattern : column.getDefinition()) {
boolean addPattern = false;
// Get interpolation variables used by the Pattern
Map<InterpolationVariable, Integer> ivs = new HashMap<InterpolationVariable, Integer>();
RuleModelVisitor rmv = new RuleModelVisitor(pattern, ivs);
rmv.visit(pattern);
if (ivs.size() == 0) {
addPattern = true;
} else if (ivs.size() > 0) {
int templateKeyCount = 0;
for (InterpolationVariable variable : ivs.keySet()) {
String value = rowDataProvider.getTemplateKeyValue(variable.getVarName());
if (!"".equals(value)) {
templateKeyCount++;
}
}
// Ensure at least one key has a value (FreeFormLines need all values to be provided)
if (pattern instanceof FreeFormLine) {
addPattern = templateKeyCount == ivs.size();
} else if (templateKeyCount > 0) {
addPattern = true;
}
}
if (addPattern) {
patterns.add(pattern);
}
}
}
}
use of org.drools.workbench.models.datamodel.rule.FreeFormLine in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testRHSWithBRLColumn_ParseToDRL_FreeFormLine.
@Test
public // This test checks a Decision Table involving BRL columns is correctly converted into DRL
void testRHSWithBRLColumn_ParseToDRL_FreeFormLine() {
GuidedDecisionTable52 dtable = new GuidedDecisionTable52();
// Row 0 should become an IAction in the resulting RuleModel as it contains values for all Template fields in the BRL Column
// Row 1 should *NOT* become an IAction in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column
// Row 2 should *NOT* become an IAction in the resulting RuleModel as it does *NOT* contain values for all Template fields in the BRL Column
// Row 3 should *NOT* become an IAction 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 Action
BRLActionColumn brl1 = new BRLActionColumn();
// BRL Action definition
List<IAction> brl1Definition = new ArrayList<IAction>();
FreeFormLine brl1DefinitionFreeFormLine = new FreeFormLine();
brl1DefinitionFreeFormLine.setText("System.out.println( \"name == @{name}, age == @{age}\" );");
brl1Definition.add(brl1DefinitionFreeFormLine);
brl1.setDefinition(brl1Definition);
// Setup BRL column bindings
BRLActionVariableColumn brl1Variable1 = new BRLActionVariableColumn("name", DataType.TYPE_STRING);
BRLActionVariableColumn brl1Variable2 = new BRLActionVariableColumn("age", DataType.TYPE_NUMERIC_INTEGER);
brl1.getChildColumns().add(brl1Variable1);
brl1.getChildColumns().add(brl1Variable2);
dtable.getActionCols().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("System.out.println( \"name == Pupa, age == 50\" );", ruleStartIndex);
assertFalse(pattern1StartIndex == -1);
// Row 1
ruleStartIndex = drl.indexOf("//from row number: 2");
assertFalse(ruleStartIndex == -1);
pattern1StartIndex = drl.indexOf("System.out.println(", ruleStartIndex);
assertTrue(pattern1StartIndex == -1);
// Row 2
ruleStartIndex = drl.indexOf("//from row number: 3");
assertFalse(ruleStartIndex == -1);
pattern1StartIndex = drl.indexOf("System.out.println(", ruleStartIndex);
assertTrue(pattern1StartIndex == -1);
// Row 3
ruleStartIndex = drl.indexOf("//from row number: 4");
assertFalse(ruleStartIndex == -1);
pattern1StartIndex = drl.indexOf("System.out.println(", ruleStartIndex);
assertTrue(pattern1StartIndex == -1);
}
Aggregations