use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraint in project drools by kiegroup.
the class BRLRuleModelTest method testDecisionTableColumnsWithLHSBoundFields.
@Test
public void testDecisionTableColumnsWithLHSBoundFields() {
GuidedDecisionTable52 dt = new GuidedDecisionTable52();
Pattern52 p1 = new Pattern52();
p1.setFactType("Driver");
p1.setBoundName("$p1");
ConditionCol52 c1 = new ConditionCol52();
c1.setFactField("name");
c1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
c1.setBinding("$c1");
p1.getChildColumns().add(c1);
dt.getConditions().add(p1);
BRLConditionColumn brlCondition = new BRLConditionColumn();
FactPattern fp = new FactPattern("Driver");
fp.setBoundName("$brl1");
SingleFieldConstraint sfc1 = new SingleFieldConstraint();
sfc1.setFieldBinding("$sfc1");
sfc1.setOperator("==");
sfc1.setFactType("Driver");
sfc1.setFieldName("name");
sfc1.setFieldType(DataType.TYPE_STRING);
fp.addConstraint(sfc1);
brlCondition.getDefinition().add(fp);
dt.getConditions().add(brlCondition);
ActionInsertFactCol52 ins = new ActionInsertFactCol52();
ins.setBoundName("$ins");
ins.setFactField("rating");
ins.setFactType("Person");
ins.setType(DataType.TYPE_STRING);
dt.getActionCols().add(ins);
BRLRuleModel model = new BRLRuleModel(dt);
FieldConstraint fcr1 = model.getLHSBoundField("$sfc1");
assertNotNull(fcr1);
assertTrue(fcr1 instanceof SingleFieldConstraint);
SingleFieldConstraint fcr1sfc = (SingleFieldConstraint) fcr1;
assertEquals("name", fcr1sfc.getFieldName());
assertEquals(DataType.TYPE_STRING, fcr1sfc.getFieldType());
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraint in project drools by kiegroup.
the class BRLRuleModelTest method testDecisionTableColumnsWithLHS.
@Test
public void testDecisionTableColumnsWithLHS() {
GuidedDecisionTable52 dt = new GuidedDecisionTable52();
Pattern52 p1 = new Pattern52();
p1.setFactType("Driver");
p1.setBoundName("$p1");
ConditionCol52 c1 = new ConditionCol52();
c1.setFactField("name");
c1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
c1.setBinding("$c1");
p1.getChildColumns().add(c1);
dt.getConditions().add(p1);
BRLConditionColumn brlCondition = new BRLConditionColumn();
FactPattern fp = new FactPattern("Driver");
fp.setBoundName("$brl1");
SingleFieldConstraint sfc1 = new SingleFieldConstraint();
sfc1.setFieldBinding("$sfc1");
sfc1.setOperator("==");
sfc1.setFactType("Driver");
sfc1.setFieldName("name");
sfc1.setFieldType(DataType.TYPE_STRING);
fp.addConstraint(sfc1);
brlCondition.getDefinition().add(fp);
dt.getConditions().add(brlCondition);
ActionInsertFactCol52 ins = new ActionInsertFactCol52();
ins.setBoundName("$ins");
ins.setFactField("rating");
ins.setFactType("Person");
ins.setType(DataType.TYPE_STRING);
dt.getActionCols().add(ins);
BRLRuleModel model = new BRLRuleModel(dt);
assertNotNull(model.getAllVariables());
assertEquals(5, model.getAllVariables().size());
assertTrue(model.getAllVariables().contains("$p1"));
assertTrue(model.getAllVariables().contains("$c1"));
assertTrue(model.getAllVariables().contains("$ins"));
assertTrue(model.getAllVariables().contains("$brl1"));
assertTrue(model.getAllVariables().contains("$sfc1"));
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraint in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testLHSWithBRLColumn_ParseToDRL_MultiplePatterns.
@Test
public // This test checks a Decision Table involving BRL columns is correctly converted into DRL
void testLHSWithBRLColumn_ParseToDRL_MultiplePatterns() {
GuidedDecisionTable52 dtable = new GuidedDecisionTable52();
// All 3 rows should render, as the code is now lower down for skipping columns with empty cells
String[][] data = new String[][] { new String[] { "1", "desc", "Pupa", "50" }, new String[] { "2", "desc", "", "50" }, new String[] { "3", "desc", "Pupa", "" } };
// 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>();
FactPattern brl1DefinitionFactPattern1 = new FactPattern("Baddie");
SingleFieldConstraint brl1DefinitionFactPattern1Constraint1 = new SingleFieldConstraint();
brl1DefinitionFactPattern1Constraint1.setFieldType(DataType.TYPE_STRING);
brl1DefinitionFactPattern1Constraint1.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
brl1DefinitionFactPattern1Constraint1.setFieldName("name");
brl1DefinitionFactPattern1Constraint1.setOperator("==");
brl1DefinitionFactPattern1Constraint1.setValue("Gargamel");
brl1DefinitionFactPattern1.addConstraint(brl1DefinitionFactPattern1Constraint1);
brl1Definition.add(brl1DefinitionFactPattern1);
FactPattern brl1DefinitionFactPattern2 = new FactPattern("Smurf");
SingleFieldConstraint brl1DefinitionFactPattern2Constraint1 = new SingleFieldConstraint();
brl1DefinitionFactPattern2Constraint1.setFieldType(DataType.TYPE_STRING);
brl1DefinitionFactPattern2Constraint1.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE);
brl1DefinitionFactPattern2Constraint1.setFieldName("name");
brl1DefinitionFactPattern2Constraint1.setOperator("==");
brl1DefinitionFactPattern2Constraint1.setValue("$name");
brl1DefinitionFactPattern2.addConstraint(brl1DefinitionFactPattern2Constraint1);
SingleFieldConstraint brl1DefinitionFactPattern2Constraint2 = new SingleFieldConstraint();
brl1DefinitionFactPattern2Constraint2.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
brl1DefinitionFactPattern2Constraint2.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE);
brl1DefinitionFactPattern2Constraint2.setFieldName("age");
brl1DefinitionFactPattern2Constraint2.setOperator("==");
brl1DefinitionFactPattern2Constraint2.setValue("$age");
brl1DefinitionFactPattern2.addConstraint(brl1DefinitionFactPattern2Constraint2);
brl1Definition.add(brl1DefinitionFactPattern2);
brl1.setDefinition(brl1Definition);
// Setup BRL column bindings
BRLConditionVariableColumn brl1Variable1 = new BRLConditionVariableColumn("$name", DataType.TYPE_STRING, "Person", "name");
brl1.getChildColumns().add(brl1Variable1);
BRLConditionVariableColumn brl1Variable2 = new BRLConditionVariableColumn("$age", DataType.TYPE_NUMERIC_INTEGER, "Person", "age");
brl1.getChildColumns().add(brl1Variable2);
dtable.getConditions().add(brl1);
dtable.setData(DataUtilities.makeDataLists(data));
// Now to test conversion
int ruleStartIndex;
int pattern1StartIndex;
int pattern2StartIndex;
GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance();
String drl = p.marshal(dtable);
System.out.println(drl);
// Row 0
ruleStartIndex = drl.indexOf("//from row number: 1");
assertFalse(ruleStartIndex == -1);
pattern1StartIndex = drl.indexOf("Baddie( name == \"Gargamel\" )", ruleStartIndex);
assertFalse(pattern1StartIndex == -1);
pattern2StartIndex = drl.indexOf("Smurf( name == \"Pupa\" , age == 50 )", ruleStartIndex);
assertFalse(pattern2StartIndex == -1);
// Row 1
ruleStartIndex = drl.indexOf("//from row number: 2");
assertFalse(ruleStartIndex == -1);
pattern1StartIndex = drl.indexOf("Baddie( name == \"Gargamel\" )", ruleStartIndex);
assertFalse(pattern1StartIndex == -1);
pattern2StartIndex = drl.indexOf("Smurf( age == 50 )", ruleStartIndex);
assertFalse(pattern2StartIndex == -1);
// Row 2
ruleStartIndex = drl.indexOf("//from row number: 3");
assertFalse(ruleStartIndex == -1);
pattern1StartIndex = drl.indexOf("Baddie( name == \"Gargamel\" )", ruleStartIndex);
assertFalse(pattern1StartIndex == -1);
pattern2StartIndex = drl.indexOf("Smurf( name == \"Pupa\" )", ruleStartIndex);
assertFalse(pattern2StartIndex == -1);
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraint in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testLHSBindings.
@Test
public void testLHSBindings() {
GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
String[] row = new String[] { "1", "desc", "mike", "33 + 1", "age > 6" };
String[][] data = new String[1][];
data[0] = row;
List<BaseColumn> allColumns = new ArrayList<BaseColumn>();
List<CompositeColumn<? extends BaseColumn>> allPatterns = new ArrayList<CompositeColumn<? extends BaseColumn>>();
allColumns.add(new RowNumberCol52());
allColumns.add(new DescriptionCol52());
Pattern52 p1 = new Pattern52();
p1.setBoundName("p1");
p1.setFactType("Person");
allPatterns.add(p1);
ConditionCol52 col = new ConditionCol52();
col.setFactField("name");
col.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
col.setOperator("==");
col.setBinding("$name");
p1.getChildColumns().add(col);
allColumns.add(col);
ConditionCol52 col2 = new ConditionCol52();
col2.setFactField("age");
col2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_RET_VALUE);
col2.setOperator("<");
col2.setBinding("$age");
p1.getChildColumns().add(col2);
allColumns.add(col2);
ConditionCol52 col3 = new ConditionCol52();
col3.setConstraintValueType(BaseSingleFieldConstraint.TYPE_PREDICATE);
col3.setBinding("$name");
p1.getChildColumns().add(col3);
allColumns.add(col3);
List<DTCellValue52> rowData = DataUtilities.makeDataRowList(row);
TemplateDataProvider rowDataProvider = new GuidedDTTemplateDataProvider(allColumns, rowData);
RuleModel rm = new RuleModel();
p.doConditions(allColumns, allPatterns, rowDataProvider, rowData, DataUtilities.makeDataLists(data), rm);
assertEquals(1, rm.lhs.length);
assertEquals("Person", ((FactPattern) rm.lhs[0]).getFactType());
assertEquals("p1", ((FactPattern) rm.lhs[0]).getBoundName());
// examine the first pattern
FactPattern person = (FactPattern) rm.lhs[0];
assertEquals(3, person.getConstraintList().getConstraints().length);
SingleFieldConstraint cons = (SingleFieldConstraint) person.getConstraint(0);
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cons.getConstraintValueType());
assertEquals("name", cons.getFieldName());
assertEquals("==", cons.getOperator());
assertEquals("mike", cons.getValue());
assertEquals("$name", cons.getFieldBinding());
cons = (SingleFieldConstraint) person.getConstraint(1);
assertEquals(BaseSingleFieldConstraint.TYPE_RET_VALUE, cons.getConstraintValueType());
assertEquals("age", cons.getFieldName());
assertEquals("<", cons.getOperator());
assertEquals("33 + 1", cons.getValue());
assertEquals("$age", cons.getFieldBinding());
cons = (SingleFieldConstraint) person.getConstraint(2);
assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, cons.getConstraintValueType());
assertEquals("age > 6", cons.getValue());
assertNull(cons.getFieldBinding());
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraint in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testLHS.
@Test
public void testLHS() {
GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
String[] row = new String[] { "1", "desc", "a", "mike", "33 + 1", "age > 6", "stilton" };
String[][] data = new String[1][];
data[0] = row;
List<BaseColumn> allColumns = new ArrayList<BaseColumn>();
List<CompositeColumn<? extends BaseColumn>> allPatterns = new ArrayList<CompositeColumn<? extends BaseColumn>>();
allColumns.add(new RowNumberCol52());
allColumns.add(new DescriptionCol52());
allColumns.add(new MetadataCol52());
Pattern52 p1 = new Pattern52();
p1.setBoundName("p1");
p1.setFactType("Person");
allPatterns.add(p1);
ConditionCol52 col = new ConditionCol52();
col.setFactField("name");
col.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
col.setOperator("==");
p1.getChildColumns().add(col);
allColumns.add(col);
ConditionCol52 col2 = new ConditionCol52();
col2.setFactField("age");
col2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_RET_VALUE);
col2.setOperator("<");
p1.getChildColumns().add(col2);
allColumns.add(col2);
ConditionCol52 col3 = new ConditionCol52();
col3.setConstraintValueType(BaseSingleFieldConstraint.TYPE_PREDICATE);
p1.getChildColumns().add(col3);
allColumns.add(col3);
Pattern52 p2 = new Pattern52();
p2.setBoundName("c");
p2.setFactType("Cheese");
allPatterns.add(p2);
ConditionCol52 col4 = new ConditionCol52();
col4.setFactField("type");
col4.setOperator("==");
col4.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
p2.getChildColumns().add(col4);
allColumns.add(col4);
RuleModel rm = new RuleModel();
List<DTCellValue52> rowData = DataUtilities.makeDataRowList(row);
TemplateDataProvider rowDataProvider = new GuidedDTTemplateDataProvider(allColumns, rowData);
p.doConditions(allColumns, allPatterns, rowDataProvider, rowData, DataUtilities.makeDataLists(data), rm);
assertEquals(2, rm.lhs.length);
assertEquals("Person", ((FactPattern) rm.lhs[0]).getFactType());
assertEquals("p1", ((FactPattern) rm.lhs[0]).getBoundName());
assertEquals("Cheese", ((FactPattern) rm.lhs[1]).getFactType());
assertEquals("c", ((FactPattern) rm.lhs[1]).getBoundName());
// examine the first pattern
FactPattern person = (FactPattern) rm.lhs[0];
assertEquals(3, person.getConstraintList().getConstraints().length);
SingleFieldConstraint cons = (SingleFieldConstraint) person.getConstraint(0);
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cons.getConstraintValueType());
assertEquals("name", cons.getFieldName());
assertEquals("==", cons.getOperator());
assertEquals("mike", cons.getValue());
cons = (SingleFieldConstraint) person.getConstraint(1);
assertEquals(BaseSingleFieldConstraint.TYPE_RET_VALUE, cons.getConstraintValueType());
assertEquals("age", cons.getFieldName());
assertEquals("<", cons.getOperator());
assertEquals("33 + 1", cons.getValue());
cons = (SingleFieldConstraint) person.getConstraint(2);
assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, cons.getConstraintValueType());
assertEquals("age > 6", cons.getValue());
// examine the second pattern
FactPattern cheese = (FactPattern) rm.lhs[1];
assertEquals(1, cheese.getConstraintList().getConstraints().length);
cons = (SingleFieldConstraint) cheese.getConstraint(0);
assertEquals("type", cons.getFieldName());
assertEquals("==", cons.getOperator());
assertEquals("stilton", cons.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cons.getConstraintValueType());
}
Aggregations