Search in sources :

Example 21 with SingleFieldConstraintEBLeftSide

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

the class RuleModelDRLPersistenceTest method testLHSReturnType.

@Test
public void testLHSReturnType() {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1087690
    RuleModel m = new RuleModel();
    m.name = "test";
    FactPattern p = new FactPattern("Number");
    m.addLhsItem(p);
    SingleFieldConstraint con1 = new SingleFieldConstraint();
    con1.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
    con1.setFieldName("this");
    con1.setOperator("!= null");
    con1.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
    p.addConstraint(con1);
    SingleFieldConstraintEBLeftSide con2 = new SingleFieldConstraintEBLeftSide();
    con2.getExpressionLeftSide().appendPart(new ExpressionUnboundFact(p.getFactType()));
    con2.getExpressionLeftSide().appendPart(new ExpressionMethod("intValue", "int", DataType.TYPE_NUMERIC_INTEGER));
    con2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    con2.setOperator("==");
    con2.setValue("0");
    p.addConstraint(con2);
    String expected = "rule \"test\"\n" + "dialect \"mvel\"\n" + "when\n" + "Number( this != null, intValue() == 0 )\n" + "then\n" + "end";
    checkMarshalling(expected, m);
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) 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) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ExpressionUnboundFact(org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact) Test(org.junit.Test)

Example 22 with SingleFieldConstraintEBLeftSide

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

the class RuleModelDRLPersistenceTest method testLHSExpressionNestedBoolean.

@Test
public void testLHSExpressionNestedBoolean() {
    RuleModel m = new RuleModel();
    m.name = "test expressionsNestedBoolean";
    FactPattern p = new FactPattern("Person");
    SingleFieldConstraintEBLeftSide con = new SingleFieldConstraintEBLeftSide();
    con.getExpressionLeftSide().appendPart(new ExpressionUnboundFact(p.getFactType()));
    con.getExpressionLeftSide().appendPart(new ExpressionField("favouriteCheese", "Cheese", DataType.TYPE_OBJECT));
    con.getExpressionLeftSide().appendPart(new ExpressionField("smelly", "java.lang.Boolean", DataType.TYPE_BOOLEAN));
    con.setOperator("==");
    con.setValue("true");
    con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
    p.addConstraint(con);
    m.addLhsItem(p);
    String expected = "rule \"test expressionsNestedBoolean\"" + "\tdialect \"mvel\"\n when " + "     Person( favouriteCheese.smelly == true )" + " then " + "end";
    checkMarshalling(expected, m);
}
Also used : SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) 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 SingleFieldConstraintEBLeftSide

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

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSNumberExpressionWithThisPrefix.

@Test
public void testLHSNumberExpressionWithThisPrefix() throws Exception {
    String drl = "package org.mortgages;\n" + "import java.lang.Number\n" + "rule \"test\"\n" + " dialect \"mvel\"\n" + " when\n" + "  Number( this.intValue() > 5 )\n" + " then\n" + "end";
    final HashMap<String, List<MethodInfo>> map = new HashMap<>();
    final ArrayList<MethodInfo> methodInfos = new ArrayList<>();
    methodInfos.add(new MethodInfo("intValue", Collections.emptyList(), "int", null, DataType.TYPE_NUMERIC_INTEGER));
    map.put("java.lang.Number", methodInfos);
    when(dmo.getModuleMethodInformation()).thenReturn(map);
    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("Number", 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(3, exp.getExpressionLeftSide().getParts().size());
    assertTrue(exp.getExpressionLeftSide().getParts().get(0) instanceof ExpressionUnboundFact);
    final ExpressionUnboundFact expPart0 = (ExpressionUnboundFact) exp.getExpressionLeftSide().getParts().get(0);
    assertEquals("Number", expPart0.getFactType());
    assertTrue(exp.getExpressionLeftSide().getParts().get(1) instanceof ExpressionField);
    final ExpressionField expPart1 = (ExpressionField) exp.getExpressionLeftSide().getParts().get(1);
    assertEquals("this", expPart1.getName());
    assertTrue(exp.getExpressionLeftSide().getParts().get(2) instanceof ExpressionMethod);
    final ExpressionMethod expPart2 = (ExpressionMethod) exp.getExpressionLeftSide().getParts().get(2);
    assertEquals("intValue", expPart2.getName());
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) 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) SingleFieldConstraintEBLeftSide(org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide) ExpressionMethod(org.drools.workbench.models.datamodel.rule.ExpressionMethod) List(java.util.List) ArrayList(java.util.ArrayList) MethodInfo(org.kie.soup.project.datamodel.oracle.MethodInfo) Test(org.junit.Test)

Example 24 with SingleFieldConstraintEBLeftSide

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

the class RuleModelDRLPersistenceUnmarshallingTest method testFullyQualifiedClassNameEnumerationNestedClasses.

@Test
public void testFullyQualifiedClassNameEnumerationNestedClasses() throws Exception {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1047879
    String drl = "import org.drools.workbench.models.commons.backend.rule.TestEnum;\n" + "import org.drools.workbench.models.commons.backend.rule.OuterClassWithEnums;\n" + "import org.drools.workbench.models.commons.backend.rule.OuterClassWithEnums.InnerClassWithEnums;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "OuterClassWithEnums( innerClass.innerField == TestEnum.VALUE1 )\n" + "then\n" + "end";
    addModelField("org.drools.workbench.models.commons.backend.rule.OuterClassWithEnums", "innerClass", "org.drools.workbench.models.commons.backend.rule.OuterClassWithEnums$InnerClassWithEnums", "OuterClassWithEnums$InnerClassWithEnums");
    addModelField("org.drools.workbench.models.commons.backend.rule.OuterClassWithEnums$InnerClassWithEnums", "innerField", TestEnum.class.getSimpleName(), DataType.TYPE_COMPARABLE);
    addJavaEnumDefinition("org.drools.workbench.models.commons.backend.rule.OuterClassWithEnums$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("org.drools.workbench.models.commons.backend.rule.OuterClassWithEnums$InnerClassWithEnums", ef1.getClassType());
    assertEquals("OuterClassWithEnums$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 25 with SingleFieldConstraintEBLeftSide

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

the class RuleModelDRLPersistenceUnmarshallingTest method testNestedFieldConstraints.

@Test
public void testNestedFieldConstraints() {
    String drl = "rule \"rule1\"\n" + "when\n" + "ParentType( this != null, this.parentChildField != null, this.parentChildField.childField == \"hello\" )\n" + "then\n" + "end";
    addModelField("org.test.ParentType", "parentChildField", "org.test.ChildType", "ChildType");
    addModelField("org.test.ChildType", "childField", "java.lang.String", "String");
    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("ParentType", fp.getFactType());
    assertEquals(3, fp.getConstraintList().getConstraints().length);
    assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint sfp0 = (SingleFieldConstraint) fp.getConstraint(0);
    assertEquals("ParentType", sfp0.getFactType());
    assertEquals("this", sfp0.getFieldName());
    assertEquals(DataType.TYPE_THIS, sfp0.getFieldType());
    assertEquals("!= null", sfp0.getOperator());
    assertNull(sfp0.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_UNDEFINED, sfp0.getConstraintValueType());
    assertNull(sfp0.getParent());
    assertTrue(fp.getConstraint(1) instanceof SingleFieldConstraintEBLeftSide);
    SingleFieldConstraintEBLeftSide sfp1 = (SingleFieldConstraintEBLeftSide) fp.getConstraint(1);
    assertEquals("ParentType", sfp1.getFactType());
    assertEquals("parentChildField", sfp1.getFieldName());
    assertEquals("org.test.ChildType", sfp1.getFieldType());
    assertEquals("!= null", sfp1.getOperator());
    assertNull(sfp1.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_UNDEFINED, sfp1.getConstraintValueType());
    assertNull(sfp1.getParent());
    assertTrue(fp.getConstraint(2) instanceof SingleFieldConstraintEBLeftSide);
    SingleFieldConstraintEBLeftSide sfp2 = (SingleFieldConstraintEBLeftSide) fp.getConstraint(2);
    assertEquals("childField", sfp2.getFieldName());
    assertEquals("java.lang.String", sfp2.getFieldType());
    assertEquals("==", sfp2.getOperator());
    assertEquals("hello", sfp2.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp2.getConstraintValueType());
    assertNull(sfp2.getParent());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) 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) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Aggregations

SingleFieldConstraintEBLeftSide (org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide)37 Test (org.junit.Test)31 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)30 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)29 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)29 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)29 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)28 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)28 ExpressionUnboundFact (org.drools.workbench.models.datamodel.rule.ExpressionUnboundFact)24 ExpressionField (org.drools.workbench.models.datamodel.rule.ExpressionField)21 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)13 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)10 ExpressionMethod (org.drools.workbench.models.datamodel.rule.ExpressionMethod)7 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)6 HashMap (java.util.HashMap)4 ArrayList (java.util.ArrayList)3 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)3 ExpressionFormLine (org.drools.workbench.models.datamodel.rule.ExpressionFormLine)3 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)3 List (java.util.List)2