Search in sources :

Example 91 with ActionFieldValue

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

the class RuleModelDRLPersistenceTest method testLiteralBigIntegerMvel.

@Test
public void testLiteralBigIntegerMvel() {
    RuleModel m = new RuleModel();
    m.name = "test literal biginteger";
    m.addAttribute(new RuleAttribute("dialect", "mvel"));
    FactPattern p = new FactPattern("Person");
    SingleFieldConstraint con = new SingleFieldConstraint();
    con.setFieldType(DataType.TYPE_NUMERIC_BIGINTEGER);
    con.setFieldName("field1");
    con.setOperator("==");
    con.setValue("44");
    con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
    p.addConstraint(con);
    m.addLhsItem(p);
    ActionInsertFact ai = new ActionInsertFact("Person");
    ai.addFieldValue(new ActionFieldValue("field1", "55", DataType.TYPE_NUMERIC_BIGINTEGER));
    m.addRhsItem(ai);
    String expected = "rule \"test literal biginteger\" \n" + "\tdialect \"mvel\"\n when \n" + "     Person(field1 == 44I ) \n" + " then \n" + "Person fact0 = new Person(); \n" + "fact0.setField1( 55I ); \n" + "insert( fact0 ); \n" + "end";
    checkMarshalling(expected, m);
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) 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) Test(org.junit.Test)

Example 92 with ActionFieldValue

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

the class RuleModelDRLPersistenceTest method testRHSDateUpdateAction.

@Test
public void testRHSDateUpdateAction() {
    String oldValue = System.getProperty("drools.dateformat");
    try {
        System.setProperty("drools.dateformat", "dd-MMM-yyyy");
        RuleModel m = new RuleModel();
        m.name = "RHS Date";
        FactPattern p = new FactPattern("Person");
        p.setBoundName("$p");
        SingleFieldConstraint con = new SingleFieldConstraint();
        con.setFieldType(DataType.TYPE_DATE);
        con.setFieldName("dateOfBirth");
        con.setOperator("==");
        con.setValue("31-Jan-2000");
        con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
        p.addConstraint(con);
        m.addLhsItem(p);
        ActionSetField au = new ActionSetField("$p");
        au.addFieldValue(new ActionFieldValue("dob", "31-Jan-2000", DataType.TYPE_DATE));
        m.addRhsItem(au);
        String result = RuleModelDRLPersistenceImpl.getInstance().marshal(m);
        assertTrue(result.indexOf("java.text.SimpleDateFormat sdf = new java.text.SimpleDateFormat(\"dd-MMM-yyyy\");") != -1);
        assertTrue(result.indexOf("$p.setDob( sdf.parse(\"31-Jan-2000\"") != -1);
        assertTrue(result.indexOf("update( $p );") == -1);
        checkMarshalling(null, m);
    } finally {
        if (oldValue == null) {
            System.clearProperty("drools.dateformat");
        } else {
            System.setProperty("drools.dateformat", oldValue);
        }
    }
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) 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 93 with ActionFieldValue

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

the class RuleModelDRLPersistenceTest method testRHSSetFieldWithVariable.

@Test
public void testRHSSetFieldWithVariable() {
    // https://bugzilla.redhat.com/show_bug.cgi?id=1077212
    RuleModel m = new RuleModel();
    m.name = "variable";
    FactPattern p = new FactPattern("Person");
    SingleFieldConstraint con = new SingleFieldConstraint();
    con.setFieldType(DataType.TYPE_NUMERIC_INTEGER);
    con.setFieldName("field1");
    con.setOperator("==");
    con.setValue("44");
    con.setConstraintValueType(SingleFieldConstraint.TYPE_LITERAL);
    con.setFieldBinding("$f");
    p.addConstraint(con);
    m.addLhsItem(p);
    ActionInsertFact ai = new ActionInsertFact("Person");
    ActionFieldValue acv = new ActionFieldValue("field1", "=$f", DataType.TYPE_OBJECT);
    acv.setNature(FieldNatureType.TYPE_VARIABLE);
    ai.addFieldValue(acv);
    m.addRhsItem(ai);
    String expected = "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( $f : field1 == 44 )\n" + "then\n" + "Person fact0 = new Person();\n" + "fact0.setField1( $f );\n" + "insert( fact0 );\n" + "end";
    checkMarshalling(expected, m);
}
Also used : SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) 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 94 with ActionFieldValue

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

the class RuleModelDRLPersistenceUnmarshallingTest method testRHSInsertFactWithFieldAsLiteralSamePackage.

@Test
public void testRHSInsertFactWithFieldAsLiteralSamePackage() {
    String drl = "package org.test\n" + "rule \"variable\"\n" + "dialect \"mvel\"\n" + "when\n" + "Person( field1 == 44 )\n" + "then\n" + "Person fact0 = new Person();\n" + "fact0.setField1( 55 );\n" + "insert( fact0 );\n" + "end";
    addModelField("org.test.Person", "field1", "java.lang.Integer", DataType.TYPE_NUMERIC_INTEGER);
    when(dmo.getPackageName()).thenReturn("org.test");
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals(1, m.lhs.length);
    IPattern p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    FactPattern fp = (FactPattern) p;
    assertEquals("Person", fp.getFactType());
    assertEquals(1, fp.getConstraintList().getConstraints().length);
    assertTrue(fp.getConstraint(0) instanceof SingleFieldConstraint);
    SingleFieldConstraint sfp = (SingleFieldConstraint) fp.getConstraint(0);
    assertEquals("Person", sfp.getFactType());
    assertEquals("field1", sfp.getFieldName());
    assertEquals("==", sfp.getOperator());
    assertEquals("44", sfp.getValue());
    assertEquals(BaseSingleFieldConstraint.TYPE_LITERAL, sfp.getConstraintValueType());
    assertEquals(1, m.rhs.length);
    IAction a = m.rhs[0];
    assertTrue(a instanceof ActionInsertFact);
    ActionInsertFact ap = (ActionInsertFact) a;
    assertEquals("Person", ap.getFactType());
    assertEquals("fact0", ap.getBoundName());
    assertEquals(1, ap.getFieldValues().length);
    ActionFieldValue afv = ap.getFieldValues()[0];
    assertEquals("field1", afv.getField());
    assertEquals(FieldNatureType.TYPE_LITERAL, afv.getNature());
    assertEquals("55", afv.getValue());
    assertEquals(DataType.TYPE_NUMERIC_INTEGER, afv.getType());
}
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) IAction(org.drools.workbench.models.datamodel.rule.IAction) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) 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 95 with ActionFieldValue

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

the class RuleModelDRLPersistenceUnmarshallingTest method newRHSFactsCanBeUsedInRHSBinding.

@Test
public void newRHSFactsCanBeUsedInRHSBinding() throws Exception {
    String drl = "package org.mortgages;\n" + "rule \"r1\"\n" + "dialect \"mvel\"\n" + "when\n" + "$l : LoanApplication()\n" + "then\n" + "Applicant $a = new Applicant();\n" + "insert( $a );\n" + "modify( $l ) {\n" + "  setApplicant( $a )" + "}\n" + "end";
    addModelField("org.mortgages.Applicant", "this", "org.mortgages.Applicant", DataType.TYPE_THIS);
    addModelField("org.mortgages.LoanApplication", "this", "org.mortgages.LoanApplication", DataType.TYPE_THIS);
    addModelField("org.mortgages.LoanApplication", "applicant", "org.mortgages.Applicant", "org.mortgages.Applicant");
    when(dmo.getPackageName()).thenReturn("org.mortgages");
    RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.emptyList(), dmo);
    assertNotNull(m);
    assertEquals("r1", m.name);
    // LHS Pattern
    assertEquals(1, m.lhs.length);
    IPattern p = m.lhs[0];
    assertTrue(p instanceof FactPattern);
    FactPattern fp = (FactPattern) p;
    assertEquals("LoanApplication", fp.getFactType());
    assertEquals(0, fp.getNumberOfConstraints());
    assertEquals(2, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionInsertFact);
    ActionInsertFact aif = (ActionInsertFact) m.rhs[0];
    assertEquals("$a", aif.getBoundName());
    assertEquals("Applicant", aif.getFactType());
    assertEquals(0, aif.getFieldValues().length);
    assertTrue(m.rhs[1] instanceof ActionUpdateField);
    ActionUpdateField auf = (ActionUpdateField) m.rhs[1];
    assertEquals("$l", auf.getVariable());
    assertEquals(1, auf.getFieldValues().length);
    ActionFieldValue afv0 = auf.getFieldValues()[0];
    assertEquals("applicant", afv0.getField());
    assertEquals("=$a", afv0.getValue());
    assertEquals(FieldNatureType.TYPE_VARIABLE, afv0.getNature());
    assertEquals("Applicant", afv0.getType());
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) 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

ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)136 Test (org.junit.Test)113 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)71 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)71 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)65 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)63 ActionUpdateField (org.drools.workbench.models.datamodel.rule.ActionUpdateField)53 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)48 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)41 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)41 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)33 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)30 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)28 TemplateModel (org.drools.workbench.models.guided.template.shared.TemplateModel)26 IAction (org.drools.workbench.models.datamodel.rule.IAction)25 BRLActionColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn)24 ActionSetField (org.drools.workbench.models.datamodel.rule.ActionSetField)23 GuidedDecisionTable52 (org.drools.workbench.models.guided.dtable.shared.model.GuidedDecisionTable52)23 BRLActionVariableColumn (org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)21 ArrayList (java.util.ArrayList)19