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