use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSAppendToList.
@Test
public void testRHSAppendToList() throws Exception {
// https://issues.jboss.org/browse/GUVNOR-2286
String drl = "package org.test;\n" + "import java.lang.Number;\n" + "rule \"RuleCheckEmail\"\n" + "dialect \"mvel\"\n" + "when\n" + "incomeData : IncomeData( email == \"myemail\" , $list : list != null )\n" + "then\n" + "Element element = new Element();\n" + "element.setId( 2 );\n" + "insertLogical( element );\n" + "$list.add( element );\n" + "end";
addModelField("org.test.IncomeData", "this", "org.test.IncomeData", DataType.TYPE_THIS);
addModelField("org.test.IncomeData", "email", String.class.getName(), DataType.TYPE_STRING);
addModelField("org.test.IncomeData", "list", List.class.getName(), DataType.TYPE_COLLECTION);
addModelField("java.util.List", "this", "java.util.List", DataType.TYPE_THIS);
addMethodInformation("java.util.List", "add", new ArrayList<String>() {
{
add("java.lang.Object");
}
}, DataType.TYPE_BOOLEAN, null, DataType.TYPE_BOOLEAN);
addMethodInformation("java.util.List", "add", new ArrayList<String>() {
{
add("java.lang.Integer");
add("java.lang.Object");
}
}, DataType.TYPE_BOOLEAN, null, DataType.TYPE_BOOLEAN);
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 p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
final FactPattern fp0 = (FactPattern) p0;
assertEquals("IncomeData", fp0.getFactType());
assertEquals(2, fp0.getNumberOfConstraints());
assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint fp0sfc0 = (SingleFieldConstraint) fp0.getConstraint(0);
assertEquals("IncomeData", fp0sfc0.getFactType());
assertEquals("email", fp0sfc0.getFieldName());
assertEquals(DataType.TYPE_STRING, fp0sfc0.getFieldType());
assertEquals("==", fp0sfc0.getOperator());
assertEquals("myemail", fp0sfc0.getValue());
assertTrue(fp0.getConstraint(1) instanceof SingleFieldConstraint);
final SingleFieldConstraint fp0sfc1 = (SingleFieldConstraint) fp0.getConstraint(1);
assertEquals("IncomeData", fp0sfc1.getFactType());
assertEquals("list", fp0sfc1.getFieldName());
assertEquals(DataType.TYPE_COLLECTION, fp0sfc1.getFieldType());
assertEquals("!= null", fp0sfc1.getOperator());
assertNull(fp0sfc1.getValue());
assertEquals(2, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionInsertLogicalFact);
final ActionInsertLogicalFact a0 = (ActionInsertLogicalFact) m.rhs[0];
assertEquals("Element", a0.getFactType());
assertEquals("element", a0.getBoundName());
assertEquals(1, a0.getFieldValues().length);
final ActionFieldValue a0f0 = a0.getFieldValues()[0];
assertEquals("id", a0f0.getField());
assertEquals("2", a0f0.getValue());
assertEquals(DataType.TYPE_NUMERIC, a0f0.getType());
assertEquals(FieldNatureType.TYPE_LITERAL, a0f0.getNature());
assertTrue(m.rhs[1] instanceof ActionCallMethod);
final ActionCallMethod a1 = (ActionCallMethod) m.rhs[1];
assertEquals("add", a1.getMethodName());
assertEquals("$list", a1.getVariable());
assertEquals(1, a1.getFieldValues().length);
final ActionFieldValue a1f0 = a1.getFieldValues()[0];
assertEquals("add", a1f0.getField());
assertEquals("element", a1f0.getValue());
assertEquals(FieldNatureType.TYPE_VARIABLE, a1f0.getNature());
assertEquals("java.lang.Object", a1f0.getType());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method actionSetFieldWithFormula.
@Test
public void actionSetFieldWithFormula() throws Exception {
String drl = "package org.mortgages;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Application()\n" + "then\n" + "$a.setName( \"Pupa\" + 20 + \"Smurf\" );" + "end";
addModelField("org.mortgages.Applicant", "this", "org.mortgages.Applicant", DataType.TYPE_THIS);
addModelField("org.mortgages.Applicant", "name", String.class.getName(), DataType.TYPE_STRING);
when(dmo.getPackageName()).thenReturn("org.mortgages");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("r1", m.name);
// LHS Pattern
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Application", fp.getFactType());
assertEquals(0, fp.getNumberOfConstraints());
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionSetField);
ActionSetField asf = (ActionSetField) m.rhs[0];
assertEquals("$a", asf.getVariable());
assertEquals(1, asf.getFieldValues().length);
ActionFieldValue afv0 = asf.getFieldValues()[0];
assertEquals("name", afv0.getField());
assertEquals("\"Pupa\" + 20 + \"Smurf\"", afv0.getValue());
assertEquals(FieldNatureType.TYPE_FORMULA, afv0.getNature());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method actionUpdateFieldWithFormula.
@Test
public void actionUpdateFieldWithFormula() throws Exception {
String drl = "package org.mortgages;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Application()\n" + "then\n" + "modify( $a ) {\n" + " setName( \"Pupa\" + 20 + \"Smurf\" )" + "}\n" + "end";
addModelField("org.mortgages.Applicant", "this", "org.mortgages.Applicant", DataType.TYPE_THIS);
addModelField("org.mortgages.Applicant", "name", String.class.getName(), DataType.TYPE_STRING);
when(dmo.getPackageName()).thenReturn("org.mortgages");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("r1", m.name);
// LHS Pattern
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Application", fp.getFactType());
assertEquals(0, fp.getNumberOfConstraints());
assertEquals(1, m.rhs.length);
assertTrue(m.rhs[0] instanceof ActionUpdateField);
ActionUpdateField auf = (ActionUpdateField) m.rhs[0];
assertEquals("$a", auf.getVariable());
assertEquals(1, auf.getFieldValues().length);
ActionFieldValue afv0 = auf.getFieldValues()[0];
assertEquals("name", afv0.getField());
assertEquals("\"Pupa\" + 20 + \"Smurf\"", afv0.getValue());
assertEquals(FieldNatureType.TYPE_FORMULA, afv0.getNature());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testStringReplaceExpression.
@Test
public void testStringReplaceExpression() throws Exception {
// https://bugzilla.redhat.com/show_bug.cgi?id=1264321
String drl = "rule \"Replace_condition_Issue\"\n" + "dialect \"mvel\"\n" + "when\n" + " MyType( myString.replace(\"a\",\"b\"))\n" + "then\n" + "end";
addModelField("org.test.MyType", "this", "org.test.MyType", DataType.TYPE_THIS);
addModelField("org.test.MyType", "myString", String.class.getName(), DataType.TYPE_STRING);
addMethodInformation("java.lang.String", "replace", new ArrayList<String>() {
{
add("String");
add("String");
}
}, "java.lang.String", null, DataType.TYPE_STRING);
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 p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
final FactPattern fp0 = (FactPattern) p0;
assertEquals("MyType", fp0.getFactType());
assertEquals(1, fp0.getNumberOfConstraints());
assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraintEBLeftSide);
final SingleFieldConstraintEBLeftSide fp0sfc0 = (SingleFieldConstraintEBLeftSide) fp0.getConstraint(0);
assertEquals(3, fp0sfc0.getExpressionLeftSide().getParts().size());
assertTrue(fp0sfc0.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
final ExpressionUnboundFact ep0 = (ExpressionUnboundFact) fp0sfc0.getExpressionLeftSide().getParts().get(0);
assertEquals("MyType", ep0.getFactType());
assertTrue(fp0sfc0.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
final ExpressionField ep1 = (ExpressionField) fp0sfc0.getExpressionLeftSide().getParts().get(1);
assertEquals("myString", ep1.getName());
assertTrue(fp0sfc0.getExpressionLeftSide().getParts().get(2) instanceof ExpressionMethod);
final ExpressionMethod ep2 = (ExpressionMethod) fp0sfc0.getExpressionLeftSide().getParts().get(2);
assertEquals("replace", ep2.getName());
assertEquals(2, ep2.getParams().size());
final ExpressionFormLine param0 = ep2.getParams().get(new ExpressionMethodParameterDefinition(0, "String"));
assertNotNull(param0);
assertEquals(1, param0.getParts().size());
assertNotNull(param0.getParts().get(0));
assertEquals("a", param0.getParts().get(0).getName());
assertEquals("String", param0.getParts().get(0).getClassType());
final ExpressionFormLine param1 = ep2.getParams().get(new ExpressionMethodParameterDefinition(1, "String"));
assertNotNull(param1);
assertEquals(1, param1.getParts().size());
assertNotNull(param1.getParts().get(0));
assertEquals("b", param1.getParts().get(0).getName());
assertEquals("String", param1.getParts().get(0).getClassType());
assertEquals(0, m.rhs.length);
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSChainedMethodCalls3.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1127303
void testRHSChainedMethodCalls3() throws Exception {
String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"mvel\"\n" + "when\n" + " Person( $n : name )\n" + "then\n" + " $n.toUpperCase().indexOf(\"(\", 1);\n" + "end";
addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
addModelField("org.test.Person", "name", String.class.getName(), DataType.TYPE_STRING);
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 p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
final FactPattern fp0 = (FactPattern) p0;
assertEquals("Person", fp0.getFactType());
assertEquals(1, fp0.getNumberOfConstraints());
assertTrue(fp0.getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc1 = (SingleFieldConstraint) fp0.getConstraint(0);
assertEquals("Person", sfc1.getFactType());
assertEquals("name", sfc1.getFieldName());
assertEquals(DataType.TYPE_STRING, sfc1.getFieldType());
assertEquals(1, m.rhs.length);
final IAction a0 = m.rhs[0];
assertTrue(a0 instanceof FreeFormLine);
final FreeFormLine ffl1 = (FreeFormLine) a0;
assertEquals("$n.toUpperCase().indexOf(\"(\", 1);", ffl1.getText());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Aggregations