Search in sources :

Example 6 with DSLSentence

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

the class RuleModelTest method testIsDSLEnhanced.

@Test
public void testIsDSLEnhanced() throws Exception {
    RuleModel m = new RuleModel();
    assertFalse(m.hasDSLSentences());
    m.addLhsItem(new FactPattern());
    assertFalse(m.hasDSLSentences());
    m.addRhsItem(new ActionSetField("q"));
    assertFalse(m.hasDSLSentences());
    m.addLhsItem(new DSLSentence());
    assertTrue(m.hasDSLSentences());
    m.addRhsItem(new DSLSentence());
    assertTrue(m.hasDSLSentences());
    m = new RuleModel();
    m.addLhsItem(new DSLSentence());
    assertTrue(m.hasDSLSentences());
    m = new RuleModel();
    m.addRhsItem(new DSLSentence());
    assertTrue(m.hasDSLSentences());
}
Also used : ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) Test(org.junit.Test)

Example 7 with DSLSentence

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

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

the class RuleModelDRLPersistenceTest method testNotSoundsLikeAndNotMatchesInDsl.

@Test
public void testNotSoundsLikeAndNotMatchesInDsl() {
    final String dslDefinition = "There is Person that field not matches and not soundslike {name}";
    final String drl = "Person( field not soundslike \"{name}\" && field not matches \"{name}\" )";
    final String dslFile = "[when]" + dslDefinition + "=" + drl;
    final String drlWithDsl = "rule \"with dsl\"\n" + "\tdialect \"mvel\"\n" + " when\n" + dslDefinition.replace("{name}", "John") + "\n" + "then\n" + "end\n";
    final RuleModel model = ruleModelPersistence.unmarshalUsingDSL(drlWithDsl, null, dmo, dslFile);
    final DSLSentence dslSentence = (DSLSentence) model.lhs[0];
    assertEquals(dslDefinition, dslSentence.getDefinition());
    assertEquals(drl, dslSentence.getDrl());
    assertEquals("John", 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 9 with DSLSentence

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

the class RuleModelDRLPersistenceTest method testMoreComplexRenderingWithDsl.

@Test
public void testMoreComplexRenderingWithDsl() {
    final RuleModel m = getComplexModel(true);
    String expected = "package org.test;\n" + "rule \"Complex Rule\"\n" + "no-loop true\n" + "salience -10\n" + "agenda-group \"aGroup\"\n" + "dialect \"mvel\"\n" + "when\n" + "  >p1 : Person( f1 : age < 42 )\n" + "  >not (Cancel( )) \n" + "then\n" + "  >modify( p1 ) {\n" + "    >setStatus( \"rejected\" ),\n" + "    >setName( \"Fred\" )\n" + "  >}\n" + "  >retract( p1 );\n" + "Send an email to administrator\n" + "end\n";
    checkMarshallingUsingDsl(expected, m);
    String drl = ruleModelPersistence.marshal(m);
    assertEqualsIgnoreWhitespace(expected, drl);
    String dslFile = "[then]Send an email to {administrator}=sendMailTo({administrator});";
    RuleModel unmarshalledModel = ruleModelPersistence.unmarshalUsingDSL(drl, null, dmo, dslFile);
    IAction[] actions = unmarshalledModel.rhs;
    DSLSentence dslSentence = (DSLSentence) actions[actions.length - 1];
    assertEquals("Send an email to {administrator}", dslSentence.getDefinition());
    checkMarshallingUsingDsl(expected, unmarshalledModel);
}
Also used : IAction(org.drools.workbench.models.datamodel.rule.IAction) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) Test(org.junit.Test)

Example 10 with DSLSentence

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

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