use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleTemplateModelDRLPersistenceTest method testRHSInsert.
@Test
public void testRHSInsert() {
TemplateModel m = new TemplateModel();
m.name = "t1";
FactPattern p = new FactPattern("Person");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_STRING);
con.setFieldName("field1");
con.setOperator("==");
con.setValue("$f1");
con.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE);
p.addConstraint(con);
m.addLhsItem(p);
ActionInsertFact actionInsertFact = new ActionInsertFact();
actionInsertFact.setFactType("Applicant");
ActionFieldValue actionFieldValue = new ActionFieldValue("age", "123", "Integer");
actionFieldValue.setNature(SingleFieldConstraint.TYPE_LITERAL);
actionInsertFact.addFieldValue(actionFieldValue);
m.addRhsItem(actionInsertFact);
m.addRow(new String[] { "foo" });
String expected = "rule \"t1_0\"" + "dialect \"mvel\"\n" + "when \n" + "Person( field1 == \"foo\" )\n" + "then \n" + " Applicant fact0 = new Applicant(); \n" + " fact0.setAge(123); \n" + " insert(fact0); \n" + "end";
checkMarshall(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RHSClassDependencyVisitorTest method visitActionInsertLogicalFact.
@Test
public void visitActionInsertLogicalFact() {
ActionInsertLogicalFact iAction = new ActionInsertLogicalFact();
iAction.setFieldValues(new ActionFieldValue[] { new ActionFieldValue("field", "value", "type") });
visitor.visit(iAction);
assertTrue(visitor.getRHSClasses().containsKey("type"));
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testActionSetFieldValueWithDSL.
@Test
public void testActionSetFieldValueWithDSL() {
String oldValue = System.getProperty("drools.dateformat");
try {
System.setProperty("drools.dateformat", "dd-MMM-yyyy");
final String dsl = "There is an Applicant";
final String dslDefinition = "[when]" + dsl + "=$a : Applicant();";
final RuleModel m = new RuleModel();
m.name = "r0";
final DSLSentence sen = new DSLSentence();
sen.setDefinition(dsl);
m.addLhsItem(sen);
final ActionInsertFact aif = new ActionInsertFact("Applicant");
aif.addFieldValue(new ActionFieldValue("applicationDate", "13-Mar-2017", DataType.TYPE_DATE));
m.addRhsItem(aif);
final String expected = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "There is an Applicant\n" + "then\n" + ">java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + ">Applicant fact0 = new Applicant();\n" + ">fact0.setApplicationDate( sdf.parse(\"13-Mar-2017\") );\n" + ">insert( fact0 );\n" + "end";
final String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertEqualsIgnoreWhitespace(expected, actual);
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
final RuleModel m2 = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(actual, Collections.emptyList(), dmo, dslDefinition);
assertNotNull(m2);
// LHS
assertEquals(1, m.lhs.length);
DSLSentence dslSentence = (DSLSentence) m.lhs[0];
assertEquals(dsl, dslSentence.getDefinition());
assertEquals(0, dslSentence.getValues().size());
// RHS
assertEquals(1, m2.rhs.length);
assertTrue(m2.rhs[0] instanceof ActionInsertFact);
final ActionInsertFact a = (ActionInsertFact) m2.rhs[0];
assertEquals("Applicant", a.getFactType());
assertEquals(1, a.getFieldValues().length);
final ActionFieldValue afv = a.getFieldValues()[0];
assertEquals("applicationDate", afv.getField());
assertEquals("13-Mar-2017", afv.getValue());
} 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 testAssertWithDSL.
@Test
public void testAssertWithDSL() throws Exception {
RuleModel m = new RuleModel();
DSLSentence sen = new DSLSentence();
sen.setDefinition("I CAN HAS DSL");
m.addRhsItem(sen);
ActionInsertFact ins = new ActionInsertFact("Shizzle");
ActionFieldValue val = new ActionFieldValue("goo", "42", "Numeric");
ins.setFieldValues(new ActionFieldValue[1]);
ins.getFieldValues()[0] = val;
m.addRhsItem(ins);
ActionInsertLogicalFact insL = new ActionInsertLogicalFact("Shizzle");
ActionFieldValue valL = new ActionFieldValue("goo", "42", "Numeric");
insL.setFieldValues(new ActionFieldValue[1]);
insL.getFieldValues()[0] = valL;
m.addRhsItem(insL);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertTrue(result.indexOf(">insert") > -1);
assertTrue(result.indexOf(">insertLogical") > -1);
}
use of org.drools.workbench.models.datamodel.rule.ActionFieldValue in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testLiteralBigDecimalJava.
@Test
public void testLiteralBigDecimalJava() {
RuleModel m = new RuleModel();
m.name = "test literal bigdecimal";
m.addAttribute(new RuleAttribute("dialect", "java"));
FactPattern p = new FactPattern("Person");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_NUMERIC_BIGDECIMAL);
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_BIGDECIMAL));
m.addRhsItem(ai);
String expected = "rule \"test literal bigdecimal\" \n" + "\tdialect \"java\"\n when \n" + " Person(field1 == 44B) \n" + " then \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( new java.math.BigDecimal( \"55\" ) ); \n" + "insert( fact0 ); \n" + "end";
checkMarshalling(expected, m);
}
Aggregations