Search in sources :

Example 11 with DSLSentence

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);
}
Also used : ExpressionFormLine(org.drools.workbench.models.datamodel.rule.ExpressionFormLine) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) ExpressionVariable(org.drools.workbench.models.datamodel.rule.ExpressionVariable) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) Test(org.junit.Test)

Example 12 with DSLSentence

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));
}
Also used : DSLVariableValue(org.drools.workbench.models.datamodel.rule.DSLVariableValue) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) DSLComplexVariableValue(org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue) Test(org.junit.Test)

Example 13 with DSLSentence

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;
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) RuleAttribute(org.drools.workbench.models.datamodel.rule.RuleAttribute) FromEntryPointFactPattern(org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence)

Example 14 with DSLSentence

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());
}
Also used : DSLVariableValue(org.drools.workbench.models.datamodel.rule.DSLVariableValue) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) Test(org.junit.Test)

Example 15 with DSLSentence

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;
}
Also used : BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) RuleAttribute(org.drools.workbench.models.datamodel.rule.RuleAttribute) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) TemplateModel(org.drools.workbench.models.guided.template.shared.TemplateModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence)

Aggregations

DSLSentence (org.drools.workbench.models.datamodel.rule.DSLSentence)29 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)19 Test (org.junit.Test)19 DSLVariableValue (org.drools.workbench.models.datamodel.rule.DSLVariableValue)7 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)6 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)6 DSLComplexVariableValue (org.drools.workbench.models.datamodel.rule.DSLComplexVariableValue)5 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)5 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)4 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)4 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)4 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)3 ActionRetractFact (org.drools.workbench.models.datamodel.rule.ActionRetractFact)3 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)3 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)3 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)3 Command (com.google.gwt.user.client.Command)2 IOException (java.io.IOException)2 StringReader (java.io.StringReader)2 ArrayList (java.util.ArrayList)2