Search in sources :

Example 1 with DSLSentence

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

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

the class RuleModelDRLPersistenceUnmarshallingTest method testDSLExpansionLHS_WithKeyword_then.

@Test
public // https://bugzilla.redhat.com/show_bug.cgi?id=1173842
void testDSLExpansionLHS_WithKeyword_then() {
    String expected_dslr = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "There is an Applicant\n" + "- age more then 55\n" + "then\n" + "end\n";
    String expected_drl = "rule \"rule1\"\n" + "dialect \"mvel\"\n" + "when\n" + "Applicant( age > 55 )\n" + "then\n" + "end\n";
    final String dslDefinition1 = "There is an Applicant";
    final String dslFile1 = "[when]" + dslDefinition1 + "=Applicant( )";
    final String dslDefinition2 = "- age more then {age}";
    final String dslFile2 = "[when]" + dslDefinition2 + "=age > {age}";
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(expected_dslr, 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());
    DSLVariableValue dslVariableValue = dslSentence2.getValues().get(0);
    assertEquals("55", dslVariableValue.getValue());
    // Check round-trip
    assertEqualsIgnoreWhitespace(expected_dslr, RuleModelDRLPersistenceImpl.getInstance().marshal(m));
    // Check DSL expansion (as BZ stated runtime was flawed as well)
    final Expander expander = new DefaultExpander();
    final List<DSLMappingFile> dsls = new ArrayList<>();
    try {
        final DSLTokenizedMappingFile dslTokenizer1 = new DSLTokenizedMappingFile();
        if (dslTokenizer1.parseAndLoad(new StringReader(dslFile1))) {
            dsls.add(dslTokenizer1);
        } else {
            fail();
        }
        final DSLTokenizedMappingFile dslTokenizer2 = new DSLTokenizedMappingFile();
        if (dslTokenizer2.parseAndLoad(new StringReader(dslFile2))) {
            dsls.add(dslTokenizer2);
        } else {
            fail();
        }
    } catch (IOException e) {
        fail();
    }
    for (DSLMappingFile dsl : dsls) {
        expander.addDSLMapping(dsl.getMapping());
    }
    final String actual_drl = expander.expand(expected_dslr);
    assertEqualsIgnoreWhitespace(expected_drl, actual_drl);
}
Also used : Expander(org.drools.compiler.lang.Expander) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) DSLVariableValue(org.drools.workbench.models.datamodel.rule.DSLVariableValue) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) IOException(java.io.IOException) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) DefaultExpander(org.drools.compiler.lang.dsl.DefaultExpander) DSLMappingFile(org.drools.compiler.lang.dsl.DSLMappingFile) DSLTokenizedMappingFile(org.drools.compiler.lang.dsl.DSLTokenizedMappingFile) Test(org.junit.Test)

Example 3 with DSLSentence

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

the class RuleModelDRLPersistenceUnmarshallingTest method testDSLDollar.

@Test
public void testDSLDollar() {
    String drl = "package org.mortgages;\n" + "rule \"testdsl\"\n" + "  dialect \"mvel\"\n" + "  when\n" + "    Price is $111\n" + "  then\n" + "end";
    String dslDefinition = "Price is ${p}";
    String dslFile = "[when]" + dslDefinition + "= Item( price == \"{p}\" )";
    when(dmo.getPackageName()).thenReturn("org.mortgages");
    final RuleModel model = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(drl, Collections.emptyList(), dmo, dslFile);
    assertEquals(1, model.lhs.length);
    DSLSentence dslSentence = (DSLSentence) model.lhs[0];
    assertEquals("Price is ${p}", dslSentence.getDefinition());
    assertEquals("111", dslSentence.getValues().get(0).getValue());
}
Also used : RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) Test(org.junit.Test)

Example 4 with DSLSentence

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

the class RuleModelDRLPersistenceUnmarshallingTest method testSimpleDSLExpansionLHS.

@Test
public void testSimpleDSLExpansionLHS() {
    String drl = "rule \"rule1\"\n" + "when\n" + "Es gibt einen Vertrag\n" + "- Rabatt nicht mehr als 123\n" + "then\n" + "end\n";
    final String dslDefinition = "Es gibt einen Vertrag";
    final String dslFile = "[condition][vertrag]" + dslDefinition + "=vertrag : Vertrag()";
    final String dslDefinition2 = "- Rabatt nicht mehr als {rabatt}";
    final String dslFile2 = "[condition][vertrag]" + dslDefinition2 + "=rabatt < {rabatt}";
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(drl, Collections.emptyList(), dmo, dslFile, dslFile2);
    assertNotNull(m);
    assertTrue(m.lhs[0] instanceof DSLSentence);
    DSLSentence dslSentence = (DSLSentence) m.lhs[0];
    assertEquals("vertrag : Vertrag()", dslSentence.getDrl());
    assertEquals(dslDefinition, dslSentence.getDefinition());
    assertEquals(0, dslSentence.getValues().size());
    DSLSentence dslSentence2 = (DSLSentence) m.lhs[1];
    assertEquals("rabatt < {rabatt}", dslSentence2.getDrl());
    assertEquals(dslDefinition2, dslSentence2.getDefinition());
    assertEquals(1, dslSentence2.getValues().size());
    assertNotNull(dslSentence2.getValues().get(0));
    DSLVariableValue dslComplexVariableValue = dslSentence2.getValues().get(0);
    assertEquals("123", dslComplexVariableValue.getValue());
}
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) Test(org.junit.Test)

Example 5 with DSLSentence

use of org.drools.workbench.models.datamodel.rule.DSLSentence 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)

Aggregations

DSLSentence (org.drools.workbench.models.datamodel.rule.DSLSentence)29 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)19 Test (org.junit.Test)19 DSLVariableValue (org.drools.workbench.models.datamodel.rule.DSLVariableValue)7 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)6 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)6 DSLComplexVariableValue (org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue)5 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)5 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)4 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)4 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)4 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)3 ActionRetractFact (org.drools.workbench.models.datamodel.rule.ActionRetractFact)3 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)3 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)3 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)3 Command (com.google.gwt.user.client.Command)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2