HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, N0 x/ t( I8 {3 K) F
0 Y+ a( ]% q, w public Object buildActions () {
$ {$ X( W4 ]$ w- a8 z+ B7 l( L& S super.buildActions();
, }$ l+ }4 X* M+ w9 A5 k
5 b K. ^& B4 u( ]% | // Create the list of simulation actions. We put these in7 m" T" {7 u; z
// an action group, because we want these actions to be) A. a7 ~1 ~& @# x
// executed in a specific order, but these steps should$ Y. N3 v) e+ L2 q" p* O. A6 r. T
// take no (simulated) time. The M(foo) means "The message
" k$ o& W' k# j5 f! t // called <foo>". You can send a message To a particular
, ?, }4 K$ }8 f+ M" L5 S // object, or ForEach object in a collection.6 U8 Q& m+ }9 {1 m7 P, z
7 v6 f4 T# G9 `/ ] // Note we update the heatspace in two phases: first run
3 G( I0 r2 X/ H( Z4 D2 j8 a1 S+ ~5 Y // diffusion, then run "updateWorld" to actually enact the
1 ^, x8 v0 s! p8 Q+ e; n" ? // changes the heatbugs have made. The ordering here is
6 P7 {1 @( ^7 s" a( y // significant!
p5 B3 [) E' V: F/ p0 L) h3 L # H u; C& W1 E% x- C: Z! ?
// Note also, that with the additional
0 ?! P3 ] t: V // `randomizeHeatbugUpdateOrder' Boolean flag we can4 H% D# ?, d% h. n b6 U/ D
// randomize the order in which the bugs actually run
( _0 T" S5 N$ F4 i // their step rule. This has the effect of removing any. q7 n0 W0 z1 R: x" H+ p
// systematic bias in the iteration throught the heatbug* U. R# t3 `: b3 q% o" \
// list from timestep to timestep$ \$ m$ {( J- D7 L9 X @8 \
# _. Q, s V$ ?3 R // By default, all `createActionForEach' modelActions have, \2 N6 \7 U, E* t8 v1 ^
// a default order of `Sequential', which means that the" S+ @: n% r1 `7 j. Y
// order of iteration through the `heatbugList' will be% N* V$ ]6 n- m$ a* p+ K
// identical (assuming the list order is not changed$ ]/ e- C! V, s
// indirectly by some other process).
0 J7 X- C& y, X- x: n' R4 f
& h4 i' F9 p; [* M m modelActions = new ActionGroupImpl (getZone ());3 Z) k+ n; W5 Q2 Q& ~3 ~+ r B
( T9 b6 ~: r/ ]3 O3 y
try {
4 T2 B: c- ]9 q7 M; \ modelActions.createActionTo$message
3 |; @ p* F: s& _ X/ G (heat, new Selector (heat.getClass (), "stepRule", false));
v1 w6 P7 N3 S8 R& ^8 S, {0 A } catch (Exception e) {
, E% K, Y; L4 {$ e System.err.println ("Exception stepRule: " + e.getMessage ());
) k5 g( }. u q% k- i }
3 `, O2 f6 u7 o/ x8 b$ _
* H! _, j$ Y$ R+ \& F4 x try {0 j3 c. T) b0 g% s J4 Y3 z: g
Heatbug proto = (Heatbug) heatbugList.get (0);
# P/ z$ `# n, Q. x' j6 ` Selector sel = 2 T0 j4 P) z1 T/ _0 Z8 w
new Selector (proto.getClass (), "heatbugStep", false);" M U$ \) {! K' R9 @# y( {0 S
actionForEach =
& w4 U: t. A9 M$ R' u7 d0 G0 Z! C modelActions.createFActionForEachHomogeneous$call
+ U. }% n+ E) u (heatbugList,
& L2 W2 `6 {, x# e. M# B new FCallImpl (this, proto, sel,. Q5 j! ~$ |1 ?6 W8 |
new FArgumentsImpl (this, sel)));
& ]5 i; r6 e& N! P# h } catch (Exception e) {& _: i+ l+ `3 P) W6 H" Z5 x
e.printStackTrace (System.err);
_. j7 c, r9 ] }
6 k% X& N$ S ?4 I9 a$ L' Y
0 {7 w- t( b8 M" W syncUpdateOrder ();6 j$ r8 y' q8 H7 Z
5 F2 M5 I# e! u( _, l
try {
2 u; B% v2 U ^2 ]4 x# x8 T modelActions.createActionTo$message
0 l5 }& J( ]) r0 F (heat, new Selector (heat.getClass (), "updateLattice", false));
4 D; Y7 @- h* V. I. ?; F } catch (Exception e) {% \' B+ c: ]" L7 U4 ^' k9 C
System.err.println("Exception updateLattice: " + e.getMessage ());; e7 h% Q. d" U+ R/ w
}
5 Y# y8 I3 |+ r# G: N" J; y
( w$ Y8 t3 B1 s: B // Then we create a schedule that executes the
1 ?5 H- x" Q2 t // modelActions. modelActions is an ActionGroup, by itself it- J, E: D7 k( R( W$ {) ], W
// has no notion of time. In order to have it executed in
3 `3 J6 [2 z' \: Z // time, we create a Schedule that says to use the
- x9 b8 b8 n, c1 u/ y' P, G // modelActions ActionGroup at particular times. This/ Z4 x) Y c2 h6 v
// schedule has a repeat interval of 1, it will loop every
" ^( l( g4 B2 n+ a+ z/ ` // time step. The action is executed at time 0 relative to
6 e% j- g* s! i$ J1 q# x x: l // the beginning of the loop.+ R8 h+ }" V- G
: g0 N, V& [; u# u2 ^: p+ i // This is a simple schedule, with only one action that is
8 Z- s u' @0 g: m, z2 A$ [7 i% a# p) d // just repeated every time. See jmousetrap for more' ~6 h' D6 `" _: r6 u8 X% g* M( _3 r
// complicated schedules.
1 g4 w$ F9 I! Z+ H$ B4 M9 Q5 s
( O A$ _& R2 A modelSchedule = new ScheduleImpl (getZone (), 1);8 C. |" k6 F0 z0 V
modelSchedule.at$createAction (0, modelActions);( s) w1 }# b. C% G8 U
7 r! M" D O$ c# }$ s" h
return this;
, I: N! X) L, ?; g } |