HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ e' t$ Y2 r5 i* ~
: ]6 |' K- l! `) v. i/ ^5 _ public Object buildActions () {0 q; K K( \" U$ O6 H
super.buildActions();$ i2 A, [6 t" y4 ?0 G7 X+ @
* Y4 V1 i; j4 U- O t // Create the list of simulation actions. We put these in4 u9 R: T, i; N. a8 g P3 a) i/ a
// an action group, because we want these actions to be
! Y/ P6 k; p( w& d% h // executed in a specific order, but these steps should
4 f8 ~8 N# F: m$ e% g/ C% o // take no (simulated) time. The M(foo) means "The message& t( b5 h* L# T# q6 n6 X$ U0 z
// called <foo>". You can send a message To a particular
" Z/ W# B, h" g1 n% X# W // object, or ForEach object in a collection.
6 d" U0 ]0 d! o$ d; c: F % @* l0 w; c6 X F' m/ {
// Note we update the heatspace in two phases: first run
9 L3 n7 [" g( V V // diffusion, then run "updateWorld" to actually enact the
5 V0 u7 D9 b4 U9 |2 ] // changes the heatbugs have made. The ordering here is
: K! S2 a& A, {7 s, K // significant!+ a" W; n* W+ O2 q/ p
; e0 W( p* G$ |+ U# j
// Note also, that with the additional |" {/ z$ H. b) a9 ^0 ?
// `randomizeHeatbugUpdateOrder' Boolean flag we can- Y! l. e5 H* q% ?' i& ~
// randomize the order in which the bugs actually run
: y8 y* [3 E, `# D // their step rule. This has the effect of removing any
* U- K; ?! X$ `4 X7 C! J# T7 i // systematic bias in the iteration throught the heatbug5 E2 f8 v% T8 y$ b/ S5 u
// list from timestep to timestep
5 v1 u! z0 |5 o% `3 k- r4 {7 v
8 j. A/ [. Y) o8 @ // By default, all `createActionForEach' modelActions have4 R7 U. p; l9 H( s* E% v; R
// a default order of `Sequential', which means that the9 p; P* G1 f1 u( Z3 T' n
// order of iteration through the `heatbugList' will be
8 H" H! q# f3 o% j // identical (assuming the list order is not changed
+ x$ o( |/ v ]! O" D" X // indirectly by some other process).0 p" o# r8 D, w& u
. I2 w1 \$ W; ~2 l y
modelActions = new ActionGroupImpl (getZone ());
6 h* F9 p) ^- z/ z, X4 ?5 L, ]% C/ d
try {
$ n2 k' l0 z3 P2 l% l; n modelActions.createActionTo$message
5 \1 t/ _' W$ k7 t3 r (heat, new Selector (heat.getClass (), "stepRule", false));1 Q6 s: @" _, a; d P4 M
} catch (Exception e) {$ {8 a8 d: _3 i) c2 q% Z9 I* ]
System.err.println ("Exception stepRule: " + e.getMessage ());
1 X& D+ o4 B4 ~- S3 w }' d) Z6 w, i. Z, R
2 x. y- P W" F$ v8 |9 ^
try {& E5 f' A% c3 r$ }
Heatbug proto = (Heatbug) heatbugList.get (0);
, J* j( H7 i9 f( H( @ Selector sel = * c# g4 z2 m0 g% u6 `4 z
new Selector (proto.getClass (), "heatbugStep", false);
7 c7 D* A. O5 Y e actionForEach =
0 @: O e; T6 s( b modelActions.createFActionForEachHomogeneous$call
9 V2 Q) X" _& D) d8 l/ [# Z" c5 S (heatbugList,
5 U1 U) F4 `# Y3 p8 D/ B new FCallImpl (this, proto, sel,% P# l8 N1 p) {! q( ^5 y/ O
new FArgumentsImpl (this, sel)));( [1 U3 Q) a4 x' B! d0 t% [& K' ^
} catch (Exception e) {. ?% R+ M0 _+ P: }# K$ o
e.printStackTrace (System.err);
k9 ~+ c( N4 H }
1 ]" f, [1 C/ y. _
) W. [6 Y0 r' D- a' K: [ syncUpdateOrder ();
: M' p+ a. U0 q7 b( l" D; J7 ?1 k8 m9 T1 |, ~
try {/ C/ C J4 T" _) N# v4 q; F8 _
modelActions.createActionTo$message
( L; J( }2 j/ c3 F (heat, new Selector (heat.getClass (), "updateLattice", false));
$ I" a+ y' U% O6 F } catch (Exception e) {
! W, E7 ^8 h. e3 `$ i System.err.println("Exception updateLattice: " + e.getMessage ());
, W. i! Y7 b* Z. m0 d }
/ n9 H) h% n3 G
, b; P5 }6 N7 [/ D1 Y' j // Then we create a schedule that executes the
* T+ v! O( b3 y // modelActions. modelActions is an ActionGroup, by itself it
( a% u* G/ ?0 K' A% D // has no notion of time. In order to have it executed in! I% M8 W9 A: o2 t. ]! L# N
// time, we create a Schedule that says to use the
! ~" P7 F6 l% I, n5 x) P // modelActions ActionGroup at particular times. This/ o: p; M2 U# P2 f- |# ]# ^1 U( R& W
// schedule has a repeat interval of 1, it will loop every
, M- l" i P& j: n // time step. The action is executed at time 0 relative to
- \, T# T5 \+ O! F/ E0 ]+ a! h, A // the beginning of the loop.( z3 Q0 f, [ K2 c. u0 ~; T7 B6 f# |
9 q% a( i7 g: P( s% \$ ?9 z* \/ |
// This is a simple schedule, with only one action that is
3 z8 F9 p) v: N // just repeated every time. See jmousetrap for more
( a% g8 H3 Y, v5 V# y6 u+ j- i // complicated schedules.) [# Q& w) Y z0 R- \
# h$ J) B' d' I' v: e
modelSchedule = new ScheduleImpl (getZone (), 1);7 M# l- E7 k. `+ M" \0 F3 ^+ `
modelSchedule.at$createAction (0, modelActions);6 h& J; x) M. W7 H0 g
1 ^1 \# O/ W5 a& B# T
return this;; E0 v* Q2 K( u
} |