use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testRHSWithBRLColumn_ParseToDRL_MultipleActions.
@Test
public // This test checks a Decision Table involving BRL columns is correctly converted into DRL
void testRHSWithBRLColumn_ParseToDRL_MultipleActions() {
GuidedDecisionTable52 dtable = new GuidedDecisionTable52();
// All three rows are entered, some columns with optional data
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
BRLActionColumn brl1 = new BRLActionColumn();
// BRL Column definition
List<IAction> brl1Definition = new ArrayList<IAction>();
ActionInsertFact brl1DefinitionAction1 = new ActionInsertFact("Baddie");
ActionFieldValue brl1DefinitionAction1FieldValue1 = new ActionFieldValue("name", "Gargamel", DataType.TYPE_STRING);
brl1DefinitionAction1FieldValue1.setNature(BaseSingleFieldConstraint.TYPE_LITERAL);
brl1DefinitionAction1.addFieldValue(brl1DefinitionAction1FieldValue1);
brl1Definition.add(brl1DefinitionAction1);
ActionInsertFact brl1DefinitionAction2 = new ActionInsertFact("Smurf");
ActionFieldValue brl1DefinitionAction2FieldValue1 = new ActionFieldValue("name", "$name", DataType.TYPE_STRING);
brl1DefinitionAction2FieldValue1.setNature(BaseSingleFieldConstraint.TYPE_TEMPLATE);
brl1DefinitionAction2.addFieldValue(brl1DefinitionAction2FieldValue1);
ActionFieldValue brl1DefinitionAction2FieldValue2 = new ActionFieldValue("age", "$age", DataType.TYPE_NUMERIC_INTEGER);
brl1DefinitionAction2FieldValue2.setNature(BaseSingleFieldConstraint.TYPE_TEMPLATE);
brl1DefinitionAction2.addFieldValue(brl1DefinitionAction2FieldValue2);
brl1Definition.add(brl1DefinitionAction2);
brl1.setDefinition(brl1Definition);
// Setup BRL column bindings
BRLActionVariableColumn brl1Variable1 = new BRLActionVariableColumn("$name", DataType.TYPE_STRING, "Person", "name");
brl1.getChildColumns().add(brl1Variable1);
BRLActionVariableColumn brl1Variable2 = new BRLActionVariableColumn("$age", DataType.TYPE_NUMERIC_INTEGER, "Person", "age");
brl1.getChildColumns().add(brl1Variable2);
dtable.getActionCols().add(brl1);
dtable.setData(DataUtilities.makeDataLists(data));
// Now to test conversion
int ruleStartIndex;
int action1StartIndex;
int action2StartIndex;
GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance();
String drl = p.marshal(dtable);
// Row 0
ruleStartIndex = drl.indexOf("//from row number: 1");
assertFalse(ruleStartIndex == -1);
action1StartIndex = drl.indexOf("Baddie fact0 = new Baddie();", ruleStartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("fact0.setName( \"Gargamel\" );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("insert( fact0 );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action2StartIndex = drl.indexOf("Smurf fact1 = new Smurf();", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact1.setName( \"Pupa\" );", action2StartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact1.setAge( 50 );", action2StartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("insert( fact1 );", action2StartIndex);
assertFalse(action2StartIndex == -1);
// Row 1
ruleStartIndex = drl.indexOf("//from row number: 2");
int ruleEndIndex = drl.indexOf("//from row number: 3");
assertFalse(ruleStartIndex == -1);
action1StartIndex = drl.indexOf("Baddie fact0 = new Baddie();", ruleStartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("fact0.setName( \"Gargamel\" );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("insert( fact0 );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action2StartIndex = drl.indexOf("Smurf fact1 = new Smurf();", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact1.setName( \"Pupa\" );", ruleStartIndex);
assertFalse(action2StartIndex < ruleEndIndex);
action2StartIndex = drl.indexOf("fact1.setAge( 50 );", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("insert( fact1 );", ruleStartIndex);
assertFalse(action2StartIndex == -1);
// Row 2
ruleStartIndex = drl.indexOf("//from row number: 3");
assertFalse(ruleStartIndex == -1);
action1StartIndex = drl.indexOf("Baddie fact0 = new Baddie();", ruleStartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("fact0.setName( \"Gargamel\" );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("insert( fact0 );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action2StartIndex = drl.indexOf("Smurf fact1 = new Smurf();", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact1.setName( \"Pupa\" );", ruleStartIndex);
assertFalse(action2StartIndex == -1);
action2StartIndex = drl.indexOf("fact1.setAge( 50 );", ruleStartIndex);
assertTrue(action2StartIndex == -1);
action2StartIndex = drl.indexOf("insert( fact1 );", ruleStartIndex);
assertFalse(action2StartIndex == -1);
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testRHSWithBRLColumn_ParseToDRL_NoVariables.
@Test
public // This test checks a Decision Table involving BRL columns is correctly converted into DRL
void testRHSWithBRLColumn_ParseToDRL_NoVariables() {
GuidedDecisionTable52 dtable = new GuidedDecisionTable52();
Object[][] data = new Object[][] { new Object[] { "1", "desc", Boolean.TRUE }, new Object[] { "2", "desc", Boolean.FALSE } };
// Simple (mandatory) columns
dtable.setRowNumberCol(new RowNumberCol52());
dtable.setDescriptionCol(new DescriptionCol52());
// BRL Column
BRLActionColumn brl1 = new BRLActionColumn();
// BRL Column definition
List<IAction> brl1Definition = new ArrayList<IAction>();
ActionInsertFact brl1DefinitionAction1 = new ActionInsertFact("Baddie");
ActionFieldValue brl1DefinitionAction1FieldValue1 = new ActionFieldValue("name", "Gargamel", DataType.TYPE_STRING);
brl1DefinitionAction1FieldValue1.setNature(BaseSingleFieldConstraint.TYPE_LITERAL);
brl1DefinitionAction1.addFieldValue(brl1DefinitionAction1FieldValue1);
brl1Definition.add(brl1DefinitionAction1);
brl1.setDefinition(brl1Definition);
// Setup BRL column bindings
BRLActionVariableColumn brl1Variable1 = new BRLActionVariableColumn("", DataType.TYPE_BOOLEAN);
brl1.getChildColumns().add(brl1Variable1);
dtable.getActionCols().add(brl1);
dtable.setData(DataUtilities.makeDataLists(data));
// Now to test conversion
int ruleStartIndex;
int action1StartIndex;
GuidedDTDRLPersistence p = GuidedDTDRLPersistence.getInstance();
String drl = p.marshal(dtable);
// Row 0
ruleStartIndex = drl.indexOf("//from row number: 1");
assertFalse(ruleStartIndex == -1);
action1StartIndex = drl.indexOf("Baddie fact0 = new Baddie();", ruleStartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("fact0.setName( \"Gargamel\" );", action1StartIndex);
assertFalse(action1StartIndex == -1);
action1StartIndex = drl.indexOf("insert( fact0 );", action1StartIndex);
assertFalse(action1StartIndex == -1);
// Row 1
ruleStartIndex = drl.indexOf("//from row number: 2");
assertFalse(ruleStartIndex == -1);
action1StartIndex = drl.indexOf("Baddie fact0 = new Baddie();", ruleStartIndex);
assertTrue(action1StartIndex == -1);
action1StartIndex = drl.indexOf("fact0.setName( \"Gargamel\" );", ruleStartIndex);
assertTrue(action1StartIndex == -1);
action1StartIndex = drl.indexOf("insert( fact0 );", ruleStartIndex);
assertTrue(action1StartIndex == -1);
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testRHSActionWorkItemInsertFacts2.
@Test
public // Test only Actions set to "true" are correctly converted to RuleModel
void testRHSActionWorkItemInsertFacts2() {
GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
String[] row = new String[] { "1", "desc", "true", "true", "false" };
List<BaseColumn> allColumns = new ArrayList<BaseColumn>();
allColumns.add(new RowNumberCol52());
allColumns.add(new DescriptionCol52());
List<ActionCol52> cols = new ArrayList<ActionCol52>();
ActionWorkItemCol52 awi = new ActionWorkItemCol52();
PortableWorkDefinition pwd = new PortableWorkDefinition();
pwd.setName("WorkItem");
awi.setWorkItemDefinition(pwd);
PortableBooleanParameterDefinition p1 = new PortableBooleanParameterDefinition();
p1.setName("BooleanResult");
pwd.addResult(p1);
PortableFloatParameterDefinition p2 = new PortableFloatParameterDefinition();
p2.setName("FloatResult");
pwd.addResult(p2);
cols.add(awi);
ActionWorkItemInsertFactCol52 asf1 = new ActionWorkItemInsertFactCol52();
asf1.setBoundName("$r");
asf1.setFactField("ResultBooleanField");
asf1.setType(DataType.TYPE_BOOLEAN);
asf1.setWorkItemName("WorkItem");
asf1.setWorkItemResultParameterName("BooleanResult");
asf1.setParameterClassName(Boolean.class.getName());
cols.add(asf1);
ActionWorkItemInsertFactCol52 asf2 = new ActionWorkItemInsertFactCol52();
asf2.setBoundName("$r");
asf2.setFactField("ResultFloatField");
asf2.setType(DataType.TYPE_NUMERIC_FLOAT);
asf2.setWorkItemName("WorkItem");
asf2.setWorkItemResultParameterName("FloatResult");
asf2.setParameterClassName(Float.class.getName());
cols.add(asf2);
RuleModel rm = new RuleModel();
allColumns.addAll(cols);
List<DTCellValue52> rowData = DataUtilities.makeDataRowList(row);
TemplateDataProvider rowDataProvider = new GuidedDTTemplateDataProvider(allColumns, rowData);
p.doActions(allColumns, cols, rowDataProvider, rowData, rm);
assertEquals(2, rm.rhs.length);
// Examine RuleModel actions
ActionExecuteWorkItem aw = (ActionExecuteWorkItem) rm.rhs[0];
assertNotNull(aw);
ActionInsertFact aif = (ActionInsertFact) rm.rhs[1];
assertNotNull(aif);
// Check ActionExecuteWorkItem
PortableWorkDefinition mpwd = aw.getWorkDefinition();
assertNotNull(mpwd);
assertEquals(2, mpwd.getResults().size());
PortableBooleanParameterDefinition mp1 = (PortableBooleanParameterDefinition) mpwd.getResult("BooleanResult");
assertNotNull(mp1);
PortableFloatParameterDefinition mp2 = (PortableFloatParameterDefinition) mpwd.getResult("FloatResult");
assertNotNull(mp2);
// Check ActionInsertFact
assertEquals(aif.getBoundName(), "$r");
assertEquals(1, aif.getFieldValues().length);
ActionFieldValue fv1 = aif.getFieldValues()[0];
assertNotNull(fv1);
assertTrue(fv1 instanceof ActionWorkItemFieldValue);
ActionWorkItemFieldValue wifv1 = (ActionWorkItemFieldValue) fv1;
assertEquals("ResultBooleanField", wifv1.getField());
assertEquals(DataType.TYPE_BOOLEAN, wifv1.getType());
assertEquals("WorkItem", wifv1.getWorkItemName());
assertEquals("BooleanResult", wifv1.getWorkItemParameterName());
assertEquals(Boolean.class.getName(), wifv1.getWorkItemParameterClassName());
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testRHS.
@Test
public void testRHS() {
GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
String[] row = new String[] { "1", "desc", "a", "a condition", "actionsetfield1", "actionupdatefield2", "retract", "actioninsertfact1", "actioninsertfact2" };
List<BaseColumn> allColumns = new ArrayList<BaseColumn>();
allColumns.add(new RowNumberCol52());
allColumns.add(new DescriptionCol52());
allColumns.add(new MetadataCol52());
allColumns.add(new ConditionCol52());
List<ActionCol52> cols = new ArrayList<ActionCol52>();
ActionSetFieldCol52 asf1 = new ActionSetFieldCol52();
asf1.setBoundName("a");
asf1.setFactField("field1");
asf1.setType(DataType.TYPE_STRING);
cols.add(asf1);
ActionSetFieldCol52 asf2 = new ActionSetFieldCol52();
asf2.setBoundName("a");
asf2.setFactField("field2");
asf2.setUpdate(true);
asf2.setType(DataType.TYPE_NUMERIC_INTEGER);
cols.add(asf2);
ActionRetractFactCol52 ret = new ActionRetractFactCol52();
cols.add(ret);
ActionInsertFactCol52 ins1 = new ActionInsertFactCol52();
ins1.setBoundName("ins");
ins1.setFactType("Cheese");
ins1.setFactField("price");
ins1.setType(DataType.TYPE_NUMERIC_INTEGER);
cols.add(ins1);
ActionInsertFactCol52 ins2 = new ActionInsertFactCol52();
ins2.setBoundName("ins");
ins2.setFactType("Cheese");
ins2.setFactField("type");
ins2.setType(DataType.TYPE_NUMERIC_INTEGER);
cols.add(ins2);
RuleModel rm = new RuleModel();
allColumns.addAll(cols);
List<DTCellValue52> rowData = DataUtilities.makeDataRowList(row);
TemplateDataProvider rowDataProvider = new GuidedDTTemplateDataProvider(allColumns, rowData);
p.doActions(allColumns, cols, rowDataProvider, rowData, rm);
assertEquals(4, rm.rhs.length);
// examine the set field action that is produced
ActionSetField a1 = (ActionSetField) rm.rhs[0];
assertEquals("a", a1.getVariable());
assertEquals(1, a1.getFieldValues().length);
assertEquals("field1", a1.getFieldValues()[0].getField());
assertEquals("actionsetfield1", a1.getFieldValues()[0].getValue());
assertEquals(DataType.TYPE_STRING, a1.getFieldValues()[0].getType());
ActionSetField a2 = (ActionSetField) rm.rhs[1];
assertEquals("a", a2.getVariable());
assertEquals(1, a2.getFieldValues().length);
assertEquals("field2", a2.getFieldValues()[0].getField());
assertEquals("actionupdatefield2", a2.getFieldValues()[0].getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, a2.getFieldValues()[0].getType());
// examine the retract
ActionRetractFact a3 = (ActionRetractFact) rm.rhs[2];
assertEquals("retract", a3.getVariableName());
// examine the insert
ActionInsertFact a4 = (ActionInsertFact) rm.rhs[3];
assertEquals("Cheese", a4.getFactType());
assertEquals(2, a4.getFieldValues().length);
assertEquals("price", a4.getFieldValues()[0].getField());
assertEquals("actioninsertfact1", a4.getFieldValues()[0].getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, a4.getFieldValues()[0].getType());
assertEquals("type", a4.getFieldValues()[1].getField());
assertEquals("actioninsertfact2", a4.getFieldValues()[1].getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, a4.getFieldValues()[1].getType());
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSInsertFactWithFieldAsLiteral.
@Test
public void testRHSInsertFactWithFieldAsLiteral() {
String drl = "package org.mortgages\n" + "import org.test.Person\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( field1 == 44 )\n" + "then\n" + "Person fact0 = new Person();\n" + "fact0.setField1( 55 );\n" + "insert( fact0 );\n" + "end";
addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
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("==", sfp.getOperator());
assertEquals("44", sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
assertEquals(1, m.rhs.length);
IAction a = m.rhs[0];
assertTrue(a instanceof ActionInsertFact);
ActionInsertFact ap = (ActionInsertFact) a;
assertEquals("Person", ap.getFactType());
assertEquals("fact0", ap.getBoundName());
assertEquals(1, ap.getFieldValues().length);
ActionFieldValue afv = ap.getFieldValues()[0];
assertEquals("field1", afv.getField());
assertEquals(FieldNatureType.TYPE_LITERAL, afv.getNature());
assertEquals("55", afv.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
}
Aggregations