1- package adf_core_python .agent ;
1+ package adf_core_python .core . agent ;
22
3- import adf .core .agent .communication .MessageManager ;
3+ import adf .core .agent .communication .standard .bundle .StandardMessageBundle ;
4+ import adf .core .agent .develop .DevelopData ;
45import adf .core .agent .info .ScenarioInfo ;
56import adf .core .agent .info .WorldInfo ;
6- import adf_core_python .agent .config .ModuleConfig ;
7- import adf_core_python .agent .develop .DevelopData ;
8- import adf_core_python .agent .info .AgentInfo ;
9- import adf_core_python .agent .module .ModuleManager ;
10- import adf_core_python .agent .precompute .PrecomputeData ;
11- import adf_core_python .component .module .AbstractModule ;
12- import adf_core_python .gateway .mapper .AbstractMapper ;
13- import adf_core_python .gateway .mapper .MapperDict ;
7+ import adf .core .launcher .ConsoleOutput ;
8+ import adf_core_python .core .agent .communication .MessageManager ;
9+ import adf_core_python .core .agent .communication .standard .StandardCommunicationModule ;
10+ import adf_core_python .core .agent .config .ModuleConfig ;
11+ import adf_core_python .core .agent .info .AgentInfo ;
12+ import adf_core_python .core .agent .module .ModuleManager ;
13+ import adf_core_python .core .agent .precompute .PrecomputeData ;
14+ import adf_core_python .core .component .communication .CommunicationModule ;
15+ import adf_core_python .core .component .module .AbstractModule ;
16+ import adf_core_python .core .gateway .Coordinator ;
17+ import adf_core_python .core .gateway .mapper .AbstractMapper ;
18+ import adf_core_python .core .gateway .mapper .MapperDict ;
1419import jakarta .annotation .Nonnull ;
1520import jakarta .annotation .Nullable ;
1621import org .apache .logging .log4j .LogManager ;
1722import org .apache .logging .log4j .Logger ;
1823import rescuecore2 .config .Config ;
1924import rescuecore2 .messages .Command ;
25+ import rescuecore2 .messages .Message ;
2026import rescuecore2 .standard .entities .StandardEntityURN ;
2127import rescuecore2 .standard .entities .StandardWorldModel ;
2228import rescuecore2 .worldmodel .ChangeSet ;
2531
2632import java .lang .reflect .Constructor ;
2733import java .lang .reflect .InvocationTargetException ;
34+ import java .util .Arrays ;
2835import java .util .Collection ;
2936import java .util .HashMap ;
3037import java .util .Objects ;
3138
3239public class Agent {
33- private final AgentInfo agentInfo ;
34- private final WorldInfo worldInfo ;
35- private final ScenarioInfo scenarioInfo ;
40+ public final AgentInfo agentInfo ;
41+ public final WorldInfo worldInfo ;
42+ public final ScenarioInfo scenarioInfo ;
3643 private final ModuleManager moduleManager ;
3744 private final DevelopData developData ;
3845 private final PrecomputeData precomputeData ;
3946 private final MessageManager messageManager ;
4047 private final HashMap <String , AbstractMapper > modules = new HashMap <>();
4148 private final MapperDict mapperDict ;
4249 private final Logger logger ;
50+ private final Coordinator coordinator ;
51+ private CommunicationModule communicationModule ;
52+ private int ignoreTime ;
4353
44- public Agent (EntityID entityID , Collection <Entity > entities , ScenarioInfo scenarioInfo , DevelopData developData , ModuleConfig moduleConfig ) {
54+ public Agent (EntityID entityID , Collection <Entity > entities , ScenarioInfo scenarioInfo , DevelopData developData , ModuleConfig moduleConfig , Coordinator coordinator ) {
4555 StandardWorldModel worldModel = new StandardWorldModel ();
4656 worldModel .addEntities (entities );
4757 worldModel .index ();
4858
59+ this .ignoreTime = scenarioInfo .getRawConfig ()
60+ .getIntValue (kernel .KernelConstants .IGNORE_AGENT_COMMANDS_KEY );
61+
4962 this .agentInfo = new AgentInfo (entityID , worldModel );
5063 this .worldInfo = new WorldInfo (worldModel );
5164 this .scenarioInfo = scenarioInfo ;
5265 this .developData = developData ;
5366 this .moduleManager = new ModuleManager (this .agentInfo , this .worldInfo , this .scenarioInfo , moduleConfig , this .developData );
67+ this .coordinator = coordinator ;
5468
5569 String dataStorageName = "" ;
5670 StandardEntityURN agentURN = Objects .requireNonNull (this .worldInfo .getEntity (this .agentInfo .getID ())).getStandardURN ();
@@ -97,13 +111,48 @@ public Class<?> registerModule(@Nonnull String moduleID, @Nonnull String moduleN
97111 }
98112
99113 public void update (int time , ChangeSet changed , Collection <Command > heard ) {
114+ worldInfo .setTime (time );
115+ worldInfo .merge (changed );
100116 agentInfo .recordThinkStartTime ();
101117 agentInfo .setTime (time );
118+
119+ if (time == 1 ) {
120+ if (this .communicationModule != null ) {
121+ ConsoleOutput .out (ConsoleOutput .State .ERROR ,
122+ "[ERROR ] Loader is not found." );
123+ ConsoleOutput .out (ConsoleOutput .State .NOTICE ,
124+ "CommunicationModule is modified - " + this );
125+ } else {
126+ this .communicationModule = new StandardCommunicationModule ();
127+ }
128+
129+ this .messageManager .registerMessageBundle (new StandardMessageBundle ());
130+ }
131+
132+ // agents can subscribe after ignore time
133+ if (time >= ignoreTime ) {
134+ this .messageManager .subscribe (this .agentInfo , this .worldInfo ,
135+ this .scenarioInfo );
136+
137+ if (!this .messageManager .getIsSubscribed ()) {
138+ int [] channelsToSubscribe = this .messageManager .getChannels ();
139+ if (channelsToSubscribe != null ) {
140+ this .messageManager .setIsSubscribed (true );
141+ }
142+ }
143+ }
144+
102145 agentInfo .setHeard (heard );
103146 agentInfo .setChanged (changed );
104- worldInfo .setTime (time );
105- worldInfo .merge (changed );
106147 worldInfo .setChanged (changed );
148+
149+ this .messageManager .refresh ();
150+ this .communicationModule .receive (this , this .messageManager );
151+
152+ this .messageManager .coordinateMessages (this .agentInfo , this .worldInfo ,
153+ this .scenarioInfo );
154+ this .communicationModule .send (this , this .messageManager );
155+
107156 logger .debug ("Agent Update (Time: {}, Changed: {}, Heard: {})" , agentInfo .getTime (), agentInfo .getChanged (), agentInfo .getHeard ());
108157 }
109158
@@ -113,4 +162,12 @@ public Config execModuleMethod(String moduleID, String methodName, Config argume
113162 logger .debug ("Executed Method Result (MethodName: {}, Result: {}" , methodName , result );
114163 return result ;
115164 }
165+
166+ public EntityID getID () {
167+ return this .agentInfo .getID ();
168+ }
169+
170+ public void send (Message [] messages ) {
171+ Arrays .stream (messages ).forEach (coordinator ::sendMessage );
172+ }
116173}
0 commit comments