Rules Engine - "Expressionvalue" should be reduced or increased

Rules Engine

Hello Knimes-Community!

I am trying to use a score value in KNIME with the “Rules Engine” node. I can handle the use and settings.

The only thing missing is a function for calculating the score value in the expression.

The value from the “Expression” should either be reduced or increased as a result of of the ruleset.

Example:

$number$ > 4 => $Score$ + 4
ExpressionColumn: Score = 4

$number$ < 10 => $Score$ - 10
ExpressionColumn: Score (4) = 4 - 10 = -6 (if the rule is true)

I know this with: Variable a == Variable a -10 = -6

The calculation of the score value is very extensive, which is why I am looking for a solution with one node for the rules rngine if possible.

Otherwise it would be possible to work with auxiliary columns. However, I have over 100 rules that I maintain in an editor. So, that seams like no solution…

Rules Engine.knwf (6.9 KB)

After all, I want the score_3 in a field score. As in the example here…

Rules Engine_2.knwf (9.9 KB)

Thank you for your support!

I recommend using an if expression in the Column Expressions node for this kind of application. Using the rules in the first workflow you posted, the expression would be:

if (column("number") > 5){ 2 }
else if (column("number") < 10){column("Score") - 2}

Having said that, I don’t really understand the rules that you’re trying to apply. The example rules you provide in your post are different from the ones in your Rules Engine workflow which are different from the ones in the Rules Engine_2 workflow.

Having these discrepancies makes this more complicated for us. Additionally, your rules aren’t mutually exclusive which could lead to weird results.

I don’t understand what the 2nd workflow is supposed to be showing. What exactly are you trying to accomplish? Can you provide clearer examples and a sample of your desired output?

7 Likes

Hello Elsamuel,

thank you very much. I’m sorry. Of course, I’ll try to describe my question as well as possible and understandably. I realise that this is the only way to expect an answer. Thank you in any case!

  • The rules are exemplary for the forum. Yes.
  • They are not meant to be mutually exclusive. That is quite deliberately chosen.

I am looking for a way to take a value from the table (score) for a rule (if true) and change this score value according to the if-consequence. Here in the example that would be

if1 (column(“number”) > 5 ){ 2 }
if2 (column(“number”) > 8) then take the value from Score (for the number 10 now 2) and add 2 to it.

In the result I would then have

Number | Score |
2 | 0 |
5 | 0 |
10 | 4 | if1 = Score 2; if2: Scorevalue + 2 (then part)
8 | 2 |

For the value 10, the “If1” rule is “true”.
The first rule sets Score goes up to 2.

In the second rule, if2, the CURRENT value is now taken from the Scorefield. Here in the example it says 2. However, there could also be another value in it. The value should now be increased by the value of the then part.

I have not been able to realise it now with the column expression, but I hope I have been able to clarify my problem. Thank you, again!

Hi @UGChris , As @elsamuel said, the column expressions node appears to be the way to go. I’m having a little difficulty deciding what of your requirement is actually the objective, and which parts of it are just based on your “implementation” of it. For example, is producing “Score” and “Score 2” just your mechanism for determining an overall total, or do you actually need to record both scores?

I’m assuming for the moment that having a pair of scores is mere implementation and not the objective, but I may of course be wrong.

The example that @elsamuel gave was based on it being mutually exclusive, but if you say they are not then you can still do that with a single column expression.

Taking a table from one of your original workflows
image

You can join that to a Column Expressions node

image

And then within the column expressions node, it’s note immediately obvious, but you can write quite complex logic:

// example of running sequence of conditions that aren't mutually exclusive
// to form a running total in "calc_score" variable that is then returned at
// the end to populate the Score column

var calc_score=0
if (column("number")> 5)
{calc_score += 2} // or shorthand for calc_score = calc_score+2 

if (column("number")> 8)
{calc_score += 2}  // or shorthand for calc_score = calc_score+2 

calc_score // this is the returned value from the above logic

You can include variables as well, so if you want to run a sequence of IF conditions against the “number” column which aren’t mutually exclusive, you can have a variable such as “calc_score” (which is just internal to the node), and in this case if the number is greater than 5, it will add 2 to the score, and if that same number is greater than 8 it will add a further 2 to it. The value of calc_score will then be returned into the column specified at the top of the node’s config dialog.

For your table, the result of the above is as follows:

image

So do you need “Score_2”?

Incidentally, you can also put separate expressions in for different columns within the same Column Expressions node, although there isn’t a multi-column version so if you wanted to execute the same expressions against multiple columns you either have to repeat for each column or you have to put the node into a table column loop.

Attached is the example column expression above
Column Expressions example.knwf (7.2 KB)

3 Likes

Dear @takbb & @elsamuel ,

thank you very much, again! Yes. Exactly. It’s not supposed to be a pair, but a total score. I had represented it that way because I thought it would serve the understanding of my requirement. You guessed correctly. :slight_smile:

You guys totally helped me out. The solution is exactly what I was looking for. I didn’t realise that I could assign a variable in a note and how to address it.

I’m looking at that part again right now. This could fit my use case very well. Thanks for that too!

“Incidentally, you can also put separate expressions in for different columns within the same Column Expressions node, although there isn’t a multi-column version so if you wanted to execute the same expressions against multiple columns you either have to repeat for each column or you have to put the node into a table column loop.”

@UGChris
I’ve just put together a workflow that may be of interest. It does nothing more than provide a variety of ways in which the same simple maths expression (in this case multiply by 20) can be applied to 3 numeric columns in a table, producing 3 new columns containing the results.

I did it for the “fun” of it as a learning exercise, and there may be errors, but maybe it’s also a useful quick-reference workflow and possibly of use to you.

2 Likes

@takbb … very very exciting. Thank you! I took a closer look at the Python node and am thinking about going over it or adding to my workflow. For me, it is very exciting to realise the range of solutions using your example. It also shows that the Math form is very versatile. Thank you in any case. Thank you very much for this exciting suggestion and for thinking of me! Thank you again!

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.