HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. t5 L$ ]# z; X+ k; P' a E
9 G3 N/ M8 [; a- j; W& g3 k public Object buildActions () {
/ n4 A3 x% C f2 P" m super.buildActions();7 y8 g5 k: _2 H0 n( L& U, t
7 Z7 q: }; t; | j# a/ g
// Create the list of simulation actions. We put these in
) L% `$ g# g7 N# ? // an action group, because we want these actions to be- `& g$ T- ?; b8 ], p) H, F9 F, n3 I
// executed in a specific order, but these steps should
+ b b. @: X( j: C7 p2 Q% i r // take no (simulated) time. The M(foo) means "The message
/ A6 M: z0 ~. m // called <foo>". You can send a message To a particular
; t" K& i$ l+ `4 S. L% o! u; z1 j$ H // object, or ForEach object in a collection.* l/ R: h* L9 O8 g) i/ D
3 \) t* Z+ U. y k8 I7 u: D
// Note we update the heatspace in two phases: first run: ^# _- z; N# \5 r& F' C
// diffusion, then run "updateWorld" to actually enact the; m6 V. x. v# W
// changes the heatbugs have made. The ordering here is, N7 a. q1 S. z0 l3 W
// significant!4 m2 I5 D0 o) l, e
* y- c- x0 z5 `0 w5 i // Note also, that with the additional
" Q A6 g; z) n3 T/ d // `randomizeHeatbugUpdateOrder' Boolean flag we can% K; A! H# X9 ]8 \2 n
// randomize the order in which the bugs actually run5 Z" [) q: y* f# m/ F y! N" i; I
// their step rule. This has the effect of removing any! Q" Y" X) Q9 e/ f6 K+ j7 O0 `
// systematic bias in the iteration throught the heatbug
: k3 I) {, K5 r% V! `) ` // list from timestep to timestep
0 K6 z8 k7 B' a7 J- M # I0 }* ]. F& Q; \
// By default, all `createActionForEach' modelActions have/ y0 `* l% p: g) D% {5 w( w
// a default order of `Sequential', which means that the
( H7 W9 f( w6 q // order of iteration through the `heatbugList' will be
; ^" o; f) ]. p' S. }* i // identical (assuming the list order is not changed
1 w Q$ L* S. @) ^+ t // indirectly by some other process).9 ?( x3 r! G+ L1 A, L1 S' h
6 f, b: B- v! k" e: D% }6 `
modelActions = new ActionGroupImpl (getZone ());* U3 r0 \3 D4 W7 p
7 L- n, r7 d0 |) V4 { try {+ x: o. r3 S3 f
modelActions.createActionTo$message" l) t4 S0 T5 l6 n. {- q
(heat, new Selector (heat.getClass (), "stepRule", false));# U! i( D+ J# }/ T
} catch (Exception e) { l5 ~) R9 h% g6 _
System.err.println ("Exception stepRule: " + e.getMessage ());) r5 E" F+ m5 K) |
}
# L B) O1 R7 A5 F4 M
9 f8 R) V" s) f& a0 g try {
, ?% |: X1 ~& y; D Heatbug proto = (Heatbug) heatbugList.get (0);# {# ~. k. @. U* P5 A# O5 i
Selector sel =
4 k3 W$ d+ Y% r% N new Selector (proto.getClass (), "heatbugStep", false);7 D, T5 y d. i; {, V
actionForEach =3 [9 @, ~2 o+ P" L0 V
modelActions.createFActionForEachHomogeneous$call
- L8 }2 }/ a0 C. V( j( m* i' @0 ^ (heatbugList,2 g Z" n, @6 K* e1 |* u) s% t, N
new FCallImpl (this, proto, sel,
, S0 m1 S/ _4 S& {+ v" } new FArgumentsImpl (this, sel)));6 @. a& F! U9 h1 E+ d, Q" @' E
} catch (Exception e) {
' \% Z/ `6 q; Y5 O e.printStackTrace (System.err);
( j: `# O, F9 s# z; h }$ l. d) v# z- |2 F3 L
5 e I! a3 \1 M- N1 }/ r4 f' r2 D syncUpdateOrder ();
$ B" ?- O+ w6 H. N( P* V+ q' U
2 `4 X) ~3 k. Z; Z try {
1 b$ u6 w7 u) n+ q% ` modelActions.createActionTo$message
0 M* c3 G& n! S+ B+ U (heat, new Selector (heat.getClass (), "updateLattice", false));
8 c/ X' O4 w+ i1 q8 O } catch (Exception e) {: {4 l% K: F$ S
System.err.println("Exception updateLattice: " + e.getMessage ());4 w w6 S% {" _
}6 v8 r) g U, g A- t* ^
6 X7 P6 G' J8 I
// Then we create a schedule that executes the
5 l' [) ^! g& L* ^/ l // modelActions. modelActions is an ActionGroup, by itself it
5 O$ T9 `1 {+ v# w. q' j5 _& _ // has no notion of time. In order to have it executed in+ C+ |) ~8 O* b/ X
// time, we create a Schedule that says to use the
( V- m2 D U& ~$ C7 L- S // modelActions ActionGroup at particular times. This0 M# y+ h& l- ~% a" p. ~
// schedule has a repeat interval of 1, it will loop every
3 B4 e$ R4 L- S: Q0 |1 H. {+ L2 F' v& ^ // time step. The action is executed at time 0 relative to
4 o% n. a3 G1 W: V6 A! w! z // the beginning of the loop.
' Y$ S/ \6 A/ A6 {1 A
, b( {+ v( O u2 I // This is a simple schedule, with only one action that is: c& H* F/ O1 S8 ^* j% L: q1 Z$ {
// just repeated every time. See jmousetrap for more2 i1 U! J5 Q1 o1 m& K
// complicated schedules.( L4 ]7 m+ K c' m
- Q* R* ]- z6 O2 W modelSchedule = new ScheduleImpl (getZone (), 1);
5 c; m0 R9 }4 q$ |# ?0 ^ modelSchedule.at$createAction (0, modelActions);
. z$ f* l! \9 v
8 O7 B8 E8 Y) { return this;
% v* B2 L! |" X } |