use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testLiteralBigIntegerMvel.
@Test
public void testLiteralBigIntegerMvel() {
RuleModel m = new RuleModel();
m.name = "test literal biginteger";
m.addAttribute(new RuleAttribute("dialect", "mvel"));
FactPattern p = new FactPattern("Person");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_NUMERIC_BIGINTEGER);
con.setFieldName("field1");
con.setOperator("==");
con.setValue("44");
con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
p.addConstraint(con);
m.addLhsItem(p);
ActionInsertFact ai = new ActionInsertFact("Person");
ai.addFieldValue(new ActionFieldValue("field1", "55", DataType.TYPE_NUMERIC_BIGINTEGER));
m.addRhsItem(ai);
String expected = "rule \"test literal biginteger\" \n" + "\tdialect \"mvel\"\n when \n" + " Person(field1 == 44I ) \n" + " then \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( 55I ); \n" + "insert( fact0 ); \n" + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSDateUpdateAction.
@Test
public void testRHSDateUpdateAction() {
String oldValue = System.getProperty("drools.dateformat");
try {
System.setProperty("drools.dateformat", "dd-MMM-yyyy");
RuleModel m = new RuleModel();
m.name = "RHS Date";
FactPattern p = new FactPattern("Person");
p.setBoundName("$p");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_DATE);
con.setFieldName("dateOfBirth");
con.setOperator("==");
con.setValue("31-Jan-2000");
con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
p.addConstraint(con);
m.addLhsItem(p);
ActionSetField au = new ActionSetField("$p");
au.addFieldValue(new ActionFieldValue("dob", "31-Jan-2000", DataType.TYPE_DATE));
m.addRhsItem(au);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertTrue(result.indexOf("java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");") != -1);
assertTrue(result.indexOf("$p.setDob( sdf.parse(\"31-Jan-2000\"") != -1);
assertTrue(result.indexOf("update( $p );") == -1);
checkMarshalling(null, m);
} finally {
if (oldValue == null) {
System.clearProperty("drools.dateformat");
} else {
System.setProperty("drools.dateformat", oldValue);
}
}
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSSetFieldWithVariable.
@Test
public void testRHSSetFieldWithVariable() {
// https://bugzilla.redhat.com/show_bug.cgi?id=1077212
RuleModel m = new RuleModel();
m.name = "variable";
FactPattern p = new FactPattern("Person");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
con.setFieldName("field1");
con.setOperator("==");
con.setValue("44");
con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
con.setFieldBinding("$f");
p.addConstraint(con);
m.addLhsItem(p);
ActionInsertFact ai = new ActionInsertFact("Person");
ActionFieldValue acv = new ActionFieldValue("field1", "=$f", DataType.TYPE_OBJECT);
acv.setNature(FieldNatureType.TYPE_VARIABLE);
ai.addFieldValue(acv);
m.addRhsItem(ai);
String expected = "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";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue 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.ActionFieldValue 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());
}
Aggregations