HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& z. w" @2 N# g+ [/ o' `) _
, s4 h; A0 W4 ]4 z4 u- J* v5 |, A public Object buildActions () {0 C2 {8 W8 w& ~- ?; }( s9 i. U% q
super.buildActions();0 {- C( P) r& \" x( ~
/ r, W7 g- Y7 k/ Y3 K. w
// Create the list of simulation actions. We put these in
( p9 x3 E1 R1 }) j' b' @ // an action group, because we want these actions to be
; V- M- y+ a! Z; V* Q$ M$ o1 T // executed in a specific order, but these steps should# M# A4 Q7 e! U! g, Q2 y& [. J
// take no (simulated) time. The M(foo) means "The message0 \/ _' R$ ?& ]* x! w
// called <foo>". You can send a message To a particular. K8 X: T1 o( o9 ~
// object, or ForEach object in a collection.+ o- ^( L4 c6 A( Q5 k5 }
4 g0 E3 v" E0 `" E
// Note we update the heatspace in two phases: first run
/ t( Q, d& z, }, N% n4 V+ r // diffusion, then run "updateWorld" to actually enact the
& ]" T+ T% r7 J) U( L0 _1 q // changes the heatbugs have made. The ordering here is
, V# X3 a" h" r- z% E // significant!3 z" ~4 q" b( |
+ @) u" f( L2 ]# j6 x // Note also, that with the additional
+ P8 W2 [0 Q& R: @: r // `randomizeHeatbugUpdateOrder' Boolean flag we can
1 j% J! v" O z* U; k7 L // randomize the order in which the bugs actually run
+ {. g7 Y' U* m3 \' y2 S // their step rule. This has the effect of removing any
: i+ `8 y$ N7 `9 T& [ // systematic bias in the iteration throught the heatbug7 o+ `, r1 I2 v8 }
// list from timestep to timestep
, Y8 A* a5 F$ f / v0 a- w! P; X$ ?- l6 _0 r, @
// By default, all `createActionForEach' modelActions have. S8 \3 A. X2 R5 E v
// a default order of `Sequential', which means that the
) h& @; ?# o; J# @ // order of iteration through the `heatbugList' will be5 }2 F7 s; r4 [( a! P# k
// identical (assuming the list order is not changed
, t s! i$ R0 C9 x8 _0 v) f) n9 V // indirectly by some other process).: _! f# j" O0 w' e$ Y# e8 [
5 u* b0 ]7 ~; n( C& N
modelActions = new ActionGroupImpl (getZone ());
2 }& c8 G/ A0 R7 o6 p2 c! N; ]! ?/ K7 H; t4 n' B
try {! O7 s. {5 V+ I
modelActions.createActionTo$message
7 q/ i: W5 h; G1 B+ H (heat, new Selector (heat.getClass (), "stepRule", false));
& n3 W. z9 \8 _ } catch (Exception e) {
! P- ]2 T& t2 ~* M; Q1 I System.err.println ("Exception stepRule: " + e.getMessage ());
: c7 e) L9 E$ I0 {; }# r. s } s# D. y: U) q' `
. x. o b, T* y7 W try {& P/ q3 I! V; \4 e$ J; |" c
Heatbug proto = (Heatbug) heatbugList.get (0);
" Y, K6 a# R6 R" b. k8 p- _: t+ Q# }; U Selector sel = 8 J4 n/ E) q+ ~
new Selector (proto.getClass (), "heatbugStep", false);
$ r& L. `! Q3 }- J0 u/ j actionForEach =
! h# C/ R) _& J% h4 o/ ~ modelActions.createFActionForEachHomogeneous$call
+ T# ^! b8 s% _; f1 A (heatbugList,
/ k' h& B9 {4 l# D1 n new FCallImpl (this, proto, sel, Z) ]! c0 w- e( a! t* w
new FArgumentsImpl (this, sel))); J) P/ z$ Q1 J
} catch (Exception e) {/ y7 T1 \" b" x' y# O% w! m
e.printStackTrace (System.err);0 ~7 m5 ~* m9 E0 ~# ?( V; x
}
; S s( L+ }* c1 | Q2 J4 l
?9 \* q$ Q' |% Z1 _' N h syncUpdateOrder ();
" w6 b% P! I0 W4 N; z
9 ^" N( F; ^( n2 b6 L# ~ try {. w1 h" ~2 U/ g# e6 f# |! G) C
modelActions.createActionTo$message `- F4 u$ s+ q& P- f
(heat, new Selector (heat.getClass (), "updateLattice", false));" \3 @! q2 j" r9 ?0 b" Y V7 L
} catch (Exception e) { c# F8 p* G, }
System.err.println("Exception updateLattice: " + e.getMessage ());
0 b3 I! M+ G2 w/ A6 H }* B* C' s% ~7 c" _5 v4 l
& g' A* f" ]0 C3 A // Then we create a schedule that executes the
@) X. c* `5 B0 r+ R' P3 c // modelActions. modelActions is an ActionGroup, by itself it
9 t* z8 U: E9 K6 I: I | // has no notion of time. In order to have it executed in: B6 o& Z6 x+ x0 _
// time, we create a Schedule that says to use the8 @: x( B1 o I/ O; L0 v! Q
// modelActions ActionGroup at particular times. This
! N# B% x# Q. h" F8 r! O% A1 I // schedule has a repeat interval of 1, it will loop every
: [) ?& F2 x: H. a# O: h) y // time step. The action is executed at time 0 relative to* U& Z g b6 _5 l
// the beginning of the loop.
/ M8 h3 e- c2 N2 e
- T0 r6 M: L6 H3 P5 A8 D! i // This is a simple schedule, with only one action that is) t/ a) Q; W/ U, `# W. W7 b
// just repeated every time. See jmousetrap for more
- H% Z+ Y& O- ` // complicated schedules.
1 O9 y% w q s$ ^1 y0 o t* ]7 } ' H1 y/ e) z; R8 D& j
modelSchedule = new ScheduleImpl (getZone (), 1);0 D5 f; d* L2 E$ ?8 E
modelSchedule.at$createAction (0, modelActions);& x& {8 B/ w; {, b. {
- a' _6 \. U9 F- e- I0 | return this;
1 X) _4 k6 @5 \' c- `/ w) p+ L8 b4 t } |