001 package aima.basic.vaccum; 002 003 import java.util.LinkedHashSet; 004 import java.util.Set; 005 006 import aima.basic.Agent; 007 import aima.basic.ObjectWithDynamicAttributes; 008 import aima.basic.Percept; 009 import aima.basic.simplerule.ANDCondition; 010 import aima.basic.simplerule.EQUALCondition; 011 import aima.basic.simplerule.Rule; 012 013 /** 014 * An example of using the aima.basic.ReflexAgentWithStateProgram. 015 */ 016 017 /** 018 * @author Ciaran O'Reilly 019 * 020 */ 021 public class ReflexVaccumAgentWithState extends Agent { 022 023 public ReflexVaccumAgentWithState() { 024 super(new ReflexAgentWithStateProgram() { 025 @Override 026 protected void init() { 027 setState(new ObjectWithDynamicAttributes()); 028 setRules(getRuleSet()); 029 } 030 031 @Override 032 protected ObjectWithDynamicAttributes updateState( 033 ObjectWithDynamicAttributes envState, String anAction, 034 Percept percept) { 035 envState.setAttribute("currentLocation", percept 036 .getAttribute("location")); 037 envState.setAttribute("currentStatus", percept 038 .getAttribute("status")); 039 // Keep track of the status of the different locations 040 envState.setAttribute((new StringBuffer("statusLocation") 041 .append(percept.getAttribute("location"))).toString(), 042 percept.getAttribute("status")); 043 044 return envState; 045 } 046 }); 047 } 048 049 // 050 // PRIVATE METHODS 051 // 052 private static Set<Rule> getRuleSet() { 053 // Note: Using a LinkedHashSet so that the iteration order (i.e. implied 054 // precedence) of rules can be guaranteed. 055 Set<Rule> rules = new LinkedHashSet<Rule>(); 056 057 rules 058 .add(new Rule(new ANDCondition(new EQUALCondition( 059 "statusLocationA", "Clean"), new EQUALCondition( 060 "statusLocationB", "Clean")), 061 Agent.NO_OP)); 062 rules 063 .add(new Rule(new EQUALCondition("currentStatus", "Dirty"), 064 "Suck")); 065 rules 066 .add(new Rule(new EQUALCondition("currentLocation", "A"), 067 "Right")); 068 rules.add(new Rule(new EQUALCondition("currentLocation", "B"), "Left")); 069 070 return rules; 071 } 072 }