HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. m# Z' G6 l# p0 b3 s. p8 t3 Y# M
6 a/ R. x0 b- m public Object buildActions () {
6 Z- d @. ]4 l) h super.buildActions();$ X* `) ? Z* c" D
& x1 g5 r5 \8 H, ^( }! U
// Create the list of simulation actions. We put these in$ L8 L4 B0 M. m5 s5 G+ [
// an action group, because we want these actions to be
9 w6 T H- s- i) z // executed in a specific order, but these steps should
# R% h& ]: }! A/ Z, }& s4 u // take no (simulated) time. The M(foo) means "The message
& L) K4 c8 r$ g, }1 S // called <foo>". You can send a message To a particular
1 V% S8 L1 ]$ a! X- q0 I; Y5 @ // object, or ForEach object in a collection.) J+ B: E, s+ P0 E" E8 u9 O, B7 L
: m2 p, o/ m+ f' F0 F) m" g // Note we update the heatspace in two phases: first run% m/ X; A% w7 ?! S4 E; i5 f: m
// diffusion, then run "updateWorld" to actually enact the
4 O+ @2 r" i3 ?0 Q1 B } // changes the heatbugs have made. The ordering here is
& L. r8 t! j; n // significant!
. c% P( r- s6 l6 v; D+ ? 9 `3 _) v# n, O8 l1 ?+ [
// Note also, that with the additional
% y1 \7 D$ B4 a3 p% o // `randomizeHeatbugUpdateOrder' Boolean flag we can2 C1 v& r' H! P4 G
// randomize the order in which the bugs actually run8 ^3 p( p- E$ s; W3 e3 q
// their step rule. This has the effect of removing any
7 D( g4 D0 G/ l( R# i4 `$ R // systematic bias in the iteration throught the heatbug
- r! V7 |6 K @6 W' s // list from timestep to timestep! B$ P" T2 [$ {4 `8 V. f4 b# i
5 v2 _) i) t1 u& }
// By default, all `createActionForEach' modelActions have
6 S( ~+ d g& H // a default order of `Sequential', which means that the3 |' |! m$ t8 b) N3 @' o1 F! R
// order of iteration through the `heatbugList' will be
! D! a4 P6 ]- Y: e# x$ a // identical (assuming the list order is not changed
8 j0 L$ m& X: `! u k/ c // indirectly by some other process).
1 w2 W, y/ V$ s6 d2 T
5 [1 D9 Y" B- i. a* B' V, R% N modelActions = new ActionGroupImpl (getZone ());+ x/ S; `, h0 x8 ] \2 b
( B1 u o" h- B/ P try {
+ J5 e+ I% L4 d: a: n7 u8 v" _2 B modelActions.createActionTo$message
) s; M' {, ~7 V! F1 e! E0 _7 @% ] (heat, new Selector (heat.getClass (), "stepRule", false));6 U% k# P6 b' V- i
} catch (Exception e) {. U; G; X O: F$ s2 ]' z0 l+ I
System.err.println ("Exception stepRule: " + e.getMessage ());
: v3 u8 v4 ?' V5 |; t }& K- V. i; ]& o J
1 i+ f F- ]% i3 n0 L try {
. `5 u, T. W7 j& P3 I) q n Heatbug proto = (Heatbug) heatbugList.get (0);
+ X$ M4 |1 U5 T; A$ Z Selector sel =
2 t5 R% ~: j- U4 X new Selector (proto.getClass (), "heatbugStep", false);5 f7 l' T5 ^: [; w5 {- L
actionForEach =4 K* H5 K/ U) n0 ]! P$ v4 K
modelActions.createFActionForEachHomogeneous$call% ~9 ~. |% U( F4 J! t
(heatbugList,2 W! {0 D/ ~% [, V+ N8 G
new FCallImpl (this, proto, sel,
' A/ D8 Y- ~' A, l2 W9 {0 r& [( U1 a new FArgumentsImpl (this, sel)));* T& P& Y8 j& a6 y0 E. p! u* O
} catch (Exception e) {
Z P* P5 t! y0 h! V! ^2 G& L e.printStackTrace (System.err);! D0 I9 l. [* c
} @3 C# x0 a" L0 f5 l) J2 ^ s
: }6 r g5 D& h; f; h# @
syncUpdateOrder ();3 o' O- e0 ^( p2 T3 \
7 z) y; r" M4 V6 H try {! z8 U0 M6 P# Y% m$ f. ]) I+ }
modelActions.createActionTo$message % c; ?3 d0 r" k7 w
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 O6 y# C, i: e' S } catch (Exception e) {/ b9 U4 `- d7 K3 @7 w7 v
System.err.println("Exception updateLattice: " + e.getMessage ());( r4 ?( e' d/ d# x( Y! V
} y, R, h6 k0 n' w
* p$ y" N( V f0 |7 ^2 ~0 ^ Q
// Then we create a schedule that executes the; h& p2 O3 W9 u& a9 U4 C
// modelActions. modelActions is an ActionGroup, by itself it
/ H7 W1 S7 C1 B0 v' D // has no notion of time. In order to have it executed in1 p3 v8 |. a a
// time, we create a Schedule that says to use the1 M: m" c8 ~* j
// modelActions ActionGroup at particular times. This6 k* C/ Y/ T$ j
// schedule has a repeat interval of 1, it will loop every
) P" U; [" t+ J8 ^1 g. f# ~ // time step. The action is executed at time 0 relative to
- [* {; { A* n; P$ Y1 d3 Q // the beginning of the loop.
+ k( I* Z$ o( s6 v9 F$ _1 i( E
& |1 D) W ~. n% ]8 I1 b // This is a simple schedule, with only one action that is
- I. i$ G% R; T! y // just repeated every time. See jmousetrap for more
$ _8 Z; ?* a @6 j( ?( w // complicated schedules.
; F3 y: j/ T9 l4 G4 W9 H; w
: I8 r: ]2 I; v modelSchedule = new ScheduleImpl (getZone (), 1);2 j3 l w/ O5 A& @
modelSchedule.at$createAction (0, modelActions);
: t- R8 l& O2 ^1 X4 T % `& @5 b8 K2 X4 q1 ]- X
return this;, G- \7 J4 ~& _: W( W+ _
} |