use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testRHSExecuteWorkItemWithMissingBindings2.
@Test
public // populated in the RHS if the Pattern is *NOT* bound to the same variable
void testRHSExecuteWorkItemWithMissingBindings2() {
RuleModel m = new RuleModel();
m.name = "WorkItem";
FactPattern fp1 = new FactPattern("Person");
fp1.setBoundName("$p");
SingleFieldConstraint con1 = new SingleFieldConstraint();
con1.setFieldType(DataType.TYPE_STRING);
con1.setFieldName("name");
con1.setOperator("==");
con1.setValue("Michael");
con1.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
fp1.addConstraint(con1);
m.addLhsItem(fp1);
ActionExecuteWorkItem awi = new ActionExecuteWorkItem();
PortableWorkDefinition pwd = new PortableWorkDefinition();
pwd.setName("WorkItem");
awi.setWorkDefinition(pwd);
PortableBooleanParameterDefinition p1 = new PortableBooleanParameterDefinition();
p1.setName("BooleanParameter");
p1.setBinding("$b2");
p1.setValue(Boolean.TRUE);
pwd.addParameter(p1);
PortableFloatParameterDefinition p2 = new PortableFloatParameterDefinition();
p2.setName("FloatParameter");
p2.setBinding("$f2");
p2.setValue(123.456f);
pwd.addParameter(p2);
PortableIntegerParameterDefinition p3 = new PortableIntegerParameterDefinition();
p3.setName("IntegerParameter");
p3.setBinding("$i2");
p3.setValue(123);
pwd.addParameter(p3);
PortableStringParameterDefinition p4 = new PortableStringParameterDefinition();
p4.setName("StringParameter");
p4.setBinding("$s2");
p4.setValue("hello");
pwd.addParameter(p4);
m.addRhsItem(awi);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertTrue(result.indexOf("org.drools.core.process.instance.WorkItemManager wim = (org.drools.core.process.instance.WorkItemManager) drools.getWorkingMemory().getWorkItemManager();") != -1);
assertTrue(result.indexOf("org.drools.core.process.instance.impl.WorkItemImpl wiWorkItem = new org.drools.core.process.instance.impl.WorkItemImpl();") != -1);
assertFalse(result.indexOf("wiWorkItem.getParameters().put( \"BooleanParameter\", $b );") != -1);
assertFalse(result.indexOf("wiWorkItem.getParameters().put( \"FloatParameter\", $f );") != -1);
assertFalse(result.indexOf("wiWorkItem.getParameters().put( \"IntegerParameter\", $i );") != -1);
assertFalse(result.indexOf("wiWorkItem.getParameters().put( \"StringParameter\", $s );") != -1);
assertTrue(result.indexOf("wim.internalExecuteWorkItem( wiWorkItem );") != -1);
checkMarshalling(null, m);
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method getComplexModel.
private RuleModel getComplexModel(boolean useDsl) {
final RuleModel m = new RuleModel();
m.name = "Complex Rule";
m.setPackageName("org.test");
m.addAttribute(new RuleAttribute("no-loop", "true"));
m.addAttribute(new RuleAttribute("salience", "-10"));
m.addAttribute(new RuleAttribute("agenda-group", "aGroup"));
final FactPattern pat = new FactPattern("Person");
pat.setBoundName("p1");
final SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldBinding("f1");
con.setFieldName("age");
con.setOperator("<");
con.setValue("42");
pat.addConstraint(con);
m.addLhsItem(pat);
final CompositeFactPattern comp = new CompositeFactPattern("not");
comp.addFactPattern(new FactPattern("Cancel"));
m.addLhsItem(comp);
final ActionUpdateField upd1 = new ActionUpdateField();
upd1.setVariable("p1");
upd1.addFieldValue(new ActionFieldValue("status", "rejected", DataType.TYPE_STRING));
upd1.addFieldValue(new ActionFieldValue("name", "Fred", DataType.TYPE_STRING));
m.addRhsItem(upd1);
final ActionRetractFact ret = new ActionRetractFact("p1");
m.addRhsItem(ret);
if (useDsl) {
final DSLSentence sen = new DSLSentence();
sen.setDefinition("Send an email to {administrator}");
m.addRhsItem(sen);
}
addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
addModelField("org.test.Person", "age", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
addModelField("org.test.Person", "status", String.class.getName(), DataType.TYPE_STRING);
return m;
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeConstraints.
@Test
public void testCompositeConstraints() {
RuleModel m = new RuleModel();
m.name = "with composite";
FactPattern p1 = new FactPattern("Person");
p1.setBoundName("p1");
m.addLhsItem(p1);
FactPattern p = new FactPattern("Goober");
m.addLhsItem(p);
CompositeFieldConstraint comp = new CompositeFieldConstraint();
comp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_OR);
p.addConstraint(comp);
final SingleFieldConstraint X = new SingleFieldConstraint();
X.setFieldName("goo");
X.setFieldType(DataType.TYPE_STRING);
X.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
X.setValue("foo");
X.setOperator("==");
X.setConnectives(new ConnectiveConstraint[1]);
X.getConnectives()[0] = new ConnectiveConstraint();
X.getConnectives()[0].setConstraintValueType(ConnectiveConstraint.TYPE_LITERAL);
X.getConnectives()[0].setFieldType(DataType.TYPE_STRING);
X.getConnectives()[0].setOperator("|| ==");
X.getConnectives()[0].setValue("bar");
comp.addConstraint(X);
final SingleFieldConstraint Y = new SingleFieldConstraint();
Y.setFieldName("goo2");
Y.setFieldType(DataType.TYPE_STRING);
Y.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
Y.setValue("foo");
Y.setOperator("==");
comp.addConstraint(Y);
CompositeFieldConstraint comp2 = new CompositeFieldConstraint();
comp2.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_AND);
final SingleFieldConstraint Q1 = new SingleFieldConstraint();
Q1.setFieldType(DataType.TYPE_STRING);
Q1.setFieldName("goo");
Q1.setOperator("==");
Q1.setValue("whee");
Q1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
comp2.addConstraint(Q1);
final SingleFieldConstraint Q2 = new SingleFieldConstraint();
Q2.setFieldType(DataType.TYPE_STRING);
Q2.setFieldName("gabba");
Q2.setOperator("==");
Q2.setValue("whee");
Q2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
comp2.addConstraint(Q2);
// now nest it
comp.addConstraint(comp2);
final SingleFieldConstraint Z = new SingleFieldConstraint();
Z.setFieldName("goo3");
Z.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
Z.setFieldType(DataType.TYPE_STRING);
Z.setValue("foo");
Z.setOperator("==");
p.addConstraint(Z);
ActionInsertFact ass = new ActionInsertFact("Whee");
m.addRhsItem(ass);
String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
String expected = "rule \"with composite\"\n" + "\tdialect \"mvel\"\n" + "\twhen\n" + "\t\tp1 : Person( )\n" + "\t\tGoober( goo == \"foo\" || == \"bar\" || goo2 == \"foo\" || ( goo == \"whee\" && gabba == \"whee\" ), goo3 == \"foo\" )\n" + "\tthen\n" + "\t\tinsert( new Whee() );\n" + "end\n";
assertEqualsIgnoreWhitespace(expected, actual);
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testNotNull.
@Test
public void testNotNull() {
String expected = "" + "rule \"my rule\" \n" + " dialect \"mvel\"\n" + " when\n" + " Customer( contact != null , contact.tel1 > 15 )\n" + " then\n" + "end\n";
final RuleModel m = new RuleModel();
FactPattern factPattern = new FactPattern();
factPattern.setFactType("Customer");
m.lhs = new IPattern[] { factPattern };
SingleFieldConstraint constraint1 = new SingleFieldConstraint("Customer", "contact", "Contact", null);
constraint1.setOperator("!= null");
factPattern.addConstraint(constraint1);
SingleFieldConstraint constraint2 = new SingleFieldConstraint("Customer", "contact", "Contact", null);
factPattern.addConstraint(constraint2);
SingleFieldConstraint constraint3 = new SingleFieldConstraint("Contact", "tel1", "Integer", constraint2);
constraint3.setOperator(">");
constraint3.setValue("15");
factPattern.addConstraint(constraint3);
m.name = "my rule";
checkMarshalling(expected, m);
}
use of org.drools.workbench.models.datamodel.rule.SingleFieldConstraint in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingI18NTest method testI18N_US_BoundField.
@Test
public void testI18N_US_BoundField() {
final String drl = "package org.test;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "Applicant( $a : age )\n" + "then\n" + "end";
addModelField("Applicant", "age", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, new ArrayList<String>(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
assertTrue(m.lhs[0] instanceof FactPattern);
final FactPattern fp = (FactPattern) m.lhs[0];
assertEquals("Applicant", fp.getFactType());
assertEquals(1, fp.getNumberOfConstraints());
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("age", sfc.getFieldName());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, sfc.getFieldType());
assertEquals("$a", sfc.getFieldBinding());
}
Aggregations