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で書いたより広範な例も気に入っています。