4

If、else if、elseステートメントに匹敵するものを書き込もうとしています。しかし、オンラインコンパイラは私に問題を与えています。

私は通常、jqueryでコードを記述し、それを出力します...しかし、今回はKRLの方法でコードを実行しようとしていますが、問題が発生しています。

(PreブロックとPostブロックの間で)次のようなものを書くと、コンパイラエラーが発生します。

if(someExpression)then{//コードを実行}else{//コードを実行}

私は理由があることを知っています...しかし私はそれを私に説明する誰かが必要です...または私にドキュメントを教えてください。

4

3 に答える 3

4

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

notこの単純な例では、一方がステートメントに含まれif、もう一方が含まれないことを除いて、同じ2つのルールを作成するのも簡単です。それは同じ目的を達成します:

if (not ent:logged_in) then {

後奏曲(firedおよびnotfired、たとえば)については、 KynetxDocsにさらに多くのドキュメントがあります。また、MikeがKynetx AppADayで書いたより広範な例も気に入っています。

于 2011-02-15T14:49:19.240 に答える
3

http://kynetxappaday.wordpress.com/2010/12/21/day-15-ternary-operators-or-conditional-expressions/に示すように、変数の割り当てにpreブロックで三項演算子を使用できます。

http://kynetxappaday.wordpress.com/2010/12/15/day-6-conditional-action-blocks-and-else-postludesに示されているように、アクションブロックが起動したかどうかに基づいて、条件付きで明示的なイベントを発生させることもできます。//

于 2011-02-15T10:16:34.467 に答える
2

これはSamによって投稿されたコードで、defactionsを使用して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;
    }
  }
}
于 2011-04-06T17:14:16.667 に答える