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