Search in sources :

Example 96 with RuleModel

use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.

the class RuleModelDRLPersistenceTest method testDateInDsl.

@Test
public void testDateInDsl() throws Exception {
    addModelField("org.test.Person", "bornDate", DataType.TYPE_DATE, DataType.TYPE_DATE);
    addModelField("org.test.Person", "retireDate", DataType.TYPE_DATE, DataType.TYPE_DATE);
    final String dslConditionDefinition = "Person was born on {date}";
    final String dslConditionDrl = "$p : Person(bornDate == \"{date}\")";
    final String dslActionDefinition = "Person will retire on {date}";
    final String dslActionDrl = "java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\"); $p.setRetireDate(sdf.parse(\"{date}\"));";
    final String dslFile = "[when]" + dslConditionDefinition + "=" + dslConditionDrl + "\n" + "[then]" + dslActionDefinition + "=" + dslActionDrl;
    final String drl = "rule \"my rule\" \n" + "dialect \"mvel\"\n" + "when\n" + "Person was born on 01-01-2017\n" + "then\n" + "Person will retire on 01-01-2067\n" + "end\n";
    final RuleModel m = ruleModelPersistence.unmarshalUsingDSL(drl, Collections.emptyList(), dmo, dslFile);
    assertEquals(1, m.lhs.length);
    assertEquals(dslConditionDefinition, ((DSLSentence) m.lhs[0]).getDefinition());
    assertEquals(dslConditionDrl, ((DSLSentence) m.lhs[0]).getDrl());
    assertEquals("01-01-2017", ((DSLSentence) m.lhs[0]).getValues().get(0).getValue());
    assertEquals(1, m.rhs.length);
    assertEquals(dslActionDefinition, ((DSLSentence) m.rhs[0]).getDefinition());
    assertEquals(dslActionDrl, ((DSLSentence) m.rhs[0]).getDrl());
    assertEquals("01-01-2067", ((DSLSentence) m.rhs[0]).getValues().get(0).getValue());
}
Also used : RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 97 with RuleModel

use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.

the class RuleModelDRLPersistenceTest method testInOperatorString.

@Test
public void testInOperatorString() {
    RuleModel m = new RuleModel();
    m.name = "in";
    FactPattern p = new FactPattern("Person");
    SingleFieldConstraint con = new SingleFieldConstraint();
    con.setFieldType(DataType.TYPE_STRING);
    con.setFieldName("field1");
    con.setOperator("in");
    con.setValue("value1, value2");
    con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
    p.addConstraint(con);
    m.addLhsItem(p);
    String expected = "rule \"in\" \n" + "dialect \"mvel\" \n" + "when \n" + "     Person(field1 in ( \"value1\", \"value2\" ) ) \n" + " then \n" + "end";
    checkMarshalling(expected, m);
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) 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) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 98 with RuleModel

use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.

the class RuleModelDRLPersistenceTest method testGenerateEmptyXML.

@Test
public void testGenerateEmptyXML() {
    final RuleModelPersistence p = RuleModelDRLPersistenceImpl.getInstance();
    final String drl = p.marshal(new RuleModel());
    assertNotNull(drl);
    assertFalse(drl.equals(""));
}
Also used : RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 99 with RuleModel

use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.

the class RuleModelDRLPersistenceTest method testInsertLogical.

@Test
public void testInsertLogical() {
    String expected = "rule \"my rule\"\n\tno-loop true\n\tdialect \"mvel\"\n\twhen\n\t\tPerson( )\n" + "\t\tAccident( )\n\tthen\n\t\tinsertLogical( new Report() );\nend\n";
    final RuleModel m = new RuleModel();
    m.addLhsItem(new FactPattern("Person"));
    m.addLhsItem(new FactPattern("Accident"));
    m.addAttribute(new RuleAttribute("no-loop", "true"));
    m.addRhsItem(new ActionInsertLogicalFact("Report"));
    m.name = "my rule";
    checkMarshalling(expected, m);
}
Also used : 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) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) ActionInsertLogicalFact(org.drools.workbench.models.datamodel.rule.ActionInsertLogicalFact) Test(org.junit.Test)

Example 100 with RuleModel

use of org.drools.workbench.models.datamodel.rule.RuleModel in project drools by kiegroup.

the class RuleModelDRLPersistenceTest method testCompositeAndConstraints.

@Test
public void testCompositeAndConstraints() {
    RuleModel m = new RuleModel();
    m.name = "and composite";
    FactPattern p = new FactPattern("Goober");
    m.addLhsItem(p);
    CompositeFieldConstraint comp = new CompositeFieldConstraint();
    comp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_AND);
    p.addConstraint(comp);
    final SingleFieldConstraint sfc1 = new SingleFieldConstraint();
    sfc1.setFactType("Goober");
    sfc1.setFieldName("gooField");
    sfc1.setFieldType(DataType.TYPE_STRING);
    sfc1.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
    sfc1.setValue("gooValue");
    sfc1.setOperator("==");
    comp.addConstraint(sfc1);
    final SingleFieldConstraint sfc2 = new SingleFieldConstraint();
    sfc2.setFactType("Goober");
    sfc2.setFieldName("fooField");
    sfc2.setFieldType(DataType.TYPE_OBJECT);
    sfc2.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
    sfc2.setOperator("!= null");
    comp.addConstraint(sfc2);
    final SingleFieldConstraint sfc3 = new SingleFieldConstraint();
    sfc3.setFactType("Bar");
    sfc3.setFieldName("barField");
    sfc3.setParent(sfc2);
    sfc3.setFieldType(DataType.TYPE_STRING);
    sfc3.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
    sfc3.setValue("barValue");
    sfc3.setOperator("==");
    comp.addConstraint(sfc3);
    ActionInsertFact ass = new ActionInsertFact("Whee");
    m.addRhsItem(ass);
    String expected = "rule \"and composite\"" + "dialect \"mvel\"\n" + "when\n" + "Goober( gooField == \"gooValue\" && fooField != null && fooField.barField == \"barValue\" )\n" + "then\n" + "insert( new Whee() );\n" + "end";
    checkMarshalling(expected, m);
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) 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) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) Test(org.junit.Test)

Aggregations

RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)384 Test (org.junit.Test)352 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)223 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)216 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)215 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)201 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)201 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)145 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)134 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)91 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)85 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)63 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)47 ArrayList (java.util.ArrayList)44 IAction (org.drools.workbench.models.datamodel.rule.IAction)31 FreeFormLine (org.drools.workbench.models.datamodel.rule.FreeFormLine)29 SingleFieldConstraintEBLeftSide (org.drools.workbench.models.datamodel.rule.SingleFieldConstraintEBLeftSide)29 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)27 RuleAttribute (org.drools.workbench.models.datamodel.rule.RuleAttribute)25 List (java.util.List)24