use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testNewKeywordVariableNamePrefix3.
@Test
public // https://issues.jboss.org/browse/GUVNOR-2143
void testNewKeywordVariableNamePrefix3() {
String oldValue = System.getProperty("drools.dateformat");
try {
System.setProperty("drools.dateformat", "dd-MMM-yyyy");
String drl = "package org.test;\n" + "rule \"rule1\"\n" + " dialect \"java\"\n" + " when\n" + " $a : Applicant()\n" + " then\n" + " java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + " java.util.Date newStartDate = new java.util.Date();\n" + " modify( $a ) \n" + " { setApplicantDate( newStartDate ) }\n" + "end\n";
addModelField("org.test.Applicant", "this", "org.test.Applicant", DataType.TYPE_THIS);
addModelField("org.test.Applicant", "applicantDate", Date.class.getName(), DataType.TYPE_DATE);
when(dmo.getPackageName()).thenReturn("org.test");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Applicant", fp.getFactType());
assertEquals("$a", fp.getBoundName());
assertNull(fp.getConstraintList());
assertEquals(2, m.rhs.length);
assertTrue(m.rhs[0] instanceof FreeFormLine);
FreeFormLine ffl = (FreeFormLine) m.rhs[0];
assertEquals("java.util.Date newStartDate = new java.util.Date();", ffl.getText());
assertTrue(m.rhs[1] instanceof ActionUpdateField);
ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
assertEquals("$a", auf.getVariable());
assertEquals(1, auf.getFieldValues().length);
ActionFieldValue afv = auf.getFieldValues()[0];
assertEquals("applicantDate", afv.getField());
assertEquals("newStartDate", afv.getValue());
assertEquals(FieldNatureType.TYPE_FORMULA, afv.getNature());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(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 RuleModelDRLPersistenceUnmarshallingTest method testNewKeywordVariableNamePrefix1.
@Test
public // https://issues.jboss.org/browse/GUVNOR-2143
void testNewKeywordVariableNamePrefix1() {
String oldValue = System.getProperty("drools.dateformat");
try {
System.setProperty("drools.dateformat", "dd-MMM-yyyy");
String drl = "package org.test;\n" + "rule \"rule1\"\n" + " dialect \"java\"\n" + " when\n" + " $bundle : Bundle( $treatmentEffectiveDt : treatmentEffectiveDt )\n" + " then\n" + " java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + " DateTime newStartDate = new DateTime();\n" + " modify( $bundle ) {\n" + " setTreatmentEffectiveDt( newStartDate.toDate() )" + " }\n" + "end\n";
addModelField("org.test.Bundle", "this", "org.test.Bundle", DataType.TYPE_THIS);
addModelField("org.test.Bundle", "treatmentEffectiveDt", Date.class.getName(), DataType.TYPE_DATE);
when(dmo.getPackageName()).thenReturn("org.test");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Bundle", fp.getFactType());
assertEquals("$bundle", fp.getBoundName());
assertEquals(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Bundle", sfp.getFactType());
assertEquals("treatmentEffectiveDt", sfp.getFieldName());
assertEquals("$treatmentEffectiveDt", sfp.getFieldBinding());
assertNull(sfp.getOperator());
assertNull(sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_UNDEFINED, sfp.getConstraintValueType());
assertEquals(2, m.rhs.length);
assertTrue(m.rhs[0] instanceof FreeFormLine);
FreeFormLine ffl = (FreeFormLine) m.rhs[0];
assertEquals("DateTime newStartDate = new DateTime();", ffl.getText());
assertTrue(m.rhs[1] instanceof ActionUpdateField);
ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
assertEquals("$bundle", auf.getVariable());
assertEquals(1, auf.getFieldValues().length);
ActionFieldValue afv = auf.getFieldValues()[0];
assertEquals("treatmentEffectiveDt", afv.getField());
assertEquals("newStartDate.toDate()", afv.getValue());
assertEquals(FieldNatureType.TYPE_FORMULA, afv.getNature());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(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 RuleModelDRLPersistenceUnmarshallingTest method testMethodCall.
@Test
public void testMethodCall() {
// BZ-1042511
String drl = "" + "package org.mortgages;\n" + "import org.mortgages.LoanApplication;\n" + "import java.util.Map;\n" + "rule \"my rule\"\n" + " dialect \"mvel\"\n" + " when\n" + " a : LoanApplication( )\n" + " m : Map()\n" + " then\n" + " m.put(\"key\", a );\n" + "end\n";
HashMap<String, String> globals = new HashMap<>();
when(dmo.getPackageGlobals()).thenReturn(globals);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertEquals(2, m.getImports().getImports().size());
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod mc = (ActionCallMethod) m.rhs[0];
assertEquals("put", mc.getMethodName());
assertEquals("m", mc.getVariable());
assertEquals(1, mc.getState());
assertEquals(2, mc.getFieldValues().length);
ActionFieldValue f1 = mc.getFieldValue(0);
assertEquals("key", f1.getValue());
ActionFieldValue f2 = mc.getFieldValue(1);
assertEquals("a", f2.getValue());
String marshalled = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
logger.debug(marshalled);
assertEqualsIgnoreWhitespace(drl, marshalled);
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testBigDecimal2.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1189930
void testBigDecimal2() throws Exception {
String drl = "package org.test;\n" + "import java.lang.Number;\n" + "import java.math.BigDecimal;\n" + "import java.util.Calendar;\n" + "rule \"BigDecimalRule\"\n" + " dialect \"java\"\n" + "when\n" + " $bd : BigDecimal( )\n" + "then\n" + " LastRunInformation lastRun = new LastRunInformation();\n" + " lastRun.setLastNumber($bd);\n" + " insert(lastRun);\n" + "end";
addModelField("org.test.LastRunInformation", "this", "org.test.LastRunInformation", DataType.TYPE_THIS);
addModelField("org.test.LastRunInformation", "lastNumber", BigDecimal.class.getName(), DataType.TYPE_NUMERIC_BIGDECIMAL);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
final IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
final FactPattern fp = (FactPattern) p;
assertEquals("$bd", fp.getBoundName());
assertEquals("BigDecimal", fp.getFactType());
assertEquals(1, m.rhs.length);
final IAction a = m.rhs[0];
assertTrue(a instanceof ActionInsertFact);
final ActionInsertFact aif = (ActionInsertFact) a;
assertEquals("lastRun", aif.getBoundName());
assertEquals("LastRunInformation", aif.getFactType());
assertEquals(1, aif.getFieldValues().length);
final ActionFieldValue afv0 = aif.getFieldValues()[0];
assertEquals("lastNumber", afv0.getField());
assertEquals(FieldNatureType.TYPE_VARIABLE, afv0.getNature());
assertEquals("=$bd", afv0.getValue());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testMethodCallCheckParameterDataTypes3.
@Test
public void testMethodCallCheckParameterDataTypes3() {
// BZ-1045423
String drl = "" + "package org.mortgages;\n" + "import org.mortgages.MyType;\n" + "rule \"my rule\"\n" + " dialect \"mvel\"\n" + " when\n" + " i : Integer( )\n" + " t : MyType( )\n" + " then\n" + " t.doSomething( i );\n" + "end\n";
Map<String, List<MethodInfo>> methodInformation = new HashMap<>();
List<MethodInfo> mapMethodInformation = new ArrayList<>();
mapMethodInformation.add(new MethodInfo("doSomething", Collections.singletonList(DataType.TYPE_NUMERIC_INTEGER), "void", "void", "org.mortgages.MyType"));
methodInformation.put("org.mortgages.MyType", mapMethodInformation);
when(dmo.getModuleMethodInformation()).thenReturn(methodInformation);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
ActionCallMethod mc = (ActionCallMethod) m.rhs[0];
assertEquals("doSomething", mc.getMethodName());
assertEquals("t", mc.getVariable());
assertEquals(1, mc.getState());
assertEquals(1, mc.getFieldValues().length);
ActionFieldValue f1 = mc.getFieldValue(0);
assertEquals("i", f1.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, f1.getType());
assertEquals(FieldNatureType.TYPE_VARIABLE, f1.getNature());
String marshalled = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
logger.debug(marshalled);
assertEqualsIgnoreWhitespace(drl, marshalled);
}
Aggregations