Tutorial
Using Tables, Arrays, Lists, Facts & Variables


Top     Installation     Introduction     Samples     Tutorial     Reference     Release Notes


Using Tables, Arrays and Lists

Fact names in ARulesXL can be indexed by names or numbers in square brackets to represent lists and arrays, for example revenues[1] or data[Q1, Expenses]. You can have as many dimensions as you need. Indexed facts are also used to load data tables into a rule set. Consider the Arrays & Lists worksheet in Techniques Basic.xls:

The function RArray() allows you to load a single dimension array (called a list) or multi-dimensional arrays. In this example, two arrays are loaded, the Income Statement by Quarter and just the Revenues row. The parameters to RArray() are:

  1. The name of the fact in the rule set.
  2. If the table has row headings.
  3. If the table has column headings.
  4. If the table is a list (vector), e.g. it has one dimension.

The first RArray() loads an array named data with two indices. The row and column headings come from the table itself, so values can be referred to as:

data[Revenues, Q2]
data[Income, Q4]

The second RArray() loads a list of numbers named revenues without any headings. Values in this list are referred to as:

revenues[1]
revenues[2]

The queries illustrate various ways of looking at the data and the use of some functions that are useful when using arrays and lists. These functions are:

The Techniques Advanced.xls spreadsheet illustrates additional capabilities with arrays, lists and rule variables (see below).

Multiple Fact Rules

The rules so far have dealt with single facts. Multiple facts can be used as well. Assume we had two types of objects to price, gizmos and widgets. We could then write the rules like:

Gizmo.UnitPrice = 10 when Gizmo.Quantity < Gizmo.DiscountQuantity
Widget.UnitPrice = 10 when Widget.Quantity < Widget.DiscountQuantity

There can be any number of properties associated with a property, for example one might need to refer to: Gizmo.Order.Date

Facts can also be indexed by one or more arguments. The arguments are enclosed in square brackets and separated by commas.

The screen shot below shows a version of the pricing application that includes gizmos and widgets as arguments of a Part property, and also a rule to compute the total price.

Pattern Matching Variables

Pattern matching variables can be used create a single rule pattern that replaces many rules. A variable is signified by an initial question mark, for example, ?x.

In the screen shot above, the rules for UnitPrice of gizmos are the same as those for widgets. Using a variable, the first four rules can be replaced with two rules:

Part[?x, UnitPrice] = 10 when Part[?x, Quantity] < Part[?x, DiscountQuantity]
Part[?x, UnitPrice] = 8 when Part[?x, Quantity] >= Part[?x, DiscountQuantity]

Notice that the names of variables apply only to a single rule. The ?x variable in the first rule above is used for the pattern in that rule; the ?x variable in the second rule is a different variable and has nothing to do with the ?x in the first rule.

The rules with variables can be seen at work in the following screen shot.

How it Works

Using the screen shot above as an example, here is what happens when the query in cell C13 is executed:

  1. Part[gizmo, quantity] is set to 22, from cell A13
  2. Part[widget, quantity] is set to 5, from cell B13
  3. The first (and only in this case) rule that can be used to find Order.Price is considered, A6.
  4. Rule A6 needs part[gizmo, quantity] and part[widget, quantity], both of which are already known, and also needs part[gizmo, unitprice] and part[widget, unitprice] which are not known.
  5. Rule A6 then sends out a sub query to find the value of part[gizmo, unitprice].
  6. Rule A1, the first of two rules that can be used for part[gizmo, unitprice] is considered, and the rule variable ?x is temporarily set to gizmo for the sake of the rule.
  7. With ?x set to gizmo, rule A1 then needs to find part[gizmo, quantity] and part[gizmo, discountquantity], both of which are known as 22 and 15 respectively. Because 22 is not less than 15, the condition fails and rule A1 is said to have failed.
  8. Rule A2, the next rule that can be used to find part[gizmo, unitprice] is then considered. The ?x rule variable in the second rule is temporarily set to gizmo. The condition of rule A2 succeeds, so now part[gizmo, unitprice] is known to be 8.
  9. This brings us back to A6 which next sends out a subquery for part[widget, unitprice].
  10. Rule A1 is again the first choice, only this time ?x is temporarily set to widget. Picking up part[widget, quantity] and part[widget, discountquantity], the condition succeeds, so A1 is said to succeed and part[widget, unitprice] is known to be 10.
  11. Now A6 has all the data it needs to perform the calculation to get order.price and return the answer to the query.

Notice that the rules used for a query are dynamically determined based on the values of the inputs. This is what makes it possible to specify the rules in a declarative, natural manner. The underlying software which determines which rules to use and when is called the reasoning engine.

The trace feature can be used to watch the reasoning process.

Previous

Next

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.