HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 u; m9 q+ q4 ^. | G0 {9 V) j8 V( g( a
public Object buildActions () {5 W% W6 u0 j3 p" e: p) C
super.buildActions();
7 k8 {+ ]9 n8 Q/ Y& K! U: x4 x % x% b1 V f5 C2 t+ g' R+ b
// Create the list of simulation actions. We put these in
/ F+ x( r X/ h6 `# ?8 f# c$ ^ // an action group, because we want these actions to be
, b7 t- T* f9 ]( W+ [ // executed in a specific order, but these steps should; w3 ]2 g5 Y8 O5 n- C) k) m
// take no (simulated) time. The M(foo) means "The message
7 j! G8 U3 ~. h: ~; S6 e( W // called <foo>". You can send a message To a particular
% r- O M/ d6 Z% k, S // object, or ForEach object in a collection.
& E& H5 ~9 |/ _" ] ( z7 w) D/ y7 C6 p; M
// Note we update the heatspace in two phases: first run; G/ K& W6 W7 z9 M/ }
// diffusion, then run "updateWorld" to actually enact the
4 ?/ ?% r' w) ^, L# f, B# U // changes the heatbugs have made. The ordering here is
/ j8 f2 M6 S& T! I // significant!
" j! T2 w6 ?4 b% S" v
o9 h0 R9 ?: p: E/ F // Note also, that with the additional7 v. i% @; H& q6 ]/ {
// `randomizeHeatbugUpdateOrder' Boolean flag we can
% B* ^" q6 j2 p$ @" e$ N // randomize the order in which the bugs actually run% {) N1 A3 O3 d4 _
// their step rule. This has the effect of removing any1 V- ~8 @( d4 ]! N% l% b
// systematic bias in the iteration throught the heatbug7 ]$ M5 s) d3 w7 J: [. }- {
// list from timestep to timestep
8 K$ k4 {- Z& u1 ]
, ]4 `; z/ ]/ o5 `" c // By default, all `createActionForEach' modelActions have
* k% i# W1 c V. L4 ~3 S5 \2 M // a default order of `Sequential', which means that the
( N- ` r; S9 X2 O // order of iteration through the `heatbugList' will be0 w1 _$ H' M2 M
// identical (assuming the list order is not changed
( Q9 u& W/ M# Z6 H4 N- p+ T // indirectly by some other process).
% v8 h8 v6 L5 B* a( A$ ^
7 M- F9 N# `8 q, T1 w' A5 A modelActions = new ActionGroupImpl (getZone ());
( X& t1 y% m" o! U
2 Z" d5 O( z. k$ i- `: X% l2 E4 I try {- g# f$ i3 G; a
modelActions.createActionTo$message7 g8 z. y# @+ @. ^9 R- ]6 E
(heat, new Selector (heat.getClass (), "stepRule", false));
: g; h/ R; }6 N t } catch (Exception e) {. g2 @. n1 \% U2 T) D$ O
System.err.println ("Exception stepRule: " + e.getMessage ());0 q7 q8 B5 s* t" C8 ~ m H l
}: x [' r' i3 K/ y6 k/ e6 @
( P2 [" T! k7 f) e* U6 ^ try {
* P9 h, f" a; ~ Heatbug proto = (Heatbug) heatbugList.get (0);
( H8 [+ m8 [) Q Selector sel = ' d% J" ~7 A2 D# G2 k) r6 X, N- O0 O$ ?
new Selector (proto.getClass (), "heatbugStep", false);
$ P$ U4 h- q$ v# ^$ E7 a6 x: z actionForEach =# B8 v, w/ m5 u. S6 O7 H
modelActions.createFActionForEachHomogeneous$call- L- q% j1 W% f( x
(heatbugList,% g' g: c+ D+ [* q' `1 x( i
new FCallImpl (this, proto, sel,
( j8 ^3 z/ n F9 f+ U new FArgumentsImpl (this, sel)));: Y; M1 @" }2 D* y/ P! u8 j
} catch (Exception e) {! s6 R" ^; M8 G6 E$ H5 L
e.printStackTrace (System.err);1 X* {7 u5 `! X$ c3 R$ x
}
# ^, |9 M8 p+ K( J. H: N6 I) q
7 z6 A" ^. j1 G0 |4 X1 } t" F syncUpdateOrder ();
! g! m6 \3 V4 k9 l
+ t% Z* ? w' e' L" N( c try {
/ P! W5 G% r. G4 S- o1 y$ m modelActions.createActionTo$message % W& c+ V3 x s) t/ i7 @
(heat, new Selector (heat.getClass (), "updateLattice", false));# J4 R) q; @7 I7 c8 d) C
} catch (Exception e) {: U1 Z7 t7 x( Q8 j4 z7 E
System.err.println("Exception updateLattice: " + e.getMessage ());
$ q# W, z" o" n1 t8 ~% `/ \ }
: ^! s. z( w5 n7 G1 z
" ]! _+ s$ F% ~+ m+ A8 K$ w // Then we create a schedule that executes the
$ L, k( a9 {# F3 L: C9 n0 B" D( y // modelActions. modelActions is an ActionGroup, by itself it
+ p( P. ]7 V9 B w8 N7 e // has no notion of time. In order to have it executed in5 T4 r/ B( \; l. T! q0 g) q
// time, we create a Schedule that says to use the
1 K; D2 a, q- t, s // modelActions ActionGroup at particular times. This6 F4 s* Y( U$ i i9 Z" @/ P/ ~$ p
// schedule has a repeat interval of 1, it will loop every
4 f8 H6 J& n2 i // time step. The action is executed at time 0 relative to: h& Y3 t& ]8 U/ s4 v
// the beginning of the loop.% t8 j; O* c; Y# N |6 K6 b5 i
& m1 Z8 \0 d9 ]9 ^ G! L // This is a simple schedule, with only one action that is$ P. C2 r+ w8 j9 T" e
// just repeated every time. See jmousetrap for more) b0 D3 t. _5 j& g3 c
// complicated schedules.
0 S$ d9 H ?8 |0 b
) M( S: D/ O+ b {8 n/ H modelSchedule = new ScheduleImpl (getZone (), 1);# p) K5 s, R# t1 \, T% d
modelSchedule.at$createAction (0, modelActions);
2 M' X# `$ z* i. \6 A ) @( x( j" B% M- ?+ m4 L
return this;! [' L! V6 r \ u. D
} |