use of org.drools.workbench.models.datamodel.rule.ExpressionVariable in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testInvalidFromSyntax.
@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1218308
void testInvalidFromSyntax() throws Exception {
String drl = "rule \"test\"\n" + " dialect \"mvel\"\n" + " when\n" + " (obj : MyClass( ) from my.package)\n" + " then\n" + " System.out.println(\"Test\")\n" + " end";
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 FromCompositeFactPattern);
final FromCompositeFactPattern fp0 = (FromCompositeFactPattern) p0;
assertEquals("MyClass", fp0.getFactType());
final FactPattern fp1 = fp0.getFactPattern();
assertEquals("MyClass", fp1.getFactType());
assertEquals(0, fp1.getNumberOfConstraints());
final ExpressionFormLine efl = fp0.getExpression();
assertNotNull(efl);
assertEquals(2, efl.getParts().size());
assertTrue(efl.getParts().get(0) instanceof ExpressionVariable);
final ExpressionVariable ev = (ExpressionVariable) efl.getParts().get(0);
assertEquals("my", ev.getName());
assertTrue(efl.getParts().get(1) instanceof ExpressionText);
final ExpressionText et = (ExpressionText) efl.getParts().get(1);
assertEquals("package", et.getName());
// Check round-trip
assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
use of org.drools.workbench.models.datamodel.rule.ExpressionVariable in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testCollectWithFreeFormDRL_MethodsUndefined.
@Test
public void testCollectWithFreeFormDRL_MethodsUndefined() 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("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 ExpressionText);
ExpressionText et1 = (ExpressionText) efl.getParts().get(1);
assertEquals("getRecCategorization()", et1.getName());
assertEquals("java.lang.String", et1.getClassType());
assertEquals(DataType.TYPE_STRING, et1.getGenericType());
assertTrue(efl.getParts().get(2) instanceof ExpressionText);
ExpressionText et2 = (ExpressionText) efl.getParts().get(2);
assertEquals("get(\"APES-01\")", et2.getName());
assertEquals("java.lang.String", et2.getClassType());
assertEquals(DataType.TYPE_STRING, et2.getGenericType());
assertEquals(0, m.rhs.length);
}
use of org.drools.workbench.models.datamodel.rule.ExpressionVariable in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testFromBoundVariable.
@Test
public void testFromBoundVariable() {
String drl = "import java.lang.Number;\n" + "import org.drools.workbench.models.commons.backend.rule.Counter;\n" + "rule \"rule1\"\n" + "when\n" + "cc : Counter()\n" + "Number() from cc.number\n" + "then\n" + "end";
addModelField("org.drools.workbench.models.commons.backend.rule.Counter", "number", "java.lang.Number", DataType.TYPE_NUMERIC);
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals("rule1", m.name);
assertEquals(2, m.lhs.length);
IPattern p0 = m.lhs[0];
assertTrue(p0 instanceof FactPattern);
FactPattern fp0 = (FactPattern) p0;
assertEquals("Counter", fp0.getFactType());
assertEquals("cc", fp0.getBoundName());
assertEquals(0, fp0.getNumberOfConstraints());
IPattern p1 = m.lhs[1];
assertTrue(p1 instanceof FromCompositeFactPattern);
FromCompositeFactPattern fcfp1 = (FromCompositeFactPattern) p1;
FactPattern fp1 = fcfp1.getFactPattern();
ExpressionFormLine efl1 = fcfp1.getExpression();
assertNotNull(fp1);
assertNotNull(efl1);
assertEquals("Number", fp1.getFactType());
assertEquals(0, fp1.getNumberOfConstraints());
assertEquals(2, efl1.getParts().size());
assertTrue(efl1.getParts().get(0) instanceof ExpressionVariable);
assertTrue(efl1.getParts().get(1) instanceof ExpressionField);
ExpressionVariable eflv1 = (ExpressionVariable) efl1.getParts().get(0);
assertEquals("cc", eflv1.getName());
assertEquals("Counter", eflv1.getClassType());
assertEquals(DataType.TYPE_NUMERIC, eflv1.getGenericType());
ExpressionField eflf1 = (ExpressionField) efl1.getParts().get(1);
assertEquals("number", eflf1.getName());
assertEquals("java.lang.Number", eflf1.getClassType());
assertEquals(DataType.TYPE_NUMERIC, eflf1.getGenericType());
}
use of org.drools.workbench.models.datamodel.rule.ExpressionVariable in project drools by kiegroup.
the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldExpressionsWithAnotherExpression.
@Test
public void testNestedFieldExpressionsWithAnotherExpression() {
String drl = "rule rule1\n" + "when\n" + "p : Person( address.postalCode == p.address.postalCode )\n" + "then\n" + "end";
addModelField("org.test.Person", "address", "org.test.Address", "Address");
addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
addModelField("org.test.Address", "postalCode", "java.lang.Integer", "Integer");
RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
assertNotNull(m);
assertEquals(1, m.lhs.length);
assertTrue(m.lhs[0] instanceof FactPattern);
assertTrue(((FactPattern) m.lhs[0]).getFieldConstraints()[0] instanceof SingleFieldConstraintEBLeftSide);
SingleFieldConstraintEBLeftSide ebLeftSide = (SingleFieldConstraintEBLeftSide) ((FactPattern) m.lhs[0]).getFieldConstraints()[0];
assertEquals("postalCode", ebLeftSide.getFieldName());
assertEquals("java.lang.Integer", ebLeftSide.getFieldType());
assertEquals("==", ebLeftSide.getOperator());
assertEquals("", ebLeftSide.getValue());
assertEquals(3, ebLeftSide.getExpressionValue().getParts().size());
assertTrue(ebLeftSide.getExpressionValue().getParts().get(0) instanceof ExpressionVariable);
ExpressionVariable expressionVariable = (ExpressionVariable) ebLeftSide.getExpressionValue().getParts().get(0);
assertEquals("p", expressionVariable.getName());
assertEquals("org.test.Person", expressionVariable.getClassType());
assertEquals(DataType.TYPE_THIS, expressionVariable.getGenericType());
assertTrue(ebLeftSide.getExpressionValue().getParts().get(1) instanceof ExpressionField);
ExpressionField ef1 = (ExpressionField) ebLeftSide.getExpressionValue().getParts().get(1);
assertEquals("address", ef1.getName());
assertEquals("org.test.Address", ef1.getClassType());
assertEquals("Address", ef1.getGenericType());
assertTrue(ebLeftSide.getExpressionValue().getParts().get(2) instanceof ExpressionField);
ExpressionField ef2 = (ExpressionField) ebLeftSide.getExpressionValue().getParts().get(2);
assertEquals("postalCode", ef2.getName());
assertEquals("java.lang.Integer", ef2.getClassType());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, ef2.getGenericType());
assertEquals(3, ebLeftSide.getExpressionLeftSide().getParts().size());
assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
ExpressionUnboundFact expressionUnboundFact = ((ExpressionUnboundFact) ebLeftSide.getExpressionLeftSide().getParts().get(0));
assertEquals("Person", expressionUnboundFact.getName());
assertEquals("Person", expressionUnboundFact.getClassType());
assertEquals("Person", expressionUnboundFact.getGenericType());
assertEquals(((FactPattern) m.lhs[0]).getFactType(), expressionUnboundFact.getFactType());
assertNull(expressionUnboundFact.getPrevious());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionUnboundFact.getNext());
assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
ExpressionField expressionField1 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(1);
assertEquals("address", expressionField1.getName());
assertEquals("org.test.Address", expressionField1.getClassType());
assertEquals("Address", expressionField1.getGenericType());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(0), expressionField1.getPrevious());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(2), expressionField1.getNext());
assertTrue(ebLeftSide.getExpressionLeftSide().getParts().get(2) instanceof ExpressionField);
ExpressionField expressionField2 = (ExpressionField) ebLeftSide.getExpressionLeftSide().getParts().get(2);
assertEquals("postalCode", expressionField2.getName());
assertEquals("java.lang.Integer", expressionField2.getClassType());
assertEquals(DataType.TYPE_NUMERIC_INTEGER, expressionField2.getGenericType());
assertEquals(ebLeftSide.getExpressionLeftSide().getParts().get(1), expressionField2.getPrevious());
assertNull(expressionField2.getNext());
}
use of org.drools.workbench.models.datamodel.rule.ExpressionVariable 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));
}
Aggregations