use of org.drools.workbench.models.datamodel.rule.DSLVariableValue 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);
}
use of org.drools.workbench.models.datamodel.rule.DSLVariableValue 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());
}
use of org.drools.workbench.models.datamodel.rule.DSLVariableValue 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));
}
use of org.drools.workbench.models.datamodel.rule.DSLVariableValue 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));
}
use of org.drools.workbench.models.datamodel.rule.DSLVariableValue in project drools by kiegroup.
the class RuleTemplateModelXMLLegacyPersistenceTest method testUnmarshalDSLVariableValues.
@Test
public void testUnmarshalDSLVariableValues() {
// See https://issues.jboss.org/browse/GUVNOR-1872
final String xml = "<rule>" + "<name>BugReportRule</name>" + "<modelVersion>1.0</modelVersion>" + "<attributes/>" + "<metadataList/>" + "<lhs>" + "<dslSentence>" + "<definition>If processInstance</definition>" + "<values/>" + "</dslSentence>" + "</lhs>" + "<rhs>" + "<dslSentence>" + "<definition>MyLog {myout}</definition>" + "<values>" + "<org.drools.workbench.models.datamodel.rule.DSLVariableValue>" + "<value>5-4 sample out</value>" + "</org.drools.workbench.models.datamodel.rule.DSLVariableValue>" + "<org.drools.workbench.models.datamodel.rule.DSLVariableValue>" + "<value>myout</value>" + "</org.drools.workbench.models.datamodel.rule.DSLVariableValue>" + "</values>" + "</dslSentence>" + "</rhs>" + "<isNegated>false</isNegated>" + "</rule>";
RuleModel rm = RuleTemplateModelXMLPersistenceImpl.getInstance().unmarshal(xml);
assertNotNull(rm);
assertEquals(1, rm.lhs.length);
assertTrue(rm.lhs[0] instanceof DSLSentence);
DSLSentence dslPattern = (DSLSentence) rm.lhs[0];
assertEquals("If processInstance", dslPattern.getDefinition());
assertEquals(0, dslPattern.getValues().size());
assertEquals(1, rm.rhs.length);
assertTrue(rm.rhs[0] instanceof DSLSentence);
DSLSentence dslAction = (DSLSentence) rm.rhs[0];
assertEquals("MyLog {myout}", dslAction.getDefinition());
assertEquals(2, dslAction.getValues().size());
assertTrue(dslAction.getValues().get(0) instanceof DSLVariableValue);
assertTrue(dslAction.getValues().get(1) instanceof DSLVariableValue);
assertEquals("5-4 sample out", dslAction.getValues().get(0).getValue());
assertEquals("myout", dslAction.getValues().get(1).getValue());
}
Aggregations