use of org.drools.workbench.models.guided.dtable.shared.model.Pattern52 in project drools by kiegroup.
the class GuidedDTXMLPersistenceTest method testRoundTrip.
@Test
public void testRoundTrip() {
GuidedDecisionTable52 dt = new GuidedDecisionTable52();
dt.getActionCols().add(new ActionInsertFactCol52());
ActionSetFieldCol52 set = new ActionSetFieldCol52();
set.setFactField("foo");
dt.getActionCols().add(set);
dt.getMetadataCols().add(new MetadataCol52());
dt.getAttributeCols().add(new AttributeCol52());
Pattern52 p = new Pattern52();
ConditionCol52 c = new ConditionCol52();
p.getChildColumns().add(c);
dt.getConditions().add(p);
dt.setData(upgrader.makeDataLists(new String[][] { new String[] { "1", "hola" } }));
dt.setTableName("blah");
String xml = GuidedDTXMLPersistence.getInstance().marshal(dt);
System.out.println(xml);
assertNotNull(xml);
assertEquals(-1, xml.indexOf("ActionSetField"));
assertEquals(-1, xml.indexOf("ConditionCol"));
assertEquals(-1, xml.indexOf("GuidedDecisionTable"));
GuidedDecisionTable52 dt_ = GuidedDTXMLPersistence.getInstance().unmarshal(xml);
assertNotNull(dt_);
assertEquals("blah", dt_.getTableName());
assertEquals(1, dt_.getMetadataCols().size());
assertEquals(1, dt_.getAttributeCols().size());
assertEquals(2, dt_.getActionCols().size());
assertEquals(1, dt_.getConditions().size());
assertEquals(1, dt_.getConditions().get(0).getChildColumns().size());
}
use of org.drools.workbench.models.guided.dtable.shared.model.Pattern52 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());
}
use of org.drools.workbench.models.guided.dtable.shared.model.Pattern52 in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testUpdateModifyMultipleFields.
@Test
public void testUpdateModifyMultipleFields() {
GuidedDecisionTable52 dt = new GuidedDecisionTable52();
Pattern52 p1 = new Pattern52();
p1.setBoundName("x");
p1.setFactType("Context");
ConditionCol52 c = new ConditionCol52();
c.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
p1.getChildColumns().add(c);
dt.getConditions().add(p1);
ActionSetFieldCol52 asf1 = new ActionSetFieldCol52();
asf1.setBoundName("x");
asf1.setFactField("age");
asf1.setType(DataType.TYPE_NUMERIC_INTEGER);
asf1.setUpdate(true);
dt.getActionCols().add(asf1);
ActionSetFieldCol52 asf2 = new ActionSetFieldCol52();
asf2.setBoundName("x");
asf2.setFactField("name");
asf2.setType(DataType.TYPE_STRING);
asf2.setUpdate(true);
dt.getActionCols().add(asf2);
dt.setData(DataUtilities.makeDataLists(new Object[][] { new Object[] { "1", "desc", "x", 55l, "Fred" } }));
String drl = GuidedDTDRLPersistence.getInstance().marshal(dt);
final String expected1 = "//from row number: 1\n" + "//desc\n" + "rule \"Row 1 null\"\n" + "dialect \"mvel\"\n" + "when\n" + " x : Context( )\n" + "then\n" + " modify( x ) {\n" + " setAge( 55 ), \n" + " setName( \"Fred\" )\n" + "}\n" + "end\n";
assertEqualsIgnoreWhitespace(expected1, drl);
dt.setData(DataUtilities.makeDataLists(new Object[][] { new Object[] { "1", "desc", "x", null, "Fred" } }));
drl = GuidedDTDRLPersistence.getInstance().marshal(dt);
final String expected2 = "//from row number: 1\n" + "//desc\n" + "rule \"Row 1 null\"\n" + "dialect \"mvel\"\n" + "when\n" + " x : Context( )\n" + "then\n" + " modify( x ) {\n" + " setName( \"Fred\" )\n" + "}\n" + "end\n";
assertEqualsIgnoreWhitespace(expected2, drl);
dt.setData(DataUtilities.makeDataLists(new Object[][] { new Object[] { "1", "desc", "x", 55l, null } }));
drl = GuidedDTDRLPersistence.getInstance().marshal(dt);
final String expected3 = "//from row number: 1\n" + "//desc\n" + "rule \"Row 1 null\"\n" + "dialect \"mvel\"\n" + "when\n" + " x : Context( )\n" + "then\n" + " modify( x ) {\n" + " setAge( 55 ) \n" + "}\n" + "end\n";
assertEqualsIgnoreWhitespace(expected3, drl);
}
use of org.drools.workbench.models.guided.dtable.shared.model.Pattern52 in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testLHSIsNullOperator.
@Test
public void testLHSIsNullOperator() {
GuidedDecisionTable52 dt = new GuidedDecisionTable52();
dt.setTableFormat(GuidedDecisionTable52.TableFormat.EXTENDED_ENTRY);
dt.setTableName("extended-entry");
Pattern52 p1 = new Pattern52();
p1.setBoundName("p1");
p1.setFactType("Smurf");
dt.getConditions().add(p1);
ConditionCol52 cc1 = new ConditionCol52();
cc1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
cc1.setFieldType(DataType.TYPE_STRING);
cc1.setFactField("name");
cc1.setOperator("== null");
p1.getChildColumns().add(cc1);
ConditionCol52 cc2 = new ConditionCol52();
cc2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
cc2.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
cc2.setFactField("age");
cc2.setOperator("== null");
p1.getChildColumns().add(cc2);
ConditionCol52 cc3 = new ConditionCol52();
cc3.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
cc3.setFieldType(DataType.TYPE_DATE);
cc3.setFactField("dateOfBirth");
cc3.setOperator("== null");
p1.getChildColumns().add(cc3);
dt.setData(DataUtilities.makeDataLists(new Object[][] { new Object[] { 1l, "desc", true, true, true }, new Object[] { 2l, "desc", false, false, false } }));
GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance();
String drl = p.marshal(dt);
int index = -1;
index = drl.indexOf("Smurf( name == null , age == null , dateOfBirth == null )");
assertTrue(index > -1);
index = drl.indexOf("Smurf( )", index + 1);
assertFalse(index > -1);
}
use of org.drools.workbench.models.guided.dtable.shared.model.Pattern52 in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testLHSOtherwisePatternString.
@Test
public void testLHSOtherwisePatternString() {
GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
String[][] row = new String[3][];
String[][] data = new String[3][];
row[0] = new String[] { "1", "desc1", "Michael1", "Michael1" };
List<DTCellValue52> rowDTModel0 = DataUtilities.makeDataRowList(row[0]);
data[0] = row[0];
row[1] = new String[] { "2", "desc2", "Michael2", "Michael2" };
List<DTCellValue52> rowDTModel1 = DataUtilities.makeDataRowList(row[1]);
data[1] = row[1];
row[2] = new String[] { "3", "desc3", "", "" };
List<DTCellValue52> rowDTModel2 = DataUtilities.makeDataRowList(row[2]);
rowDTModel2.get(2).setOtherwise(true);
rowDTModel2.get(3).setOtherwise(true);
data[2] = row[2];
final List<List<DTCellValue52>> allDTData = new ArrayList<List<DTCellValue52>>() {
{
add(rowDTModel0);
add(rowDTModel1);
add(rowDTModel2);
}
};
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.setFieldType(DataType.TYPE_STRING);
col.setOperator("==");
p1.getChildColumns().add(col);
allColumns.add(col);
Pattern52 p2 = new Pattern52();
p2.setBoundName("p2");
p2.setFactType("Person");
allPatterns.add(p2);
ConditionCol52 col2 = new ConditionCol52();
col2.setFactField("name");
col2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
col2.setFieldType(DataType.TYPE_STRING);
col2.setOperator("!=");
p2.getChildColumns().add(col2);
allColumns.add(col2);
RuleModel rm = new RuleModel();
TemplateDataProvider rowDataProvider0 = new GuidedDTTemplateDataProvider(allColumns, rowDTModel0);
p.doConditions(allColumns, allPatterns, rowDataProvider0, rowDTModel0, allDTData, rm);
String drl0 = RuleModelDRLPersistenceImpl.getInstance().marshal(rm);
assertEquals(2, rm.lhs.length);
assertEquals("Person", ((FactPattern) rm.lhs[0]).getFactType());
assertEquals("p1", ((FactPattern) rm.lhs[0]).getBoundName());
assertEquals("Person", ((FactPattern) rm.lhs[1]).getFactType());
assertEquals("p2", ((FactPattern) rm.lhs[1]).getBoundName());
assertTrue(drl0.indexOf("p1 : Person( name == \"Michael1\" )") > 0);
assertTrue(drl0.indexOf("p2 : Person( name != \"Michael1\" )") > 0);
TemplateDataProvider rowDataProvider1 = new GuidedDTTemplateDataProvider(allColumns, rowDTModel1);
p.doConditions(allColumns, allPatterns, rowDataProvider1, rowDTModel1, allDTData, rm);
String drl1 = RuleModelDRLPersistenceImpl.getInstance().marshal(rm);
assertEquals(2, rm.lhs.length);
assertEquals("Person", ((FactPattern) rm.lhs[0]).getFactType());
assertEquals("p1", ((FactPattern) rm.lhs[0]).getBoundName());
assertEquals("Person", ((FactPattern) rm.lhs[1]).getFactType());
assertEquals("p2", ((FactPattern) rm.lhs[1]).getBoundName());
assertTrue(drl1.indexOf("p1 : Person( name == \"Michael2\" )") > 0);
assertTrue(drl1.indexOf("p2 : Person( name != \"Michael2\" )") > 0);
TemplateDataProvider rowDataProvider2 = new GuidedDTTemplateDataProvider(allColumns, rowDTModel2);
p.doConditions(allColumns, allPatterns, rowDataProvider2, rowDTModel2, allDTData, rm);
String drl2 = RuleModelDRLPersistenceImpl.getInstance().marshal(rm);
assertEquals(2, rm.lhs.length);
assertEquals("Person", ((FactPattern) rm.lhs[0]).getFactType());
assertEquals("p1", ((FactPattern) rm.lhs[0]).getBoundName());
assertEquals("Person", ((FactPattern) rm.lhs[1]).getFactType());
assertEquals("p2", ((FactPattern) rm.lhs[1]).getBoundName());
assertTrue(drl2.indexOf("p1 : Person( name not in ( \"Michael1\", \"Michael2\" )") > 0);
assertTrue(drl2.indexOf("p2 : Person( name in ( \"Michael1\", \"Michael2\" )") > 0);
}
Aggregations