Search in sources :

Example 41 with ActionInsertFact

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

the class RuleTemplateModelDRLPersistenceTest method testRHSInsert.

@Test
public void testRHSInsert() {
    TemplateModel m = new TemplateModel();
    m.name = "t1";
    FactPattern p = new FactPattern("Person");
    SingleFieldConstraint con = new SingleFieldConstraint();
    con.setFieldType(DataType.TYPE_STRING);
    con.setFieldName("field1");
    con.setOperator("==");
    con.setValue("$f1");
    con.setConstraintValueType(SingleFieldConstraint.TYPE_TEMPLATE);
    p.addConstraint(con);
    m.addLhsItem(p);
    ActionInsertFact actionInsertFact = new ActionInsertFact();
    actionInsertFact.setFactType("Applicant");
    ActionFieldValue actionFieldValue = new ActionFieldValue("age", "123", "Integer");
    actionFieldValue.setNature(SingleFieldConstraint.TYPE_LITERAL);
    actionInsertFact.addFieldValue(actionFieldValue);
    m.addRhsItem(actionInsertFact);
    m.addRow(new String[] { "foo" });
    String expected = "rule \"t1_0\"" + "dialect \"mvel\"\n" + "when \n" + "Person( field1 == \"foo\" )\n" + "then \n" + "  Applicant fact0 = new Applicant(); \n" + "  fact0.setAge(123); \n" + "  insert(fact0); \n" + "end";
    checkMarshall(expected, m);
}
Also used : BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) 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 42 with ActionInsertFact

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

the class RuleTemplateModelXMLPersistenceTest method testCompositeConstraintsRoundTrip.

@Test
public void testCompositeConstraintsRoundTrip() throws Exception {
    TemplateModel m = new TemplateModel();
    m.name = "with composite";
    FactPattern p1 = new FactPattern("Person");
    p1.setBoundName("p1");
    m.addLhsItem(p1);
    FactPattern p = new FactPattern("Goober");
    m.addLhsItem(p);
    CompositeFieldConstraint comp = new CompositeFieldConstraint();
    comp.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_OR);
    p.addConstraint(comp);
    final SingleFieldConstraint X = new SingleFieldConstraint();
    X.setFieldName("goo");
    X.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
    X.setValue("foo");
    X.setOperator("==");
    X.setConnectives(new ConnectiveConstraint[1]);
    X.getConnectives()[0] = new ConnectiveConstraint();
    X.getConnectives()[0].setConstraintValueType(ConnectiveConstraint.TYPE_LITERAL);
    X.getConnectives()[0].setOperator("|| ==");
    X.getConnectives()[0].setValue("bar");
    comp.addConstraint(X);
    final SingleFieldConstraint Y = new SingleFieldConstraint();
    Y.setFieldName("goo2");
    Y.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
    Y.setValue("foo");
    Y.setOperator("==");
    comp.addConstraint(Y);
    CompositeFieldConstraint comp2 = new CompositeFieldConstraint();
    comp2.setCompositeJunctionType(CompositeFieldConstraint.COMPOSITE_TYPE_AND);
    final SingleFieldConstraint Q1 = new SingleFieldConstraint();
    Q1.setFieldName("goo");
    Q1.setOperator("==");
    Q1.setValue("whee");
    Q1.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    comp2.addConstraint(Q1);
    final SingleFieldConstraint Q2 = new SingleFieldConstraint();
    Q2.setFieldName("gabba");
    Q2.setOperator("==");
    Q2.setValue("whee");
    Q2.setConstraintValueType(BaseSingleFieldConstraint.TYPE_LITERAL);
    comp2.addConstraint(Q2);
    // now nest it
    comp.addConstraint(comp2);
    final SingleFieldConstraint Z = new SingleFieldConstraint();
    Z.setFieldName("goo3");
    Z.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
    Z.setValue("foo");
    Z.setOperator("==");
    p.addConstraint(Z);
    ActionInsertFact ass = new ActionInsertFact("Whee");
    m.addRhsItem(ass);
    String xml = RuleTemplateModelXMLPersistenceImpl.getInstance().marshal(m);
    // System.err.println(xml);
    RuleModel m2 = RuleTemplateModelXMLPersistenceImpl.getInstance().unmarshal(xml);
    assertNotNull(m2);
    assertEquals("with composite", m2.name);
    assertEquals(m2.lhs.length, m.lhs.length);
    assertEquals(m2.rhs.length, m.rhs.length);
}
Also used : BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) ConnectiveConstraint(org.drools.workbench.models.datamodel.rule.ConnectiveConstraint) TemplateModel(org.drools.workbench.models.guided.template.shared.TemplateModel) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) Test(org.junit.Test)

Example 43 with ActionInsertFact

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

the class RuleTemplateModelXMLPersistenceTest method testBasics.

@Test
public void testBasics() {
    final RuleTemplateModelPersistence p = RuleTemplateModelXMLPersistenceImpl.getInstance();
    final TemplateModel m = new TemplateModel();
    m.addLhsItem(new FactPattern("Person"));
    m.addLhsItem(new FactPattern("Accident"));
    m.addAttribute(new RuleAttribute("no-loop", "true"));
    m.addRhsItem(new ActionInsertFact("Report"));
    ActionGlobalCollectionAdd ag = new ActionGlobalCollectionAdd();
    ag.setFactName("x");
    ag.setGlobalName("g");
    m.addRhsItem(ag);
    m.name = "my rule";
    final String xml = p.marshal(m);
    System.out.println(xml);
    assertTrue(xml.indexOf("Person") > -1);
    assertTrue(xml.indexOf("Accident") > -1);
    assertTrue(xml.indexOf("no-loop") > -1);
    assertTrue(xml.indexOf("org.kie") == -1);
    assertTrue(xml.indexOf("addToGlobal") > -1);
    RuleModel rm_ = RuleTemplateModelXMLPersistenceImpl.getInstance().unmarshal(xml);
    assertEquals(2, rm_.rhs.length);
}
Also used : ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) RuleAttribute(org.drools.workbench.models.datamodel.rule.RuleAttribute) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) TemplateModel(org.drools.workbench.models.guided.template.shared.TemplateModel) ActionGlobalCollectionAdd(org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 44 with ActionInsertFact

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

the class RuleModelDRLPersistenceTest method testEnumTypeString.

@Test
public void testEnumTypeString() {
    // A legacy "Guvnor" enums (i.e pick-list of underlying field data-type)
    String expected = "rule \"my rule\"\n\tdialect \"mvel\"\n\twhen\n\t\tCheese( type == \"CHEDDAR\" )\n" + "\tthen\n\t\tinsert( new Report() );\nend\n";
    final RuleModel m = new RuleModel();
    final FactPattern pat = new FactPattern("Cheese");
    m.addLhsItem(pat);
    final SingleFieldConstraint con = new SingleFieldConstraint();
    con.setFieldName("type");
    con.setOperator("==");
    con.setValue("CHEDDAR");
    con.setFieldType(DataType.TYPE_STRING);
    con.setConstraintValueType(BaseSingleFieldConstraint.TYPE_ENUM);
    pat.addConstraint(con);
    m.addRhsItem(new ActionInsertFact("Report"));
    m.name = "my rule";
    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) Test(org.junit.Test)

Example 45 with ActionInsertFact

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

Aggregations

ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)78 Test (org.junit.Test)64 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)48 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)47 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)43 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)42 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)39 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)34 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)34 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)34 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)32 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)24 IAction (org.drools.workbench.models.datamodel.rule.IAction)17 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)13 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)12 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)12 ArrayList (java.util.ArrayList)11 CompositeFieldConstraint (org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint)10 RuleAttribute (org.drools.workbench.models.datamodel.rule.RuleAttribute)10 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)10