use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSInsertFactWithFieldAsLiteralSamePackage.
@Test
public void testRHSInsertFactWithFieldAsLiteralSamePackage() {
String drl = "package org.test\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);
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("==", 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());
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method newRHSFactsCanBeUsedInRHSBinding.
@Test
public void newRHSFactsCanBeUsedInRHSBinding() throws Exception {
String drl = "package org.mortgages;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$l : LoanApplication()\n" + "then\n" + "Applicant $a = new Applicant();\n" + "insert( $a );\n" + "modify( $l ) {\n" + " setApplicant( $a )" + "}\n" + "end";
addModelField("org.mortgages.Applicant", "this", "org.mortgages.Applicant", DataType.TYPE_THIS);
addModelField("org.mortgages.LoanApplication", "this", "org.mortgages.LoanApplication", DataType.TYPE_THIS);
addModelField("org.mortgages.LoanApplication", "applicant", "org.mortgages.Applicant", "org.mortgages.Applicant");
when(dmo.getPackageName()).thenReturn("org.mortgages");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("r1", m.name);
// LHS Pattern
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("LoanApplication", fp.getFactType());
assertEquals(0, fp.getNumberOfConstraints());
assertEquals(2, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionInsertFact);
ActionInsertFact aif = (ActionInsertFact) m.rhs[0];
assertEquals("$a", aif.getBoundName());
assertEquals("Applicant", aif.getFactType());
assertEquals(0, aif.getFieldValues().length);
assertTrue(m.rhs[1] instanceof ActionUpdateField);
ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
assertEquals("$l", auf.getVariable());
assertEquals(1, auf.getFieldValues().length);
ActionFieldValue afv0 = auf.getFieldValues()[0];
assertEquals("applicant", afv0.getField());
assertEquals("=$a", afv0.getValue());
assertEquals(FieldNatureType.TYPE_VARIABLE, afv0.getNature());
assertEquals("Applicant", afv0.getType());
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSInsertFactWithFieldAsVariable.
@Test
public void testRHSInsertFactWithFieldAsVariable() {
// https://bugzilla.redhat.com/show_bug.cgi?id=1077212
String drl = "package org.mortgages\n" + "import org.test.Person\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( $f : field1 == 44 )\n" + "then\n" + "Person fact0 = new Person();\n" + "fact0.setField1( $f );\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("$f", sfp.getFieldBinding());
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_VARIABLE, afv.getNature());
assertEquals("=$f", afv.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method actionInsertFactWithFormula.
@Test
public void actionInsertFactWithFormula() throws Exception {
String drl = "package org.mortgages;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Application()\n" + "then\n" + "Applicant $a = new Applicant();\n" + "$a.setName( \"Pupa\" + 20 + \"Smurf\" );\n" + "insert( $a );\n" + "end";
addModelField("org.mortgages.Applicant", "this", "org.mortgages.Applicant", DataType.TYPE_THIS);
addModelField("org.mortgages.Applicant", "name", String.class.getName(), DataType.TYPE_STRING);
when(dmo.getPackageName()).thenReturn("org.mortgages");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("r1", m.name);
// LHS Pattern
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Application", fp.getFactType());
assertEquals(0, fp.getNumberOfConstraints());
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionInsertFact);
ActionInsertFact aif = (ActionInsertFact) m.rhs[0];
assertEquals("$a", aif.getBoundName());
assertEquals(1, aif.getFieldValues().length);
ActionFieldValue afv0 = aif.getFieldValues()[0];
assertEquals("name", afv0.getField());
assertEquals("\"Pupa\" + 20 + \"Smurf\"", afv0.getValue());
assertEquals(FieldNatureType.TYPE_FORMULA, afv0.getNature());
}
use of org.drools.workbench.models.datamodel.rule.ActionInsertFact in project drools by kiegroup.
the class GuidedDTDRLPersistenceTest method testRHSActionWorkItemInsertFacts1.
@Test
public // Test all Actions inserting Facts are correctly converted to RuleModel
void testRHSActionWorkItemInsertFacts1() {
GuidedDTDRLPersistence p = new GuidedDTDRLPersistence();
String[] row = new String[] { "1", "desc", "true", "true", "true", "true", "true" };
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);
PortableIntegerParameterDefinition p3 = new PortableIntegerParameterDefinition();
p3.setName("IntegerResult");
pwd.addResult(p3);
PortableStringParameterDefinition p4 = new PortableStringParameterDefinition();
p4.setName("StringResult");
pwd.addResult(p4);
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);
ActionWorkItemInsertFactCol52 asf3 = new ActionWorkItemInsertFactCol52();
asf3.setBoundName("$r");
asf3.setFactField("ResultIntegerField");
asf3.setType(DataType.TYPE_NUMERIC_INTEGER);
asf3.setWorkItemName("WorkItem");
asf3.setWorkItemResultParameterName("IntegerResult");
asf3.setParameterClassName(Integer.class.getName());
cols.add(asf3);
ActionWorkItemInsertFactCol52 asf4 = new ActionWorkItemInsertFactCol52();
asf4.setBoundName("$r");
asf4.setFactField("ResultStringField");
asf4.setType(DataType.TYPE_STRING);
asf4.setWorkItemName("WorkItem");
asf4.setWorkItemResultParameterName("StringResult");
asf4.setParameterClassName(String.class.getName());
cols.add(asf4);
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(4, mpwd.getResults().size());
PortableBooleanParameterDefinition mp1 = (PortableBooleanParameterDefinition) mpwd.getResult("BooleanResult");
assertNotNull(mp1);
PortableFloatParameterDefinition mp2 = (PortableFloatParameterDefinition) mpwd.getResult("FloatResult");
assertNotNull(mp2);
PortableIntegerParameterDefinition mp3 = (PortableIntegerParameterDefinition) mpwd.getResult("IntegerResult");
assertNotNull(mp3);
PortableStringParameterDefinition mp4 = (PortableStringParameterDefinition) mpwd.getResult("StringResult");
assertNotNull(mp4);
// Check ActionInsertFact
assertEquals(aif.getBoundName(), "$r");
assertEquals(4, 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());
ActionFieldValue fv2 = aif.getFieldValues()[1];
assertNotNull(fv2);
assertTrue(fv2 instanceof ActionWorkItemFieldValue);
ActionWorkItemFieldValue wifv2 = (ActionWorkItemFieldValue) fv2;
assertEquals("ResultFloatField", wifv2.getField());
assertEquals(DataType.TYPE_NUMERIC_FLOAT, wifv2.getType());
assertEquals("WorkItem", wifv2.getWorkItemName());
assertEquals("FloatResult", wifv2.getWorkItemParameterName());
assertEquals(Float.class.getName(), wifv2.getWorkItemParameterClassName());
ActionFieldValue fv3 = aif.getFieldValues()[2];
assertNotNull(fv3);
assertTrue(fv3 instanceof ActionWorkItemFieldValue);
ActionWorkItemFieldValue wifv3 = (ActionWorkItemFieldValue) fv3;
assertEquals("ResultIntegerField", wifv3.getField());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, wifv3.getType());
assertEquals("WorkItem", wifv3.getWorkItemName());
assertEquals("IntegerResult", wifv3.getWorkItemParameterName());
assertEquals(Integer.class.getName(), wifv3.getWorkItemParameterClassName());
ActionFieldValue fv4 = aif.getFieldValues()[3];
assertNotNull(fv4);
assertTrue(fv4 instanceof ActionWorkItemFieldValue);
ActionWorkItemFieldValue wifv4 = (ActionWorkItemFieldValue) fv4;
assertEquals("ResultStringField", wifv4.getField());
assertEquals(DataType.TYPE_STRING, wifv4.getType());
assertEquals("WorkItem", wifv4.getWorkItemName());
assertEquals("StringResult", wifv4.getWorkItemParameterName());
assertEquals(String.class.getName(), wifv4.getWorkItemParameterClassName());
}
Aggregations