Search in sources :

Example 1 with CompositeFactPattern

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

the class RuleTemplateModelDRLPersistenceTest method testCompositeFactPatternSecondValue.

@Test
public void testCompositeFactPatternSecondValue() {
    TemplateModel m = new TemplateModel();
    m.name = "t1";
    CompositeFactPattern cp = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_OR);
    FactPattern p1 = new FactPattern("Person");
    SingleFieldConstraint sfc1 = new SingleFieldConstraint();
    sfc1.setFieldName("field1");
    sfc1.setFieldType(DataType.TYPE_STRING);
    sfc1.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE);
    sfc1.setValue("$f1");
    sfc1.setOperator("==");
    p1.addConstraint(sfc1);
    FactPattern p2 = new FactPattern("Person");
    SingleFieldConstraint sfc2 = new SingleFieldConstraint();
    sfc2.setFieldName("field2");
    sfc2.setFieldType(DataType.TYPE_STRING);
    sfc2.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE);
    sfc2.setValue("$f2");
    sfc2.setOperator("==");
    p2.addConstraint(sfc2);
    cp.addFactPattern(p1);
    cp.addFactPattern(p2);
    m.addLhsItem(cp);
    String expected = "rule \"t1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "(\n" + "Person( )\n" + "or\n" + "Person( field2 == \"bar\" )\n" + ")\n" + "then\n" + "end\n";
    m.addRow(new String[] { null, "bar" });
    checkMarshall(expected, m);
}
Also used : BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) TemplateModel(org.drools.workbench.models.guided.template.shared.TemplateModel) Test(org.junit.Test)

Example 2 with CompositeFactPattern

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

the class RuleTemplateModelDRLPersistenceTest method testCompositeFactPatternFirstValue.

@Test
public void testCompositeFactPatternFirstValue() {
    TemplateModel m = new TemplateModel();
    m.name = "t1";
    CompositeFactPattern cp = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_OR);
    FactPattern p1 = new FactPattern("Person");
    SingleFieldConstraint sfc1 = new SingleFieldConstraint();
    sfc1.setFieldName("field1");
    sfc1.setFieldType(DataType.TYPE_STRING);
    sfc1.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE);
    sfc1.setValue("$f1");
    sfc1.setOperator("==");
    p1.addConstraint(sfc1);
    FactPattern p2 = new FactPattern("Person");
    SingleFieldConstraint sfc2 = new SingleFieldConstraint();
    sfc2.setFieldName("field2");
    sfc2.setFieldType(DataType.TYPE_STRING);
    sfc2.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE);
    sfc2.setValue("$f2");
    sfc2.setOperator("==");
    p2.addConstraint(sfc2);
    cp.addFactPattern(p1);
    cp.addFactPattern(p2);
    m.addLhsItem(cp);
    String expected = "rule \"t1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "(\n" + "Person( field1 == \"foo\" )\n" + "or\n" + "Person( )\n" + ")\n" + "then\n" + "end\n";
    m.addRow(new String[] { "foo", null });
    checkMarshall(expected, m);
}
Also used : BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) TemplateModel(org.drools.workbench.models.guided.template.shared.TemplateModel) Test(org.junit.Test)

Example 3 with CompositeFactPattern

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

the class RuleTemplateModelDRLPersistenceTest method testCompositeFactPatternBothValues.

@Test
public void testCompositeFactPatternBothValues() {
    TemplateModel m = new TemplateModel();
    m.name = "t1";
    CompositeFactPattern cp = new CompositeFactPattern(CompositeFactPattern.COMPOSITE_TYPE_OR);
    FactPattern p1 = new FactPattern("Person");
    SingleFieldConstraint sfc1 = new SingleFieldConstraint();
    sfc1.setFieldName("field1");
    sfc1.setFieldType(DataType.TYPE_STRING);
    sfc1.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE);
    sfc1.setValue("$f1");
    sfc1.setOperator("==");
    p1.addConstraint(sfc1);
    FactPattern p2 = new FactPattern("Person");
    SingleFieldConstraint sfc2 = new SingleFieldConstraint();
    sfc2.setFieldName("field2");
    sfc2.setFieldType(DataType.TYPE_STRING);
    sfc2.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE);
    sfc2.setValue("$f2");
    sfc2.setOperator("==");
    p2.addConstraint(sfc2);
    cp.addFactPattern(p1);
    cp.addFactPattern(p2);
    m.addLhsItem(cp);
    String expected = "rule \"t1_0\"\n" + "dialect \"mvel\"\n" + "when\n" + "(\n" + "Person( field1 == \"foo\" )\n" + "or\n" + "Person( field2 == \"bar\" )\n" + ")\n" + "then\n" + "end\n";
    m.addRow(new String[] { "foo", "bar" });
    checkMarshall(expected, m);
}
Also used : BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) TemplateModel(org.drools.workbench.models.guided.template.shared.TemplateModel) Test(org.junit.Test)

Example 4 with CompositeFactPattern

use of org.drools.workbench.models.datamodel.rule.CompositeFactPattern 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)

Example 5 with CompositeFactPattern

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

the class RuleModelDRLPersistenceUnmarshallingTest method testCompositeFactPatternWithOr.

@Test
public void testCompositeFactPatternWithOr() {
    String drl = "rule \"rule1\"\n" + "when\n" + "( Person( age == 42 ) or Person( age == 43 ) )\n" + "then\n" + "end";
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals("rule1", m.name);
    // LHS Pattern
    assertEquals(1, m.lhs.length);
    IPattern p = m.lhs[0];
    assertTrue(p instanceof CompositeFactPattern);
    CompositeFactPattern cfp = (CompositeFactPattern) p;
    assertEquals(CompositeFactPattern.COMPOSITE_TYPE_OR, cfp.getType());
    // LHS sub-patterns
    assertEquals(2, cfp.getPatterns().length);
    IPattern cfp_p1 = cfp.getPatterns()[0];
    assertTrue(cfp_p1 instanceof FactPattern);
    FactPattern fp1 = (FactPattern) cfp_p1;
    assertEquals("Person", fp1.getFactType());
    assertEquals(1, fp1.getConstraintList().getConstraints().length);
    assertTrue(fp1.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint cfp_sfp1 = (SingleFieldConstraint) fp1.getConstraint(0);
    assertEquals("Person", cfp_sfp1.getFactType());
    assertEquals("age", cfp_sfp1.getFieldName());
    assertEquals("==", cfp_sfp1.getOperator());
    assertEquals("42", cfp_sfp1.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cfp_sfp1.getConstraintValueType());
    IPattern cfp_p2 = cfp.getPatterns()[1];
    assertTrue(cfp_p2 instanceof FactPattern);
    FactPattern fp2 = (FactPattern) cfp_p2;
    assertEquals("Person", fp2.getFactType());
    assertTrue(fp2.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint cfp_sfp2 = (SingleFieldConstraint) fp2.getConstraint(0);
    assertEquals("Person", cfp_sfp2.getFactType());
    assertEquals("age", cfp_sfp2.getFieldName());
    assertEquals("==", cfp_sfp2.getOperator());
    assertEquals("43", cfp_sfp2.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, cfp_sfp2.getConstraintValueType());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) 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) 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)

Aggregations

CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)28 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)24 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)22 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)22 Test (org.junit.Test)22 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)21 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)18 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)16 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)15 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)9 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)5 ActionRetractFact (org.drools.workbench.models.datamodel.rule.ActionRetractFact)4 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)4 IFactPattern (org.drools.workbench.models.datamodel.rule.IFactPattern)4 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)4 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)3 DSLSentence (org.drools.workbench.models.datamodel.rule.DSLSentence)3 RuleAttribute (org.drools.workbench.models.datamodel.rule.RuleAttribute)3 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)2 IAction (org.drools.workbench.models.datamodel.rule.IAction)2