HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
( c, ]+ m3 {- V* F
" y* c1 w: E( W# l' f H, j public Object buildActions () {/ _$ c! ^% T1 J0 S! z; ]
super.buildActions();
4 W, a6 K4 L# x0 c i2 _
- _; b* C/ r- j) r9 X O) `0 [3 B1 { // Create the list of simulation actions. We put these in
" `2 a$ f3 W0 E5 d // an action group, because we want these actions to be
/ ^, x8 l) p/ y+ e' O+ v0 f // executed in a specific order, but these steps should: T- u5 O1 N3 h* |) v
// take no (simulated) time. The M(foo) means "The message9 t1 k5 n1 n6 Z* p, r4 y& [
// called <foo>". You can send a message To a particular
9 B M4 ]2 u9 c* ^9 [" F; [. x // object, or ForEach object in a collection.
2 b% y( ^- e5 J& v9 Y
% Z4 i" |% t/ ?# i: p // Note we update the heatspace in two phases: first run
. c' E* p: A4 e1 ?9 |* m( f6 M // diffusion, then run "updateWorld" to actually enact the
+ y5 j# j9 k8 B9 T0 p // changes the heatbugs have made. The ordering here is' ~$ t4 S0 o- Y& i, ]
// significant!+ l4 Q& L; {( q3 k- L+ u
. J- U& Z5 ~5 E& w: L$ T1 E
// Note also, that with the additional& D n5 r, k: x* `5 x) W
// `randomizeHeatbugUpdateOrder' Boolean flag we can% ^' m$ y7 I! D
// randomize the order in which the bugs actually run$ Z* F" J" K, \8 J
// their step rule. This has the effect of removing any
' W$ m& }. p! ~( F // systematic bias in the iteration throught the heatbug1 g2 n' ] o+ {6 x% w
// list from timestep to timestep5 `$ \: W5 N' J7 [6 ?
' M [1 b) W$ b- C // By default, all `createActionForEach' modelActions have1 [# O6 F- ^6 u! K6 g
// a default order of `Sequential', which means that the% a- d0 Z i0 x8 W
// order of iteration through the `heatbugList' will be
" G) ~0 h* \2 K6 h8 L% Q // identical (assuming the list order is not changed; y; A' O3 M" B( W) R7 Z) s6 E( [
// indirectly by some other process).8 r8 k+ p2 m0 p4 s% e8 P
/ J' Y. `* r, R, K
modelActions = new ActionGroupImpl (getZone ());
9 d. t0 ~# V: f# ?1 |2 t; U7 i, D# O$ }3 A T( c! c8 w
try {4 |0 S$ d5 i2 |2 j' m; U
modelActions.createActionTo$message
2 i; r1 [: G1 `3 L N$ O9 C3 |: K( \8 p (heat, new Selector (heat.getClass (), "stepRule", false));3 x f# @+ X7 ^4 L9 M
} catch (Exception e) {" L4 B5 {: |0 q) a
System.err.println ("Exception stepRule: " + e.getMessage ());
$ B( u- N& S4 c }. X- V3 _( m: I; O
7 J5 [3 \8 F3 o% V# v1 D try {
2 N7 M7 B( x" N2 W Heatbug proto = (Heatbug) heatbugList.get (0);
5 y4 e, f, Y& R$ F2 ]& e; l Selector sel = # [; c' M, K& ^" F- Q: h% o: q8 M
new Selector (proto.getClass (), "heatbugStep", false);4 [8 D; p6 _; z6 ]7 q! U
actionForEach =' w: b2 O6 D1 o% R
modelActions.createFActionForEachHomogeneous$call
. t; u0 M' e3 P# Q (heatbugList,. d3 l' h! g- i0 P+ ]% z% b+ z
new FCallImpl (this, proto, sel,0 C7 M: E8 I) h5 y8 g
new FArgumentsImpl (this, sel)));
# C- a$ \6 q" R- w+ i } catch (Exception e) {
) z7 H5 D! _) K4 F e.printStackTrace (System.err);
3 K) T9 f) S8 g5 ~' ?7 B( c) w }6 m! f& s7 _, i) Y
9 @% R" \: ]; E( `' R syncUpdateOrder ();
! d% I6 G- d8 p; h o2 c& ]$ ?. S& d$ F/ I! ~# e
try {! N/ P' j: z6 e- t& A/ x N1 w
modelActions.createActionTo$message
" _$ V% E" ?+ o (heat, new Selector (heat.getClass (), "updateLattice", false));/ b% o. Q, M' M! a0 y
} catch (Exception e) {
4 _6 e& r& ` M+ C8 x0 \ System.err.println("Exception updateLattice: " + e.getMessage ());
0 j1 U3 G& n% M& H& `9 k8 R }
: u' G* a$ c( D; l& k+ W9 J4 H, R
# \- j9 W6 R7 P // Then we create a schedule that executes the
0 H* @3 l2 n: u) i // modelActions. modelActions is an ActionGroup, by itself it
# D& x; D5 A4 x: a3 z // has no notion of time. In order to have it executed in- D3 w! P! ]* B" M {
// time, we create a Schedule that says to use the
2 m8 n0 ^& V8 l% h( B0 M' w // modelActions ActionGroup at particular times. This0 ]" h) m5 f3 m+ v% \
// schedule has a repeat interval of 1, it will loop every
: B& T4 k$ B4 \) x9 f @5 ? // time step. The action is executed at time 0 relative to& L( b0 m5 I- i( o" F3 x2 T$ S b
// the beginning of the loop.- j/ O% q0 h9 E8 u3 w% c$ ]* o
! y" X9 N8 Z0 X5 d // This is a simple schedule, with only one action that is
0 K4 {$ G8 ~4 @ // just repeated every time. See jmousetrap for more
& t+ Q/ [9 k- z& n0 M" D o // complicated schedules.
1 j6 E7 i/ [% [( d# x; P" A$ l 6 Q/ L: t+ Y! q- _7 \+ q7 m ?
modelSchedule = new ScheduleImpl (getZone (), 1);
1 T1 e/ P, k" f0 e modelSchedule.at$createAction (0, modelActions);
8 o* H" N& J/ O& k2 A
: ]9 Y) Y# Z- l return this;5 c# B( B( I3 U/ V
} |