HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 ]# [0 O5 |6 y) E9 B
8 ~. G' _4 n; U0 G- i* y+ d7 [ public Object buildActions () {! j8 b$ T- a1 r( W3 k8 _. l- n
super.buildActions();
. |; Q2 o2 J8 V ?0 A8 n
9 e7 Z, V1 z. f // Create the list of simulation actions. We put these in6 K# R9 u! C6 q6 ] S: Z
// an action group, because we want these actions to be) f; x3 n* A" f) j) j0 o9 V
// executed in a specific order, but these steps should
" K9 x k) G0 x# |+ ? // take no (simulated) time. The M(foo) means "The message
) C: v1 q+ |7 a2 G // called <foo>". You can send a message To a particular3 z' I0 l& N* y, C! t2 t9 V+ a
// object, or ForEach object in a collection.
9 v; S7 T7 C6 z$ I% r 7 F9 ~- t7 r3 \$ D% Q4 |. ?5 y
// Note we update the heatspace in two phases: first run
" n) v7 ^6 K! B% | // diffusion, then run "updateWorld" to actually enact the
/ ~) y0 r0 M+ f$ g! G. r // changes the heatbugs have made. The ordering here is I! |5 V( d; z8 f
// significant!
$ o7 e) q8 D! y4 @- N - }# n) k/ q. K$ M; _
// Note also, that with the additional
0 P0 O1 q- F, y- o3 C- K5 c, M# I // `randomizeHeatbugUpdateOrder' Boolean flag we can
8 M3 f: b# a- J+ t // randomize the order in which the bugs actually run6 ?9 I! p5 o y- i% P2 S7 r$ f
// their step rule. This has the effect of removing any4 {: z( W: R* }; h8 ^5 }
// systematic bias in the iteration throught the heatbug
# _1 C ]4 u9 L8 b/ t8 T. N m // list from timestep to timestep
0 o. l. m" t# T4 N& I! n, ~& F
! t, U; ]2 l: J1 L6 L // By default, all `createActionForEach' modelActions have/ n9 g. O; b8 T2 q+ B
// a default order of `Sequential', which means that the9 }! ]8 ~, Q, q0 N, L8 s, _9 C- P
// order of iteration through the `heatbugList' will be
- t; f- a' h- D( V/ L1 l // identical (assuming the list order is not changed: C* x* C1 ?+ x8 k1 _3 L9 V
// indirectly by some other process).
& h! {6 q3 M L, t+ c8 y
$ t0 T4 j7 q# D% H2 M' X9 ^ modelActions = new ActionGroupImpl (getZone ());( {9 \/ {' w; @+ f; ?9 }3 \
& g, L* y% ]- I) w d& m4 }$ ^; B try {" K& a) g6 K- z' r$ M
modelActions.createActionTo$message
, g$ A! B. l. `+ A4 H. C. A3 ~& _ (heat, new Selector (heat.getClass (), "stepRule", false));+ \4 [$ \9 |9 [
} catch (Exception e) {
* K" W D0 o6 }# V System.err.println ("Exception stepRule: " + e.getMessage ());
& Z6 a5 Q0 J2 n- m5 U& } }
! s% S8 }+ C$ S8 `: S% O( V( J1 G4 Z" {" u
try {' f, A& T, C9 p+ h
Heatbug proto = (Heatbug) heatbugList.get (0);- w4 j- _/ Y" @, B; o# h3 Y9 r0 M# [
Selector sel = 2 J! m) \& k+ y" I" w" W
new Selector (proto.getClass (), "heatbugStep", false);3 \4 h6 e: E! G$ _. g* u; J
actionForEach =0 b; b1 p# z4 M& R5 F$ ]
modelActions.createFActionForEachHomogeneous$call2 p9 d* k: R2 l {7 l {
(heatbugList,
# b+ w/ e- P! h, k% O8 V* P* S new FCallImpl (this, proto, sel,
8 B& w$ W# I! p new FArgumentsImpl (this, sel)));: f+ z! ]# `0 u: |* k
} catch (Exception e) {
, f$ s7 H8 f& [5 Q e.printStackTrace (System.err);' \$ P9 L0 A3 R1 k- W/ ]8 X
}& S/ X7 a8 G( w: z6 A" Y. `
% @3 h9 P5 g8 V: d# o syncUpdateOrder ();
+ ~/ e3 Y4 V) s# f
* K9 f5 @8 K7 A- U try {
- O' B6 Q5 E% K* u8 {; |! i modelActions.createActionTo$message
2 W3 U$ D0 L* Y$ U9 y( C& H9 y' [ (heat, new Selector (heat.getClass (), "updateLattice", false));, F a- R7 }+ J, Z
} catch (Exception e) {
# b, R+ |( W9 t; q System.err.println("Exception updateLattice: " + e.getMessage ());
' v+ k$ c R. g: c' m c$ ^' s }
6 w4 L- G/ f1 L; @4 L$ v& f
+ [3 H; e! Z4 h: Y // Then we create a schedule that executes the4 Z( g" F) ?4 A# }2 c
// modelActions. modelActions is an ActionGroup, by itself it" l8 r7 ]. B3 [$ u* [
// has no notion of time. In order to have it executed in( c q( H1 z; j( S) O; M; |: c
// time, we create a Schedule that says to use the
! W ]& t" i- R8 l/ e( @5 g/ m // modelActions ActionGroup at particular times. This
& Q# q- F0 f! {& l: ] // schedule has a repeat interval of 1, it will loop every. a( W* b' g. F1 g: O
// time step. The action is executed at time 0 relative to9 r' J( g0 P6 {
// the beginning of the loop. E! H5 B U: w* U) ~
6 ]9 `/ ? K5 W0 U6 b3 F8 T
// This is a simple schedule, with only one action that is
( N2 L$ {* ?+ @3 g // just repeated every time. See jmousetrap for more0 g5 r: z3 H/ K. q8 v& {! ^8 U
// complicated schedules.
9 d. r- `( |7 m8 ] ' m: n4 m3 c4 r' h; q* \
modelSchedule = new ScheduleImpl (getZone (), 1);5 r) P$ @7 v4 G: j0 {1 G8 I
modelSchedule.at$createAction (0, modelActions);
) H2 E: r8 @5 I- x# }% q- E
3 c0 ]& q; S9 [5 T& X0 g( c return this;
1 y. ^* n' W q1 }0 R9 M. m } |