use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testLiteralNoType.
@Test
public void testLiteralNoType() {
RuleModel m = new RuleModel();
m.name = "test literal no type";
FactPattern p = new FactPattern("Person");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldName("field1");
con.setOperator("==");
con.setValue("bananna");
con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
p.addConstraint(con);
SingleFieldConstraint con2 = new SingleFieldConstraint();
con2.setFieldName("field2");
con2.setOperator("==");
con2.setValue("variableHere");
con2.setConstraintValueType(SingleFieldConstraint.TYPE_VARIABLE);
p.addConstraint(con2);
m.addLhsItem(p);
String expected = "rule \"test literal no type\"" + "\tdialect \"mvel\"\n when " + " Person(field1 == \"bananna\", field2 == variableHere)" + " then " + "end";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSDateInsertActionWithoutSystemProperty.
@Test
public void testRHSDateInsertActionWithoutSystemProperty() {
// RHBRMS-3034
String oldValue = System.getProperty("drools.dateformat");
try {
System.clearProperty("drools.dateformat");
RuleModel m = new RuleModel();
m.name = "RHS Date";
FactPattern p = new FactPattern("Person");
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);
ActionInsertFact ai = new ActionInsertFact("Birthday");
ai.addFieldValue(new ActionFieldValue("dob", "31-Jan-2000", DataType.TYPE_DATE));
m.addRhsItem(ai);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertTrue("result DRL : " + result, result.indexOf("java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");") != -1);
assertTrue(result.indexOf("fact0.setDob( sdf.parse(\"31-Jan-2000\"") != -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.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testActionCallMethod.
@Test
public void testActionCallMethod() {
final String drl = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Applicant( )\n" + "then\n" + "$a.addName( \"Michael\" );\n" + "end\n";
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
assertNotNull(m);
// LHS
assertEquals(1, m.lhs.length);
assertTrue(m.lhs[0] instanceof FactPattern);
final FactPattern p = (FactPattern) m.lhs[0];
assertEquals("$a", p.getBoundName());
assertEquals("Applicant", p.getFactType());
// RHS
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionCallMethod);
final ActionCallMethod a = (ActionCallMethod) m.rhs[0];
assertEquals("$a", a.getVariable());
assertEquals("addName", a.getMethodName());
assertEquals(1, a.getFieldValues().length);
final ActionFieldValue fv = a.getFieldValue(0);
assertEquals("Michael", fv.getValue());
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeOrConstraints.
@Test
public void testCompositeOrConstraints() {
RuleModel m = new RuleModel();
m.name = "or composite";
FactPattern p = new FactPattern("Goober");
m.addLhsItem(p);
CompositeFieldConstraint comp = new CompositeFieldConstraint();
comp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_OR);
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 actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
String expected = "rule \"or 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);
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testNotInOperatorString.
@Test
public void testNotInOperatorString() {
RuleModel m = new RuleModel();
m.name = "not in";
FactPattern p = new FactPattern("Person");
SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldType(DataType.TYPE_STRING);
con.setFieldName("field1");
con.setOperator("not in");
con.setValue("value1, value2");
con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
p.addConstraint(con);
m.addLhsItem(p);
String expected = "rule \"not in\" \n" + "dialect \"mvel\" \n" + "when \n" + " Person(field1 not in ( \"value1\", \"value2\" ) ) \n" + " then \n" + "end";
checkMarshalling(expected, m);
}
Aggregations