HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# i% I% H" l# @- {/ F2 [- W- r9 G
- R5 _7 ]- |6 h! i2 o( | E
public Object buildActions () {; V- @1 L4 a) Y1 C- e D
super.buildActions();: }+ g+ e3 l+ K, O
8 \& r7 [' {& w4 u
// Create the list of simulation actions. We put these in
$ `& t! e3 b) v- { // an action group, because we want these actions to be8 v4 [, B' R* K& |1 t1 N* e/ x6 k. o5 @
// executed in a specific order, but these steps should* I% M% Y) N2 h2 r% ^) {. G
// take no (simulated) time. The M(foo) means "The message
$ T* M, C/ W# }' p! l // called <foo>". You can send a message To a particular
! [0 ]1 N( |! K+ M" g+ ]% C' H* _ // object, or ForEach object in a collection.
/ o, [1 P( a5 ^4 a- L
& y3 r! C3 D5 m9 P/ y; x1 N // Note we update the heatspace in two phases: first run. V, x- r" o& {
// diffusion, then run "updateWorld" to actually enact the& J4 @& x- B2 i2 M3 y3 z
// changes the heatbugs have made. The ordering here is
) f3 _- W1 P( p // significant!
$ c0 k) R/ s- f6 o3 P# F
[! m9 r& I) h- m Z# L7 v // Note also, that with the additional
# N8 s4 \( G5 h! X& f, X1 |0 C // `randomizeHeatbugUpdateOrder' Boolean flag we can* S$ K5 `% }; Y
// randomize the order in which the bugs actually run+ A; d5 J# ]! {- P+ J \
// their step rule. This has the effect of removing any" _* b! u( G% w1 Y
// systematic bias in the iteration throught the heatbug
6 s7 E" y* I, b3 w // list from timestep to timestep9 _) {' x: i5 t8 i
2 r5 h) c6 \* E% }$ k. ]2 A
// By default, all `createActionForEach' modelActions have8 X- \: B% H/ J K+ I2 h
// a default order of `Sequential', which means that the
# {/ n0 T" O3 c* o // order of iteration through the `heatbugList' will be) T3 q) u9 j- M+ o
// identical (assuming the list order is not changed' w- b t" _4 g# |6 P/ D
// indirectly by some other process).0 a) ?% d, O( U. a& T8 r# g7 D
& q9 M3 W5 |2 ], i: L1 Y
modelActions = new ActionGroupImpl (getZone ());( u+ k# ?+ Y0 q- i
1 p7 f6 l& I- C+ G$ N
try {, ^& P2 L! w0 T
modelActions.createActionTo$message d0 j& T2 Q1 e) I' N6 C% p, c
(heat, new Selector (heat.getClass (), "stepRule", false));
+ P) o0 G* Z$ r( Y4 `" D3 E } catch (Exception e) {
- i K1 \' x j/ q+ t5 r9 W System.err.println ("Exception stepRule: " + e.getMessage ());- o$ X4 M* i: J; v1 L
}
~2 I Z# j; Y: K: v
7 V1 P9 x2 L6 d2 J try {
7 a7 Y( ^4 \: e3 D Heatbug proto = (Heatbug) heatbugList.get (0);* h6 y# o5 G, G V6 Q- {
Selector sel = ' y3 e$ j: w6 y# Z
new Selector (proto.getClass (), "heatbugStep", false);
' m6 i% f9 l4 e+ ? v% A* e actionForEach =
* [, _) m4 _. g" ^+ a5 @0 K modelActions.createFActionForEachHomogeneous$call
% B- O: E. E0 @$ j6 j0 I (heatbugList,
& e+ }" s( B+ ~+ h0 b, u$ Q new FCallImpl (this, proto, sel,
& {+ I8 ?6 O. n; o: T# W new FArgumentsImpl (this, sel)));
+ d+ a6 t2 d$ O4 [( ^9 [) Z- v- p } catch (Exception e) {- p. s/ B, G* _
e.printStackTrace (System.err);
$ K5 f: ]6 t6 L9 |0 E8 o2 l }4 Y$ k b8 w& X) e. D
* g3 G8 a- K# h3 V- D+ p
syncUpdateOrder ();8 u6 c8 v/ k- l
/ s! J. S& M3 u# H/ _. Q6 L1 I% v! q) X8 { try {5 U. \, c: n% d# n3 W; x0 n3 {
modelActions.createActionTo$message " y, X7 k) _% _9 J$ F+ H" B& n
(heat, new Selector (heat.getClass (), "updateLattice", false));
. I# p- y9 z$ [8 E" e } catch (Exception e) {
4 \% y. B% P, G2 X8 e7 D7 Z; u System.err.println("Exception updateLattice: " + e.getMessage ());
/ \0 q" M0 z' U y( G }! p) P$ \4 }6 L$ D& { L! ]1 d" e
3 C" G7 x2 I- o: J* I! _ ^0 J& K3 A
// Then we create a schedule that executes the
( x7 L) ^3 o" O- ?0 e2 `$ [ // modelActions. modelActions is an ActionGroup, by itself it
, P/ D9 q% Q3 \, a // has no notion of time. In order to have it executed in
5 Y! m! T& t# @ // time, we create a Schedule that says to use the
6 r9 }5 m3 O; ?9 e; k // modelActions ActionGroup at particular times. This% I% K; \+ K& S* M
// schedule has a repeat interval of 1, it will loop every
) u( P! P: l9 B: x& v/ x // time step. The action is executed at time 0 relative to0 Z6 g6 |7 n, F8 b, f9 {
// the beginning of the loop.
5 m9 H" A& e f8 G3 ]! J- Y9 o/ A B, o1 h* f7 H# k
// This is a simple schedule, with only one action that is
; e) m; f6 Z R4 \$ t8 W // just repeated every time. See jmousetrap for more
) k8 @4 R+ ?+ d i) q/ B' | // complicated schedules.3 a8 w! n7 Q* r4 v- ]) L; q7 \
* @% d4 M% P7 v" t! ~ modelSchedule = new ScheduleImpl (getZone (), 1);
# y, F0 E5 A" B modelSchedule.at$createAction (0, modelActions);
$ Y) O0 M! N! T7 u " }1 q! Z% A4 z) {% S8 I% F" h
return this;
1 u" d. t4 J8 I8 z! D7 Y } |