1 / 18

L10-11. An Example of Forward Chaining Program in Java

L10-11. An Example of Forward Chaining Program in Java. An Rule Base System in Java. RuleBaseSystem.java. CarShop.txt. A Rule-base System Architecture. class RuleBaseSystem{}. Rule Base. Working Memory. class RuleBase{}. class WorkingMemory{}. class Rule{}. Interaction with.

Download Presentation

L10-11. An Example of Forward Chaining Program in Java

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. L10-11. An Example of Forward Chaining Program in Java An Rule Base System in Java RuleBaseSystem.java CarShop.txt

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

  3. Rule-base System Examples rule "CarRule1" if "?x is inexpensive" then "?x is made in Japan" antecedents class WorkingMemory { Vector assertions; WorkingMemory(){ assertions = new Vector(); } ……. name rule "CarRule4" if "?x is big" "?x needs a lot of gas" then "?x is a foreign car" consequent class RuleBase { String fileName; WorkingMemory wm; Vector rules; RuleBase(){ fileName = “ex10/CarShop.data"; wm = new WorkingMemory(); wm.addAssertion("my-car is inexpensive"); wm.addAssertion("my-car is a wagon"); rules = new Vector(); loadRules(fileName); } class Rule { String name; Vector antecedents; String consequent; Rule(String theName,Vector theAntecedents, String theConsequent){ this.name = theName; this.antecedents = theAntecedents; this.consequent = theConsequent; } public void addAssertion(String theAssertion){ System.out.println("ADD:"+theAssertion); assertions.addElement(theAssertion); }

  4. loadRules method 193: private void loadRules(String theFileName){194: String line;195: try{196: int token;197: f = new FileReader(theFileName);198: st = new StreamTokenizer(f);199: while((token = st.nextToken())!= StreamTokenizer.TT_EOF){200: switch(token){201: case StreamTokenizer.TT_WORD:202: String name = null;203: Vector antecedents = null;204: String consequent = null;205: if("rule".equals(st.sval)){206: if(st.nextToken() == '"'){207: name = st.sval;208: st.nextToken();209: if("if".equals(st.sval)){210: antecedents = new Vector();211: st.nextToken();212: while(!"then".equals(st.sval)){ 213: antecedents.addElement(st.sval);214: st.nextToken();215: } 216: if("then".equals(st.sval)){217: st.nextToken();218: consequent = st.sval;219: }220: }221: } 222: }223: rules.addElement(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.elementAt(i)).toString());236: }237: }238:}

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

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

  7. matchable() Method public Vector matchingAssertions(Vector theAntecedents){ Vector bindings = new Vector(); return matchable(theAntecedents,0,bindings); } 40: private Vector matchable(Vector theAntecedents,int n, Vector bindings){ 41: if(n == theAntecedents.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)theAntecedents.elementAt(n), 49: (String)assertions.elementAt(i), 50: binding)){ 51: bindings.addElement(binding); 52: success = true; 53: } 54: } 55: if(success){ 56: return matchable(theAntecedents, n+1, bindings); 57: } else { 58: return null; 59: } 60: } else { 61: boolean success = false; 62: Vector newBindings = 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)theAntecedents.elementAt(n), 67: (String)assertions.elementAt(j), 68: (Hashtable)bindings.elementAt(i))){ 69: newBindings.addElement(bindings.elementAt(i)); 70: success = true; 71: } 72: } 73: } 74: if(success){ 75: return matchable(theAntecedents,n+1,newBindings); 76: } else { 77: return null; 78: } 79: } 80: }

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

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

  10. Forward Chainingのプログラム • Rule Baseクラス public class RuleBase { String fileName; FileReader f; StreamTokenizer st; WorkingMemory wm; Vector rules; //コンストラクタ RuleBase() { } //前向き推論を行うためのメソッド public void forwardChain() {} //変数を具体化する為のメソッド private String instantiate(String thePattern, Hashtable theBindings) {} //引数として与えられた文字列が変数かどうかを判定するメソッド private boolean var(String str1) {} //ファイルからルールをロードする(読み込む)為のメソッド private void loadRules(String theFileName) { } }

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

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

  13. rule    "CarRule1" if      "?x is inexpensive" then    "?x is made in Japan" Rule   "CarRule2" if      "?x is small" then    "?x is made in Japan" rule     "CarRule3" If      "?x is expensive" then     "?x is a foreign car" rule     "CarRule4" if      "?x is big"        "?x needs a lot of gas" then    "?x is a foreign car" Rule    "CarRule5" If      "?x is made in Japan"        "?x has Toyota's logo" then     "?x is a Toyota" rule     "CarRule6" if       "?x is made in Japan"        "?x is a popular car" Then    "?x is a Toyota" rule    "CarRule7" if      "?x is made in Japan"      "?x has Honda's logo" then    "?x is a Honda" rule    "CarRule8" if      "?x is made in Japan"       "?x has a VTEC engine" then    "?x is a Honda" rule    "CarRule9" if      "?x is a Toyota"       "?x has several seats"       "?x is a wagon" then   "?x is a Carolla Wagon" rule    "CarRule10" if      "?x is a Toyota"       "?x has several seats"       "?x is a hybrid car" then   "?x is a Prius" rule    "CarRule11" 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 "CarRule12" if "?x is a Honda"      "?x has an aluminium body" "?x has only 2 seats" then "?x is a NSX" rule "CarRule13" if "?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 "CarRule14" if "?x is a foreign car" "?x is a sports car" "?x is red" "?x has a big engine" then "?x is a Ferrari F50" rule "CarRule15" if "?x is a foreign car" "?x is a good face" then "?x is a Jaguar XJ8"

  14. Output: % java RuleBaseSystem % java RuleBaseSystem 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 CarRule1 [?x is inexpensive]->?x is made in Japan CarRule2 [?x is small]->?x is made in Japan CarRule3 [?x is expensive]->?x is a foreign car CarRule4 [?x is big, ?x needs a lot of gas]->?x is a foreign car CarRule5 [?x is made in Japan, ?x has Toyota's logo]->?x is a Toyota CarRule6 [?x is made in Japan, ?x is a popular car]->?x is a Toyota CarRule7 [?x is made in Japan, ?x has Honda's logo]->?x is a Honda CarRule8 [?x is made in Japan, ?x has a VTEC engine]->?x is a Honda CarRule9 [?x is a Toyota, ?x has several seats, ?x is a wagon]->?x is a Carolla Wagon CarRule10 [?x is a Toyota, ?x has several seats, ?x is a hybrid car]->?x is a Prius CarRule11 [?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 CarRule12 [?x is a Honda, ?x has an aluminium body, ?x has only 2 seats]->?x is a NSX CarRule13 [?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 CarRule14 [?x is a foreign car, ?x is a sports car, ?x is red, ?x has a big engine]->?x is a Ferrari F50 CarRule15 [?x is a foreign car, ?x is a good face]->?x is a Jaguar XJ8 apply rule:CarRule1 Success: my-car is made in Japan ADD:my-car is made in Japan apply rule:CarRule2 apply rule:CarRule3 apply rule:CarRule4 apply rule:CarRule5 apply rule:CarRule6 apply rule:CarRule7 Initial facts in the working memory my-car is inexpensive A new fact added to the working memory rule    "CarRule1" if      "?x is inexpensive" then    "?x is made in Japan" my-car is made in Japan

  15. apply rule:CarRule8 Success: my-car is a Honda ADD:my-car is a Honda apply rule:CarRule9 apply rule:CarRule10 apply rule:CarRule11 Success: my-car is an Accord Wagon ADD:my-car is an Accord Wagon apply rule:CarRule12 apply rule:CarRule13 apply rule:CarRule14 apply rule:CarRule15 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:CarRule1 apply rule:CarRule2 apply rule:CarRule3 apply rule:CarRule4 apply rule:CarRule5 apply rule:CarRule6 apply rule:CarRule7 apply rule:CarRule8 apply rule:CarRule9 apply rule:CarRule10 apply rule:CarRule11 apply rule:CarRule12 apply rule:CarRule13 apply rule:CarRule14 apply rule:CarRule15 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] No rule produces a new assertion my-car is made in Japan my-car is inexpensive rule "CarRule8" if "?x is made in Japan" "?x has a VTEC engine" then "?x is a Honda" A new fact added to the working memory my-car is a Honda my-car is stylish my-car has several color models my-car has several seats my-car is wagon rule “CarRule11" 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" A new fact added to the working memory my-car is an Accord Wagon

  16. Forward Chaining

  17. Exercises:1. Input RuleBaseSystem.java, understand it, run the program, and check the output. 2. Make some modifications to RuleBaseSystem.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)

  18. Output:

More Related