Presentation is loading. Please wait.

Presentation is loading. Please wait.

L10. Unify Program in Java Unify.java.

Similar presentations


Presentation on theme: "L10. Unify Program in Java Unify.java."— Presentation transcript:

1 L10. Unify Program in Java Unify.java

2 String matching: string1 = string2
e.g. “rose” = “rose” if string1.equals(string2) “I am Rose” = “I am Rose” “I am ?x” = “I am Rose” “I am ?x” = “?y am Rose” I = ?y am = am ?x = Rose ? e.g. ?x is ?y and ?x Rose is rose and ?y ?x = Rose ?y = rose ?x = ?y

3 Output Output from Matching.java ?x is ?y and ?x Rose is rose and ?y
false Output from Unify.java {?y=rose, ?x=Rose} true

4 ? ? Token matching: token1 = token2 Three pairs of tokens’ matching:
e.g. two strings’ matching, “I am ?x” = “?y am Rose” Three pairs of tokens’ matching: I = ?y am = am ?x = Rose 64: boolean tokenMatching(String token1,String token2){ 65: if(token1.equals(token2)) return true; 66: if( var(token1) && !var(token2)) 67: return varMatching(token1,token2); 68: if(!var(token1) && var(token2)) 69: return varMatching(token2,token1); 70: return false; 71: } 73: boolean varMatching(String vartoken,String token){ 74: if(vars.containsKey(vartoken)){ 75: if(token.equals(vars.get(vartoken))){ 76: return true; 77: } else { 78: return false; 79: } 80: } else { 81: vars.put(vartoken,token); 82: } 83: return true; 84: } ? ? 86: boolean var(String str1){ 87: // 先頭が ? なら変数 88: return str1.startsWith("?"); 89: } 27: Matcher(){ 28: vars = new Hashtable(); 29: }

5 Exercises Understand the program (Unify.java) and compare the execution results from Matching.java and Unify.java. Answer the following questions: (1) What does the method, replaceBuffer(), do? (2) What does the method, replaceBindings(), do? 3. Modify the program (Unify.java) so that the program (Unify1.java) can do the following matching as well . e.g. ?x is ?y and ?x Rose is rose and ?y ?x is ?y and ?x name(?x) Rose is rose and ?y name(Rose)


Download ppt "L10. Unify Program in Java Unify.java."

Similar presentations


Ads by Google