HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 e3 I6 V* [4 i! X! u
, r. G/ e, S" J" ~4 } public Object buildActions () {
% g) A& j, x$ J! x0 l super.buildActions();& h9 C& K0 s) _( i. V
8 h% x$ w4 Q. ]3 N // Create the list of simulation actions. We put these in
; R7 a: M% {! f: c4 o // an action group, because we want these actions to be
2 [1 K+ h4 ~* C, B B // executed in a specific order, but these steps should4 A1 n! M; ^- e* v" a* D
// take no (simulated) time. The M(foo) means "The message, T$ P! G) }0 D' P8 |3 |0 v. c
// called <foo>". You can send a message To a particular
8 g/ Y7 C! v7 O+ m6 o // object, or ForEach object in a collection.
/ e1 m7 A; ?4 ?5 C" C ( U$ {2 `* R& [8 h
// Note we update the heatspace in two phases: first run
8 X0 M6 C6 N5 U // diffusion, then run "updateWorld" to actually enact the, S3 a0 n* H* c5 g+ h' e. x% S
// changes the heatbugs have made. The ordering here is
2 w6 V' d* T* D // significant!
' `7 f' Z* F1 e0 Y0 U* l # r8 t, `1 n8 ^. m$ A0 ?4 v
// Note also, that with the additional# {; h3 N& J7 \+ B5 |: q; b3 G" s
// `randomizeHeatbugUpdateOrder' Boolean flag we can0 h3 W: `& j4 n2 G4 u/ m& @
// randomize the order in which the bugs actually run
1 O9 y5 g7 B9 Z) _ l( S L' B // their step rule. This has the effect of removing any
; K* g0 q1 ^; Q( Z' A8 C // systematic bias in the iteration throught the heatbug' R, B; n3 U$ `; \0 m4 M+ ~
// list from timestep to timestep3 W5 E! {( q& c! {( f- l& d/ F
, I$ e) T% y6 O, K // By default, all `createActionForEach' modelActions have$ I, X% n4 G2 t
// a default order of `Sequential', which means that the! u& F4 I8 g! |
// order of iteration through the `heatbugList' will be
6 _5 h" I7 `. y // identical (assuming the list order is not changed
& x; M! r; `8 v, _ // indirectly by some other process).+ y _7 a' J. j' u B
( {/ v$ k/ T- A modelActions = new ActionGroupImpl (getZone ());
$ d s; b, S* \- w$ G
! a. @3 R; ^( d$ V) ?& G4 P6 a try {8 V* u, \+ S7 [0 K$ M
modelActions.createActionTo$message
; C0 l* N, v, Q$ H" ^2 Z (heat, new Selector (heat.getClass (), "stepRule", false));
- P' f- l& Z; d' i' G5 R } catch (Exception e) {
) O5 W6 ]8 T. E& ] System.err.println ("Exception stepRule: " + e.getMessage ());/ h: p( z) d) c; d7 e8 W8 g/ [3 d
}
/ I1 f6 ]4 J1 R! {. z/ w5 ?/ y2 F- @9 M3 S; [1 x0 N
try {
! h, f0 s; v& l* x" B, G Heatbug proto = (Heatbug) heatbugList.get (0);% h6 I' _' p* G8 Q! ]/ S4 A
Selector sel = 0 j9 p. M# X5 W! N$ o' f7 P
new Selector (proto.getClass (), "heatbugStep", false);
7 y# O1 I6 q# o- d# i6 i# Q: L" }% r actionForEach =
, Z8 ]6 K' m/ r' w% j; @ modelActions.createFActionForEachHomogeneous$call
h* `1 s7 d( V' D2 \ (heatbugList,
9 B- J# ^# v1 e5 h new FCallImpl (this, proto, sel,3 _8 H& l; J: ~
new FArgumentsImpl (this, sel)));$ d6 R9 w: C; q
} catch (Exception e) {3 d6 A; R% ^& _6 ^' @- O7 ?( z
e.printStackTrace (System.err);% H, K: ?, b6 \# a( u; l; K
}0 Y( b- V$ v: T1 L9 Q8 U
3 o! Q; `) Q+ K% e/ B( B0 p
syncUpdateOrder ();
' u# x7 W" l' M. k7 u6 O
3 v& }/ g0 O/ x* t8 g [ try {) ?& e% H* @, j
modelActions.createActionTo$message
7 T3 y; M+ O7 z) k. s, A: x; f (heat, new Selector (heat.getClass (), "updateLattice", false));
& Z* ^3 O: h4 `$ h) K } catch (Exception e) {
5 s/ a- m$ c8 d0 F6 i System.err.println("Exception updateLattice: " + e.getMessage ());$ \* Y) J" V3 S- ]& L: k
}, {8 O. }5 l& x- }
' Q: N) m2 M+ s5 n2 p( s; P4 ]" T6 S // Then we create a schedule that executes the
8 s7 }5 H0 c) A; F+ l9 _2 [. \ // modelActions. modelActions is an ActionGroup, by itself it* |) p& { ~& G
// has no notion of time. In order to have it executed in6 A* F6 S9 u$ M/ l
// time, we create a Schedule that says to use the
o! A! R3 K4 ^5 L // modelActions ActionGroup at particular times. This
# C# h& Y2 S. f6 d0 Q" | // schedule has a repeat interval of 1, it will loop every0 t8 Q# L0 W! U8 j7 P4 Y) L
// time step. The action is executed at time 0 relative to
4 M9 I$ p+ |8 Q; u3 W // the beginning of the loop.7 C, [6 s X, T2 n% A' q
4 ]1 G7 F! Q$ Y/ X" ]/ R0 O8 f0 s( d
// This is a simple schedule, with only one action that is5 k/ @2 v D, W; E& n
// just repeated every time. See jmousetrap for more5 g! \7 `2 M# [" s9 F. l, A) a/ F4 q
// complicated schedules.
& H6 x& v; z6 J& V+ c6 {: W2 |4 ~ - Z, _8 f" N; P7 _3 Z1 f
modelSchedule = new ScheduleImpl (getZone (), 1);
5 Q- |8 i1 k0 x) M( y$ j' ? modelSchedule.at$createAction (0, modelActions);
' k3 ?/ }" `# H5 }9 |% r1 C2 y- _6 r$ L
' n) b# k& {8 ?1 L: \ return this;
2 k" d& R+ o6 T' s2 g; j" b } |