Data Tables and
Pattern Matching Rules


Tutorial Index     Rules & Decision Tables     Column Queries     Arrays     Tables & Pattern Matching     Lists     VBA     Multiple Rule Sets     Array Queries


loans_05_data_tables.xls

In the previous examples, the inputs facts for RQuery() were entered in the RQuery():

=RQuery(ResaleRules, "FIND Resell['Acme'] WHEN FICO = _1 AND LTV = _2 AND Occupancy = _3", D6, G6, F6)

Another way to provide input facts is to make the rule set directly aware of them. This is done using the function RArray(), which loads entire arrays of data into a rule set.

The RArray() function is used inside of a rule set, and creates an array of facts from a range of cells external to the rule set. The array is stored as data in the rule set.

For this example, the loan information will be read into a two-dimensional array named loans. The array will be indexed by the row and column headers on the spreadsheet, so we can refer directly to values such as loans[102, 'LTV'].

The default syntax for RArray() is:

=RArray( FactName, ExcelRange ) 

The following figure illustrates the effect of RArray():


Default RArray() Operation

Putting an RArray() function call in the ResaleRules rule set, means rules can be written using the values from the array loans. Instead of FICO being an input fact of an RQuery() for loan #104, a rule can directly refer to loans[104, FICO].

Now a rule set could have all of the resale rules written in it like this:

resell['Ace', 101] = "yes" WHEN loans[101, FICO] >= 720 AND loans[101, LTV] >= 90
resell['Ace', 102] = "yes" WHEN loans[102, FICO] >= 720 AND loans[102, LTV] >= 90
...
resell['Ace', 115] = "yes" WHEN loans[115, FICO] >= 720 AND loans[115, LTV] >= 90 

And RQuery() could ask about different loan ids:

=RQuery(ResaleRules, "FIND resell['Ace', 104]")

But there is an easier way to write these rules.

ARulesXL supports pattern-matching variables that can be used to create pattern rules that apply over fact arrays. A pattern-matching variable is indicated by a leading ? and applies for just the rule it is in.

The pattern rules below for the two investors in our example use the pattern-matching variable ?id which will have values like 101, 102, ..., 115. The rules are:

resell[Ace, ?id] = "yes" WHEN loans[?id, FICO] >= 720 AND loans[?id, LTV] >= 90
resell[Ace, ?id] = "yes" WHEN loans[?id, Occupancy] = "Owner" AND loans[?id, FICO] >= 720 AND loans[?id, LTV] >= 80
resell[Ace, ?id] = "yes" WHEN loans[?id, Occupancy] = "Owner" AND loans[?id, FICO] >= 700 AND loans[?id, LTV]] >= 90
resell[Ace, ?id] = "no"
     
resell[Acme, ?id] = "yes" WHEN loans[?id, FICO] >= 680 AND loans[?id, Rate] > 6.2 AND loans[?id, Occupancy] = "Owner"
resell[Acme, ?id] = "yes" WHEN loans[?id, FICO] >= 700 AND loans[?id, Rate] > 6.2 AND loans[?id, Occupancy] = "2nd Home"
resell[Acme, ?id] = "no" 

How does ?id get a value? It gets a value when RQuery() does a FIND. Consider this query:

=RQuery(ResellRules, "FIND resell[Acme, 103]")

The ? variables are called pattern-matching variables because the goal in the query, resell[Acme, 103], is matched against the pattern facts for resell in the rule set. Looking at the seven rules above, that query goal will first match the 5th rule by giving ?id the value 103.

If the conditions in the 5th rule aren't met using 103 for the value of ?id, then the rule engine will match the pattern with the 6th rule, just as it does for rules without pattern-matching variables.

The screen shot below shows these ideas at work with the following differences from previous examples:

 


loans_05_data_tables.xls

 


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.