Remove whitespaces, from input string type paramet

2019-08-14 17:13发布

I have a rule which take a String type input parameter. Can I remove the whitespaces from the value this parameter holds using BAL.

If not what is the other option to do this. In this JRule there is a decision table where condition column is this parameter and then output is action column.

Say you define Rulset Paramter "Name" of type String for Rule IsDepartmentManager where output ruleset parameter is boolean.

Now in the decision table the values in Name column is say "John" and out for this is True. Otherwise False.

Now when this rule is invoked as web-service the input send is "John ". As the name contains white spaces and the decision table do the exact matching, the result return is False.

2条回答
\"骚年 ilove
2楼-- · 2019-08-14 17:50

You can add an Initial Action in your Rule Task (that contains the Decision Table or the rule) in which you can execute theInputString.trim();

查看更多
该账号已被封号
3楼-- · 2019-08-14 18:10

Can you post an example of the rule? It would be good to see why you need to trim the string in the first place.

But you could write a function to do this and expose it via the BOM. This can be done two ways. First, you could write a virtual function directly in the BOM that takes a string and trims it. The second option if you use Java XOM's is to write the function in Java and expose that via the BOM.

If your using the virtual function approach, then the code will be written using IRL, but this is essentially a cut down version of Java so it will have the String object methods needed to trim. For example:

return theString.trim();

To add a BOM function, do the following steps:

  1. Right click the "bom" folder in the Eclipse rules project.
  2. Select "BOM Entry" from the menu.
  3. Select the "Create an empty BOM entry" option and then click "Finish".
  4. Double click the new BOM entry to bring up the BOM editor view, and then click "New Class".
  5. Enter the class name and then click "Finish".
  6. Double click the new BOM class from the list, then under the "Members" section, click the "New" button.
  7. In the new member dialog box, select the "Method" option, enter a name for the method, and add a parameter as a String type. Finally set the return type as a String type. then click the "Finish" button.
  8. Then double click the new method under the "Members" section, and select the "Static" and "Final" options, and create a default verbalisation under the "Member Verbalisation" section.
  9. Under the "BOM to XOM Mapping" section, enter the code I put in my original answer above, changing the parameter name to match what you have used.
  10. Go back to the class level BOM editor and set the "Execution name" to the value "void" in the "BOM to XOM mapping" section. This is needed because the BOM class is not linked to a Java class (XOM).

Once you have done this, you should then be able to see the new method in the BAL editor for the rule.

However, what I would say is that you should try and trim and prepare the data before passing it into the rule set. Ideally you want a little custom functions in the rule set as possible to keep the rules as clean as possible.

查看更多
登录 后发表回答