L An Example of Forward Chaining Program in Java

Slides:



Advertisements
Similar presentations
Craps. /* * file : Craps.java * file : Craps.java * author: george j. grevera, ph.d. * author: george j. grevera, ph.d. * desc. : program to simulate.
Advertisements

1 L11. An Example of Backward Chaining Program in Java An Rule Base System in Java RuleBaseSystem.java CarShop.data.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Introduction to Object-Oriented Programming CS 21a: Introduction to Computing I First Semester,
For Loops & Iterators CSC 171 FALL 2001 LECTURE 7.
StringBuffer class  Alternative to String class  Can be used wherever a string is used  More flexible than String  Has three constructors and more.
Scanner class for input Instantiate a new scanner object Scanner in = new Scanner(System.in); Getting input using scanner – int i = scanner.nextInt() –
Writing a Class (defining a data-type). Create a new project : Project (uncheck the “Create Main Class”)
Sadegh Aliakbary Sharif University of Technology Fall 2012.
COMP More About Classes Yi Hong May 22, 2015.
Sadegh Aliakbary Sharif University of Technology Spring 2011.
L6: Searching Program in Java Breadth-first search vs depth-first search.
Classes, Objects, Arrays, Collections and Autoboxing Dr. Andrew Wallace PhD BEng(hons) EurIng
By Nicholas Policelli An Introduction to Java. Basic Program Structure public class ClassName { public static void main(String[] args) { program statements.
Chapter 7 Recursion Recursive methods Recursion in two- dimensional grids Recursive helper method Analysis of recursive algorithms.
L5: Searching Program in Java Breadth-first search vs depth-first search.
BUILDING JAVA PROGRAMS CHAPTER 2 Days of For Loops Past.
Regular Expressions.
Peyman Dodangeh Sharif University of Technology Spring 2014.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
OOP in Java : © W. Milner 2005 : Slide 1 Java and OOP Part 2 – Classes and objects.
1 Lesson: Applets with User Input and Output with GUI ICS4M.
1 Unification ( 統一 ) Forward Chaining ( 前向き仮説推理 ) Backward Chaining ( 後ろ向き仮説推理 ) Unification ( 統一 ) Forward Chaining ( 前向き仮説推理 ) Backward Chaining ( 後ろ向き仮説推理.
CSC1401 Classes - 2. Learning Goals Computing concepts Adding a method To show the pictures in the slide show Creating accessors and modifiers That protect.
Mutual Recursion A set of methods calls each other cooperatively and repeatedly Expression is a term, or sum or difference of terms Term is a factor, or.
1 Unification ( 統一 ) Forward Chaining ( 前向き推論 ) Backward Chaining ( 後ろ向き推論 ) Unification ( 統一 ) Forward Chaining ( 前向き推論 ) Backward Chaining ( 後ろ向き推論 )
Stacks Ellen Walker CPSC 201 Data Structures Hiram College.
More loops while and do-while. Recall the for loop in general for (initialization; boolean_expression; update) { }
CreatingClasses-SlideShow-part31 Creating Classes part 3 Barb Ericson Georgia Institute of Technology Dec 2009.
1 Forward Chaining based rule base system. Reasoning with Rules  Knowledge represented as if-then rules –Natural –Easy to understand –Each rule as a.
Files Review For output to a file: –FileOutputStream variable initialized to filename (String) and append/not append (boolean) –PrintWriter variable initialized.
1 L Term project (期末課題) A recipe recommendation system (レシピお勧めシステム) ( 満点: 30 点 ) RecipeRecommendation.java recipes.data materialsWm.data.
String str1 = JOptionPane.showInputDialog(“enter a string”); String str1 = JOptionPane.showInputDialog(“enter a string”); String str2 = JOptionPane.showInputDialog(“enter.
Java: Variables and Methods By Joshua Li Created for the allAboutJavaClasses wikispace.
A Introduction to Computing II Lecture 1: Java Review Fall Session 2000.
WAP to find out the number is prime or not Import java.util.*; Class Prime { public static void main(string args[]) { int n,I,res; boolean flag=true;
Methods. Creating your own methods Java allows you to create custom methods inside its main body. public class Test { // insert your own methods right.
Programming in Java Transitioning from Alice. Becomes not myFirstMethod but …. public static void main (String[] arg) { // code for testing classes goes.
Comp1004: Introduction III Java. Content How Java Works: The JVM Writing a Class in Java – Class – Member Variables – Method – Statement Magic incantations.
L3. Knowledge Representation and Matching in Java Matching.java Unify.java.
Session Three Review & Conditional Control Flow. Java File Hierarchy Projects Packages Classes Methods.
Information and Computer Sciences University of Hawaii, Manoa
GC211 Data structure Lecture 3 Sara Alhajjam.
CSC111 Quick Revision.
Chapter No. : 1 Introduction to Java.
10. A rule based system Forward Chaining based rule base system.
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Advanced Programming in Java
CS Week 8 Jim Williams, PhD.
Chapter 4 Unordered List.
L An Example of Forward Chaining Program in Java
null, true, and false are also reserved.
An Introduction to Java – Part I, language basics
CS 302 Week 9 Jim Williams.
Assignment 7 User Defined Classes Part 2
L9. Matching Program in Java
class PrintOnetoTen { public static void main(String args[]) {
Goals Understand how to create and compare Strings.
Unification Unification (Unify.java) Doing exercises.
Module 2 - Part 1 Variables, Assignment, and Data Types
Introduction to Object-Oriented Programming
Building Java Programs
L9. Matching Program in Java
Loops CGS3416 Spring 2019 Lecture 7.
Objects with ArrayLists as Attributes
Methods/Functions.
Control Structure.
L10. Unify Program in Java Unify.java.
Exceptions and Exception Handling
Comparing objects booleans &&, ||and !
Presentation transcript:

L11-12. 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 Inference Engine Matching class Matcher{} Fire a Rule Select a Rule Acting

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); }

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:}

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

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: }

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: }

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

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); }

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) { } }

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"); } ワーキングメモリに初期事実を挿入している

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”);   ルールベースからルールを一つずつ取り出し、ワーキングメモリ中の「事実」とマッチングする マッチングが成功し、変数束縛が生じれば、変数の具体化を行う

rule    "CarRule1" if      "?x is inexpensive" then    "?x is made in Japan" Rule   "CarRule2" if      "?x is small" 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"       "?x has a VTEC engine" rule    "CarRule9" if      "?x is a Toyota"       "?x has several seats"       "?x is a wagon" then   "?x is a Carolla Wagon" rule    "CarRule10"       "?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" 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 red" then "?x is a Ferrari F50" rule "CarRule15" "?x is a good face" then "?x is a Jaguar XJ8"

Output: Initial facts in the working memory % 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

A new fact added to the working memory 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 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

Forward Chaining

Exercises: 1. Input RuleBaseSystem 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)

Output: