KRL: if there are still problems

I am trying to write something comparable to the expression If, else if, else. However, the online compiler gives me problems.

I usually write my code in jquery and just output it ... but this time I'm trying to do it with the KRL method and I'm having problems.

When I write something like the following (between the Pre and Post blocks), I get compiler errors:

if (someExpression), then {// Make the code} else {// Make the code}

I know there is a reason ... but I need someone to explain this to me ... or point me to the documentation.

+4
source share
3 answers

You can use triple operators in the pre-block to assign variables, as shown at http://kynetxappaday.wordpress.com/2010/12/21/day-15-ternary-operators-or-conditional-expressions/

You can also conditionally increase explicit events based on whether the action block was started or not, as shown at http://kynetxappaday.wordpress.com/2010/12/15/day-6-conditional-action-blocks-and- else-postludes /

+3
source

In KRL, it is often better to have separate rules for handling the "if ... then" and "more" cases described in your question. It is simply because it is a language of rules; you seem to have to change your mindset about the problem using the usual procedural way of doing it.

However, Mike’s suggestion of raising explicit events is usually the best way to solve the problem. Here is an example:

ruleset a163x47 { meta { name "If-then-else" description << How to use explicit events to simulate if..then..else behavior in a ruleset. >> author "Steve Nay" logging off } dispatch { } global { } rule when_true { select when web pageview ".*" //Imagine we have an entity variable that tracks // whether the user is logged in or not if (ent:logged_in) then { notify("My app", "You are already logged in"); } notfired { //This is the equivalent of an else block; we're sending // control to another rule. raise explicit event not_logged_in; } } rule when_false { select when explicit not_logged_in notify("My app", "You are not logged in"); } } 

In this simple example, it would also be simple enough to write two rules that are the same, except that if has a not operator and the other doesn't. This accomplishes the same goal:

 if (not ent:logged_in) then { 

There is more documentation on postlude ( fired and notfired , for example) at Kynetx Docs . I also like the more extensive example of what Mike wrote on Kynetx App A Day .

+4
source

Here is some code posted by Sam that explains how to use deflation to mimic ifthenelse behavior. All this credit for this genius belongs to Sam Carren. This is probably the best answer.

 ruleset a8x152 { meta { name "if then else" description << Demonstrates the power of actions to enable 'else' in krl! >> author "Sam Curren" logging off } dispatch { // Deploy via bookmarklet } global { ifthenelse = defaction(cond, t, f){ a = cond => t | f; a(); }; } rule first_rule { select when pageview ".*" setting () pre { testcond = ent:counter % 2 == 1; } ifthenelse( testcond, defaction(){notify("test","counter odd!");}, defaction(){notify("test","counter even!");} ); always { ent:counter += 1 from 1; } } } 
+2
source

Source: https://habr.com/ru/post/1339795/


All Articles