use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testCalendarsAttribute.
@Test
public void testCalendarsAttribute() {
String drl = "rule \"rule1\"\n" + "calendars \"myCalendar\", \"Yet Another Calendar\"\n" + "when\n" + "then\n" + "end";
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertEquals(1, m.attributes.length);
assertEquals("calendars", m.attributes[0].getAttributeName());
assertEquals("myCalendar, Yet Another Calendar", m.attributes[0].getValue());
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRuleModelPersistenceHelperUnwrapParenthesis.
@Test
public // https://issues.jboss.org/browse/GUVNOR-2144
void testRuleModelPersistenceHelperUnwrapParenthesis() throws Exception {
String drl = "package org.test;\n" + "rule \"Load all schedules\"\n" + "dialect \"mvel\"\n" + "agenda-group \"LoadSchedules\"\n" + "salience 900\n" + " when\n" + " $bundle : Bundle( evaluated == false )\n" + " then\n" + " $bundle.setEvaluated( true );\n" + " update( $bundle );\n" + "end";
// Expected is different as we convert "update" to "modify" blocks
String expected = "package org.test;\n" + "rule \"Load all schedules\"\n" + "dialect \"mvel\"\n" + "agenda-group \"LoadSchedules\"\n" + "salience 900\n" + " when\n" + " $bundle : Bundle( evaluated == false )\n" + " then\n" + " modify( $bundle ) {\n" + " setEvaluated( true )\n" + " }\n" + "end";
addModelField("org.test.Bundle", "this", "org.test.Bundle", DataType.TYPE_THIS);
addModelField("org.test.Person", "evaluated", Boolean.class.getName(), 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);
assertEquals(1, m.rhs.length);
assertEquals(3, m.attributes.length);
assertEqualsIgnoreWhitespace(expected, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFieldConstraintLessThanOrEqualTo.
@Test
public void testFieldConstraintLessThanOrEqualTo() {
String drl = "rule \"rule1\"\n" + "when\n" + "Applicant( age <= 22 )\n" + "then\n" + "end";
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Applicant", fp.getFactType());
assertEquals(1, fp.getNumberOfConstraints());
FieldConstraint fc = fp.getConstraint(0);
assertNotNull(fc);
assertTrue(fc instanceof SingleFieldConstraint);
SingleFieldConstraint sfc = (SingleFieldConstraint) fc;
assertEquals("<=", sfc.getOperator());
assertEquals("22", sfc.getValue());
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSFormulaTruncation.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1160658
void testRHSFormulaTruncation() throws Exception {
String drl = "package org.test;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + " $p : Person()\n" + "then\n" + " $p.setTextOut( $p.getType() + \"\" );\n" + "end";
addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
addModelField("org.test.Person", "textOut", "java.lang.String", DataType.TYPE_STRING);
addModelField("org.test.Person", "type", "java.lang.String", 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);
assertEquals(1, m.rhs.length);
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testCollectWithFreeFormDRL_MethodsDefined.
@Test
public void testCollectWithFreeFormDRL_MethodsDefined() throws Exception {
// https://bugzilla.redhat.com/show_bug.cgi?id=1060816
String drl = "package org.sample.resourceassignment;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$trans : Transactions()\n" + "$transactions : java.util.List( eval( size > 0 ) ) from collect ( Transaction() from $trans.getRecCategorization().get(\"APES-01\") )\n" + "then\n" + "end";
addModelField("Transactions", "recCategorization", "java.util.Map", DataType.TYPE_COLLECTION);
addMethodInformation("Transactions", "getRecCategorization()", Collections.emptyList(), "java.util.Map", null, DataType.TYPE_COLLECTION);
addMethodInformation("java.util.Map", "get", new ArrayList<String>() {
{
add("p0");
}
}, "java.lang.String", null, DataType.TYPE_STRING);
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(2, m.lhs.length);
IPattern p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
FactPattern fp0 = (FactPattern) p0;
assertEquals("Transactions", fp0.getFactType());
assertEquals("$trans", fp0.getBoundName());
assertEquals(0, fp0.getNumberOfConstraints());
IPattern p1 = m.lhs[1];
assertTrue(p1 instanceof FromCollectCompositeFactPattern);
FromCollectCompositeFactPattern fp1 = (FromCollectCompositeFactPattern) p1;
assertEquals("java.util.List", fp1.getFactPattern().getFactType());
assertEquals("$transactions", fp1.getFactPattern().getBoundName());
assertEquals(1, fp1.getFactPattern().getNumberOfConstraints());
assertTrue(fp1.getFactPattern().getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint fp1sfc = (SingleFieldConstraint) fp1.getFactPattern().getConstraint(0);
assertEquals("size > 0", fp1sfc.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, fp1sfc.getConstraintValueType());
assertTrue(fp1.getRightPattern() instanceof FromCompositeFactPattern);
FromCompositeFactPattern fp2 = (FromCompositeFactPattern) fp1.getRightPattern();
assertNotNull(fp2.getFactPattern());
FactPattern fp3 = fp2.getFactPattern();
assertEquals("Transaction", fp3.getFactType());
assertEquals(0, fp3.getNumberOfConstraints());
assertNotNull(fp2.getExpression());
ExpressionFormLine efl = fp2.getExpression();
assertEquals(3, efl.getParts().size());
assertTrue(efl.getParts().get(0) instanceof ExpressionVariable);
ExpressionVariable ev = (ExpressionVariable) efl.getParts().get(0);
assertEquals("$trans", ev.getName());
assertEquals("Transactions", ev.getClassType());
assertTrue(efl.getParts().get(1) instanceof ExpressionMethod);
ExpressionMethod em = (ExpressionMethod) efl.getParts().get(1);
assertEquals("getRecCategorization()", em.getName());
assertEquals("java.util.Map", em.getClassType());
assertEquals(DataType.TYPE_COLLECTION, em.getGenericType());
assertTrue(efl.getParts().get(2) instanceof ExpressionText);
ExpressionText et = (ExpressionText) efl.getParts().get(2);
assertEquals("get(\"APES-01\")", et.getName());
assertEquals("java.lang.String", et.getClassType());
assertEquals(DataType.TYPE_STRING, et.getGenericType());
assertEquals(0, m.rhs.length);
}
Aggregations