Search in sources :

Example 1 with DSLComplexVariableValue

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

the class RuleModelDRLPersistenceUnmarshallingTest method testDSLExpansionLHS.

@Test
public void testDSLExpansionLHS() {
    String drl = "rule \"rule1\"\n" + "when\n" + "The credit rating is AA\n" + "then\n" + "end\n";
    final String dslDefinition = "The credit rating is {rating:ENUM:Applicant.creditRating}";
    final String dslFile = "[when]" + dslDefinition + "=Applicant( creditRating == {rating} )";
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(drl, Collections.emptyList(), dmo, dslFile);
    assertNotNull(m);
    assertTrue(m.lhs[0] instanceof DSLSentence);
    DSLSentence dslSentence = (DSLSentence) m.lhs[0];
    assertEquals(dslDefinition, dslSentence.getDefinition());
    assertEquals(1, dslSentence.getValues().size());
    assertTrue(dslSentence.getValues().get(0) instanceof DSLComplexVariableValue);
    DSLComplexVariableValue dslComplexVariableValue = (DSLComplexVariableValue) dslSentence.getValues().get(0);
    assertEquals("AA", dslComplexVariableValue.getValue());
    assertEquals("ENUM:Applicant.creditRating", dslComplexVariableValue.getId());
}
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 2 with DSLComplexVariableValue

use of org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue 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 3 with DSLComplexVariableValue

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

the class RuleModelDRLPersistenceUnmarshallingTest method testDSLExpansionRHS.

@Test
public void testDSLExpansionRHS() {
    String drl = "rule \"rule1\"\n" + "when\n" + "> $a : Applicant()\n" + "then\n" + "Set applicant name to Bob\n" + "end\n";
    final String dslDefinition = "Set applicant name to {name:\\w+ \\w+}";
    final String dslFile = "[then]" + dslDefinition + "=$a.setName( \"{name}\" )";
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(drl, Collections.emptyList(), dmo, dslFile);
    assertNotNull(m);
    assertTrue(m.lhs[0] instanceof FactPattern);
    FactPattern pattern = (FactPattern) m.lhs[0];
    assertEquals("Applicant", pattern.getFactType());
    assertEquals("$a", pattern.getBoundName());
    assertTrue(m.rhs[0] instanceof DSLSentence);
    DSLSentence dslSentence = (DSLSentence) m.rhs[0];
    assertEquals(dslDefinition, dslSentence.getDefinition());
    assertEquals(1, dslSentence.getValues().size());
    assertTrue(dslSentence.getValues().get(0) instanceof DSLComplexVariableValue);
    DSLComplexVariableValue dslComplexVariableValue = (DSLComplexVariableValue) dslSentence.getValues().get(0);
    assertEquals("Bob", dslComplexVariableValue.getValue());
    assertEquals("\\w+ \\w+", dslComplexVariableValue.getId());
}
Also used : 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) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) DSLComplexVariableValue(org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue) Test(org.junit.Test)

Example 4 with DSLComplexVariableValue

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

the class RuleModelDRLPersistenceTest method testDSLExpansionContainingRegex.

@Test
public void testDSLExpansionContainingRegex() {
    String expected = "rule \"RegexDslRule\"\n" + "dialect \"mvel\"\n" + "when\n" + "When the ages is less than  57\n" + "then\n" + "end\n";
    final String dslDefinition = "When the ages is less than {num:1?[0-9]?[0-9]}";
    final DSLSentence dsl = new DSLSentence();
    dsl.setDefinition(dslDefinition);
    // Check values are correctly parsed
    final List<DSLVariableValue> values = dsl.getValues();
    assertEquals(1, values.size());
    assertTrue(values.get(0) instanceof DSLComplexVariableValue);
    assertEquals("num", values.get(0).getValue());
    assertEquals("1?[0-9]?[0-9]", ((DSLComplexVariableValue) values.get(0)).getId());
    // The following line is normally performed by the UI when the user sets values
    dsl.getValues().get(0).setValue("57");
    // Check interpolation
    final String expansion = dsl.interpolate();
    assertEquals("When the ages is less than 57", expansion);
    assertEquals(dsl.getDefinition(), dslDefinition);
    final RuleModel m = new RuleModel();
    m.name = "RegexDslRule";
    m.addLhsItem(dsl);
    String drl = ruleModelPersistence.marshal(m);
    assertEqualsIgnoreWhitespace(expected, drl);
    String dslFile = "[when]" + dslDefinition + "=applicant:Applicant(age<{num})";
    RuleModel model = ruleModelPersistence.unmarshalUsingDSL(drl, null, null, dslFile);
    DSLSentence dslSentence = (DSLSentence) model.lhs[0];
    assertEquals(dslDefinition, dslSentence.getDefinition());
    assertEquals(1, dslSentence.getValues().size());
    assertTrue(dslSentence.getValues().get(0) instanceof DSLComplexVariableValue);
    DSLComplexVariableValue dslComplexVariableValue = (DSLComplexVariableValue) dslSentence.getValues().get(0);
    assertEquals("57", dslComplexVariableValue.getValue());
    assertEquals("1?[0-9]?[0-9]", dslComplexVariableValue.getId());
    assertEqualsIgnoreWhitespace(drl, ruleModelPersistence.marshal(model));
}
Also used : DSLVariableValue(org.drools.workbench.models.datamodel.rule.DSLVariableValue) 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 5 with DSLComplexVariableValue

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

the class RuleModelDRLPersistenceTest method testDSLExpansion.

@Test
public void testDSLExpansion() {
    String expected = "rule \"Rule With DSL\"\n" + "\tdialect \"mvel\"\n" + "\twhen\n" + "\t\tThe credit rating is AA\n" + "\tthen\n" + "end\n";
    final String dslDefinition = "The credit rating is {rating:ENUM:Applicant.creditRating}";
    final DSLSentence dsl = new DSLSentence();
    dsl.setDefinition(dslDefinition);
    // Check values are correctly parsed
    final List<DSLVariableValue> values = dsl.getValues();
    assertEquals(1, values.size());
    assertTrue(values.get(0) instanceof DSLComplexVariableValue);
    assertEquals("rating", values.get(0).getValue());
    assertEquals("ENUM:Applicant.creditRating", ((DSLComplexVariableValue) values.get(0)).getId());
    // The following line is normally performed by the UI when the user sets values
    dsl.getValues().get(0).setValue("AA");
    // Check interpolation
    final String expansion = dsl.interpolate();
    assertEquals("The credit rating is AA", expansion);
    assertEquals(dsl.getDefinition(), dslDefinition);
    final RuleModel m = new RuleModel();
    m.name = "Rule With DSL";
    m.addLhsItem(dsl);
    String drl = ruleModelPersistence.marshal(m);
    assertEqualsIgnoreWhitespace(expected, drl);
    String dslFile = "[when]" + dslDefinition + "=Credit( rating == {rating} )";
    RuleModel unmarshalledModel = ruleModelPersistence.unmarshalUsingDSL(drl, null, null, dslFile);
    DSLSentence dslSentence = (DSLSentence) unmarshalledModel.lhs[0];
    assertEquals(dslDefinition, dslSentence.getDefinition());
    assertEquals(1, dslSentence.getValues().size());
    assertTrue(dslSentence.getValues().get(0) instanceof DSLComplexVariableValue);
    DSLComplexVariableValue dslComplexVariableValue = (DSLComplexVariableValue) dslSentence.getValues().get(0);
    assertEquals("AA", dslComplexVariableValue.getValue());
    assertEquals("ENUM:Applicant.creditRating", dslComplexVariableValue.getId());
    assertEqualsIgnoreWhitespace(expected, ruleModelPersistence.marshal(unmarshalledModel));
}
Also used : DSLVariableValue(org.drools.workbench.models.datamodel.rule.DSLVariableValue) 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)

Aggregations

DSLComplexVariableValue (org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue)5 DSLSentence (org.drools.workbench.models.datamodel.rule.DSLSentence)5 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)5 Test (org.junit.Test)5 DSLVariableValue (org.drools.workbench.models.datamodel.rule.DSLVariableValue)2 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)1 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)1 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)1 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)1 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)1