Search in sources :

Example 11 with ActionCallMethod

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

the class RuleModelDRLPersistenceTest method testRHSSetMethodCallsMethodMVEL.

@Test
public void testRHSSetMethodCallsMethodMVEL() {
    String oldValue = System.getProperty("drools.dateformat");
    try {
        System.setProperty("drools.dateformat", "dd-MMM-yyyy");
        RuleModel m = new RuleModel();
        m.name = "RHS SetMethodCallsMethod";
        m.addAttribute(new RuleAttribute("dialect", "mvel"));
        FactPattern p = new FactPattern("Person");
        p.setBoundName("$p");
        m.addLhsItem(p);
        ActionCallMethod acm = new ActionCallMethod();
        acm.setMethodName("method");
        acm.setVariable("$p");
        acm.addFieldValue(new ActionFieldFunction("f1", "String", DataType.TYPE_STRING));
        acm.addFieldValue(new ActionFieldFunction("f2", "true", DataType.TYPE_BOOLEAN));
        acm.addFieldValue(new ActionFieldFunction("f3", "31-Jan-2012", DataType.TYPE_DATE));
        acm.addFieldValue(new ActionFieldFunction("f4", "100", DataType.TYPE_NUMERIC_INTEGER));
        acm.addFieldValue(new ActionFieldFunction("f5", "100", DataType.TYPE_NUMERIC_BIGDECIMAL));
        m.addRhsItem(acm);
        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.method( \"String\", true, sdf.parse(\"31-Jan-2012\"), 100, 100B );") != -1);
        checkMarshalling(null, m);
    } finally {
        if (oldValue == null) {
            System.clearProperty("drools.dateformat");
        } else {
            System.setProperty("drools.dateformat", oldValue);
        }
    }
}
Also used : RuleAttribute(org.drools.workbench.models.datamodel.rule.RuleAttribute) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) 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) ActionFieldFunction(org.drools.workbench.models.datamodel.rule.ActionFieldFunction) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) Test(org.junit.Test)

Example 12 with ActionCallMethod

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

the class RuleModelDRLPersistenceTest method testActionCallMethod.

@Test
public void testActionCallMethod() {
    final String drl = "rule \"r0\"\n" + "dialect \"mvel\"\n" + "when\n" + "$a : Applicant( )\n" + "then\n" + "$a.addName( \"Michael\" );\n" + "end\n";
    PackageDataModelOracle dmo = mock(PackageDataModelOracle.class);
    final RuleModel m = RuleModelDRLPersistenceImpl.getInstance().unmarshal(drl, Collections.EMPTY_LIST, dmo);
    assertNotNull(m);
    // LHS
    assertEquals(1, m.lhs.length);
    assertTrue(m.lhs[0] instanceof FactPattern);
    final FactPattern p = (FactPattern) m.lhs[0];
    assertEquals("$a", p.getBoundName());
    assertEquals("Applicant", p.getFactType());
    // RHS
    assertEquals(1, m.rhs.length);
    assertTrue(m.rhs[0] instanceof ActionCallMethod);
    final ActionCallMethod a = (ActionCallMethod) m.rhs[0];
    assertEquals("$a", a.getVariable());
    assertEquals("addName", a.getMethodName());
    assertEquals(1, a.getFieldValues().length);
    final ActionFieldValue fv = a.getFieldValue(0);
    assertEquals("Michael", fv.getValue());
}
Also used : ActionFieldValue(org.drools.workbench.models.datamodel.rule.ActionFieldValue) PackageDataModelOracle(org.kie.soup.project.datamodel.oracle.PackageDataModelOracle) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) 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 13 with ActionCallMethod

use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools-wb by kiegroup.

the class RuleModelCloneVisitorTest method testRuleModelCloning.

@Test
public void testRuleModelCloning() {
    RuleModel model = new RuleModel();
    // --------------------------------------------------------------------------------------------------------------
    // LHS
    // --------------------------------------------------------------------------------------------------------------
    // 
    RuleAttribute attribute = new RuleAttribute("att1_name", "att1_val");
    model.addAttribute(attribute);
    RuleMetadata metadata = new RuleMetadata("meta1_name", "meta1_val");
    model.addMetadata(metadata);
    FactPattern f1 = buildFactPattern();
    FactPattern f2 = new FactPattern();
    f2.setBoundName("$f2");
    f2.setFactType("Fact2");
    f2.addConstraint(buildCompositeFieldConstraint());
    CompositeFactPattern composite = new CompositeFactPattern();
    composite.setType(CompositeFactPattern.COMPOSITE_TYPE_EXISTS);
    composite.addFactPattern(f1);
    composite.addFactPattern(f2);
    model.addLhsItem(composite);
    model.addLhsItem(buildFromAccumulateCompositeFactPattern());
    model.addLhsItem(buildFromCollectCompositeFactPattern());
    model.addLhsItem(buildFromCompositeFactPattern());
    model.addLhsItem(buildFreeFormLine());
    model.addLhsItem(buildDslSentence());
    ActionGlobalCollectionAdd addGlobal = new ActionGlobalCollectionAdd();
    addGlobal.setFactName("MyFact");
    addGlobal.setGlobalName("glist");
    model.addRhsItem(addGlobal);
    // --------------------------------------------------------------------------------------------------------------
    // RHS
    // --------------------------------------------------------------------------------------------------------------
    // 
    ActionInsertFact aif = new ActionInsertFact();
    aif.setBoundName("$f");
    aif.setFactType("FactType");
    aif.addFieldValue(buildActionFieldValue());
    aif.addFieldValue(buildActionFieldValue());
    model.addRhsItem(aif);
    ActionUpdateField auf = new ActionUpdateField();
    auf.setVariable("$var");
    auf.addFieldValue(buildActionFieldValue());
    auf.addFieldValue(buildActionFieldValue());
    model.addRhsItem(auf);
    ActionSetField asf = new ActionSetField();
    asf.setVariable("$var");
    asf.addFieldValue(buildActionFieldValue());
    asf.addFieldValue(buildActionFieldValue());
    model.addRhsItem(asf);
    ActionRetractFact arf = new ActionRetractFact();
    arf.setVariableName("$f");
    model.addRhsItem(arf);
    ActionCallMethod callMethod1 = new ActionCallMethod();
    callMethod1.setVariable("var1");
    callMethod1.setMethodName("testMethod1 ");
    callMethod1.setState(ActionCallMethod.TYPE_DEFINED);
    callMethod1.addFieldValue(new ActionFieldFunction("field1", "value1", "type1"));
    model.addRhsItem(callMethod1);
    ActionCallMethod callMethod2 = new ActionCallMethod();
    callMethod2.setVariable("var2");
    callMethod2.setMethodName("testMethod2 ");
    callMethod2.setState(ActionCallMethod.TYPE_UNDEFINED);
    callMethod2.addFieldValue(new ActionFieldFunction("field2", "value2", "type2"));
    model.addRhsItem(callMethod2);
    model.addRhsItem(buildFreeFormLine());
    // --------------------------------------------------------------------------------------------------------------
    // Clone and verify
    // --------------------------------------------------------------------------------------------------------------
    // 
    RuleModel clone = new RuleModelCloneVisitor().visitRuleModel(model);
    assertArrayEquals(model.attributes, clone.attributes);
    int attIndex = 0;
    for (RuleAttribute att : model.attributes) {
        assertNotSame(att, clone.attributes[attIndex++]);
    }
    assertArrayEquals(model.metadataList, clone.metadataList);
    int metIndex = 0;
    for (RuleMetadata met : model.metadataList) {
        assertNotSame(met, clone.metadataList[metIndex++]);
    }
    assertArrayEquals(model.lhs, clone.lhs);
    int lhsIndex = 0;
    for (IPattern pattern : model.lhs) {
        assertNotSame(pattern, clone.lhs[lhsIndex++]);
    }
    assertArrayEquals(model.rhs, clone.rhs);
    int rhsIndex = 0;
    for (IAction action : model.rhs) {
        assertNotSame(action, clone.rhs[rhsIndex++]);
    }
}
Also used : IPattern(org.drools.workbench.models.datamodel.rule.IPattern) IAction(org.drools.workbench.models.datamodel.rule.IAction) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) IFactPattern(org.drools.workbench.models.datamodel.rule.IFactPattern) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) FromAccumulateCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern) CompositeFactPattern(org.drools.workbench.models.datamodel.rule.CompositeFactPattern) FromCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern) FromCollectCompositeFactPattern(org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern) ActionFieldFunction(org.drools.workbench.models.datamodel.rule.ActionFieldFunction) RuleModel(org.drools.workbench.models.datamodel.rule.RuleModel) CompositeFieldConstraint(org.drools.workbench.models.datamodel.rule.CompositeFieldConstraint) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) BaseSingleFieldConstraint(org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint) ActionRetractFact(org.drools.workbench.models.datamodel.rule.ActionRetractFact) ActionUpdateField(org.drools.workbench.models.datamodel.rule.ActionUpdateField) ActionSetField(org.drools.workbench.models.datamodel.rule.ActionSetField) ActionInsertFact(org.drools.workbench.models.datamodel.rule.ActionInsertFact) RuleAttribute(org.drools.workbench.models.datamodel.rule.RuleAttribute) RuleMetadata(org.drools.workbench.models.datamodel.rule.RuleMetadata) ActionGlobalCollectionAdd(org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd) Test(org.junit.Test)

Example 14 with ActionCallMethod

use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools-wb by kiegroup.

the class ConditionColumnSynchronizerTest method actionCallMethod.

private BRLActionColumn actionCallMethod(final String boundName) {
    final ActionCallMethod action = new ActionCallMethod();
    action.setVariable(boundName);
    action.setMethodName("toString()");
    final BRLActionColumn brl = new BRLActionColumn();
    brl.setHeader("brl-action");
    brl.setDefinition(Collections.singletonList(action));
    brl.getChildColumns().add(new BRLActionVariableColumn() {

        {
            setHeader("brl-action-v0");
        }
    });
    return brl;
}
Also used : BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn)

Example 15 with ActionCallMethod

use of org.drools.workbench.models.datamodel.rule.ActionCallMethod in project drools-wb by kiegroup.

the class BRLConditionColumnSynchronizerTest method checkBRLFragmentConditionCannotBeUpdatedWhenFieldBindingIsUsedInAction.

@Test
public void checkBRLFragmentConditionCannotBeUpdatedWhenFieldBindingIsUsedInAction() throws VetoException {
    final BRLConditionColumn column = new BRLConditionColumn();
    column.setDefinition(Collections.singletonList(new FactPattern("Applicant") {

        {
            setBoundName("$a");
            addConstraint(new SingleFieldConstraint("age") {

                {
                    setBoundName("$age");
                }
            });
        }
    }));
    final BRLConditionVariableColumn columnV0 = new BRLConditionVariableColumn("$age", DataType.TYPE_NUMERIC_INTEGER, "Applicant", "age");
    column.getChildColumns().add(columnV0);
    column.setHeader("col1");
    columnV0.setHeader("col1v0");
    modelSynchronizer.appendColumn(column);
    final BRLActionColumn action = new BRLActionColumn();
    action.setDefinition(Collections.singletonList(new ActionCallMethod() {

        {
            setVariable("$age");
            setMethodName("toString()");
        }
    }));
    final BRLActionVariableColumn columnV1 = new BRLActionVariableColumn("$age", DataType.TYPE_NUMERIC_INTEGER, "Applicant", "age");
    action.getChildColumns().add(columnV1);
    action.setHeader("col2");
    columnV1.setHeader("col2v0");
    modelSynchronizer.appendColumn(action);
    try {
        final BRLConditionColumn editedColumn = new BRLConditionColumn();
        editedColumn.setDefinition(Collections.singletonList(new FactPattern("Applicant") {

            {
                setBoundName("$a");
                addConstraint(new SingleFieldConstraint("age") {

                    {
                        setBoundName("$age2");
                    }
                });
            }
        }));
        final BRLConditionVariableColumn editedColumnV0 = new BRLConditionVariableColumn("$age", DataType.TYPE_NUMERIC_INTEGER, "Applicant", "age");
        editedColumn.getChildColumns().add(editedColumnV0);
        editedColumn.setHeader("col1");
        editedColumnV0.setHeader("col1v0");
        modelSynchronizer.updateColumn(column, editedColumn);
        fail("Deletion of the column should have been vetoed.");
    } catch (VetoUpdatePatternInUseException veto) {
    // This is expected
    } catch (VetoException veto) {
        fail("VetoUpdatePatternInUseException was expected.");
    }
    assertEquals(4, model.getExpandedColumns().size());
    assertTrue(model.getExpandedColumns().get(0) instanceof RowNumberCol52);
    assertTrue(model.getExpandedColumns().get(1) instanceof DescriptionCol52);
    assertEquals(columnV0, model.getExpandedColumns().get(2));
    assertEquals(action.getChildColumns().get(0), model.getExpandedColumns().get(3));
}
Also used : BRLConditionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionColumn) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint) VetoException(org.drools.workbench.screens.guided.dtable.client.widget.table.model.synchronizers.ModelSynchronizer.VetoException) BRLActionColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionColumn) DescriptionCol52(org.drools.workbench.models.guided.dtable.shared.model.DescriptionCol52) ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) FactPattern(org.drools.workbench.models.datamodel.rule.FactPattern) BRLActionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLActionVariableColumn) VetoUpdatePatternInUseException(org.drools.workbench.screens.guided.dtable.client.widget.table.model.synchronizers.ModelSynchronizer.VetoUpdatePatternInUseException) BRLConditionVariableColumn(org.drools.workbench.models.guided.dtable.shared.model.BRLConditionVariableColumn) RowNumberCol52(org.drools.workbench.models.guided.dtable.shared.model.RowNumberCol52) Test(org.junit.Test)

Aggregations

ActionCallMethod (org.drools.workbench.models.datamodel.rule.ActionCallMethod)25 Test (org.junit.Test)19 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)17 List (java.util.List)12 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)11 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)9 MethodInfo (org.kie.soup.project.datamodel.oracle.MethodInfo)9 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)8 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)7 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)7 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)7 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)7 ActionFieldFunction (org.drools.workbench.models.datamodel.rule.ActionFieldFunction)5 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)5 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)5 ActionSetField (org.drools.workbench.models.datamodel.rule.ActionSetField)3 BaseSingleFieldConstraint (org.drools.workbench.models.datamodel.rule.BaseSingleFieldConstraint)3 IAction (org.drools.workbench.models.datamodel.rule.IAction)3 IPattern (org.drools.workbench.models.datamodel.rule.IPattern)3