Search in sources :

Example 46 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testLHSFreeFormLineWithDsl.

@Test
public void testLHSFreeFormLineWithDsl() {
    String drl = "rule rule1\n" + "when\n" + ">//A comment\n" + "then\n" + "end";
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    assertTrue(m.lhs[0] instanceof FreeFormLine);
    assertEquals("//A comment", ((FreeFormLine) m.lhs[0]).getText());
}
Also used : FreeFormLine(org.drools.workbench.models.datamodel.rule.FreeFormLine) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 47 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testSingleFieldConstraintConnectives4.

@Test
public // https://issues.jboss.org/browse/RHBRMS-2854
void testSingleFieldConstraintConnectives4() {
    String drl = "package org.test;\n" + "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "    Applicant(age != null && != 1 && < 10 && > 5)\n" + "then\n" + "end";
    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);
    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.getConstraintList().getConstraints().length);
    assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
    assertEquals("Applicant", sfp.getFactType());
    assertEquals("age", sfp.getFieldName());
    assertEquals("!= null", sfp.getOperator());
    assertNull(sfp.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_UNDEFINED, sfp.getConstraintValueType());
    assertEquals(3, sfp.getConnectives().length);
    ConnectiveConstraint cc1 = sfp.getConnectives()[0];
    assertEquals("Applicant", cc1.getFactType());
    assertEquals("age", cc1.getFieldName());
    assertEquals("&& !=", cc1.getOperator());
    assertEquals("1", cc1.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cc1.getConstraintValueType());
    ConnectiveConstraint cc2 = sfp.getConnectives()[1];
    assertEquals("Applicant", cc2.getFactType());
    assertEquals("age", cc2.getFieldName());
    assertEquals("&& <", cc2.getOperator());
    assertEquals("10", cc2.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cc2.getConstraintValueType());
    ConnectiveConstraint cc3 = sfp.getConnectives()[2];
    assertEquals("Applicant", cc3.getFactType());
    assertEquals("age", cc3.getFieldName());
    assertEquals("&& >", cc3.getOperator());
    assertEquals("5", cc3.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cc3.getConstraintValueType());
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
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) 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) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 48 with RuleModel

use of org.drools.workbench.models.datamodel.rule.RuleModel 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);
}
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) ExpressionFormLine(org.drools.workbench.models.datamodel.rule.ExpressionFormLine) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) 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) ExpressionVariable(org.drools.workbench.models.datamodel.rule.ExpressionVariable) ExpressionText(org.drools.workbench.models.datamodel.rule.ExpressionText) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 49 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRuleNameWithoutTheQuotes.

@Test
public void testRuleNameWithoutTheQuotes() {
    String drl = "rule rule1\n" + "when\n" + "then\n" + "end";
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals("rule1", m.name);
}
Also used : RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 50 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testNestedEvalWithDifferentCompositeOperatorsAndTrailingConstraint.

@Test
public // https://issues.jboss.org/browse/RHBPMS-4695
void testNestedEvalWithDifferentCompositeOperatorsAndTrailingConstraint() {
    String drl = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "MyTestDateObject(firstName != null || eval($td.getFirstName().toUpperCase().equals(\"NONAME\") || $td.getFirstName().toUpperCase().equals(\"NAMENOTPROVIDED\")) && firstName != \"\" )\n" + "then\n" + "end";
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    // Check round-trip
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Also used : RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Aggregations

RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)384 Test (org.junit.Test)352 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)223 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)216 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)215 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)201 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)201 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)145 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)134 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)91 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)85 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)63 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)47 ArrayList (java.util.ArrayList)44 IAction (org.drools.workbench.models.datamodel.rule.IAction)31 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)29 SingleFieldConstraintEBLeftSide (org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide)29 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)27 RuleAttribute (org.drools.workbench.models.datamodel.rule.RuleAttribute)25 List (java.util.List)24