use of org.drools.workbench.models.datamodel.rule.FactPattern 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.FactPattern 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);
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testDSLExpansionRHS.
@Test
public void testDSLExpansionRHS() {
String drl = "rule \"rule1\"\n" + "when\n" + "> $a : Applicant()\n" + "then\n" + "Set applicant name to Bob\n" + "end\n";
final String dslDefinition = "Set applicant name to {name:\\w+ \\w+}";
final String dslFile = "[then]" + dslDefinition + "=$a.setName( \"{name}\" )";
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(drl, Collections.emptyList(), dmo, dslFile);
assertNotNull(m);
assertTrue(m.lhs[0] instanceof FactPattern);
FactPattern pattern = (FactPattern) m.lhs[0];
assertEquals("Applicant", pattern.getFactType());
assertEquals("$a", pattern.getBoundName());
assertTrue(m.rhs[0] instanceof DSLSentence);
DSLSentence dslSentence = (DSLSentence) m.rhs[0];
assertEquals(dslDefinition, dslSentence.getDefinition());
assertEquals(1, dslSentence.getValues().size());
assertTrue(dslSentence.getValues().get(0) instanceof DSLComplexVariableValue);
DSLComplexVariableValue dslComplexVariableValue = (DSLComplexVariableValue) dslSentence.getValues().get(0);
assertEquals("Bob", dslComplexVariableValue.getValue());
assertEquals("\\w+ \\w+", dslComplexVariableValue.getId());
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testLHSFromCollectWithoutListDeclared.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1158813
void testLHSFromCollectWithoutListDeclared() throws Exception {
String drl = "package org.test;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + " c : Customer( )\n" + " items : java.util.List( eval( size == c.items.size )) \n" + " from collect ( var : Item( price > 10 )) \n" + "then \n" + "end";
addModelField("org.test.Customer", "this", "org.test.Customer", DataType.TYPE_THIS);
addModelField("org.test.Item", "this", "org.test.Item", DataType.TYPE_THIS);
addModelField("org.test.Item", "price", "java.lang.Double", DataType.TYPE_NUMERIC_DOUBLE);
addMethodInformation("org.test.Customer", "items", new ArrayList<String>() {
{
}
}, DataType.TYPE_NUMERIC_INTEGER, null, DataType.TYPE_NUMERIC_INTEGER);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(2, m.lhs.length);
assertEquals(0, m.rhs.length);
assertTrue(m.lhs[0] instanceof FactPattern);
final FactPattern fp = (FactPattern) m.lhs[0];
assertEquals("Customer", fp.getFactType());
assertEquals("c", fp.getBoundName());
assertEquals(0, fp.getNumberOfConstraints());
assertTrue(m.lhs[1] instanceof FromCollectCompositeFactPattern);
final FromCollectCompositeFactPattern fcfp = (FromCollectCompositeFactPattern) m.lhs[1];
assertNotNull(fcfp.getFactPattern());
assertEquals("java.util.List", fcfp.getFactPattern().getFactType());
assertEquals("items", fcfp.getFactPattern().getBoundName());
assertEquals(1, fcfp.getFactPattern().getNumberOfConstraints());
assertTrue(fcfp.getFactPattern().getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc0 = (SingleFieldConstraint) fcfp.getFactPattern().getConstraint(0);
assertNull(sfc0.getFactType());
assertNull(sfc0.getFieldName());
assertEquals("size == c.items.size", sfc0.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, sfc0.getConstraintValueType());
assertNotNull(fcfp.getRightPattern());
assertTrue(fcfp.getRightPattern() instanceof FactPattern);
final FactPattern rfp = (FactPattern) fcfp.getRightPattern();
assertEquals("Item", rfp.getFactType());
assertEquals("var", rfp.getBoundName());
assertEquals(1, rfp.getNumberOfConstraints());
assertTrue(rfp.getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint sfc1 = (SingleFieldConstraint) rfp.getConstraint(0);
assertEquals("Item", sfc1.getFactType());
assertEquals("price", sfc1.getFieldName());
assertEquals("10", sfc1.getValue());
assertEquals(">", sfc1.getOperator());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfc1.getConstraintValueType());
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.FactPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFromAccumulate.
@Test
public void testFromAccumulate() {
String drl = "package org.test;\n" + "import java.lang.Number;\n" + "rule \"rule1\"\n" + "when\n" + " total : Number( intValue > 0 ) from accumulate ( Applicant( age < 30 ), count() )\n" + "then\n" + "end";
addModelField("java.lang.Number", "intValue", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
addModelField("org.test.Applicant", "this", "org.test.Applicant", DataType.TYPE_THIS);
addModelField("org.test.Applicant", "age", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
when(dmo.getPackageName()).thenReturn("org.test");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertTrue(m.lhs[0] instanceof FromAccumulateCompositeFactPattern);
FromAccumulateCompositeFactPattern pattern = (FromAccumulateCompositeFactPattern) m.lhs[0];
assertNotNull(pattern.getFactPattern());
FactPattern factPattern = pattern.getFactPattern();
assertEquals("total", factPattern.getBoundName());
assertNotNull(factPattern.getConstraintList());
assertEquals(1, factPattern.getConstraintList().getNumberOfConstraints());
FieldConstraint constraint = factPattern.getConstraintList().getConstraint(0);
assertTrue(constraint instanceof SingleFieldConstraint);
SingleFieldConstraint fieldConstraint = (SingleFieldConstraint) constraint;
assertEquals("Number", fieldConstraint.getFactType());
assertEquals("intValue", fieldConstraint.getFieldName());
assertEquals("Integer", fieldConstraint.getFieldType());
assertEquals(">", fieldConstraint.getOperator());
assertEquals("0", fieldConstraint.getValue());
}
Aggregations