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());
}
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());
}
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());
}
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);
}
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));
}
Aggregations