use of org.drools.workbench.models.datamodel.rule.DSLSentence in project drools by kiegroup.
the class RuleModelDRLPersistenceImpl method toDSLSentence.
private DSLSentence toDSLSentence(final List<SimpleDSLSentence> simpleDslSentences, final String dslLine) {
DSLSentence dslSentence = new DSLSentence();
for (SimpleDSLSentence simpleDslSentence : simpleDslSentences) {
String dslPattern = simpleDslSentence.getDsl();
// Dollar breaks the matcher, need to escape them.
dslPattern = dslPattern.replace("$", "\\$");
// A DSL Pattern can contain Regex itself, for example "When the ages is less than {num:1?[0-9]?[0-9]}"
String regex = dslPattern.replaceAll("\\{.*?\\}", "(.*)");
Matcher matcher = Pattern.compile(regex).matcher(dslLine);
if (matcher.matches()) {
dslPattern = dslPattern.replace("\\$", "$");
dslSentence.setDrl(simpleDslSentence.getDrl());
dslSentence.setDefinition(dslPattern);
for (int i = 0; i < matcher.groupCount(); i++) {
dslSentence.getValues().get(i).setValue(matcher.group(i + 1));
}
return dslSentence;
}
}
dslSentence.setDefinition(dslLine);
return dslSentence;
}
use of org.drools.workbench.models.datamodel.rule.DSLSentence in project drools by kiegroup.
the class RuleTemplateModelXMLLegacyPersistenceTest method testUnmarshalDSLVariableValuesLegacy.
@Test
public void testUnmarshalDSLVariableValuesLegacy() {
// 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>" + "<string>sample out rule 1</string>" + "<string>myout</string>" + "</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("sample out rule 1", dslAction.getValues().get(0).getValue());
assertEquals("myout", dslAction.getValues().get(1).getValue());
}
use of org.drools.workbench.models.datamodel.rule.DSLSentence in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testActionSetFieldValueWithDSL.
@Test
public void testActionSetFieldValueWithDSL() {
String oldValue = System.getProperty("drools.dateformat");
try {
System.setProperty("drools.dateformat", "dd-MMM-yyyy");
final String dsl = "There is an Applicant";
final String dslDefinition = "[when]" + dsl + "=$a : Applicant();";
final RuleModel m = new RuleModel();
m.name = "r0";
final DSLSentence sen = new DSLSentence();
sen.setDefinition(dsl);
m.addLhsItem(sen);
final ActionInsertFact aif = new ActionInsertFact("Applicant");
aif.addFieldValue(new ActionFieldValue("applicationDate", "13-Mar-2017", DataType.TYPE_DATE));
m.addRhsItem(aif);
final String expected = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "There is an Applicant\n" + "then\n" + ">java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");\n" + ">Applicant fact0 = new Applicant();\n" + ">fact0.setApplicationDate( sdf.parse(\"13-Mar-2017\") );\n" + ">insert( fact0 );\n" + "end";
final String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertEqualsIgnoreWhitespace(expected, actual);
PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
final RuleModel m2 = RuleModelDRLPersistenceImpl.getInstance().unmarshalUsingDSL(actual, Collections.emptyList(), dmo, dslDefinition);
assertNotNull(m2);
// LHS
assertEquals(1, m.lhs.length);
DSLSentence dslSentence = (DSLSentence) m.lhs[0];
assertEquals(dsl, dslSentence.getDefinition());
assertEquals(0, dslSentence.getValues().size());
// RHS
assertEquals(1, m2.rhs.length);
assertTrue(m2.rhs[0] instanceof ActionInsertFact);
final ActionInsertFact a = (ActionInsertFact) m2.rhs[0];
assertEquals("Applicant", a.getFactType());
assertEquals(1, a.getFieldValues().length);
final ActionFieldValue afv = a.getFieldValues()[0];
assertEquals("applicationDate", afv.getField());
assertEquals("13-Mar-2017", afv.getValue());
} finally {
if (oldValue == null) {
System.clearProperty("drools.dateformat");
} else {
System.setProperty("drools.dateformat", oldValue);
}
}
}
use of org.drools.workbench.models.datamodel.rule.DSLSentence in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testAssertWithDSL.
@Test
public void testAssertWithDSL() throws Exception {
RuleModel m = new RuleModel();
DSLSentence sen = new DSLSentence();
sen.setDefinition("I CAN HAS DSL");
m.addRhsItem(sen);
ActionInsertFact ins = new ActionInsertFact("Shizzle");
ActionFieldValue val = new ActionFieldValue("goo", "42", "Numeric");
ins.setFieldValues(new ActionFieldValue[1]);
ins.getFieldValues()[0] = val;
m.addRhsItem(ins);
ActionInsertLogicalFact insL = new ActionInsertLogicalFact("Shizzle");
ActionFieldValue valL = new ActionFieldValue("goo", "42", "Numeric");
insL.setFieldValues(new ActionFieldValue[1]);
insL.getFieldValues()[0] = valL;
m.addRhsItem(insL);
String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
assertTrue(result.indexOf(">insert") > -1);
assertTrue(result.indexOf(">insertLogical") > -1);
}
use of org.drools.workbench.models.datamodel.rule.DSLSentence in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testDSLExpansionLHS.
@Test
public void testDSLExpansionLHS() {
final String dslDefinition = "The credit rating is {rating:ENUM:Applicant.creditRating}";
final String drlExpected = "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "The credit rating is AA\n" + "then\n" + "end";
final DSLSentence dsl = new DSLSentence();
dsl.setDefinition(dslDefinition);
// The following line is normally performed by the UI when the user sets values
dsl.getValues().get(0).setValue("AA");
// Append DSL to RuleModel to check marshalling
final RuleModel m = new RuleModel();
m.name = "r1";
m.addLhsItem(dsl);
final String drlActual = ruleModelPersistence.marshal(m);
assertEqualsIgnoreWhitespace(drlExpected, drlActual);
}
Aggregations