HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* t3 n" n- N r* y; [3 X! Q s7 {! W/ @
public Object buildActions () {. W, M- `( F, }% K9 @
super.buildActions();$ u7 @* t9 v1 P" b4 Y0 J
! `# I, [0 \+ F6 H2 A1 X$ ?
// Create the list of simulation actions. We put these in+ J2 E* J9 C; ]% c1 k) a
// an action group, because we want these actions to be' @; |$ Z4 v. ~) k1 n* j
// executed in a specific order, but these steps should/ \& a9 S5 v8 r# ^3 i- p- L' z
// take no (simulated) time. The M(foo) means "The message
8 I% I6 |: s5 ]3 m2 A9 x" z // called <foo>". You can send a message To a particular
2 R% i* M6 D+ M7 u. o5 m$ Q) y* { // object, or ForEach object in a collection.
$ x( K" C9 Q9 G" o" D8 `
8 K+ F$ R3 p$ L // Note we update the heatspace in two phases: first run
: }8 k* R+ j# h, M // diffusion, then run "updateWorld" to actually enact the
# ?6 r7 t3 j/ K // changes the heatbugs have made. The ordering here is
1 k4 U; p5 |7 ]! Q // significant!
0 s+ {" A# x8 q8 u% c , j9 r$ ]7 z4 q# R1 d$ D' |
// Note also, that with the additional
6 P4 a3 H# ? L% `6 k; a* H. a // `randomizeHeatbugUpdateOrder' Boolean flag we can( [. C) y M1 X9 i
// randomize the order in which the bugs actually run
& c& S) ]3 D' o // their step rule. This has the effect of removing any
: {& N7 b' P- j& g0 a: _ // systematic bias in the iteration throught the heatbug' v* r/ e: u% f" ]+ n9 n
// list from timestep to timestep
; a- K, }8 {% n3 j: v8 P) Z9 Q 3 R! e4 T" w1 }6 S) O' p, h
// By default, all `createActionForEach' modelActions have
" w+ b5 b9 b5 U# T% C // a default order of `Sequential', which means that the
7 p3 b( w9 O8 h // order of iteration through the `heatbugList' will be
/ T; Y* S+ S. R4 |+ b$ u5 a& p: B // identical (assuming the list order is not changed+ o! V: G% g2 t, R8 m$ S: E: f/ h4 p
// indirectly by some other process).
$ Y. T0 ]3 f \, W6 ^" w1 b
; y( \# r& H" S& ?' z3 B9 s, o modelActions = new ActionGroupImpl (getZone ());
$ V( t: Y3 O! P1 a2 g0 b( S2 E- U4 U) U- H; ~0 K2 [0 e' |
try {, ^' B2 T: u' ?$ c6 F: J6 r
modelActions.createActionTo$message$ e" s. i& H- K1 w, O
(heat, new Selector (heat.getClass (), "stepRule", false));4 H5 @. ~7 G @% G. w
} catch (Exception e) {
7 j! q; R2 M8 r6 Z9 a8 X& x. n2 b System.err.println ("Exception stepRule: " + e.getMessage ());
# I5 W/ D+ i! P. C5 ^$ J }
/ S% |- h5 u" J/ h; I6 w) R4 W
$ C) ?6 ]+ m) G q) M s7 a try {2 w0 t1 k" _' \8 g8 x2 S7 l" B
Heatbug proto = (Heatbug) heatbugList.get (0);
( h3 x" L, f Y( o# N Selector sel =
, n# ^, C% k2 I: E V3 x new Selector (proto.getClass (), "heatbugStep", false);8 }2 Y' o% ~) m! \4 Y
actionForEach =
}5 P/ K5 [# \) \ modelActions.createFActionForEachHomogeneous$call( X" h2 Z1 {8 ^( g, B
(heatbugList,
1 X5 z4 N/ J- _& b# s H- T7 i9 g new FCallImpl (this, proto, sel,; H/ X, [) U2 C9 i4 R+ p
new FArgumentsImpl (this, sel)));
8 V" Z1 t2 Y4 L0 a W' p } catch (Exception e) {
# }( p0 v' S4 c9 m3 x. a9 s e.printStackTrace (System.err);
3 l" d( Z8 I |+ N1 z; h }' R/ N' D* Z; Z& [& n# s# o- D
3 f3 z+ g/ d. f& E8 d" C
syncUpdateOrder ();3 A$ F3 r. ]0 q5 E: v& P2 M: F( C& }
9 V1 V! S( W" _8 N& l- d# [% ^
try {$ L5 |1 |1 n! C/ ]
modelActions.createActionTo$message . H8 k4 {& x$ ]" k- r' g
(heat, new Selector (heat.getClass (), "updateLattice", false));/ z5 \3 l2 M5 N7 q5 W' L
} catch (Exception e) {
6 `: {. b. X. e% J. L System.err.println("Exception updateLattice: " + e.getMessage ());
, ~* a+ w6 N ?3 ?4 l0 h }
1 Q/ O% O5 _6 O- U : k, J3 z( F: H/ M7 i
// Then we create a schedule that executes the
( f: g; s" U" ^# R% m2 L& D // modelActions. modelActions is an ActionGroup, by itself it" \0 J3 z9 K& g; l+ \
// has no notion of time. In order to have it executed in+ o5 T, i$ {6 C& ]/ R
// time, we create a Schedule that says to use the
; N/ O& `* N0 {- c, K // modelActions ActionGroup at particular times. This0 I1 T y! Y& [- h$ i9 h2 j
// schedule has a repeat interval of 1, it will loop every
( ]: ^% s& u4 t$ M$ A$ K0 h // time step. The action is executed at time 0 relative to
7 E# n2 @: P N+ e; b5 O // the beginning of the loop.
1 P, s! t8 T+ r7 i7 F6 V. c. g6 l; }. f
// This is a simple schedule, with only one action that is
# p0 A; _& \; {/ z // just repeated every time. See jmousetrap for more9 b2 Y$ } d R$ n0 R. i/ ] R
// complicated schedules.
- M+ i- Q9 B* L3 G4 G ; o! z/ D/ T+ u- G* A. r% B0 k
modelSchedule = new ScheduleImpl (getZone (), 1);/ p+ H: {" C3 T4 W" J
modelSchedule.at$createAction (0, modelActions);/ E" L* V0 p) ^& r5 ^
4 d" Y- n3 G% A8 g7 m
return this;* q' ^: u0 m% V' H$ u5 }" c" ~
} |