1
- package adf_core_python .agent ;
1
+ package adf_core_python .core . agent ;
2
2
3
- import adf .core .agent .communication .MessageManager ;
3
+ import adf .core .agent .communication .standard .bundle .StandardMessageBundle ;
4
+ import adf .core .agent .develop .DevelopData ;
4
5
import adf .core .agent .info .ScenarioInfo ;
5
6
import 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 ;
14
19
import jakarta .annotation .Nonnull ;
15
20
import jakarta .annotation .Nullable ;
16
21
import org .apache .logging .log4j .LogManager ;
17
22
import org .apache .logging .log4j .Logger ;
18
23
import rescuecore2 .config .Config ;
19
24
import rescuecore2 .messages .Command ;
25
+ import rescuecore2 .messages .Message ;
20
26
import rescuecore2 .standard .entities .StandardEntityURN ;
21
27
import rescuecore2 .standard .entities .StandardWorldModel ;
22
28
import rescuecore2 .worldmodel .ChangeSet ;
25
31
26
32
import java .lang .reflect .Constructor ;
27
33
import java .lang .reflect .InvocationTargetException ;
34
+ import java .util .Arrays ;
28
35
import java .util .Collection ;
29
36
import java .util .HashMap ;
30
37
import java .util .Objects ;
31
38
32
39
public 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 ;
36
43
private final ModuleManager moduleManager ;
37
44
private final DevelopData developData ;
38
45
private final PrecomputeData precomputeData ;
39
46
private final MessageManager messageManager ;
40
47
private final HashMap <String , AbstractMapper > modules = new HashMap <>();
41
48
private final MapperDict mapperDict ;
42
49
private final Logger logger ;
50
+ private final Coordinator coordinator ;
51
+ private CommunicationModule communicationModule ;
52
+ private int ignoreTime ;
43
53
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 ) {
45
55
StandardWorldModel worldModel = new StandardWorldModel ();
46
56
worldModel .addEntities (entities );
47
57
worldModel .index ();
48
58
59
+ this .ignoreTime = scenarioInfo .getRawConfig ()
60
+ .getIntValue (kernel .KernelConstants .IGNORE_AGENT_COMMANDS_KEY );
61
+
49
62
this .agentInfo = new AgentInfo (entityID , worldModel );
50
63
this .worldInfo = new WorldInfo (worldModel );
51
64
this .scenarioInfo = scenarioInfo ;
52
65
this .developData = developData ;
53
66
this .moduleManager = new ModuleManager (this .agentInfo , this .worldInfo , this .scenarioInfo , moduleConfig , this .developData );
67
+ this .coordinator = coordinator ;
54
68
55
69
String dataStorageName = "" ;
56
70
StandardEntityURN agentURN = Objects .requireNonNull (this .worldInfo .getEntity (this .agentInfo .getID ())).getStandardURN ();
@@ -97,13 +111,48 @@ public Class<?> registerModule(@Nonnull String moduleID, @Nonnull String moduleN
97
111
}
98
112
99
113
public void update (int time , ChangeSet changed , Collection <Command > heard ) {
114
+ worldInfo .setTime (time );
115
+ worldInfo .merge (changed );
100
116
agentInfo .recordThinkStartTime ();
101
117
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
+
102
145
agentInfo .setHeard (heard );
103
146
agentInfo .setChanged (changed );
104
- worldInfo .setTime (time );
105
- worldInfo .merge (changed );
106
147
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
+
107
156
logger .debug ("Agent Update (Time: {}, Changed: {}, Heard: {})" , agentInfo .getTime (), agentInfo .getChanged (), agentInfo .getHeard ());
108
157
}
109
158
@@ -113,4 +162,12 @@ public Config execModuleMethod(String moduleID, String methodName, Config argume
113
162
logger .debug ("Executed Method Result (MethodName: {}, Result: {}" , methodName , result );
114
163
return result ;
115
164
}
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
+ }
116
173
}
0 commit comments