Conditional Expressions and Explicit Logging


Kynetx Logo

This afternoon I released build 329 of Kynetx Network Services (KNS). This build includes two new features for the Kynetx Rule Language (KRL): explicit logging and conditional expressions.

Explicit logging allows developers to place information in the ruleset log when a ruleset runs. For example, the following example would place a string with the value of a variable named query in the log if the rule fired:

fired {
  log "query:"+query
}
Explicit logging is useful for recording information about the rule environment in the logs for later analysis.

Conditional expressions allow expressions to take on different values depending on the results of a predicate evaluation. You can use conditional expressions as part of any expression anywhere in the language. Here's a simple example:

pre {
  z = (x > y) => y | y + 3; 
} 

Conditional expressions nest nicely. Here's a longer example:

month = (m eq "01") => "January" |
        (m eq "02") => "February" |
        (m eq "03") => "March" |
        (m eq "04") => "April" |
        (m eq "05") => "May" |
        (m eq "06") => "June" |
        (m eq "07") => "July" |
        (m eq "08") => "August" |
        (m eq "09") => "September" |
        (m eq "10") => "October" |
        (m eq "11") => "November" |
                       "December"

In order to make the parser more efficient (and avoid hoops to avoid left recursion in the BNF) most predicate expressions require parentheses. The only exceptions are standalone intrinsic predicates:

pre {
  weather = cloudy() => "cloudy" | "sunny"
}

For as simple as they are, conditional expressions introduce significat expressive power to KRL. I think they'll come in handy in lots of situations.