use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testDateInDsl.
@Test
public void testDateInDsl() throws Exception {
addModelField("org.test.Person", "bornDate", DataType.TYPE_DATE, DataType.TYPE_DATE);
addModelField("org.test.Person", "retireDate", DataType.TYPE_DATE, DataType.TYPE_DATE);
final String dslConditionDefinition = "Person was born on {date}";
final String dslConditionDrl = "$p : Person(bornDate == \"{date}\")";
final String dslActionDefinition = "Person will retire on {date}";
final String dslActionDrl = "java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\"); $p.setRetireDate(sdf.parse(\"{date}\"));";
final String dslFile = "[when]" + dslConditionDefinition + "=" + dslConditionDrl + "\n" + "[then]" + dslActionDefinition + "=" + dslActionDrl;
final String drl = "rule \"my rule\" \n" + "dialect \"mvel\"\n" + "when\n" + "Person was born on 01-01-2017\n" + "then\n" + "Person will retire on 01-01-2067\n" + "end\n";
final RuleModel m = ruleModelPersistence.unmarshalUsingDSL(drl, Collections.emptyList(), dmo, dslFile);
assertEquals(1, m.lhs.length);
assertEquals(dslConditionDefinition, ((DSLSentence) m.lhs[0]).getDefinition());
assertEquals(dslConditionDrl, ((DSLSentence) m.lhs[0]).getDrl());
assertEquals("01-01-2017", ((DSLSentence) m.lhs[0]).getValues().get(0).getValue());
assertEquals(1, m.rhs.length);
assertEquals(dslActionDefinition, ((DSLSentence) m.rhs[0]).getDefinition());
assertEquals(dslActionDrl, ((DSLSentence) m.rhs[0]).getDrl());
assertEquals("01-01-2067", ((DSLSentence) m.rhs[0]).getValues().get(0).getValue());
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testInOperatorString.
@Test
public void testInOperatorString() {
RuleModel m = new RuleModel();
m.name = "in";
FactPattern p = new FactPattern("Person");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_STRING);
con.setFieldName("field1");
con.setOperator("in");
con.setValue("value1, value2");
con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
p.addConstraint(con);
m.addLhsItem(p);
String expected = "rule \"in\" \n" + "dialect \"mvel\" \n" + "when \n" + " Person(field1 in ( \"value1\", \"value2\" ) ) \n" + " then \n" + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testGenerateEmptyXML.
@Test
public void testGenerateEmptyXML() {
final RuleModelPersistence p = RuleModelDRLPersistenceImpl.getInstance();
final String drl = p.marshal(new RuleModel());
assertNotNull(drl);
assertFalse(drl.equals(""));
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testInsertLogical.
@Test
public void testInsertLogical() {
String expected = "rule \"my rule\"\n\tno-loop true\n\tdialect \"mvel\"\n\twhen\n\t\tPerson( )\n" + "\t\tAccident( )\n\tthen\n\t\tinsertLogical( new Report() );\nend\n";
final RuleModel m = new RuleModel();
m.addLhsItem(new FactPattern("Person"));
m.addLhsItem(new FactPattern("Accident"));
m.addAttribute(new RuleAttribute("no-loop", "true"));
m.addRhsItem(new ActionInsertLogicalFact("Report"));
m.name = "my rule";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeAndConstraints.
@Test
public void testCompositeAndConstraints() {
RuleModel m = new RuleModel();
m.name = "and composite";
FactPattern p = new FactPattern("Goober");
m.addLhsItem(p);
CompositeFieldConstraint comp = new CompositeFieldConstraint();
comp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_AND);
p.addConstraint(comp);
final SingleFieldConstraint sfc1 = new SingleFieldConstraint();
sfc1.setFactType("Goober");
sfc1.setFieldName("gooField");
sfc1.setFieldType(DataType.TYPE_STRING);
sfc1.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc1.setValue("gooValue");
sfc1.setOperator("==");
comp.addConstraint(sfc1);
final SingleFieldConstraint sfc2 = new SingleFieldConstraint();
sfc2.setFactType("Goober");
sfc2.setFieldName("fooField");
sfc2.setFieldType(DataType.TYPE_OBJECT);
sfc2.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc2.setOperator("!= null");
comp.addConstraint(sfc2);
final SingleFieldConstraint sfc3 = new SingleFieldConstraint();
sfc3.setFactType("Bar");
sfc3.setFieldName("barField");
sfc3.setParent(sfc2);
sfc3.setFieldType(DataType.TYPE_STRING);
sfc3.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
sfc3.setValue("barValue");
sfc3.setOperator("==");
comp.addConstraint(sfc3);
ActionInsertFact ass = new ActionInsertFact("Whee");
m.addRhsItem(ass);
String expected = "rule \"and composite\"" + "dialect \"mvel\"\n" + "when\n" + "Goober( gooField == \"gooValue\" && fooField != null && fooField.barField == \"barValue\" )\n" + "then\n" + "insert( new Whee() );\n" + "end";
checkMarshalling(expected, m);
}
Aggregations