HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 B" J4 w" ~! R& t4 l! G- W+ d% {- R0 k3 O S
public Object buildActions () {
% P$ u' V1 b3 G; u3 I: I3 l super.buildActions(); c4 x- c/ ^* Z' o- ^$ |# o3 y
! n; p( {5 T+ c `$ j$ V // Create the list of simulation actions. We put these in
\( _, @6 x |+ Q; N // an action group, because we want these actions to be
0 G' u2 b, @/ D* n) A$ K // executed in a specific order, but these steps should) Z: v2 ^$ }8 f3 V
// take no (simulated) time. The M(foo) means "The message
$ k( R3 z+ y1 X- Q7 M // called <foo>". You can send a message To a particular8 s1 _. R; R- b$ k
// object, or ForEach object in a collection.
2 B% w: E2 U: r* f9 b
N8 x3 @; m2 V. c // Note we update the heatspace in two phases: first run
+ ]0 u; m9 N) X( L/ m) e // diffusion, then run "updateWorld" to actually enact the& b# W* O# d( j$ p5 F2 X
// changes the heatbugs have made. The ordering here is
8 S# c4 d7 Q; @3 Z- ] // significant!
! p& k9 C9 ^+ w$ |5 b s1 B6 B/ [) q* z7 O2 L
// Note also, that with the additional: e( J: _3 Q$ ~/ g7 ]( H
// `randomizeHeatbugUpdateOrder' Boolean flag we can
( h; E$ w% b1 x- N, B // randomize the order in which the bugs actually run& J; R$ G. a& x( k' m6 F+ l) ^
// their step rule. This has the effect of removing any
( U, F2 i" e5 [. Y/ d8 t+ W+ m: y // systematic bias in the iteration throught the heatbug4 x! B! R6 y( k+ \
// list from timestep to timestep* r$ t* }" f0 Z
1 V% ^/ t5 ^: v) z$ z+ D
// By default, all `createActionForEach' modelActions have# Q$ C" j" P3 t7 _8 ]/ N
// a default order of `Sequential', which means that the3 g' a% Y2 d% p4 J# U
// order of iteration through the `heatbugList' will be
5 }+ d. O! ^; M# O) T) r. d // identical (assuming the list order is not changed
9 |5 A. Q8 k$ P4 U7 w% K, T // indirectly by some other process).' ]2 n% U. w3 Z" ^
2 L. ]9 i& R* o/ ~ modelActions = new ActionGroupImpl (getZone ());* D8 v: `& m- y/ G
- d# B- c$ b* f7 {5 d7 C
try {
: p5 m" @6 |) g! o$ s5 v/ }$ y6 ?) d1 c modelActions.createActionTo$message
5 n: \- D! `0 X _3 e; ? (heat, new Selector (heat.getClass (), "stepRule", false));2 K! x/ s7 P+ S" G9 Q& V S8 i
} catch (Exception e) {
9 z' x( g- s7 Y System.err.println ("Exception stepRule: " + e.getMessage ());
! E% Z) `6 ~; R }+ |% r3 l3 M" _$ j4 R( k V; ^- E% a
1 k) ~* Z# H0 n' }/ ]9 F! G
try {
: I+ u7 V5 E& Y1 n; x6 f% n, ^ Heatbug proto = (Heatbug) heatbugList.get (0);
& H" D. j1 I* P% i# k( K Selector sel = / D0 n* b4 W [# e
new Selector (proto.getClass (), "heatbugStep", false);
# U# y( k {1 O' N: m+ i6 c actionForEach =
, R( b' Z/ p g9 j modelActions.createFActionForEachHomogeneous$call
) c2 L; k7 D/ [, Z0 e (heatbugList,
. {/ j' B* M( R/ ]' A" n new FCallImpl (this, proto, sel,: J) t: F7 t+ E4 M
new FArgumentsImpl (this, sel))); {* X2 z9 I* \8 O+ d% ~! k# D
} catch (Exception e) {
' E8 I5 b! ~5 v+ u e.printStackTrace (System.err);
8 K! t5 t( u7 U$ P5 g: M- _- c4 u }
7 m2 l, ?5 B+ }
8 {) _: }3 p9 a: T syncUpdateOrder ();
5 _3 I1 Q$ P& I
B3 h' q$ c% Z6 O6 [ try {
3 [" o) t" e/ x0 i3 w modelActions.createActionTo$message 3 T1 M" R+ y9 o- f) V( A, j! P
(heat, new Selector (heat.getClass (), "updateLattice", false));7 }7 u4 F3 z' U8 ]
} catch (Exception e) { O: |. d/ F1 v
System.err.println("Exception updateLattice: " + e.getMessage ());8 v: `- y! p: k& Y0 E# ^9 J- Q+ a( I! r9 Y
}: o( d& l) l6 K( R# ~7 U
- a/ t, E" A- c" d. _ // Then we create a schedule that executes the8 e6 ~$ A0 @6 d/ x ~) l4 ]& M
// modelActions. modelActions is an ActionGroup, by itself it
( s2 d( O- X x5 j7 G* k1 v // has no notion of time. In order to have it executed in
# |% y5 R4 S- ~! s' O // time, we create a Schedule that says to use the
7 [- f- I! P3 F6 H9 a // modelActions ActionGroup at particular times. This
8 M, U+ h4 R$ H/ E/ w* u // schedule has a repeat interval of 1, it will loop every7 Z. b) `# f8 B7 S
// time step. The action is executed at time 0 relative to
_% s; S4 _' m1 Y4 E // the beginning of the loop.
) [' G Y8 L7 F2 F
1 v: }9 X2 v3 l4 m+ M2 `7 y // This is a simple schedule, with only one action that is
/ Q1 t' o, y% k2 d- D // just repeated every time. See jmousetrap for more
8 `1 \9 \& }" n Z. {: h // complicated schedules.
0 w4 }7 l M5 A1 J3 N& q' U: Q
& h* a$ W2 Y: G1 ?+ c9 L modelSchedule = new ScheduleImpl (getZone (), 1);
* F b u& G# N7 |7 R1 I9 G: P! E modelSchedule.at$createAction (0, modelActions);
- h& J3 A1 }- Q) ]2 c/ T0 Q 4 G- R) l: E6 Q5 | P
return this;0 k. x" l3 z# Q& h9 e
} |