use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testLHSInOperatorFieldNameNotContainingNotInLiteral.
@Test
public void testLHSInOperatorFieldNameNotContainingNotInLiteral() {
String drl = "package org.test\n" + "rule \"in\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( field1 not in (1, 2) )\n" + "then\n" + "end";
addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Person", fp.getFactType());
assertEquals(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Person", sfp.getFactType());
assertEquals("field1", sfp.getFieldName());
assertEquals("not in", sfp.getOperator());
assertEquals("1, 2", sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
}
use of org.drools.workbench.models.datamodel.rule.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testValidSyntaxNonImportedType.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1218308
void testValidSyntaxNonImportedType() throws Exception {
String drl = "rule \"x\"\n" + "dialect \"mvel\"\n" + "when\n" + " var : NotImported( )\n" + " OtherType( field != var.field )\n" + " (MyType( ) from var.collectionField)\n" + "then\n" + "end";
addModelField("org.test.OtherType", "this", "org.test.OtherType", DataType.TYPE_THIS);
addModelField("org.test.OtherType", "field", String.class.getName(), DataType.TYPE_STRING);
addModelField("org.test.MyType", "this", "org.test.MyType", DataType.TYPE_THIS);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(3, m.lhs.length);
final IPattern p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
final FactPattern fp0 = (FactPattern) p0;
assertEquals("NotImported", fp0.getFactType());
assertEquals("var", fp0.getBoundName());
assertEquals(0, fp0.getNumberOfConstraints());
final IPattern p1 = m.lhs[1];
assertTrue(p1 instanceof FactPattern);
final FactPattern fp1 = (FactPattern) p1;
assertEquals("OtherType", fp1.getFactType());
assertEquals(1, fp1.getNumberOfConstraints());
assertTrue(fp1.getConstraint(0) instanceof SingleFieldConstraint);
final SingleFieldConstraint fp1sfc0 = (SingleFieldConstraint) fp1.getConstraint(0);
assertEquals("OtherType", fp1sfc0.getFactType());
assertEquals("field", fp1sfc0.getFieldName());
assertEquals("!=", fp1sfc0.getOperator());
assertEquals(2, fp1sfc0.getExpressionValue().getParts().size());
assertTrue(fp1sfc0.getExpressionValue().getParts().get(0) instanceof ExpressionText);
final ExpressionText fp1sfc0e0 = (ExpressionText) fp1sfc0.getExpressionValue().getParts().get(0);
assertEquals("var", fp1sfc0e0.getName());
assertTrue(fp1sfc0.getExpressionValue().getParts().get(1) instanceof ExpressionText);
final ExpressionText fp1sfc0e1 = (ExpressionText) fp1sfc0.getExpressionValue().getParts().get(1);
assertEquals("field", fp1sfc0e1.getName());
final IPattern p2 = m.lhs[2];
assertTrue(p2 instanceof FromCompositeFactPattern);
final FromCompositeFactPattern fp2 = (FromCompositeFactPattern) p2;
assertEquals("MyType", fp2.getFactType());
assertEquals(2, fp2.getExpression().getParts().size());
assertTrue(fp2.getExpression().getParts().get(0) instanceof ExpressionVariable);
final ExpressionVariable fp2e0 = (ExpressionVariable) fp2.getExpression().getParts().get(0);
assertEquals("var", fp2e0.getName());
assertTrue(fp2.getExpression().getParts().get(1) instanceof ExpressionText);
final ExpressionText fp2e1 = (ExpressionText) fp2.getExpression().getParts().get(1);
assertEquals("collectionField", fp2e1.getName());
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 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.IPattern 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.IPattern in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testRHSInsertFactWithFieldAsVariableSamePackage.
@Test
public void testRHSInsertFactWithFieldAsVariableSamePackage() {
// https://bugzilla.redhat.com/show_bug.cgi?id=1077212
String drl = "package org.test\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( $f : field1 == 44 )\n" + "then\n" + "Person fact0 = new Person();\n" + "fact0.setField1( $f );\n" + "insert( fact0 );\n" + "end";
addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
when(dmo.getPackageName()).thenReturn("org.test");
final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
IPattern p = m.lhs[0];
assertTrue(p instanceof FactPattern);
FactPattern fp = (FactPattern) p;
assertEquals("Person", fp.getFactType());
assertEquals(1, fp.getConstraintList().getConstraints().length);
assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
assertEquals("Person", sfp.getFactType());
assertEquals("field1", sfp.getFieldName());
assertEquals("==", sfp.getOperator());
assertEquals("44", sfp.getValue());
assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
assertEquals("$f", sfp.getFieldBinding());
assertEquals(1, m.rhs.length);
IAction a = m.rhs[0];
assertTrue(a instanceof ActionInsertFact);
ActionInsertFact ap = (ActionInsertFact) a;
assertEquals("Person", ap.getFactType());
assertEquals("fact0", ap.getBoundName());
assertEquals(1, ap.getFieldValues().length);
ActionFieldValue afv = ap.getFieldValues()[0];
assertEquals("field1", afv.getField());
assertEquals(FieldNatureType.TYPE_VARIABLE, afv.getNature());
assertEquals("=$f", afv.getValue());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
}
Aggregations