KRL:如果那么其他问题

发布于 2024-10-17 14:08:06 字数 273 浏览 3 评论 0原文

我正在尝试编写类似于 If、else if、else 语句的内容。然而,在线编译器给我带来了问题。

我通常用 jquery 编写代码,然后发出它......但这次我尝试以 KRL 方式执行此操作,但遇到了问题。

当我编写如下内容(在 Pre 和 Post 块之间)时,出现编译器错误:

if (someExpression) then { //做一些代码 } 别的 { //做一些代码 我知道

这是有原因的...但我需要有人向我解释...或者向我指出文档。

I am trying to write something comparable to an If, else if, else statement. However, the online compiler is giving me problems.

I usually write my code in jquery and just emit it... but I am trying to do it the KRL way this time and I am running into problems.

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

if (someExpression) then {
//Do some code
} else {
//DO some code
}

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷…雨湿花 2024-10-24 14:08:06

对于 KRL,通常最好有单独的规则来处理问题中描述的“if...then”和“else”情况。这只是因为它是一种规则语言;你必须改变你思考问题的方式,而不是通常的程序方式。

也就是说,迈克提出明确事件的建议通常是解决问题的最佳方法。下面是一个示例:

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");
  }
}

在这个简单的示例中,编写两个相同的规则也很容易,只是一个规则在 if 语句中包含一个 not,另一个规则在 if 语句中包含一个 not。没有。这实现了相同的目的:

if (not ent:logged_in) then {

Kynetx 文档。我也喜欢迈克在 Kynetx 应用一天

With KRL, it's often best to have separate rules to handle the "if...then" and "else" cases described in your question. That's simply because it's a rule language; you kind of have to change your way of thinking about the problem from the usual procedural way of doing it.

That said, Mike's suggestion to raise explicit events is usually the best way to solve the problem. Here's 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 easy enough to write two rules that are the same except that one has a not in the if statement and the other does not. That accomplishes the same purpose:

if (not ent:logged_in) then {

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

临风闻羌笛 2024-10-24 14:08:06

您可以在 pre 块中使用三元运算符进行变量赋值,如 http://kynetxappaday.wordpress.com/2010/12/21/day-15-ternary-operators-or-conditional-expressions/

您还可以根据条件有条件地引发显式事件是否触发操作块,如 http://kynetxappaday.wordpress.com/2010/12/15/day-6-conditional-action-blocks-and-else-postludes/

You can use ternary operators in the pre block for variable assignment as illustrated at http://kynetxappaday.wordpress.com/2010/12/21/day-15-ternary-operators-or-conditional-expressions/

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

紫瑟鸿黎 2024-10-24 14:08:06

以下是 Sam 发布的一些代码,解释了如何使用缺陷来模仿 ifthenelse 行为。这位天才的所有功劳都归功于萨姆·柯伦。这可能是您能得到的最佳答案。

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;
    }
  }
}

Here is some code posted by Sam, that explains how to use defactions to mimic an ifthenelse behavior. All this credit for this genius belongs to Sam Curren. This is probably the best answer you could get.

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;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文