HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:) w2 m" C" L' Y
+ i: V: N5 h& d: V public Object buildActions () {
- }+ o% a) l+ W8 H: b9 b3 l1 X super.buildActions();$ \1 V) ?, P5 y4 D& A1 B7 `
9 O2 f# Q c/ a, w9 E) w: Z
// Create the list of simulation actions. We put these in2 N' \, v0 v, D
// an action group, because we want these actions to be
( d. ^# O$ U# v D$ q o( L0 O // executed in a specific order, but these steps should* M& {6 Y$ t, t5 Z7 {
// take no (simulated) time. The M(foo) means "The message
" y# `6 u' D4 t0 U$ k // called <foo>". You can send a message To a particular6 N% o5 |8 X" m& I
// object, or ForEach object in a collection.4 b% T- i# T- ^% z" J
" Z2 \+ G( l' |. U7 e1 y
// Note we update the heatspace in two phases: first run; z8 w5 |5 H+ _' d' G) L
// diffusion, then run "updateWorld" to actually enact the
3 i1 k2 t) s- y- ]% j: M // changes the heatbugs have made. The ordering here is
3 O% k' P$ d* B8 [& K0 c: U5 N // significant!
% K; g# c; b; ]) o2 P
7 Z5 @( Y( k1 _% o! w5 K // Note also, that with the additional
( \2 U' B3 w! C3 l // `randomizeHeatbugUpdateOrder' Boolean flag we can
$ p0 T) u, N1 v5 `% h // randomize the order in which the bugs actually run, L' C* \: R2 b
// their step rule. This has the effect of removing any
* F! v1 b+ D" L5 ]8 h$ I // systematic bias in the iteration throught the heatbug
* c3 ?9 F2 A# s5 y" J // list from timestep to timestep- r4 }. v! k! x
7 w+ S7 e( W" U$ `! I
// By default, all `createActionForEach' modelActions have
! C. t& h X6 e; w3 H' K // a default order of `Sequential', which means that the' S, X2 h: q9 d
// order of iteration through the `heatbugList' will be
. ^7 y6 P. X8 G4 R // identical (assuming the list order is not changed7 t3 E4 k6 Y5 u2 Z) d. a
// indirectly by some other process).
* S8 `1 \, W/ Z, ^( p, k
' r' H ^- l/ {$ [3 A4 p modelActions = new ActionGroupImpl (getZone ());
7 b" |2 I5 p$ k3 {
; v0 T# h: K; p: B8 Q+ _ try {
. `1 j2 c7 ~$ d& C) O9 L3 l: B modelActions.createActionTo$message
2 s# H k/ h; u9 N2 P (heat, new Selector (heat.getClass (), "stepRule", false));
% i3 R1 W# ^. ]+ N6 b } catch (Exception e) {0 l5 k, I! I% e5 I; m
System.err.println ("Exception stepRule: " + e.getMessage ());' j; V' ~+ G7 l/ E9 n& H
}% g9 W+ n9 ?8 `9 O
2 Y, e* `+ ?& s try {
# q4 K/ B) `+ {7 T1 s Heatbug proto = (Heatbug) heatbugList.get (0);* b. n% t3 L; N$ Q6 E. s7 ]+ U; z
Selector sel =
- R* y0 V$ o- L9 i! L& z new Selector (proto.getClass (), "heatbugStep", false);
# }+ @1 D( V; L! Q actionForEach =
- P' S% x! B6 F* T9 a* }/ w modelActions.createFActionForEachHomogeneous$call
! b3 u5 K# I" O3 ` w (heatbugList,
0 C! ]: E* B6 P new FCallImpl (this, proto, sel,
H; Y b/ B' h$ v7 f- H6 Q new FArgumentsImpl (this, sel)));) y/ Z$ b! l" S z- ^7 q
} catch (Exception e) {
- `6 `+ V1 m) C! R# x e.printStackTrace (System.err);- l I% N* g% }" j
} o C- ?) L1 d7 T8 q; D
. h/ H L3 X# X+ D
syncUpdateOrder ();
" W% w+ c, [: }- c6 `3 L/ B
) {- D8 e+ v6 ~5 U' J( C/ P0 \2 b try {2 o8 [. x+ H+ e$ }
modelActions.createActionTo$message & x' X2 U9 R3 \
(heat, new Selector (heat.getClass (), "updateLattice", false));
# j% H; x2 i( y0 e, J& q- ` } catch (Exception e) {
8 w4 B& O" u9 d3 K: P System.err.println("Exception updateLattice: " + e.getMessage ());
, i/ ?% k) Y( D- V }
3 j) m& z5 Y |4 e
+ f* Z5 A ?. P // Then we create a schedule that executes the
; M; `: d: ^, S- j0 j3 ?9 F // modelActions. modelActions is an ActionGroup, by itself it
$ _' Z3 R/ _+ O) s5 _8 _/ h+ V // has no notion of time. In order to have it executed in7 b6 c. u! t, n
// time, we create a Schedule that says to use the
( x: V% x2 `. i // modelActions ActionGroup at particular times. This3 f! _5 G8 R1 B6 {! G
// schedule has a repeat interval of 1, it will loop every* i+ y p4 r T+ J) F4 X
// time step. The action is executed at time 0 relative to
# Y7 F5 Q2 l) Y% v6 o // the beginning of the loop.
9 { n1 J( `4 L% G/ V# u e( N! H' ?0 M7 S0 ^) H! q9 [* D
// This is a simple schedule, with only one action that is6 I) r( L t3 y# x* E8 T9 }
// just repeated every time. See jmousetrap for more
* Y' |9 w* ~8 {6 w" ?1 x! [ // complicated schedules.
$ d8 z* ^* J8 ~
/ F# k1 a9 ^ l/ x! Q modelSchedule = new ScheduleImpl (getZone (), 1);$ M# E N+ L% N! x
modelSchedule.at$createAction (0, modelActions);- g; Q! S0 d- y9 u2 e7 J% \* F
" |$ V7 g+ @, a5 H
return this;, R" u) K" `! B) N) m! q" U% g* V9 s
} |