Loop and switch case

Hi guys,

I’m trying to implement a worflow using a DateTime range node; I need for example a week of time serie with a 1 hour of spacing.
So I need of a switch case node: I need to loop on this calendar and if the time is from 00:00 to 07:00 I need to set a variable to a value, if the time is in the range 07:00 and 18:00 I need a variable is set to 0, if the time is in 18:00 to 23:00 I need to set another value.
I don’t understand how to set a loop node with a switch case node to set my conditions.
Any help is apreciated.

1 Like

Hello @giuseppeR,

I do not fully understand your problem. However, I suspect the following nodes might help you (without even needing a loop) to get the hour and assign values based on the hour:

I hope this solves your case, otherwise some example data may help to understand the problem better.

Best regards,
nan

1 Like

Hi @giuseppeR ,

I believe that it can resolve your problem, just a date&time range and a rule engine.

image

If you use date&time rage node, you can set the start and end date-time information.

image

As you can see the image above, I set a variable rows (I don’t know how many rows can be expected). After that, i set the start date/time and at the ending point, i set +1 hour (+1h) ant set the date and time.

When you run it, the node will generate all rows necessary with this interval.

After that, i used a rule engine with sone regular expression to set the limits as you said the periods.

$Date&Time$ MATCHES “\d±\d±\d+T[0][0-6]:[0-9]+” => 0
$Date&Time$ MATCHES “\d±\d±\d+T([0][7-9]|[1][0-7]):[0-9]+” => 1
$Date&Time$ MATCHES “\d±\d±\d+T([1][8-9]|[2][0-3]):[0-9]+” => 2

I set the “prediction” variable to be 0, 1 or 2 for the conditions above.

The results were:

Is that what you want right?

datetime_rage.knwf (12.4 KB)

Tks,

Denis

1 Like

Hi guys,

thank you for your suggestion, they drive me to an acceptable solution.
But there are some problems: If I use my nodes it seems they don’t work, so I import the @denisfi workflow and it works correctly. It may be a version problem I’m using Knime 4.7.0.
@denisfi can you explain your regex expression?
Another question is: if in the rule engine node the setted values they can be taken from another node, as example a math formula node?
In the pic there is my actual workflow, so i need the rule engine take the values from the 2 math formula nodes.

Thank you for your precious help.

1 Like

Hi @giuseppeR ,

Sure, lets talk about regular expression… lol

\d+ means digital numbers - 0 to 9 and the + means that it can repeat 1 or more time.

[ ] > used to define limits like a-z 0-9

I create a pattern to identify the period for each situation and set a value as a integer (0,1,2)

You give us some conditions:

0 > between 0 and 6:59 (7h)
1 > between 7:00 and 17:59 (18h)
2 > between 18:00 and 23:59 (0h)

The field with the values have a datetime stamp like: year(4 digits)-month(2 digits)-day(2 digits)T00:00

Thats it…

Tks,

Denis

2 Likes

Thank ypu @denisfi for your clear explanation…do you have any suggestions on how to set rule values by taking those values from a mathematical node formula?
Thanks for your hints.

Hi @giuseppeR ,

Math Formula node just make a calc… rule engine test some information using other tools as string manipulation. Can you use the example that I passed before and adapt it to your case?

If you have to split data and the csv information, you can use a joiner node or cell replacer to complete with these variables. Can you send again a complete wf here as example?

Tks,

Denis

Hi @denisfi,

I use you example…so I have modified rules adding an extract datetime node and it works fine.
I attached my workwflow…if you see in the rules, I have the values 10 and 100 and I want the value100 is PMax and 10 is the value Prid, PMAx and Prid are caluclated with the 2 math formula nodes.

PI_test.knwf (19.0 KB)

Hope this can help.
Thanks.

Hi @giuseppeR ,

I don’t understand the match from each line, but as you can see from this new workflow, you can set and calc using a regular math formula, not the multicolumns because this options will apply the same calc for multiples columns to make it quick calc for all cases. Using the simple math formula, I can put the Pmax and the Prid at the same single line.

Not what do you need to do with the time to match with the calc? just a merge/joiner?

PI_test2.knwf (58.9 KB)

Tks,

Denis

Great work! With the row with CSV reader I have the 2 variables: PMax and Prid, so this I want is:
if the time is in 0 to 6, the condition is equal to Prid, if time is 7 the value must be Pmax, if the time is in 8-18 the value is 0, and in the end if time is in 18-23 the value is PMax.
I hope now it is clear what I expect.
Thanks.

Simple, just put the PMax and Prid in variables and use it with another rule engine to set the final value.

PI_test3.knwf (65.4 KB)

That’s it…

Denis

2 Likes

Hi @denisfi thank you for your solution! It works! Great!

1 Like

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