Reference


Top     Installation     Introduction     Samples     Tutorial     Reference     Release Notes


Rule Syntax

Rule sets manipulate properties. Facts associated with a rule set are defined implicitly by their use in rules.

Rules establish relationships between various facts.

Rules

A rule is of the form:

fact = expression WHEN conditions

The WHEN clause is optional, and a rule can simply set a value for a fact.

expression can be either a formula or a simple value.

conditions are boolean tests for other fact values.

Examples:

price = 10 WHEN order_quantity >5 and order_color = "blue"
cost = base + surcharge WHEN duration < 30 or day = "Saturday"
loan_status = "Loan is " & loan_approval
loan_approval = "approved" WHEN credit_rating >100
loan_approval = "denied" WHEN credit_rating <= 100

See following sections for details on facts, formulas, values and conditions

Facts

Facts are represented by:

ruleset : fact[ instance ]

The ruleset and instance are optional; a fact can be used by itself.

The ruleset specification is used to trigger reasoning in other rule sets.

Examples of facts:

part["gizmo"]
loan[1]
customer_name
color
price
GizmoRules:price
Parts:part["gizmo"]

The asterisk can be used in the fact instance to specify an array or list. For example consider this table which is loaded via RArray() into a fact named profit_loss[?,?]:

  Q1 Q2 Q3 Q4
Revenues $100.00 $120.00 $140.00 $190.00
Expenses $90.00 $115.00 $100.00 $160.00
Income $10.00 $5.00 $40.00 $30.00
Taxes $2.50 $1.25 $10.00 $7.50

So profit_loss[ "Expenses", Q2 ] has the value 115.

The asterisk can be used to refer to a column or row. So profit_loss[*, "Q1"] returns this value [100, 90, 10, 2.5]

profit_loss["Revenues", *] returns [100, 120, 140, 190]

Pattern matching variables can be used in the fact instance. For example:

income = profit_loss["Income", ?q] WHEN ?q = quarter

Variables can also be used for the ruleset specification. This allows the dynamic choosing of rule sets. For example:

price = ?x : price when ?x = part_rule_set

Fact names are case insensitive.

Facts are not typed, so any fact can be assigned any type of value.

Fact Values

Facts can take on different types of values:

Numbers can be expressed in any standard form, including integers, decimals and floating point notation.

Text values must be enclosed in either single or double quotes.

Boolean values are "true" and "false". Boolean values can be explicitly stated, or left off. A fact name in a rule without a value is assumed to have the value "true".

Examples:

part["gizmo", "unit_price"] = 4.99
age_limit = 16
day_off = "Saturday"
loan_status = "Loan is approved"
work = no WHEN day = 'New Years'
filename = 'pricing.xls'
weekend WHEN day = 'Saturday' OR day = 'Sunday'
rate = 10 WHEN NOT weekend
weekend = "true" WHEN day = 'Saturday' OR day = 'Sunday'
rate = 10 WHEN weekend = "false"

Dates and times can also be used as values. See the section on date/time for details.

Formulas

Formulas can be numeric or textual. They can also deal with date/times as explained in the section on date time.

Numeric formulas can contain the standard arithmetic operators, + - / *, as well as:

X ** Y raise X to the Yth power;
X // Y integer divide X by Y, returning an integer.

Example:

price = base_price + (duration - 10) * surcharge WHEN duration > 10
area = 3.14159 * radius ** 2 WHEN shape = "circle"

Textual formulas use the & operator to concatenate textual property values. Formatting can be controlled with the special words:

Example:

report = 'The status is ' & status & line & reason
reason = 'bad credit' WHEN credit_rating < 100
reason = 'insufficient down payment' WHEN down_payment < 0.2 * purchase_price

See Rule Functions for a complete list of all the functions that can be included in numeric and text formulas.

Conditions

The conditions on a rule are expressed as:

Fact <test> Expression

Where <test> is one of =, >, >=, <=, <. Values are as above. Example:

duration > 10
loan <= assets - liabilities
weekend = yes

Conditions can be joined together by booleans: AND, OR, NOT:

weekend when day = "Sunday" or day = "Saturday"

Pattern Matching Variables

Unlike facts, which are accessible by all rules in a rule set, pattern matching variables only pertain to the rule they occur in.

Variables take on values in the conditions clause of a rule. Those values can then be used when setting the fact in the rule.

A variable is indicated by a leading ?.

Variables can be used to dynamically pick an instance of an fact, or to dynamically select a secondary rule set to query.

Examples:

price = ?x WHEN ?x = unit_price * quantity
price = quantity * part[?part, unit_price] WHEN ?part = part
price = quantity * ?partrules:unit_price WHEN ?partrules = part_name & "rules"

The first example is not a particularly good way to code that rule, but does illustrate how a rule variable works. The equality test in the condition sets the value of ?x, which can then be used in setting a value for price.

The second example illustrates that ?part is local to the rule, but can get a value from part which is a fact known in the rule set.

The third example shows the use of a text formula to create a variable value that represents the name of a rule set to be used for calculating unit price.

Linking Facts and Cells

Rule facts can be linked to cells in three ways:

With each of these techniques, when a value in a linked cell changes, the rule set is considered to have changed as well, and thus any queries dependent on the rule set will be recalculated.

Open in New Window to Print


Copyright ©2005-7 Amzi! inc. All Rights Reserved.
ARulesXL and Amzi! are trademarks or registered trademarks of Amzi!
Microsoft and Excel are trademarks or registered trademarks of Microsoft Corporation.