Search in sources :

Example 21 with DSLSentence

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;
}
Also used : Matcher(java.util.regex.Matcher) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) FieldConstraint(org.drools.workbench.models.datamodel.rule.FieldConstraint) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)

Example 22 with 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());
}
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 23 with DSLSentence

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);
        }
    }
}
Also used : ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) DSLSentence(org.drools.workbench.models.datamodel.rule.DSLSentence) Test(org.junit.Test)

Example 24 with DSLSentence

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

Example 25 with DSLSentence

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

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