001 package aima.basic; 002 003 import java.util.ArrayList; 004 import java.util.List; 005 006 /** 007 * Artificial Intelligence A Modern Approach (2nd Edition): page 32. 008 * 009 * Environment. 010 */ 011 012 /** 013 * @author Ravi Mohan 014 * 015 */ 016 public abstract class Environment { 017 018 protected ArrayList<EnvironmentObject> objects; 019 020 protected ArrayList<Agent> agents; 021 022 protected ArrayList<BasicEnvironmentView> views; 023 024 public abstract void executeAction(Agent a, String act); 025 026 public abstract Percept getPerceptSeenBy(Agent anAgent); 027 028 protected Environment() { 029 agents = new ArrayList<Agent>(); 030 objects = new ArrayList<EnvironmentObject>(); 031 views = new ArrayList<BasicEnvironmentView>(); 032 } 033 034 public void registerView(BasicEnvironmentView bev) { 035 views.add(bev); 036 } 037 038 public void updateViews(String command) { 039 040 for (BasicEnvironmentView view : views) { 041 view.envChanged(command); 042 } 043 } 044 045 public boolean isDone() { 046 047 for (Agent agent : agents) { 048 if (agent.isAlive()) { 049 return false; 050 } 051 } 052 053 return true; 054 } 055 056 public void createExogenousChange() { 057 058 } 059 060 public void step() { 061 if (!(this.isDone())) { 062 063 for (Agent agent : agents) { 064 065 String anAction = agent.execute(this.getPerceptSeenBy(agent)); 066 067 updateViews(anAction); 068 this.executeAction(agent, anAction); 069 } 070 this.createExogenousChange(); 071 } 072 } 073 074 public void step(int n) { 075 076 for (int i = 0; i < n; i++) { 077 078 step(); 079 080 } 081 } 082 083 public void stepUntilDone() { 084 while (!(this.isDone())) { 085 step(); 086 } 087 } 088 089 public ArrayList getAgents() { 090 return agents; 091 } 092 093 public ArrayList getObjects() { 094 return objects; 095 } 096 097 public boolean alreadyContains(EnvironmentObject o) { 098 099 boolean retval = false; 100 101 for (EnvironmentObject eo : objects) { 102 if (eo.equals(o)) { 103 retval = true; 104 } 105 } 106 107 return retval; 108 } 109 110 public boolean alreadyContains(Agent anAgent) { 111 boolean retval = false; 112 for (Agent agent : agents) { 113 if (agent.equals(anAgent)) { 114 retval = true; 115 } 116 } 117 return retval; 118 } 119 120 public void addAgent(Agent a, String attributeName, Object attributeValue) { 121 if (!(alreadyContains(a))) { 122 a.setAttribute(attributeName, attributeValue); 123 agents.add(a); 124 } 125 } 126 127 public void addObject(EnvironmentObject o, String attributeName, 128 Object attributeValue) { 129 if (!(alreadyContains(o))) { 130 o.setAttribute(attributeName, attributeValue); 131 objects.add(o); 132 } 133 } 134 135 public void addObject(EnvironmentObject o) { 136 if (!(alreadyContains(o))) { 137 objects.add(o); 138 } 139 } 140 141 public void addAgent(Agent a) { 142 if (!(alreadyContains(a))) { 143 agents.add(a); 144 } 145 } 146 147 public List<ObjectWithDynamicAttributes> getAllObjects() { 148 List<ObjectWithDynamicAttributes> l = new ArrayList<ObjectWithDynamicAttributes>(); 149 l.addAll(objects); 150 l.addAll(agents); 151 return l; 152 } 153 154 }