use of org.drools.workbench.models.datamodel.rule.DSLSentence in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method testCompositeFactPatternWithFromWithDSL.
@Test
public void testCompositeFactPatternWithFromWithDSL() {
final RuleModel m = new RuleModel();
m.name = "model";
final DSLSentence sen = new DSLSentence();
sen.setDefinition("A DSL phrase");
m.addLhsItem(sen);
final FactPattern fp1 = new FactPattern("Data");
fp1.setBoundName("$d");
m.addLhsItem(fp1);
final CompositeFactPattern cp = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_NOT);
final FactPattern fp2 = new FactPattern("Person");
final FromCompositeFactPattern ffp1 = new FromCompositeFactPattern();
ffp1.setExpression(new ExpressionFormLine(new ExpressionVariable(fp1.getBoundName(), fp1.getFactType())));
ffp1.setFactPattern(fp2);
cp.addFactPattern(ffp1);
m.addLhsItem(cp);
final String actual = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
final String expected = "rule \"model\"\n" + "dialect \"mvel\"\n" + "when\n" + "A DSL phrase\n" + ">$d : Data( )\n" + ">not ( Person( ) from $d\n" + ")\n" + "then\n" + "end\n";
assertEqualsIgnoreWhitespace(expected, actual);
}
use of org.drools.workbench.models.datamodel.rule.DSLSentence 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.DSLSentence in project drools by kiegroup.
the class RuleModelDRLPersistenceTest method getComplexModel.
private RuleModel getComplexModel(boolean useDsl) {
final RuleModel m = new RuleModel();
m.name = "Complex Rule";
m.setPackageName("org.test");
m.addAttribute(new RuleAttribute("no-loop", "true"));
m.addAttribute(new RuleAttribute("salience", "-10"));
m.addAttribute(new RuleAttribute("agenda-group", "aGroup"));
final FactPattern pat = new FactPattern("Person");
pat.setBoundName("p1");
final SingleFieldConstraint con = new SingleFieldConstraint();
con.setFieldBinding("f1");
con.setFieldName("age");
con.setOperator("<");
con.setValue("42");
pat.addConstraint(con);
m.addLhsItem(pat);
final CompositeFactPattern comp = new CompositeFactPattern("not");
comp.addFactPattern(new FactPattern("Cancel"));
m.addLhsItem(comp);
final ActionUpdateField upd1 = new ActionUpdateField();
upd1.setVariable("p1");
upd1.addFieldValue(new ActionFieldValue("status", "rejected", DataType.TYPE_STRING));
upd1.addFieldValue(new ActionFieldValue("name", "Fred", DataType.TYPE_STRING));
m.addRhsItem(upd1);
final ActionRetractFact ret = new ActionRetractFact("p1");
m.addRhsItem(ret);
if (useDsl) {
final DSLSentence sen = new DSLSentence();
sen.setDefinition("Send an email to {administrator}");
m.addRhsItem(sen);
}
addModelField("org.test.Person", "this", "org.test.Person", DataType.TYPE_THIS);
addModelField("org.test.Person", "age", Integer.class.getName(), DataType.TYPE_NUMERIC_INTEGER);
addModelField("org.test.Person", "status", String.class.getName(), DataType.TYPE_STRING);
return m;
}
use of org.drools.workbench.models.datamodel.rule.DSLSentence 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());
}
use of org.drools.workbench.models.datamodel.rule.DSLSentence in project drools by kiegroup.
the class RuleTemplateModelXMLPersistenceTest method getComplexModel.
private TemplateModel getComplexModel() {
final TemplateModel m = new TemplateModel();
m.addAttribute(new RuleAttribute("no-loop", "true"));
final FactPattern pat = new FactPattern("Person");
pat.setBoundName("p1");
final SingleFieldConstraint con = new SingleFieldConstraint();
con.setFactType("Person");
con.setFieldBinding("f1");
con.setFieldName("age");
con.setOperator("<");
con.setValue("42");
pat.addConstraint(con);
m.addLhsItem(pat);
final CompositeFactPattern comp = new CompositeFactPattern("not");
comp.addFactPattern(new FactPattern("Cancel"));
m.addLhsItem(comp);
final ActionUpdateField set = new ActionUpdateField();
set.setVariable("p1");
set.addFieldValue(new ActionFieldValue("status", "rejected", DataType.TYPE_STRING));
m.addRhsItem(set);
final ActionRetractFact ret = new ActionRetractFact("p1");
m.addRhsItem(ret);
final DSLSentence sen = new DSLSentence();
sen.setDefinition("Send an email to {administrator}");
m.addRhsItem(sen);
return m;
}
Aggregations