HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:: z* j/ ^' r- \7 Q# X" f! Q
, ~ M- _" s$ v- D8 n public Object buildActions () {, \ W+ | F6 q. \2 b
super.buildActions();. M0 q9 ~+ Z: z+ k4 f
/ ]. Q9 m) v) a9 S // Create the list of simulation actions. We put these in) b# A9 N \+ ]
// an action group, because we want these actions to be
/ J3 x, q; {+ T // executed in a specific order, but these steps should* B/ u; D2 p* b3 }$ J1 v
// take no (simulated) time. The M(foo) means "The message
+ }7 y, ~6 b0 c // called <foo>". You can send a message To a particular
+ r( |, h" _) v6 k. n0 j& q // object, or ForEach object in a collection.
8 q# n2 ^" P8 ]# J$ J 0 O+ c9 ^9 r( |: n
// Note we update the heatspace in two phases: first run
6 R; ?" r( Y/ J! J4 @ // diffusion, then run "updateWorld" to actually enact the
& H5 g, f7 N3 R5 z/ w- q2 z" V // changes the heatbugs have made. The ordering here is! e; y+ J7 W; e. M
// significant!* l5 F+ h. L, t8 v6 y, |" ]
* _+ M7 B- W" ^3 d; @ // Note also, that with the additional* F3 b: ^1 \1 x4 J
// `randomizeHeatbugUpdateOrder' Boolean flag we can- r" C2 u7 x4 ]9 Z. h! X
// randomize the order in which the bugs actually run" l. [7 Q' x" E
// their step rule. This has the effect of removing any! o5 L: {! }1 ^' s8 u
// systematic bias in the iteration throught the heatbug! _0 ~# q: s/ u' t
// list from timestep to timestep. G! _* ~* j R3 j2 c/ f' a
, \ e- o) l. [
// By default, all `createActionForEach' modelActions have" u7 F6 t; v( A0 |; B
// a default order of `Sequential', which means that the
1 P4 b. u1 X/ G // order of iteration through the `heatbugList' will be
7 }1 M5 L$ u0 _3 T/ Y1 j; ^ // identical (assuming the list order is not changed S9 X! G) K; q( L/ [+ H) w
// indirectly by some other process).
' I0 y, n5 _( y# K1 b - @2 U+ n: D; z7 j/ n2 x' O
modelActions = new ActionGroupImpl (getZone ());; o' `2 ^- \$ r$ v1 b: @4 E: |& e
H* o C7 Y$ C! n! E6 v try {
# S$ l4 X1 V! z3 I modelActions.createActionTo$message
( T2 ?+ G; z; M3 t% k) ~ (heat, new Selector (heat.getClass (), "stepRule", false));
( J% p7 B Y5 h- l: d } catch (Exception e) {
4 ~8 Y& X3 G4 A- [ System.err.println ("Exception stepRule: " + e.getMessage ());
4 l" b* h6 A% q# g4 S }
& |* f8 r8 T9 ], y7 E+ m/ C
3 K: h$ Z! F/ H4 H try {
+ z3 x( @9 M* N: r5 \ Heatbug proto = (Heatbug) heatbugList.get (0);8 z. Y! V! _7 `( E& _3 p
Selector sel =
0 |% [( D6 j2 p* U# I& P new Selector (proto.getClass (), "heatbugStep", false);
* X" V+ ~4 j; x actionForEach =( L) l! W' U! m( K: r& n+ ~
modelActions.createFActionForEachHomogeneous$call9 L) V( A- C4 ]! [- ~, M2 ^
(heatbugList,1 F/ F# \) ]- G5 h! c) ^0 L
new FCallImpl (this, proto, sel,/ `9 r5 t9 ~! R% E
new FArgumentsImpl (this, sel)));$ m+ U- o/ v$ h* U4 I* q7 s# ]
} catch (Exception e) {! h- l! j8 Y5 O& U2 ~" W, U% m+ ?
e.printStackTrace (System.err);
5 b& [: Q( r$ j% j }
, Z/ g+ w9 L" U
, V( z! C3 x p; D( b syncUpdateOrder ();& o& S1 x2 T6 S4 V
4 @7 E& l0 u% s6 N- T+ D5 B
try {' \% K3 r1 q- a9 e: i
modelActions.createActionTo$message 0 h0 W/ }# g7 `8 ?" t4 Z
(heat, new Selector (heat.getClass (), "updateLattice", false));9 E. e* ?1 g& S# d, [1 |
} catch (Exception e) {
. v# T0 k4 I+ l$ g System.err.println("Exception updateLattice: " + e.getMessage ());* @$ l- l. s b$ T
}# h- V' p" A$ h# [: u6 C: p, p
* A, O4 r* S; N* q8 W
// Then we create a schedule that executes the4 U( a4 [: x$ V# a- O/ A
// modelActions. modelActions is an ActionGroup, by itself it
5 x% y- G6 s* M7 \; r // has no notion of time. In order to have it executed in
3 @ y( M, X+ g6 C0 F8 e W // time, we create a Schedule that says to use the' A6 a) s0 k8 K: o
// modelActions ActionGroup at particular times. This1 L4 r& U3 f9 p5 K$ ]* S
// schedule has a repeat interval of 1, it will loop every
a {# E5 S+ [- @( t5 Y- }8 g // time step. The action is executed at time 0 relative to/ ~. T6 Z% L" d+ {6 m- N2 N
// the beginning of the loop.4 `( c2 X0 L* c% w5 q" W1 o5 G8 n
/ a( j/ m1 W4 x( T3 }7 Y // This is a simple schedule, with only one action that is% i+ G! j( x# l, }. U& D
// just repeated every time. See jmousetrap for more$ U+ v# i& m A
// complicated schedules.
7 A: g/ U" ^) }8 R3 H$ k
: B% I; I4 u) U( b! a, o modelSchedule = new ScheduleImpl (getZone (), 1);; v* x& {; d$ f$ |- i) i4 o
modelSchedule.at$createAction (0, modelActions);
! U4 q4 |, F& }3 V) g
+ q* l! L8 I8 q' p8 A5 ~ return this;
6 W, Q; l" A* @2 Z N } |