# Rule Engine

**URL:** <https://forum.knime.com/t/rule-engine/76369>\
**Category:** KNIME Analytics Platform\
**Created:** [January 4, 2024, 3:06pm UTC](https://forum.knime.com/t/rule-engine/76369 "2024-01-04T15:06:34Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brain](https://forum-cdn.knime.com/user_avatar/forum.knime.com/brain/32/53424_2.png) [@Brain](https://forum.knime.com/u/Brain)\
**Post date:** [January 4, 2024, 3:06pm UTC](https://forum.knime.com/t/rule-engine/76369/1 "2024-01-04T15:06:34Z")

</div>

Hello,  
I use this dataset i have several string columns to transform.  
[train.csv](https://forum.knime.com/uploads/short-url/2SWnAJEt4GcloSK6qVCFv2b9d7.csv) (223.8 KB)

Example Surgery Yes/No → 1/0  
I will have to do the same for all the string column

 ![Capture d'écran 2024-01-04 160234](https://forum-cdn.knime.com/uploads/default/original/3X/3/e/3e2f67bfb55859c2042c554c761ee756199bddea.png)  
Result is “?”  
Where is the problem ?

Thanks

---

<div class="post-metadata">

**Author:** ![ArjenEX](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/97f17d/32.png) [@ArjenEX](https://forum.knime.com/u/ArjenEX)\
**Post date:** [January 4, 2024, 3:51pm UTC](https://forum.knime.com/t/rule-engine/76369/2 "2024-01-04T15:51:30Z")

</div>

Hi @Brain

Three issues:

- You are using LIKE which, as per the explanation, looks for a wildcard pattern. Something that you don’t have 😉

- You don’t have a fallback default outcome (as per the explanation notes).

- Your rule has capital Y, your data is all lower case.

Use:

```auto
$surgery$ = "yes" => 1
TRUE => 0

```

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/8/4/84f3308fdce252f494719f6f90bbfdf9fbe58f2a.png)

---

<div class="post-metadata">

**Author:** ![evert.homan\_scilifelab.se](https://forum-cdn.knime.com/user_avatar/forum.knime.com/evert.homan_scilifelab.se/32/18181_2.png) [@evert.homan\_scilifelab.se](https://forum.knime.com/u/evert.homan_scilifelab.se)\
**Post date:** [January 4, 2024, 3:55pm UTC](https://forum.knime.com/t/rule-engine/76369/3 "2024-01-04T15:55:07Z")

</div>

You beat me to it 🙂

This also works, so there is some flexibility in syntax:

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/0/8/087cd63c9ce9a30c20e94e0b7c8b9a11d3318262.png)

BW/Evert

---

<div class="post-metadata">

**Author:** ![Daniel\_Weikert](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@Daniel\_Weikert](https://forum.knime.com/u/Daniel_Weikert)\
**Post date:** [January 4, 2024, 5:10pm UTC](https://forum.knime.com/t/rule-engine/76369/4 "2024-01-04T17:10:17Z")

</div>

Are you sure you do not want to one hot encode instead or do your features have an order?  
br

---

<div class="post-metadata">

**Author:** ![Brain](https://forum-cdn.knime.com/user_avatar/forum.knime.com/brain/32/53424_2.png) [@Brain](https://forum.knime.com/u/Brain)\
**Post date:** [January 5, 2024, 7:57am UTC](https://forum.knime.com/t/rule-engine/76369/5 "2024-01-05T07:57:09Z")

</div>

![RULE ENGINE](https://forum-cdn.knime.com/uploads/default/original/3X/e/a/ea3ecf67329d85bd14bc84bba0489f0c69f8826a.png)

I used 14 times Rule Engine for 14 different columns.  
If you have something easier …

Thanks  
Br

---

<div class="post-metadata">

**Author:** ![takbb](https://forum-cdn.knime.com/user_avatar/forum.knime.com/takbb/32/57077_2.png) [@takbb](https://forum.knime.com/u/takbb)\
**Post date:** [January 5, 2024, 11:20am UTC](https://forum.knime.com/t/rule-engine/76369/6 "2024-01-05T11:20:10Z")

</div>

Hi @Brain , for this use case I would suggest String Manipulation (multi column) using the following syntax. (This is my go-to “multi-column Rule Engine” 🙂 )

```auto
toInt(
	$$CURRENTCOLUMN$$.equals("Yes")?1:0
	)

```

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/5/d/5d1d2d5204dd3804ac1ccfe416e869e15b931d19.png)

Make sure to select only the required String columns, otherwise for some data types the node produces nonsensical (to the user) error messages. String Manipulation cannot cope with being presented with Longs, Binary, Date and many other data types. Including such data types results in this message during configuration.

One day I’d like to see that particular annoyance fixed, since the non-multi-column version of this node has no trouble handling them…

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/3/c/3c2557b27790fa0e483c68c27eac6ae27262c299.png)  
… just putting that out there… 😉

btw… multiple rules can be accommodated by nesting the conditions,  
e.g.

```auto
toInt(
	$$CURRENTCOLUMN$$.equals("Yes")?1:
	$$CURRENTCOLUMN$$.equals("No")?0:
	$$CURRENTCOLUMN$$.equals("Maybe")?2:-1
	)

```

edit:  
If there is a possibility that a column could containing missing value (`?`) then it is important to handle this as the first condition to prevent an error:

```auto
toInt(
	$$CURRENTCOLUMN$$==null?null:
	$$CURRENTCOLUMN$$.equals("Yes")?1:
	$$CURRENTCOLUMN$$.equals("No")?0:
	$$CURRENTCOLUMN$$.equals("Maybe")?2:-1		
	)

```

---

<div class="post-metadata">

**Author:** ![tqAkshay95](https://forum-cdn.knime.com/user_avatar/forum.knime.com/tqakshay95/32/67469_2.png) [@tqAkshay95](https://forum.knime.com/u/tqAkshay95)\
**Post date:** [January 9, 2024, 6:01am UTC](https://forum.knime.com/t/rule-engine/76369/7 "2024-01-09T06:01:43Z")

</div>

Hello,

For transforming string columns like ‘Surgery’ to binary (1/0) in KNIME, you can use the ‘Rule Engine’ node with an expression like:

$Surgery$ MATCHES “yes” =\> 1  
TRUE =\> 0

- `$Surgery$ MATCHES "yes"`: This condition checks if the ‘Surgery’ column contains the string ‘yes’.
- `=> 1`: If the condition is true, it assigns the value 1.
- `TRUE => 0`: If the condition is not true (i.e., ‘Surgery’ does not contain ‘yes’), it assigns the value 0.

This expression will effectively convert ‘Surgery’ values of ‘yes’ to 1 and all other values to 0.

---

<div class="post-metadata">

**Author:** ![takbb](https://forum-cdn.knime.com/user_avatar/forum.knime.com/takbb/32/57077_2.png) [@takbb](https://forum.knime.com/u/takbb)\
**Post date:** [January 9, 2024, 6:59am UTC](https://forum.knime.com/t/rule-engine/76369/8 "2024-01-09T06:59:36Z")

</div>

Hi @tqAkshay95 , keep in mind that `MATCHES` is for matching regular expressions whereas a simple `=` is more correctly used for matching exact strings. Whilst it would work in this instance, using MATCHES can lead to unexpected outcomes if your string unintentionally contains regular expression special characters such as “.”.

Also one issue the OP was trying to overcome here was working across a large number of columns, which is something Rule Engine is not well suited for, but you are correct that for individual columns, Rule Engine can work effectively.

---

<div class="post-metadata">

**Author:** ![ArjenEX](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/a/97f17d/32.png) [@ArjenEX](https://forum.knime.com/u/ArjenEX)\
**Post date:** [January 9, 2024, 7:30am UTC](https://forum.knime.com/t/rule-engine/76369/9 "2024-01-09T07:30:26Z")

</div>

ChatGPT response 😉 Should be flagged as per Scott’s instruction.

---

<div class="post-metadata">

**Author:** ![tqAkshay95](https://forum-cdn.knime.com/user_avatar/forum.knime.com/tqakshay95/32/67469_2.png) [@tqAkshay95](https://forum.knime.com/u/tqAkshay95)\
**Post date:** [January 9, 2024, 7:31am UTC](https://forum.knime.com/t/rule-engine/76369/10 "2024-01-09T07:31:57Z")

</div>

Thanks for the correction.

I’ll be mindful of using = for exact string matching and MATCHES for regular expressions.

Also noted about Rule Engine limitations with a large number of columns.

---

<div class="post-metadata">

**Author:** ![Brain](https://forum-cdn.knime.com/user_avatar/forum.knime.com/brain/32/53424_2.png) [@Brain](https://forum.knime.com/u/Brain)\
**Post date:** [January 9, 2024, 7:54am UTC](https://forum.knime.com/t/rule-engine/76369/11 "2024-01-09T07:54:09Z")

</div>

Hi  
This is the Rule workflow. My problem is each column is not only Yes or No.  
This is the result i would like to have using only one node.

[RULE.knwf](https://forum.knime.com/uploads/short-url/ihR62Jy29q0rJeSZcxEjugZm3D7.knwf) (93.5 KB)  
[train.csv](https://forum.knime.com/uploads/short-url/2SWnAJEt4GcloSK6qVCFv2b9d7.csv) (223.8 KB)

Thanks  
Br

---

<div class="post-metadata">

**Author:** ![Daniel\_Weikert](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@Daniel\_Weikert](https://forum.knime.com/u/Daniel_Weikert)\
**Post date:** [January 9, 2024, 4:55pm UTC](https://forum.knime.com/t/rule-engine/76369/12 "2024-01-09T16:55:04Z")

</div>

If the data is not ordinal you could try one hot encoding with one to many

---

<div class="post-metadata">

**Author:** ![Brain](https://forum-cdn.knime.com/user_avatar/forum.knime.com/brain/32/53424_2.png) [@Brain](https://forum.knime.com/u/Brain)\
**Post date:** [January 9, 2024, 5:05pm UTC](https://forum.knime.com/t/rule-engine/76369/13 "2024-01-09T17:05:52Z")

</div>

It’s what i look for.

---

<div class="post-metadata">

**Author:** ![Daniel\_Weikert](https://forum-cdn.knime.com/letter_avatar_proxy/v4/letter/d/e47c2d/32.png) [@Daniel\_Weikert](https://forum.knime.com/u/Daniel_Weikert)\
**Post date:** [January 9, 2024, 5:08pm UTC](https://forum.knime.com/t/rule-engine/76369/14 "2024-01-09T17:08:39Z")

</div>

Then try the node (One to Many Node)  
br

---

<div class="post-metadata">

**Author:** ![takbb](https://forum-cdn.knime.com/user_avatar/forum.knime.com/takbb/32/57077_2.png) [@takbb](https://forum.knime.com/u/takbb)\
**Post date:** [January 9, 2024, 7:05pm UTC](https://forum.knime.com/t/rule-engine/76369/15 "2024-01-09T19:05:28Z")

</div>

This one is more luke-warm encoding 😉 but here is a new component I’ve been playing with:

> **[Rule Engine (Multi Column Dictionary) – takbb](https://hub.knime.com/takbb/spaces/Public/Components/Rule%20Engine%20(Multi%20Column%20Dictionary)~QbL7FBZO2Qxzg4yW/current-state)**
>
> Provides the capabilities of the Rule Engine but allows a single set of rules to be supplied which can act on Multiple Columns The data table is passed to the …

![image](https://forum-cdn.knime.com/uploads/default/original/3X/0/5/05f61a7852ab948b7551517aed8fe37b0edd8915.png)

You give it a set of rules in a single table. I used Excel for this purpose as it was easier to edit. They use exact Rule Engine syntax, and I’ve copied and pasted from all of your rule engines:

 ![image](https://forum-cdn.knime.com/uploads/default/original/3X/c/c/cc4275a958d345a629aee9c013473ab1a1316997.png)

The biggest limitation would be performance on very large data sets because of the amount of Column Appending it has to do, which can slow things down significantly, especially on a large number of columns, but for general use, it should work.

I would suggest editing rules in a regular Rule Engine to ensure they are syntactically correct and then copy/paste all together into an Excel spreadsheet to be read in by the workflow. They can of course be manually edited in a table editor (`Table Creator` ) or using a text editor and plain csv files, but I found Excel more convenient to use.

[RULES using multi-column component.knwf](https://forum.knime.com/uploads/short-url/e1MUHpGHNkuxPDlKh0anEtft3u.knwf) (256.9 KB)

---

<div class="post-metadata">

**Author:** ![Brain](https://forum-cdn.knime.com/user_avatar/forum.knime.com/brain/32/53424_2.png) [@Brain](https://forum.knime.com/u/Brain)\
**Post date:** [January 10, 2024, 10:38am UTC](https://forum.knime.com/t/rule-engine/76369/16 "2024-01-10T10:38:35Z")

</div>

Hi,  
Great, I tried it and it’s fantastic. What’s perfect is that it’s a problem we often encounter. THANK YOU.

---

<div class="post-metadata">

**Author:** ![takbb](https://forum-cdn.knime.com/user_avatar/forum.knime.com/takbb/32/57077_2.png) [@takbb](https://forum.knime.com/u/takbb)\
**Post date:** [January 10, 2024, 10:41am UTC](https://forum.knime.com/t/rule-engine/76369/17 "2024-01-10T10:41:48Z")

</div>

your welcome @Brain . Do post a message (tagging me) if you encounter any issues with it, as it’s new and I’ve only done basic testing.

---

<div class="post-metadata">

**Author:** ![system](https://forum-cdn.knime.com/uploads/default/original/1X/ab3ccf34482a0329361734a18199390177204f15.png) [@system](https://forum.knime.com/u/system)\
**Post date:** [January 17, 2024, 10:41am UTC](https://forum.knime.com/t/rule-engine/76369/18 "2024-01-17T10:41:55Z")

</div>

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