HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
# @% x0 \) I3 b/ C2 B4 g: l. z5 q( l+ p. a8 I) W; y P
public Object buildActions () {
7 z" v |' i5 G( e Y0 _! z super.buildActions();
2 q) |0 M, c) F' s/ D% d! ]- s3 R
5 k- c1 D; r* @8 v) l // Create the list of simulation actions. We put these in4 y, h" j9 @. f) u! \( s
// an action group, because we want these actions to be2 Z- u( J& G3 o6 U
// executed in a specific order, but these steps should
# ~( M+ u3 Q, k/ |9 B! a; }6 Z; M, D // take no (simulated) time. The M(foo) means "The message
; M/ I4 O2 C" j$ K // called <foo>". You can send a message To a particular
$ ^5 d1 p" v: B& u // object, or ForEach object in a collection.+ F% F% x: `" W% H3 O) g0 n
8 d# U& w/ a8 o' X ?# @) F // Note we update the heatspace in two phases: first run, ?* N1 ?* w7 w( |0 ]! Q
// diffusion, then run "updateWorld" to actually enact the
" f; L) ^0 M1 K* b3 C // changes the heatbugs have made. The ordering here is
& g b: ]3 g' D V' t" p9 ] // significant!, Z1 Q$ {" U% ^# N1 u
* K/ c6 B+ ]7 X4 g5 J; S$ i( ^
// Note also, that with the additional4 H2 W6 O5 v. d' D/ k
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ o1 f' n5 ]3 B; U
// randomize the order in which the bugs actually run
: i- s& r+ C) l H& k // their step rule. This has the effect of removing any
9 P* t6 |8 X F( a+ v // systematic bias in the iteration throught the heatbug
8 v( j( e3 L. j2 | // list from timestep to timestep
' d. t! o9 H7 `% K( D- N. Y& t6 J
; d& s+ \+ d2 X0 n7 V // By default, all `createActionForEach' modelActions have7 ^. s o' y7 A! u( d" u; a
// a default order of `Sequential', which means that the
. L( {3 T( O! d4 ~ // order of iteration through the `heatbugList' will be
, ? s5 n* B" i& v; Z // identical (assuming the list order is not changed
; _5 C4 ?1 b- W0 ` // indirectly by some other process).& e* R8 {# K0 u& R/ H& d* A
$ d( I0 A8 P! _: c: r modelActions = new ActionGroupImpl (getZone ());2 E4 H9 q8 s: \6 E" k+ i# ?. ~
! ~+ v% g8 h. Z, s# }0 v3 [
try {6 w" v: N- D% ?( d
modelActions.createActionTo$message5 d z u- d6 s, b
(heat, new Selector (heat.getClass (), "stepRule", false));- |7 T8 p: c7 ~
} catch (Exception e) {1 g t4 ^) ]8 L/ r
System.err.println ("Exception stepRule: " + e.getMessage ());/ U; `1 Y& V% D8 m( b. Z" b
}9 Y: B* L1 ~) U d$ G+ _5 `2 @. m
! j; T8 Q6 V: O% Q# @/ y3 d try {3 N y, U3 ~2 m! H4 ~' Y
Heatbug proto = (Heatbug) heatbugList.get (0);
- c; K; l; B% \' F3 `. v Selector sel = 3 \0 F8 a5 W3 ]# e
new Selector (proto.getClass (), "heatbugStep", false);
! }3 T. H1 X& | actionForEach =5 ]9 k4 c* P3 j
modelActions.createFActionForEachHomogeneous$call! H8 @3 B% a1 b0 t; h# P
(heatbugList,
7 ?7 L" n) a- ~" f new FCallImpl (this, proto, sel,
9 k$ M! a, L5 C( s1 m5 y& C new FArgumentsImpl (this, sel)));0 W3 D, `; ~" n6 k. E2 e H
} catch (Exception e) {
# G e0 Q6 B; f9 S* h, K' m# M e.printStackTrace (System.err);4 q, W1 H3 |3 a7 j
}
3 G+ \- G5 k2 \( i) O6 {
* g+ g' D1 C. ]( r: n& g( Q7 x syncUpdateOrder ();0 ~& L/ l4 J7 w0 F5 s7 k
* m4 M; p: D5 g try {
. X* a$ M( M T$ Z modelActions.createActionTo$message + e2 ^! n4 k! j( f: P% K
(heat, new Selector (heat.getClass (), "updateLattice", false));
) ~. l1 {# v! a0 C; r l3 T } catch (Exception e) {
3 j5 P+ w3 Z) D- o. C System.err.println("Exception updateLattice: " + e.getMessage ());; W8 v t9 y! K- w) N5 V: C+ B
}
' x8 k. A+ F" T3 p ! l7 G9 s. U4 x- J# |/ A3 p/ y& u
// Then we create a schedule that executes the
6 @" x# W @; M d5 U# l) i& c // modelActions. modelActions is an ActionGroup, by itself it
( k3 d. C+ {, W, D // has no notion of time. In order to have it executed in
0 B) A, S5 y' n! K) j2 y // time, we create a Schedule that says to use the! s# ^! V: a1 ?4 _) w
// modelActions ActionGroup at particular times. This
& r' v' B7 g" o; u* L // schedule has a repeat interval of 1, it will loop every
: O1 f) e, g5 i- w, F- U! z // time step. The action is executed at time 0 relative to
& S3 s/ l) e( l // the beginning of the loop.
0 q9 ]: W \$ M5 [
3 M, ~7 n9 C' x- R; T4 j X // This is a simple schedule, with only one action that is% }$ M# M+ w7 V3 l" ~5 Y
// just repeated every time. See jmousetrap for more: D8 `: y7 F3 \% I0 Q
// complicated schedules.
& F: L% p; Y- s; m8 k5 w" J; P
: W X; S$ z [" G4 ? modelSchedule = new ScheduleImpl (getZone (), 1);1 h% ^- M- M( ]% n6 U9 Z8 R
modelSchedule.at$createAction (0, modelActions);
0 o7 p# ?; E; t7 a9 s7 Z
6 G- s9 k7 e l1 P. [( P6 f return this;" i6 t& {# ~& d6 R" o4 A
} |