HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' |0 R/ k g& r# ^9 C6 {" a: W$ H- u0 d4 L' R. y
public Object buildActions () {
% Y% ]0 `8 p4 b ]. s; z super.buildActions();; Q0 `/ x- _& \6 G- z: c2 W* l) P
, I7 d' A: [$ T: H
// Create the list of simulation actions. We put these in
. t/ F0 f4 r9 ~* y D" N! [% Z/ B // an action group, because we want these actions to be
) k$ u; ]& i( U& c // executed in a specific order, but these steps should
) y: R$ `: H+ F // take no (simulated) time. The M(foo) means "The message
$ D+ m7 x# u s( {8 V // called <foo>". You can send a message To a particular
0 r' x1 y! }9 ?: | // object, or ForEach object in a collection.8 p6 u8 C3 H3 o! e- g
; M$ o' N2 p2 [0 O% Z7 E$ m
// Note we update the heatspace in two phases: first run
2 K/ [* q0 E& t // diffusion, then run "updateWorld" to actually enact the6 @/ ]! L0 U' g& O" ?, h/ {0 v
// changes the heatbugs have made. The ordering here is
% Q5 c: P0 R( I) [, Z- q // significant!* b( ~! A. y( D3 N- ]" Q1 b' Z
- J* ?. F) R2 D9 S. {! x1 J // Note also, that with the additional
+ A% K7 M3 K" d, l: U# U6 m* x8 f // `randomizeHeatbugUpdateOrder' Boolean flag we can5 Z7 q" I5 U- L" d
// randomize the order in which the bugs actually run
" L+ c1 G. T9 a% Y // their step rule. This has the effect of removing any
, a' W/ i' o; @. y5 { // systematic bias in the iteration throught the heatbug
3 B& N |/ s- x6 ^4 B: U# R0 Y. w" z7 K // list from timestep to timestep. U0 L2 d7 \) V& S
7 ?1 n. i" F- ?/ _$ Z // By default, all `createActionForEach' modelActions have
" C P, _' L& d# o& _- b; l* s // a default order of `Sequential', which means that the! W* d3 Y. m% Q4 s! Z. B; O ]
// order of iteration through the `heatbugList' will be
3 u, G. O) \! i7 W1 I // identical (assuming the list order is not changed
- K5 w! R% C/ c2 n& }) W$ r // indirectly by some other process).
/ ~: W& X' l0 ]8 y, Y+ s) }+ {, ~
7 t Y" y+ i- B" j' u6 W+ c& i5 z modelActions = new ActionGroupImpl (getZone ());
8 @* }' M6 g7 T Z5 V: Z
8 h/ A: d5 ~- j5 |2 r& O1 T try {
! L( f1 y& Q( C4 I) g8 c' ^& Q modelActions.createActionTo$message! E7 _2 T8 ]- i6 n* j: L9 x
(heat, new Selector (heat.getClass (), "stepRule", false));
7 o8 q" b' }/ d } catch (Exception e) {8 e/ e: V4 w4 G9 C' }6 E* F
System.err.println ("Exception stepRule: " + e.getMessage ());6 P% U5 _! K6 ?. L+ {
}* L2 R+ A$ C% @( J+ y) m1 R9 o
2 f$ u+ y4 J3 M, X$ D+ W# d try {% V% {* y- { Q2 v c
Heatbug proto = (Heatbug) heatbugList.get (0);! _1 S$ e% i$ Y$ t2 W. ~* s+ y m
Selector sel = % ~% Q9 M) [7 `+ p( I$ L
new Selector (proto.getClass (), "heatbugStep", false);. K' |. ~# W$ |5 N- P% @
actionForEach =* \, L% N x* W2 B& G1 H/ S7 U
modelActions.createFActionForEachHomogeneous$call g( C, V" i: e
(heatbugList,0 X2 |* k! R8 q1 q
new FCallImpl (this, proto, sel,
! `# O4 F4 w: P9 F new FArgumentsImpl (this, sel)));
4 i& ^+ v7 i+ @6 h3 S7 A5 } } catch (Exception e) {/ t+ \) |4 q8 T( j0 Z% v) |
e.printStackTrace (System.err);
8 h# ]2 k+ S1 a0 h. n6 S5 n6 L& o }
9 b% V/ y R2 ~8 B( ^
" t- W* Y6 H" ?9 T& i# C5 N; g syncUpdateOrder ();" |" R$ B7 ^/ F
) `* G" O: z" W try {
: b. z) I1 ]$ I+ J; w! u) L# f modelActions.createActionTo$message ) H3 {5 [, C- |4 b; j( {$ O* A
(heat, new Selector (heat.getClass (), "updateLattice", false));
; o0 N1 \+ ?. h7 z$ N8 \) h% b } catch (Exception e) {
% q7 y" Q7 K1 o9 x S: Y* \! q6 f System.err.println("Exception updateLattice: " + e.getMessage ());( e) x, L+ S9 G7 t$ t7 e! T0 `
}6 }' W+ B( q8 q$ T
$ q# n6 ]! y- ^' @- J, G% w! p
// Then we create a schedule that executes the& a0 T( g1 b7 D9 B. a! G
// modelActions. modelActions is an ActionGroup, by itself it
5 G6 r' \& @ l+ o& O: f // has no notion of time. In order to have it executed in a* j; w$ a: w
// time, we create a Schedule that says to use the, g$ _# H- _7 d1 e
// modelActions ActionGroup at particular times. This% Y/ h: y$ B$ ~9 r# B7 m7 G/ l
// schedule has a repeat interval of 1, it will loop every+ |' ~; E' p- b8 A8 m/ q
// time step. The action is executed at time 0 relative to
! `# w1 ?' m( I7 W& ^ // the beginning of the loop.
9 P0 U* [! s: v
" Y h- p% {+ p // This is a simple schedule, with only one action that is
! y: _; Q. Z5 c; f$ ?. N+ J // just repeated every time. See jmousetrap for more
1 b, A7 \0 T# t- ~& j) @* | // complicated schedules.
' }* r( `! U4 v P6 @
4 m1 L. p6 Z% W, O modelSchedule = new ScheduleImpl (getZone (), 1);) _% p" a. k3 M# F& h- l
modelSchedule.at$createAction (0, modelActions);+ X1 c4 ?3 i! \$ c
1 M; j$ T6 Q& M8 R0 }/ l( W return this;
. L) Q6 A7 j7 [4 t C P4 |; D } |