001 package aima.basic.simplerule; 002 003 import aima.basic.ObjectWithDynamicAttributes; 004 005 /** 006 * A simple implementation of a "condition-action rule". 007 * 008 */ 009 010 /** 011 * @author Ciaran O'Reilly 012 * 013 */ 014 public class Rule { 015 private Condition con; 016 017 private String action; 018 019 public Rule(Condition aCon, String anAction) { 020 assert (null != aCon); 021 assert (null != anAction); 022 023 con = aCon; 024 action = anAction; 025 } 026 027 public boolean evaluate(ObjectWithDynamicAttributes p) { 028 return (con.evaluate(p)); 029 } 030 031 public String getAction() { 032 return action; 033 } 034 035 @Override 036 public boolean equals(Object o) { 037 if (o == null || !(o instanceof Rule)) { 038 return super.equals(o); 039 } 040 return (toString().equals(((Rule) o).toString())); 041 } 042 043 @Override 044 public int hashCode() { 045 return toString().hashCode(); 046 } 047 048 @Override 049 public String toString() { 050 StringBuffer sb = new StringBuffer(); 051 052 return sb.append("if ").append(con).append(" then ").append(action) 053 .append(".").toString(); 054 } 055 }