Search in sources :

Example 31 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testDSLExpansionLHS2.

@Test
public void testDSLExpansionLHS2() {
    String drl = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "There is an Applicant\n" + "- credit rating is AA\n" + "then\n" + "end\n";
    final String dslDefinition1 = "There is an Applicant";
    final String dslFile1 = "[when]" + dslDefinition1 + "=Applicant( )";
    final String dslDefinition2 = "- credit rating is {rating:ENUM:Applicant.creditRating}";
    final String dslFile2 = "[when]" + dslDefinition2 + "=creditRating == {rating}";
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(drl, Collections.emptyList(), dmo, dslFile1, dslFile2);
    assertNotNull(m);
    assertEquals(2, m.lhs.length);
    assertTrue(m.lhs[0] instanceof DSLSentence);
    assertTrue(m.lhs[1] instanceof DSLSentence);
    DSLSentence dslSentence1 = (DSLSentence) m.lhs[0];
    assertEquals(dslDefinition1, dslSentence1.getDefinition());
    assertEquals(0, dslSentence1.getValues().size());
    DSLSentence dslSentence2 = (DSLSentence) m.lhs[1];
    assertEquals(dslDefinition2, dslSentence2.getDefinition());
    assertEquals(1, dslSentence2.getValues().size());
    assertTrue(dslSentence2.getValues().get(0) instanceof DSLComplexVariableValue);
    DSLComplexVariableValue dslComplexVariableValue = (DSLComplexVariableValue) dslSentence2.getValues().get(0);
    assertEquals("AA", dslComplexVariableValue.getValue());
    assertEquals("ENUM:Applicant.creditRating", dslComplexVariableValue.getId());
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Also used : RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) DSLComplexVariableValue(org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue) Test(org.junit.Test)

Example 32 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method compositeFieldConstraintWithTwoPredicates.

@Test
public void compositeFieldConstraintWithTwoPredicates() {
    String drl = "rule \"rule1\"\n" + "when\n" + "Person( eval( age > 18 ) && eval(age < 45) )\n" + "then\n" + "end";
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals("rule1", m.name);
    // LHS Pattern
    assertEquals(1, m.lhs.length);
    IPattern p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    FactPattern fp = (FactPattern) p;
    // LHS sub-patterns
    assertEquals(1, fp.getNumberOfConstraints());
    assertTrue(fp.getConstraint(0) instanceof CompositeFieldConstraint);
    CompositeFieldConstraint fp_cfp = (CompositeFieldConstraint) fp.getConstraint(0);
    assertEquals("&&", fp_cfp.getCompositeJunctionType());
    assertEquals(2, fp_cfp.getNumberOfConstraints());
    assertTrue(fp_cfp.getConstraint(0) instanceof SingleFieldConstraint);
    assertTrue(fp_cfp.getConstraint(1) instanceof SingleFieldConstraint);
    SingleFieldConstraint fp_cfp_sfp1 = (SingleFieldConstraint) fp_cfp.getConstraint(0);
    assertEquals("age > 18", fp_cfp_sfp1.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, fp_cfp_sfp1.getConstraintValueType());
    SingleFieldConstraint fp_cfp_sfp2 = (SingleFieldConstraint) fp_cfp.getConstraint(1);
    assertEquals("age < 45", fp_cfp_sfp2.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_PREDICATE, fp_cfp_sfp2.getConstraintValueType());
}
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) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) Test(org.junit.Test)

Example 33 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testBigDecimal1.

@Test
public // Formatted as generated from a Guided Rule in the Workbench
void testBigDecimal1() throws Exception {
    String drl = "package org.test;\n" + "import java.lang.Number;\n" + "import java.math.BigDecimal;\n" + "import java.util.Calendar;\n" + "rule \"BigDecimalRule\"\n" + "  dialect \"java\"\n" + "when\n" + "  $bd : BigDecimal( )\n" + "then\n" + "  LastRunInformation lastRun = new LastRunInformation();\n" + "  lastRun.setLastNumber( $bd );\n" + "  insert( lastRun );\n" + "end";
    addModelField("org.test.LastRunInformation", "this", "org.test.LastRunInformation", DataType.TYPE_THIS);
    addModelField("org.test.LastRunInformation", "lastNumber", BigDecimal.class.getName(), DataType.TYPE_NUMERIC_BIGDECIMAL);
    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 p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    final FactPattern fp = (FactPattern) p;
    assertEquals("$bd", fp.getBoundName());
    assertEquals("BigDecimal", fp.getFactType());
    assertEquals(1, m.rhs.length);
    final IAction a = m.rhs[0];
    assertTrue(a instanceof ActionInsertFact);
    final ActionInsertFact aif = (ActionInsertFact) a;
    assertEquals("lastRun", aif.getBoundName());
    assertEquals("LastRunInformation", aif.getFactType());
    assertEquals(1, aif.getFieldValues().length);
    final ActionFieldValue afv0 = aif.getFieldValues()[0];
    assertEquals("lastNumber", afv0.getField());
    assertEquals(FieldNatureType.TYPE_VARIABLE, afv0.getNature());
    assertEquals("=$bd", afv0.getValue());
    // Check round-trip
    assertEqualsIgnoreWhitespace(drl, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) IAction(org.drools.workbench.models.datamodel.rule.IAction) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) 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) BigDecimal(java.math.BigDecimal) Test(org.junit.Test)

Example 34 with RuleModel

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHS_DataTypeSuffixes.

@Test
public // See https://issues.jboss.org/browse/GUVNOR-2455
void testRHS_DataTypeSuffixes() throws Exception {
    String drl = "package org.test;\n" + "rule \"MyRule\"\n" + "dialect \"mvel\"\n" + "when\n" + "  $p : Person( )\n" + "then\n" + "  modify( $p ) { setDouble( 25.0d ), setFloat( 25.0f ), setLong( 25L ) }\n" + "end";
    addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
    addModelField("org.test.Person", "double", Double.class.getName(), DataType.TYPE_NUMERIC_DOUBLE);
    addModelField("org.test.Person", "float", Float.class.getName(), DataType.TYPE_NUMERIC_FLOAT);
    addModelField("org.test.Person", "long", Long.class.getName(), DataType.TYPE_NUMERIC_LONG);
    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("Person", fp0.getFactType());
    assertEquals(0, fp0.getNumberOfConstraints());
    assertEquals(1, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionUpdateField);
    ActionUpdateField auf = (ActionUpdateField) m.rhs[0];
    assertEquals("$p", auf.getVariable());
    assertEquals(3, auf.getFieldValues().length);
    ActionFieldValue afv0 = auf.getFieldValues()[0];
    assertEquals("double", afv0.getField());
    assertEquals("25.0", afv0.getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, afv0.getNature());
    ActionFieldValue afv1 = auf.getFieldValues()[1];
    assertEquals("float", afv1.getField());
    assertEquals("25.0", afv1.getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, afv1.getNature());
    ActionFieldValue afv2 = auf.getFieldValues()[2];
    assertEquals("long", afv2.getField());
    assertEquals("25", afv2.getValue());
    assertEquals(FieldNatureType.TYPE_LITERAL, afv2.getNature());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) 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)

Example 35 with RuleModel

use of org.drools.workbench.models.datamodel.rule.RuleModel 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));
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) 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) 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