At this page, one can find the documentation concerning rules.
In this section we describe the way rules are defined in d3web KnowWE.
Rules are defined by the Rule tag in KnowWE. The basic syntax is as follows:
%%Rule IF (rule condition) THEN (rule action) %
A condition can look like "temperature > 38" or "weather = rain'"'. Such simple conditions can be combined to more complex ones, using the following syntax:
For more information about conditions and other condition types, see Doc Conditions.
In a rule action we can set other questions or variables. Examples could be "Show warning = Yes" or "umbrella = Yes"
It is also possible to set more complex values and values depending on other variables. For more information, go to Extended Expressions: Conditions and actions, but be aware, that this extension is non-LGPL-licensed for commercial use.
It is possible to define more than one action to be executed for a rule. Just add more THEN clauses.
IF (rule condition) THEN (rule action 1) THEN (rule action 2) THEN (rule action 3)
Furthermore, it is possible to add an exception condition to the rule, like in the following example:
IF (rule condition 1) THEN (rule action) EXCEPT (rule condition 2)
Of course it is also possible to define the EXCEPT(...) statement as AND NOT within the IF clause. The difference is, that EXCEPT(...) doesn't need to be evaluated, i.e. the rule will also fire even if the EXCEPT clause is unknown.
Using such rules, we often not only want something to happen if the condition is true (e.g. temperature <= 0), but also when the condition is false. We can of course always do this, by writing a second rule, using the negated condition of the first rule (e.g. temperature > 0). With d3web/KnowWE, we provide the possibility, to just use the keyword ELSE, similar to how it is known and done in software engineering.
IF temperature < 0 THEN icy roads = Yes ELSE icy roads = No
Example: Lets assume, the temperature is currently unknown, because the sensor is broken. In this case, we really do not know whether the roads are icy or not. So to expand on the example above, we can do the following:
IF temperature < 0 THEN icy roads = Yes ELSE icy roads = No UNKNOWN icy roads = NoThis way, if the temperature is Unknown, we also set the variable/question icy roads to Unknown, without having the write rules for each of the actions.
Be aware that, if you just use THEN and ELSE without an action for UNKNOWN, the action of ELSE will also fire if temperature is Unknown.
It is also possible to use an EXCEPT condition together with ELSE and UNKNOWN. If the EXCEPT condition is true, the ELSE action will fire. The UNKNOWN action only fires if the original IF condition is Unknown. For clarification, check out the following truth tables:
Rule with THEN and ELSE action only:
IF Condition | EXCEPT Condition | THEN Action | ELSE Action |
---|---|---|---|
true | true | fire | |
false | true | fire | |
unknown | true | fire | |
true | false | fire | |
false | false | fire | |
unknown | false | fire | |
true | unknown | fire | |
false | unknown | fire | |
unknown | unknown | fire |
IF Condition | EXCEPT Condition | THEN Action | ELSE Action | UNKNOWN Action |
---|---|---|---|---|
true | true | fire | ||
false | true | fire | ||
unknown | true | fire | ||
true | false | fire | ||
false | false | fire | ||
unknown | false | fire | ||
true | unknown | fire | ||
false | unknown | fire | ||
unknown | unknown | fire |
IF eval(question A = question B) THEN (rule action)
IF eval(question A = 2 * x + b) THEN (rule action)
or if a complex action shall be conducted
IF questionA < 2 THEN questionB = eval(max(question C, question D))
eval(...) must not be used in combination with KNOWN/UNKOWN
IF question A = UNKNOWN THEN question B = 2