11package adf_core_python .agent ;
22
3- import adf .core .agent .communication .MessageManager ;
3+ import adf .core .agent .communication .standard . bundle . StandardMessageBundle ;
44import adf .core .agent .info .ScenarioInfo ;
55import adf .core .agent .info .WorldInfo ;
6+ import adf .core .launcher .ConsoleOutput ;
7+ import adf_core_python .agent .communication .MessageManager ;
8+ import adf_core_python .agent .communication .standard .StandardCommunicationModule ;
69import adf_core_python .agent .config .ModuleConfig ;
710import adf_core_python .agent .develop .DevelopData ;
811import adf_core_python .agent .info .AgentInfo ;
912import adf_core_python .agent .module .ModuleManager ;
1013import adf_core_python .agent .precompute .PrecomputeData ;
14+ import adf_core_python .component .communication .CommunicationModule ;
1115import adf_core_python .component .module .AbstractModule ;
16+ import adf_core_python .gateway .Coordinator ;
1217import adf_core_python .gateway .mapper .AbstractMapper ;
1318import adf_core_python .gateway .mapper .MapperDict ;
1419import jakarta .annotation .Nonnull ;
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 ;
28+ import rescuecore2 .standard .messages .AKSubscribe ;
2229import rescuecore2 .worldmodel .ChangeSet ;
2330import rescuecore2 .worldmodel .Entity ;
2431import rescuecore2 .worldmodel .EntityID ;
2532
2633import java .lang .reflect .Constructor ;
2734import java .lang .reflect .InvocationTargetException ;
35+ import java .util .Arrays ;
2836import java .util .Collection ;
2937import java .util .HashMap ;
3038import java .util .Objects ;
3139
3240public class Agent {
33- private final AgentInfo agentInfo ;
34- private final WorldInfo worldInfo ;
35- private final ScenarioInfo scenarioInfo ;
41+ public final AgentInfo agentInfo ;
42+ public final WorldInfo worldInfo ;
43+ public final ScenarioInfo scenarioInfo ;
3644 private final ModuleManager moduleManager ;
3745 private final DevelopData developData ;
3846 private final PrecomputeData precomputeData ;
3947 private final MessageManager messageManager ;
48+ private CommunicationModule communicationModule ;
4049 private final HashMap <String , AbstractMapper > modules = new HashMap <>();
4150 private final MapperDict mapperDict ;
4251 private final Logger logger ;
52+ private int ignoreTime ;
53+ private final Coordinator coordinator ;
4354
44- public Agent (EntityID entityID , Collection <Entity > entities , ScenarioInfo scenarioInfo , DevelopData developData , ModuleConfig moduleConfig ) {
55+ public Agent (EntityID entityID , Collection <Entity > entities , ScenarioInfo scenarioInfo , DevelopData developData , ModuleConfig moduleConfig , Coordinator coordinator ) {
4556 StandardWorldModel worldModel = new StandardWorldModel ();
4657 worldModel .addEntities (entities );
4758 worldModel .index ();
4859
60+ this .ignoreTime = scenarioInfo .getRawConfig ()
61+ .getIntValue (kernel .KernelConstants .IGNORE_AGENT_COMMANDS_KEY );
62+
4963 this .agentInfo = new AgentInfo (entityID , worldModel );
5064 this .worldInfo = new WorldInfo (worldModel );
5165 this .scenarioInfo = scenarioInfo ;
5266 this .developData = developData ;
5367 this .moduleManager = new ModuleManager (this .agentInfo , this .worldInfo , this .scenarioInfo , moduleConfig , this .developData );
68+ this .coordinator = coordinator ;
5469
5570 String dataStorageName = "" ;
5671 StandardEntityURN agentURN = Objects .requireNonNull (this .worldInfo .getEntity (this .agentInfo .getID ())).getStandardURN ();
@@ -97,13 +112,48 @@ public Class<?> registerModule(@Nonnull String moduleID, @Nonnull String moduleN
97112 }
98113
99114 public void update (int time , ChangeSet changed , Collection <Command > heard ) {
115+ worldInfo .setTime (time );
116+ worldInfo .merge (changed );
100117 agentInfo .recordThinkStartTime ();
101118 agentInfo .setTime (time );
119+
120+ if (time == 1 ) {
121+ if (this .communicationModule != null ) {
122+ ConsoleOutput .out (ConsoleOutput .State .ERROR ,
123+ "[ERROR ] Loader is not found." );
124+ ConsoleOutput .out (ConsoleOutput .State .NOTICE ,
125+ "CommunicationModule is modified - " + this );
126+ } else {
127+ this .communicationModule = new StandardCommunicationModule ();
128+ }
129+
130+ this .messageManager .registerMessageBundle (new StandardMessageBundle ());
131+ }
132+
133+ // agents can subscribe after ignore time
134+ if (time >= ignoreTime ) {
135+ this .messageManager .subscribe (this .agentInfo , this .worldInfo ,
136+ this .scenarioInfo );
137+
138+ if (!this .messageManager .getIsSubscribed ()) {
139+ int [] channelsToSubscribe = this .messageManager .getChannels ();
140+ if (channelsToSubscribe != null ) {
141+ this .messageManager .setIsSubscribed (true );
142+ }
143+ }
144+ }
145+
102146 agentInfo .setHeard (heard );
103147 agentInfo .setChanged (changed );
104- worldInfo .setTime (time );
105- worldInfo .merge (changed );
106148 worldInfo .setChanged (changed );
149+
150+ this .messageManager .refresh ();
151+ this .communicationModule .receive (this , this .messageManager );
152+
153+ this .messageManager .coordinateMessages (this .agentInfo , this .worldInfo ,
154+ this .scenarioInfo );
155+ this .communicationModule .send (this , this .messageManager );
156+
107157 logger .debug ("Agent Update (Time: {}, Changed: {}, Heard: {})" , agentInfo .getTime (), agentInfo .getChanged (), agentInfo .getHeard ());
108158 }
109159
@@ -113,4 +163,12 @@ public Config execModuleMethod(String moduleID, String methodName, Config argume
113163 logger .debug ("Executed Method Result (MethodName: {}, Result: {}" , methodName , result );
114164 return result ;
115165 }
166+
167+ public EntityID getID () {
168+ return this .agentInfo .getID ();
169+ }
170+
171+ public void send (Message [] messages ) {
172+ Arrays .stream (messages ).forEach (coordinator ::sendMessage );
173+ }
116174}
0 commit comments