Search in sources :

Example 21 with ExpressionUnboundFact

use of org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact 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));
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod) ExpressionFormLine(org.drools.workbench.models.datamodel.rule.ExpressionFormLine) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) ExpressionMethodParameterDefinition(org.drools.workbench.models.datamodel.rule.ExpressionMethodParameterDefinition) Test(org.junit.Test)

Example 22 with ExpressionUnboundFact

use of org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testEnumerationNestedClasses.

@Test
public void testEnumerationNestedClasses() throws Exception {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1047879
    String drl = "import org.drools.workbench.models.commons.backend.rule.TestEnum;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "OuterClassWithEnums( innerClass.innerField == TestEnum.VALUE1 )\n" + "then\n" + "end";
    addModelField("OuterClassWithEnums", "innerClass", "InnerClassWithEnums", "InnerClassWithEnums");
    addModelField("InnerClassWithEnums", "innerField", TestEnum.class.getSimpleName(), DataType.TYPE_COMPARABLE);
    addJavaEnumDefinition("InnerClassWithEnums", "innerField", new String[] { "TestEnum.VALUE1=TestEnum.VALUE1", "TestEnum.VALUE2=TestEnum.VALUE2" });
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    FactPattern pattern = (FactPattern) m.lhs[0];
    assertEquals(1, pattern.getNumberOfConstraints());
    assertTrue(pattern.getConstraint(0) instanceof SingleFieldConstraintEBLeftSide);
    final SingleFieldConstraintEBLeftSide constraint = (SingleFieldConstraintEBLeftSide) pattern.getConstraint(0);
    assertEquals(3, constraint.getExpressionLeftSide().getParts().size());
    assertTrue(constraint.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
    final ExpressionUnboundFact eubf = ((ExpressionUnboundFact) constraint.getExpressionLeftSide().getParts().get(0));
    assertEquals("OuterClassWithEnums", eubf.getName());
    assertEquals("OuterClassWithEnums", eubf.getClassType());
    assertEquals("OuterClassWithEnums", eubf.getGenericType());
    assertTrue(constraint.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
    final ExpressionField ef1 = ((ExpressionField) constraint.getExpressionLeftSide().getParts().get(1));
    assertEquals("innerClass", ef1.getName());
    assertEquals("InnerClassWithEnums", ef1.getClassType());
    assertEquals("InnerClassWithEnums", ef1.getGenericType());
    assertTrue(constraint.getExpressionLeftSide().getParts().get(2) instanceof ExpressionField);
    final ExpressionField ef2 = ((ExpressionField) constraint.getExpressionLeftSide().getParts().get(2));
    assertEquals("innerField", ef2.getName());
    assertEquals("TestEnum", ef2.getClassType());
    assertEquals(DataType.TYPE_COMPARABLE, ef2.getGenericType());
    assertEquals("OuterClassWithEnums", constraint.getFactType());
    assertEquals("innerField", constraint.getFieldName());
    assertEquals("TestEnum", constraint.getFieldType());
    assertEquals("==", constraint.getOperator());
    assertEquals("TestEnum.VALUE1", constraint.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_ENUM, constraint.getConstraintValueType());
    final String drl2 = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
    assertEqualsIgnoreWhitespace(drl, drl2);
}
Also used : SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) Test(org.junit.Test)

Example 23 with ExpressionUnboundFact

use of org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact in project drools by kiegroup.

the class CopyExpressionVisitor method visit.

public void visit(ExpressionUnboundFact part) {
    add(new ExpressionUnboundFact(part.getFactType()));
    moveNext(part);
}
Also used : ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact)

Example 24 with ExpressionUnboundFact

use of org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact in project drools by kiegroup.

the class CopyExpressionVisitorTest method testExpressionFormLineCopy.

@Test
public void testExpressionFormLineCopy() {
    ExpressionFormLine efl = new ExpressionFormLine();
    efl.appendPart(new ExpressionCollection("collection", "CT", "GT", "PT"));
    efl.appendPart(new ExpressionCollectionIndex("collectionIndex", "CT", "GT"));
    efl.appendPart(new ExpressionField("field", "CT", "FT", "PT"));
    efl.appendPart(new ExpressionFieldVariable("fieldVariable", "Type"));
    efl.appendPart(new ExpressionGlobalVariable("globalVariable", "CT", "GT", "PT"));
    efl.appendPart(new ExpressionMethod("method", "CT", "GT"));
    efl.appendPart(new ExpressionMethodParameter("methodParam", "CT", "GT"));
    efl.appendPart(new ExpressionText("text"));
    efl.appendPart(new ExpressionUnboundFact("FactType"));
    efl.appendPart(new ExpressionVariable("binding", "FactType"));
    // verify that the new instance created with copy constructor is equal to original
    assertEquals(efl, new ExpressionFormLine(efl));
}
Also used : ExpressionFormLine(org.drools.workbench.models.datamodel.rule.ExpressionFormLine) ExpressionFieldVariable(org.drools.workbench.models.datamodel.rule.ExpressionFieldVariable) ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod) ExpressionCollectionIndex(org.drools.workbench.models.datamodel.rule.ExpressionCollectionIndex) ExpressionMethodParameter(org.drools.workbench.models.datamodel.rule.ExpressionMethodParameter) ExpressionCollection(org.drools.workbench.models.datamodel.rule.ExpressionCollection) ExpressionText(org.drools.workbench.models.datamodel.rule.ExpressionText) ExpressionVariable(org.drools.workbench.models.datamodel.rule.ExpressionVariable) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) ExpressionGlobalVariable(org.drools.workbench.models.datamodel.rule.ExpressionGlobalVariable) Test(org.junit.Test)

Example 25 with ExpressionUnboundFact

use of org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact in project drools by kiegroup.

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSNestedMethodCalls.

@Test
public void testLHSNestedMethodCalls() throws Exception {
    String drl = "package org.mortgages;\n" + "rule \"test\"\n" + " dialect \"mvel\"\n" + " when\n" + "  Parent( methodToGetChild1().methodToGetChild2().field1 > 5 )\n" + " then\n" + "end";
    addMethodInformation("Parent", "methodToGetChild1", Collections.emptyList(), "Child1", null, "Child1");
    addMethodInformation("Child1", "methodToGetChild2", Collections.emptyList(), "Child2", null, "Child2");
    addModelField("Child2", "field1", "int", DataType.TYPE_NUMERIC_INTEGER);
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    assertTrue(m.lhs[0] instanceof FactPattern);
    final FactPattern fp = (FactPattern) m.lhs[0];
    assertEquals("Parent", fp.getFactType());
    assertEquals(1, fp.getNumberOfConstraints());
    assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraintEBLeftSide);
    final SingleFieldConstraintEBLeftSide exp = (SingleFieldConstraintEBLeftSide) fp.getConstraint(0);
    assertEquals("int", exp.getFieldType());
    assertEquals(">", exp.getOperator());
    assertEquals("5", exp.getValue());
    assertEquals(4, exp.getExpressionLeftSide().getParts().size());
    assertTrue(exp.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
    final ExpressionUnboundFact expPart0 = (ExpressionUnboundFact) exp.getExpressionLeftSide().getParts().get(0);
    assertEquals("Parent", expPart0.getFactType());
    assertTrue(exp.getExpressionLeftSide().getParts().get(1) instanceof ExpressionMethod);
    final ExpressionMethod expPart1 = (ExpressionMethod) exp.getExpressionLeftSide().getParts().get(1);
    assertEquals("methodToGetChild1", expPart1.getName());
    assertTrue(exp.getExpressionLeftSide().getParts().get(2) instanceof ExpressionMethod);
    final ExpressionMethod expPart2 = (ExpressionMethod) exp.getExpressionLeftSide().getParts().get(2);
    assertEquals("methodToGetChild2", expPart2.getName());
    assertTrue(exp.getExpressionLeftSide().getParts().get(3) instanceof ExpressionField);
    final ExpressionField expPart3 = (ExpressionField) exp.getExpressionLeftSide().getParts().get(3);
    assertEquals("field1", expPart3.getName());
}
Also used : SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ExpressionField(org.drools.workbench.models.datamodel.rule.ExpressionField) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) Test(org.junit.Test)

Aggregations

ExpressionUnboundFact (org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact)27 Test (org.junit.Test)25 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)24 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)24 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)24 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)24 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)24 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)24 SingleFieldConstraintEBLeftSide (org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide)24 ExpressionField (org.drools.workbench.models.datamodel.rule.ExpressionField)21 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)12 ExpressionMethod (org.drools.workbench.models.datamodel.rule.ExpressionMethod)9 HashMap (java.util.HashMap)3 ExpressionFormLine (org.drools.workbench.models.datamodel.rule.ExpressionFormLine)3 ArrayList (java.util.ArrayList)2 List (java.util.List)2 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)2 ExpressionCollection (org.drools.workbench.models.datamodel.rule.ExpressionCollection)2 ExpressionMethodParameter (org.drools.workbench.models.datamodel.rule.ExpressionMethodParameter)2 ExpressionText (org.drools.workbench.models.datamodel.rule.ExpressionText)2