HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% I% ~$ f v6 o/ V' o
1 f" C! Y, {7 v
public Object buildActions () {8 T6 T7 h6 [( |
super.buildActions();5 a% U% z# H4 @0 R: R! C4 O! z
) D7 z. i! }& u2 ]/ ^ // Create the list of simulation actions. We put these in, A4 o0 y* ]% C! T( w; z4 \" }
// an action group, because we want these actions to be
! p0 }/ K% S6 Y5 P // executed in a specific order, but these steps should
0 G3 q, [3 R3 a9 W! D // take no (simulated) time. The M(foo) means "The message
2 [. a5 z4 u0 W // called <foo>". You can send a message To a particular0 T& c9 v! Z0 j+ y
// object, or ForEach object in a collection.
, u, g* g4 L" T# O : [0 ~; Q4 `+ Z9 k: k
// Note we update the heatspace in two phases: first run" }& j% O- F, ` V% S: ]
// diffusion, then run "updateWorld" to actually enact the
' v* z5 R- J: Y t! W# E9 J // changes the heatbugs have made. The ordering here is
( z# K6 \2 i+ ~# \; B) a // significant!
6 m, U! T2 C P D7 `/ e+ m ' C* @, e9 J/ k' j9 c
// Note also, that with the additional: j: @3 ^- {1 v7 G$ K
// `randomizeHeatbugUpdateOrder' Boolean flag we can. u+ I/ N! S' W9 J. F
// randomize the order in which the bugs actually run9 P/ T- k5 W1 O* {* U
// their step rule. This has the effect of removing any- `8 k2 a! i9 g' o# M9 I
// systematic bias in the iteration throught the heatbug) k) a# x! x. M( p4 ?& A7 J& o' O: u0 ]
// list from timestep to timestep7 L( Q( c0 t. X1 x
1 _' i0 b: M" j$ k6 {8 l y: e // By default, all `createActionForEach' modelActions have5 Z9 d& f- p7 F; D, B# g
// a default order of `Sequential', which means that the8 ^8 ~! R9 @. W' b0 o
// order of iteration through the `heatbugList' will be. H& i5 Z* S9 P) _, i: \3 r' \
// identical (assuming the list order is not changed
2 E" a7 J' l$ ^ // indirectly by some other process).0 C8 D& Q0 ~) @9 R2 I- m' w
5 B9 V( g( V8 D5 @* X; r
modelActions = new ActionGroupImpl (getZone ());, `4 i5 r! a' i) k3 m3 ^1 y9 n6 W
$ ]7 t8 K- q7 a try {
# B' D2 O! g' t modelActions.createActionTo$message6 T7 C; ]4 I/ |. @( _
(heat, new Selector (heat.getClass (), "stepRule", false));
& |9 V) Q$ L/ @( {0 P5 E. B' h } catch (Exception e) {) d; T. }# o2 _5 e8 O
System.err.println ("Exception stepRule: " + e.getMessage ());
& Z8 v$ Z1 \' ^- s& w. x% ? }
- N/ a' `8 C0 Z# ]5 Y- M
4 T7 n0 V; d. N& Y0 z" ?; m: M try {5 k+ r( @' d- v, N' T4 f0 y1 P
Heatbug proto = (Heatbug) heatbugList.get (0);
9 N: X g+ E7 @. l, P4 c7 s Selector sel = + Y. M+ C/ H5 F' F8 ], u
new Selector (proto.getClass (), "heatbugStep", false);# j. U8 k% @! y2 }+ G, R. P4 F
actionForEach =
8 w4 i! _! ^5 `8 e! B modelActions.createFActionForEachHomogeneous$call
9 Z; E5 c# }9 c! F (heatbugList,, s/ T. M0 o# D$ ~# }3 I0 l6 W; W
new FCallImpl (this, proto, sel,
5 v' x! p, ~6 B7 v/ e new FArgumentsImpl (this, sel)));3 u. y8 {" T' ~- Z$ C( j* m4 Z- Z
} catch (Exception e) {: L4 X/ L5 n2 w
e.printStackTrace (System.err);
" g7 c1 {+ G9 \/ A0 c6 k0 v }' l' y0 q* O" G O7 ~& O' n" J1 t
. w1 W4 x( K) i: b2 r
syncUpdateOrder ();
" l1 i/ B/ V" L$ Q0 w1 Q
. l' B. N: o) _' P try {& j9 g$ Y" \2 R5 ]( A- }! T7 _
modelActions.createActionTo$message
. j0 |. Z, l5 d7 \2 D (heat, new Selector (heat.getClass (), "updateLattice", false));
' p/ Y; G- [2 f V0 `% j3 N } catch (Exception e) {5 B9 l6 j5 ^& ?, D$ o
System.err.println("Exception updateLattice: " + e.getMessage ());
3 e- ?) p- p& n }
* [2 y7 S5 y! U7 O) N3 \" K3 w, l. q
4 x2 ]. y2 k$ L4 `: B$ ^ // Then we create a schedule that executes the" t& J) R2 I" ~" q, x0 G; A+ E# b6 l
// modelActions. modelActions is an ActionGroup, by itself it2 k N6 a: g% H9 k2 B
// has no notion of time. In order to have it executed in- z+ ~% s4 v+ m! J8 v2 X" U: X
// time, we create a Schedule that says to use the) Y2 b8 X) @, H# H
// modelActions ActionGroup at particular times. This
6 c4 t1 X; f1 Y4 N6 H. \# K // schedule has a repeat interval of 1, it will loop every, \* P" Y8 X+ }7 H$ n5 @' \1 W; L
// time step. The action is executed at time 0 relative to
8 {( Q; R* H2 S // the beginning of the loop.
) _8 B \! f* x" f/ u" T& C
/ ^! j7 T. a/ H( L- R // This is a simple schedule, with only one action that is* t% P$ S; R0 r$ k# b
// just repeated every time. See jmousetrap for more; ~1 N+ M5 x" e8 H# z& C& E+ Y7 r4 ~
// complicated schedules.
: _" H% i& Q; E) B& J& ^ 1 \8 P; `: O+ q) x- |; h+ O" R
modelSchedule = new ScheduleImpl (getZone (), 1);
5 l M7 i4 C& h7 \0 l7 i modelSchedule.at$createAction (0, modelActions);
& A7 G) S V& l0 I$ N
6 ]7 ^* h4 D' u. y( q+ a9 | return this;
" j% ?+ d; H' @ } |