Search in sources :

Example 1 with ActionFieldFunction

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

the class ActionCallMethodBuilder method get.

public ActionCallMethod get(String variable, String methodName, String[] parameters) {
    this.variable = variable;
    this.methodName = methodName;
    this.parameters = parameters;
    ActionCallMethod actionCallMethod = new ActionCallMethod();
    actionCallMethod.setMethodName(methodName);
    actionCallMethod.setVariable(variable);
    actionCallMethod.setState(ActionCallMethod.TYPE_DEFINED);
    for (ActionFieldFunction parameter : getActionFieldFunctions()) {
        actionCallMethod.addFieldValue(parameter);
    }
    return actionCallMethod;
}
Also used : ActionCallMethod(org.drools.workbench.models.datamodel.rule.ActionCallMethod) ActionFieldFunction(org.drools.workbench.models.datamodel.rule.ActionFieldFunction)

Example 2 with ActionFieldFunction

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

the class ActionCallMethodBuilder method getActionFieldFunction.

private ActionFieldFunction getActionFieldFunction(String param, String dataType) {
    param = removeNumericSuffix(param, dataType);
    final int fieldNature = inferFieldNature(dataType, param, boundParams, isJavaDialect);
    // If the field is a formula don't adjust the param value
    String paramValue = param;
    switch(fieldNature) {
        case FieldNatureType.TYPE_FORMULA:
            break;
        case FieldNatureType.TYPE_VARIABLE:
            break;
        case FieldNatureType.TYPE_TEMPLATE:
            paramValue = unwrapTemplateKey(param);
            break;
        default:
            paramValue = adjustParam(dataType, param, boundParams, isJavaDialect);
    }
    ActionFieldFunction actionField = new ActionFieldFunction(methodName, paramValue, dataType);
    actionField.setNature(fieldNature);
    return actionField;
}
Also used : ActionFieldFunction(org.drools.workbench.models.datamodel.rule.ActionFieldFunction)

Example 3 with ActionFieldFunction

use of org.drools.workbench.models.datamodel.rule.ActionFieldFunction 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 4 with ActionFieldFunction

use of org.drools.workbench.models.datamodel.rule.ActionFieldFunction 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 5 with ActionFieldFunction

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

the class ActionCallMethodWidget method showAddFieldPopup.

protected void showAddFieldPopup(Widget w) {
    final AsyncPackageDataModelOracle oracle = this.getModeller().getDataModelOracle();
    final FormStylePopup popup = new FormStylePopup(GuidedRuleEditorImages508.INSTANCE.Wizard(), GuidedRuleEditorResources.CONSTANTS.ChooseAMethodToInvoke());
    final ListBox box = new ListBox();
    box.addItem("...");
    for (int i = 0; i < fieldCompletionTexts.length; i++) {
        box.addItem(fieldCompletionValues[i], fieldCompletionTexts[i]);
    }
    box.setSelectedIndex(0);
    popup.addAttribute(GuidedRuleEditorResources.CONSTANTS.ChooseAMethodToInvoke(), box);
    box.addChangeHandler(new ChangeHandler() {

        public void onChange(ChangeEvent event) {
            final String methodNameWithParams = box.getItemText(box.getSelectedIndex());
            oracle.getMethodParams(variableClass, methodNameWithParams, new Callback<List<String>>() {

                @Override
                public void callback(final List<String> methodParameters) {
                    final String methodName = box.getValue(box.getSelectedIndex());
                    model.setMethodName(methodName);
                    model.setState(ActionCallMethod.TYPE_DEFINED);
                    for (String methodParameter : methodParameters) {
                        model.addFieldValue(new ActionFieldFunction(methodName, null, methodParameter));
                    }
                    getModeller().refreshWidget();
                    popup.hide();
                }
            });
        }
    });
    popup.show();
}
Also used : AsyncPackageDataModelOracle(org.kie.workbench.common.widgets.client.datamodel.AsyncPackageDataModelOracle) Callback(org.uberfire.client.callbacks.Callback) ChangeEvent(com.google.gwt.event.dom.client.ChangeEvent) ChangeHandler(com.google.gwt.event.dom.client.ChangeHandler) FormStylePopup(org.uberfire.ext.widgets.common.client.common.popups.FormStylePopup) ActionFieldFunction(org.drools.workbench.models.datamodel.rule.ActionFieldFunction) List(java.util.List) ListBox(org.gwtbootstrap3.client.ui.ListBox) SingleFieldConstraint(org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)

Aggregations

ActionFieldFunction (org.drools.workbench.models.datamodel.rule.ActionFieldFunction)9 ActionCallMethod (org.drools.workbench.models.datamodel.rule.ActionCallMethod)5 CompositeFactPattern (org.drools.workbench.models.datamodel.rule.CompositeFactPattern)3 FactPattern (org.drools.workbench.models.datamodel.rule.FactPattern)3 FromAccumulateCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromAccumulateCompositeFactPattern)3 FromCollectCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCollectCompositeFactPattern)3 FromCompositeFactPattern (org.drools.workbench.models.datamodel.rule.FromCompositeFactPattern)3 RuleAttribute (org.drools.workbench.models.datamodel.rule.RuleAttribute)3 RuleModel (org.drools.workbench.models.datamodel.rule.RuleModel)3 SingleFieldConstraint (org.drools.workbench.models.datamodel.rule.SingleFieldConstraint)3 Test (org.junit.Test)3 FromEntryPointFactPattern (org.drools.workbench.models.datamodel.rule.FromEntryPointFactPattern)2 ChangeEvent (com.google.gwt.event.dom.client.ChangeEvent)1 ChangeHandler (com.google.gwt.event.dom.client.ChangeHandler)1 FlexTable (com.google.gwt.user.client.ui.FlexTable)1 List (java.util.List)1 ActionFieldValue (org.drools.workbench.models.datamodel.rule.ActionFieldValue)1 ActionGlobalCollectionAdd (org.drools.workbench.models.datamodel.rule.ActionGlobalCollectionAdd)1 ActionInsertFact (org.drools.workbench.models.datamodel.rule.ActionInsertFact)1 ActionRetractFact (org.drools.workbench.models.datamodel.rule.ActionRetractFact)1