Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

## [381f18]: doc / html / logic_programming / rules / index.html Maximize Restore History

### index.html    230 lines (216 with data), 11.0 kB

 ``` 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228``` ``` Rules

Hosted by:

Rules

Conceptually, a rule is very simple:

if     A     B     C then     D     E

Meaning, "if A, B and C are true, then D and E are also true". These are often called if-then rules.

And what are A, B, C, D and E?

They are simply statements. Specifically, they are generalized statements containing patterns as arguments. You'll see more about this later.

Premises and Conclusions

Rules have two parts to them: an if part (containing a list of statements called the premises), and a then part (containing a list of statements called the conclusions).

Note

Pyke doesn't use the words if and then, as you'll see shortly.

Each of these if and then parts contain one or more facts or goals which are just generalized statements (meaning statements with patterns as arguments).

Processing the Premises Within the If Clause

Because each premise with the if clause is a generalized statement, the premise is pattern matched to known facts. This means that it may match more than one fact.

Pyke tries all combinations of matching facts through a process called backtracking. This will cause the same rule to potentially succeed multiple times, once for each unique combination of matching facts.

Backtracking

Note that while processing the list of premises within a rule's if clause:

• If Pyke succeeds at proving a premise:
• Pyke will proceed down to the next premise in the list.
• Trying to proceed down from the last premise in the list (when it succeeds) causes the rule to succeed.
• If Pyke fails at proving a premise:
• Pyke will back up to the prior premise in the list and try to find another solution for it. The process starts over at the prior premise, going back down or further up the list depending on whether another solution can be found to the prior premise or not.
• Trying to back up from the first premise in the list (when it fails) causes the rule to fail.

Backtracking Flow Diagram

Thus, execution within each rule's if clause proceeds both backwards and forwards, up and down the list of premises, depending on whether each premise succeeds or fails. The process of backing up within the if clause to try alternate solutions is called backtracking.

Inferencing

Rules are specified individually within a rule base. They are not nested or explicitly linked to each other. So Pyke must automatically figure out how to combine these rules to accomplish some larger task. This is called inferencing and there are two different approaches that Pyke uses:

• All forward-chaining rules are processed when a rule base is activated.
• Backward-chaining rules are processed when your program asks Pyke to prove a specific goal (i.e., asks Pyke a question).
• These rules are designed to answer a question rather than assert new facts or activate more specific rule bases.
• They also have the ability to assemble Python functions into a customized call graph or program, called a plan, to meet a specific need.

Note

Forward-chaining and backward-chaining rules may both be included within the same rule base. Pyke knows the difference between forward-chaining and backward-chaining rules because they have different syntax.

More:

Forward Chaining

Explanation of forward-chaining rules and how forward-chaining works.

Backward Chaining

Explanation of backward-chaining rules, including how backward-chaining and backtracking works.