L 11 12 An Example of Forward Chaining

  • Slides: 18
Download presentation
L 11 -12. An Example of Forward Chaining Program in Java An Rule Base

L 11 -12. An Example of Forward Chaining Program in Java An Rule Base System in Java Rule. Base. System. java Car. Shop. txt 1

A Rule-base System Architecture class Rule. Base. System{} Rule Base Working Memory class Rule.

A Rule-base System Architecture class Rule. Base. System{} Rule Base Working Memory class Rule. Base{} class Rule{} class Working. Memory{} Interaction with Inference Engine class Matcher{} Matching Fire a Rule Select a Rule Acting 2

Rule-base System Examples rule if then "Car. Rule 1" "? x is inexpensive" "?

Rule-base System Examples rule if then "Car. Rule 1" "? x is inexpensive" "? x is made in Japan" rule if then antecedents class Working. Memory { Vector assertions; name "Car. Rule 4" "? x is big" "? x needs a lot of gas" "? x is a foreign car" Working. Memory(){ assertions = new Vector(); } consequent class Rule. Base { String file. Name; Working. Memory wm; Vector rules; Rule. Base(){ file. Name = “ex 10/Car. Shop. data"; wm = new Working. Memory(); wm. add. Assertion("my-car is inexpensive"); wm. add. Assertion("my-car is a wagon"); rules = new Vector(); load. Rules(file. Name); } public void add. Assertion(String the. Assertion){ System. out. println("ADD: "+the. Assertion); assertions. add. Element(the. Assertion); } ……. class Rule { String name; Vector antecedents; String consequent; Rule(String the. Name, Vector the. Antecedents, String the. Consequent){ this. name = the. Name; this. antecedents = the. Antecedents; this. consequent = the. Consequent; } 3

load. Rules method 193: private void load. Rules(String the. File. Name){ 194: String line;

load. Rules method 193: private void load. Rules(String the. File. Name){ 194: String line; 195: try{ 196: int token; 197: f = new File. Reader(the. File. Name); 198: st = new Stream. Tokenizer(f); 199: while((token = st. next. Token())!= Stream. Tokenizer. TT_EOF){ 200: switch(token){ 201: case Stream. Tokenizer. TT_WORD: 202: String name = null; 203: Vector antecedents = null; 204: String consequent = null; 205: if("rule". equals(st. sval)){ 206: if(st. next. Token() == '"'){ 207: name = st. sval; 208: st. next. Token(); 209: if("if". equals(st. sval)){ 210: antecedents = new Vector(); 211: st. next. Token(); 212: while(!"then". equals(st. sval)){ 213: 214: 215: antecedents. add. Element(st. sval); st. next. Token(); } 216: if("then". equals(st. sval)){ 217: st. next. Token(); 218: consequent = st. sval; 219: } 220: } 221: } 222: } 223: rules. add. Element( 224: new Rule(name, antecedents, consequent)); 225: break; 226: default: 227: System. out. println(token); 228: break; 229: } 230: } 231: } catch(Exception e){ 232: System. out. println(e); 233: } 234: for(int i = 0 ; i < rules. size() ; i++){ 235: System. out. println(((Rule)rules. element. At(i)). to. String()); 236: } 237: } 238: } 4

Rule-base System Examples (1) public class Rule. Base. System { static Rule. Base rb;

Rule-base System Examples (1) public class Rule. Base. System { static Rule. Base rb; public static void main(String args[]){ rb = new Rule. Base(); rb. forward. Chain(); } } 5

Rule-base System Examples (1) public class Rule. Base. System { static Rule. Base rb;

Rule-base System Examples (1) public class Rule. Base. System { static Rule. Base rb; public static void main(String args[]){ rb = new Rule. Base(); rb. forward. Chain(); } } 142: public void forward. Chain(){ 143: boolean new. Assertion. Created; 144: // 新しいアサーションが生成されなくなるまで続ける. 145: do { 146: new. Assertion. Created = false; 147: for(int i = 0 ; i < rules. size(); i++){ 148: Rule a. Rule = (Rule)rules. element. At(i); 149: System. out. println("apply rule: "+a. Rule. get. Name()); 150: Vector antecedents = a. Rule. get. Antecedents(); 151: String consequent = a. Rule. get. Consequent(); 152: //Hashtable bindings = wm. matching. Assertions(antecedents); 153: Vector bindings = wm. matching. Assertions(antecedents); 154: if(bindings != null){ 155: for(int j = 0 ; j < bindings. size() ; j++){ 156: //後件をインスタンシエーション 157: String new. Assertion = 158: instantiate((String)consequent, 159: (Hashtable)bindings. element. At(j)); 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: //ワーキングメモリーになければ成功 if(!wm. contains(new. Assertion)){ System. out. println("Success: "+new. Assertion); wm. add. Assertion(new. Assertion); new. Assertion. Created = true; } } System. out. println("Working Memory"+wm); } while(new. Assertion. Created); System. out. println("No rule produces a new assertion"); 172: } 6

matchable() Method public Vector matching. Assertions(Vector the. Antecedents){ Vector bindings = new Vector(); return

matchable() Method public Vector matching. Assertions(Vector the. Antecedents){ Vector bindings = new Vector(); return matchable(the. Antecedents, 0, bindings); } 40: private Vector matchable(Vector the. Antecedents, int n, Vector bindings){ 41: if(n == the. Antecedents. size()){ 42: return bindings; 43: } else if (n == 0){ 44: boolean success = false; 45: for(int i = 0 ; i < assertions. size() ; i++){ 46: Hashtable binding = new Hashtable(); 47: if((new Matcher()). matching( 48: (String)the. Antecedents. element. At(n), 49: (String)assertions. element. At(i), 50: binding)){ 51: bindings. add. Element(binding); 52: success = true; 53: } 54: } 55: if(success){ 56: return matchable(the. Antecedents, n+1, bindings); 57: } else { 58: return null; 59: } 60: } else { 61: boolean success = false; 62: Vector new. Bindings = new Vector(); 63: for(int i = 0 ; i < bindings. size() ; i++){ 64: for(int j = 0 ; j < assertions. size() ; j++){ 65: if((new Matcher()). matching( 66: (String)the. Antecedents. element. At(n), 67: (String)assertions. element. At(j), 68: (Hashtable)bindings. element. At(i))){ 69: new. Bindings. add. Element(bindings. element. At(i)); 70: success = true; 71: } 72: } 73: } 74: if(success){ 75: return matchable(the. Antecedents, n+1, new. Bindings); 76: } else { 77: return null; 78: } 79: } 7 80: }

Forward Chainingのプログラム • Ruleクラス public class Rule { //ルール名 String name; //前件部 Vector antecedents;

Forward Chainingのプログラム • Ruleクラス public class Rule { //ルール名 String name; //前件部 Vector antecedents; //後件部 String consequent; ・・・ } 8

Forward Chainingのプログラム • Working Memoryクラス public class Working. Memory { Vector assertions;     /**

Forward Chainingのプログラム • Working Memoryクラス public class Working. Memory { Vector assertions;     /** * マッチするアサーションに対するバインディング情報を返す (再帰的) * * @param 前件を示す Vector * @return バインディング情報が入っている Vector */ public Vector matching. Assertions(Vector the. Antecedents) { Vector bindings = new Vector(); return matchable(the. Antecedents, 0, bindings); } ・・・・・・ /** * アサーションをワーキングメモリに加える. * * @param アサーションを表す String */ public void add. Assertion(String the. Assertion) { System. out. println("ADD: " + the. Assertion); assertions. add. Element(the. Assertion); } ・・・・・・ } 9

Forward Chainingのプログラム • Rule Baseクラス public class Rule. Base { String file. Name; File.

Forward Chainingのプログラム • Rule Baseクラス public class Rule. Base { String file. Name; File. Reader f; Stream. Tokenizer st; Working. Memory wm; Vector rules; //コンストラクタ Rule. Base() { } //前向き推論を行うためのメソッド public void forward. Chain() {   } //変数を具体化する為のメソッド private String instantiate(String the. Pattern, Hashtable the. Bindings) {  } //引数として与えられた文字列が変数かどうかを判定するメソッド private boolean var(String str 1) {  } //ファイルからルールをロードする(読み込む)為のメソッド private void load. Rules(String the. File. Name) { } } 10

Forward Chainingのプログラム • Rule Baseクラス – Rule Base() public class Rule. Base { …

Forward Chainingのプログラム • Rule Baseクラス – Rule Base() public class Rule. Base { … Working. Memory wm; ワーキングメモリに初期事 実を挿入している … Rule. Base() { … wm = new Working. Memory(); wm. add. Assertion("my-car is inexpensive"); wm. add. Assertion("my-car has a VTEC engine"); wm. add. Assertion("my-car is stylish"); wm. add. Assertion("my-car has several color models"); wm. add. Assertion("my-car has several seats"); wm. add. Assertion("my-car is a wagon"); … } } 11

Forward Chainingのプログラム • Rule Baseクラス - forward. Chain() //前向き推論を行うためのメソッド public void forward. Chain() {

Forward Chainingのプログラム • Rule Baseクラス - forward. Chain() //前向き推論を行うためのメソッド public void forward. Chain() { boolean new. Assertion. Created; // 新しいアサーションが生成されなくなるまで続ける. do { new. Assertion. Created = false; for (int i = 0; i < rules. size(); i++) { Rule a. Rule = (Rule) rules. element. At(i); ルールベースからルール System. out. println("apply rule: " + a. Rule. get. Name()); を一つずつ取り出し、ワー Vector antecedents = a. Rule. get. Antecedents(); キングメモリ中の「事実」と String consequent = a. Rule. get. Consequent(); マッチングする Vector bindings = wm. matching. Assertions(antecedents); if (bindings != null) { マッチングが成功し、変数 for (int j = 0; j < bindings. size(); j++) { 束縛が生じれば、変数の //後件をインスタンシエーション String new. Assertion = instantiate((String) consequent, (Hashtable) bindings. element. At(j)); 具体化を行う //ワーキングメモリーになければ成功 if (!wm. contains(new. Assertion)) { System. out. println("Success: " + new. Assertion); wm. add. Assertion(new. Assertion); new. Assertion. Created = true; } } System. out. println("Working Memory" + wm); } while (new. Assertion. Created); System. out. println(“No rule produces a new assertion”);    } 12

rule    "Car. Rule 1" if      "? x is inexpensive" then    "? x is made

rule    "Car. Rule 1" if      "? x is inexpensive" then    "? x is made in Japan" Rule   "Car. Rule 2" if      "? x is small" then    "? x is made in Japan" rule     "Car. Rule 3" If      "? x is expensive" then     "? x is a foreign car" rule     "Car. Rule 4" if      "? x is big"        "? x needs a lot of gas" then    "? x is a foreign car" rule    "Car. Rule 7" if      "? x is made in Japan"      "? x has Honda's logo" then    "? x is a Honda" rule    "Car. Rule 8" if      "? x is made in Japan"       "? x has a VTEC engine" then    "? x is a Honda" rule    "Car. Rule 9" if      "? x is a Toyota"       "? x has several seats"       "? x is a wagon" then   "? x is a Carolla Wagon" rule    "Car. Rule 10" Rule    "Car. Rule 5" if      "? x is a Toyota" If      "? x is made in Japan"       "? x has several seats" "? x has Toyota's logo"       "? x is a hybrid car" then     "? x is a Toyota" then   "? x is a Prius" rule     "Car. Rule 6" if       "? x is made in Japan"        "? x is a popular car" Then    "? x is a Toyota" rule    "Car. Rule 11" if      "? x is a Honda"       "? x is stylish"       "? x has several color models"       "? x has several seats"       "? x is a wagon" then   "? x is an Accord Wagon" rule "Car. Rule 12" if "? x is a Honda"      "? x has an aluminium body" "? x has only 2 seats" then "? x is a NSX" rule if "Car. Rule 13" "? x is a foreign car" "? x is a sports car" "? x is stylish"      "? x has several color models" "? x has a big engine" then  "? x is a Lamborghini Countach" rule if then "Car. Rule 14" "? x is a foreign car" "? x is a sports car" "? x is red" "? x has a big engine" "? x is a Ferrari F 50" "Car. Rule 15" "? x is a foreign car" "? x is a good face" "? x is a Jaguar XJ 8" 13

Output: % java Rule. Base. System ADD: my-car is inexpensive ADD: my-car has a

Output: % java Rule. Base. System ADD: my-car is inexpensive ADD: my-car has a VTEC engine ADD: my-car is stylish ADD: my-car has several color models ADD: my-car has several seats ADD: my-car is a wagon Car. Rule 1 [? x is inexpensive]->? x is made in Japan Car. Rule 2 [? x is small]->? x is made in Japan Car. Rule 3 [? x is expensive]->? x is a foreign car Car. Rule 4 [? x is big, ? x needs a lot of gas]->? x is a foreign car Car. Rule 5 [? x is made in Japan, ? x has Toyota's logo]->? x is a Toyota Car. Rule 6 [? x is made in Japan, ? x is a popular car]->? x is a Toyota Car. Rule 7 [? x is made in Japan, ? x has Honda's logo]->? x is a Honda Car. Rule 8 [? x is made in Japan, ? x has a VTEC engine]->? x is a Honda Car. Rule 9 [? x is a Toyota, ? x has several seats, ? x is a wagon]->? x is a Carolla Wagon Car. Rule 10 [? x is a Toyota, ? x has several seats, ? x is a hybrid car]->? x is a Prius Car. Rule 11 [? x is a Honda, ? x is stylish, ? x has several color models, ? x has several seats, ? x is a wagon]->? x is an Accord Wagon Car. Rule 12 [? x is a Honda, ? x has an aluminium body, ? x has only 2 seats]->? x is a NSX Car. Rule 13 [? x is a foreign car, ? x is a sports car, ? x is stylish, ? x has several color models, ? x has a big engine]->? x is a Lamborghini Countach Car. Rule 14 [? x is a foreign car, ? x is a sports car, ? x is red, ? x has a big engine]->? x is a Ferrari F 50 Car. Rule 15 [? x is a foreign car, ? x is a good face]->? x is a Jaguar XJ 8 apply rule: Car. Rule 1 Success: my-car is made in Japan my-car is inexpensive A new fact added to the ADD: my-car is made in Japan working memory apply rule: Car. Rule 2 apply rule: Car. Rule 3 rule    "Car. Rule 1" apply rule: Car. Rule 4 my-car is made in Japan if      "? x is inexpensive" apply rule: Car. Rule 5 then    "? x is made in Japan" 14 apply rule: Car. Rule 6 apply rule: Car. Rule 7 Initial facts in the working memory

apply rule: Car. Rule 8 my-car is made in Japan my-car is inexpensive Success:

apply rule: Car. Rule 8 my-car is made in Japan my-car is inexpensive Success: my-car is a Honda ADD: my-car is a Honda apply rule: Car. Rule 9 rule "Car. Rule 8" apply rule: Car. Rule 10 if "? x is made in Japan" apply rule: Car. Rule 11 "? x has a VTEC engine" Success: my-car is an Accord Wagon then "? x is a Honda" ADD: my-car is an Accord Wagon apply rule: Car. Rule 12 A new fact added to apply rule: Car. Rule 13 my-car is a Honda the working memory apply rule: Car. Rule 14 apply rule: Car. Rule 15 Working Memory[my-car is inexpensive, my-car has a VTEC engine, my-car is stylish, my-car has several color models, my-car has several seats, my-car is a wagon, my-car is made in Japan, my-car is a Honda, my-car is an Accord Wagon] apply rule: Car. Rule 1 my-car is stylish apply rule: Car. Rule 2 my-car has several color models apply rule: Car. Rule 3 my-car has several seats rule “Car. Rule 11" apply rule: Car. Rule 4 my-car is wagon if "? x is a Honda" apply rule: Car. Rule 5 "? x is stylish“ apply rule: Car. Rule 6 "? x has several color models" apply rule: Car. Rule 7 "? x has several seats" apply rule: Car. Rule 8 "? x is a wagon" apply rule: Car. Rule 9 then "? x is an Accord Wagon" apply rule: Car. Rule 10 apply rule: Car. Rule 11 apply rule: Car. Rule 12 A new fact added to apply rule: Car. Rule 13 my-car is an Accord Wagon the working memory apply rule: Car. Rule 14 apply rule: Car. Rule 15 Working Memory[my-car is inexpensive, my-car has a VTEC engine, my-car is stylish, my-car has several color models, my-car has several seats, my-car is a wagon, my-car is made in Japan, my-car is a Honda, my-car is an Accord Wagon] 15 No rule produces a new assertion

Forward Chaining 16

Forward Chaining 16

Exercises: 1. Input Rule. Base. System. java, understand it, run the program, and check

Exercises: 1. Input Rule. Base. System. java, understand it, run the program, and check the output. 2. Make some modifications to Rule. Base. System. java ( data file name: Outruns. data, rules in the data file, and the initial content in the working memory) so that the program can run for the following case. Bob is a buffalo | 1. Buffalo(Bob) Pat is a pig | 2. Pig(Pat) Buffaloes outrun pigs | 3. x, y Buffalo(x) Pig(y) Faster(x, y) --------------------------------------------------Bob outruns Pat -------------------------------------------------Apply (3) to 1 And 2 | 4. Buffalo(Bob) Pig(Pat) Apply (8) to 3 {x/Bob, y/Pat} | 5. Buffalo(Bob) Pig(Pat) Faster(Bob, Pat) Apply (1) to 4 And 5 | 6. Faster(Bob, Pat) 17

Output: 18

Output: 18