HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 v) i/ L9 t5 a/ c
+ [# j* E5 n& q) O2 s- d1 z public Object buildActions () {5 [) f# q# I! N0 G7 o" C: i8 d( b* k
super.buildActions();
, M& ]' F7 W |" h8 a" V
7 I( u9 y; [% c& S // Create the list of simulation actions. We put these in
; Y# L7 q0 f1 q6 Y- S( n // an action group, because we want these actions to be+ H+ q& [, s- L' m5 H/ {
// executed in a specific order, but these steps should+ q/ V9 \0 ^- x4 A" _
// take no (simulated) time. The M(foo) means "The message# u& v! Q& w+ W. P0 V
// called <foo>". You can send a message To a particular
& D5 V% v! p! I- A // object, or ForEach object in a collection.
: i- I# ?. R- u
/ t! s) l5 \9 N9 q' y // Note we update the heatspace in two phases: first run5 F. D( o d9 E1 T* f
// diffusion, then run "updateWorld" to actually enact the
5 ~0 ] c1 B1 t; M' o // changes the heatbugs have made. The ordering here is
, t) f( [4 y/ X) F5 e // significant!, ^5 b2 J; ~( ~6 J7 N) N5 D/ P
4 g4 m/ m; t! n) b4 E, B // Note also, that with the additional
: f8 ~3 W. B6 e& f% h // `randomizeHeatbugUpdateOrder' Boolean flag we can
; l7 _/ P' Z/ Y // randomize the order in which the bugs actually run
+ r" ~# K% j/ p$ y/ W# \$ _ // their step rule. This has the effect of removing any3 ]) j- f% u1 V: d5 F' @0 k' w
// systematic bias in the iteration throught the heatbug
( p* |4 D( H [8 z' U. O# w1 `, r // list from timestep to timestep. D9 `3 r3 @+ M$ t* L: j
. u+ Z3 O3 D) w& s0 n // By default, all `createActionForEach' modelActions have3 v$ ?3 J2 I9 r. @* V
// a default order of `Sequential', which means that the
( ` O1 B* `: c x, V& H! k/ E- Z* k // order of iteration through the `heatbugList' will be( i! j/ g O1 V9 ]5 J# k, V
// identical (assuming the list order is not changed
1 S& q& E2 F, ~! {" O* }. b# d // indirectly by some other process).3 Q& c; U2 U% ?7 D4 U( p
3 S$ i5 s% z5 z! z- U
modelActions = new ActionGroupImpl (getZone ());" {# s* q1 F# |6 F/ ]. ?; L
3 w" g+ g9 n# y! q$ e try {
3 t9 j4 n- p. R' w. d4 k modelActions.createActionTo$message' C: }' ?: U; j' F9 U
(heat, new Selector (heat.getClass (), "stepRule", false));
* H5 ^0 q3 k& M } catch (Exception e) {) O" |+ X/ @. }- B6 Q4 P( H% B
System.err.println ("Exception stepRule: " + e.getMessage ());
: W& `+ @3 P* N* c% t7 h }1 S5 Q8 p# J5 Z
. I5 d; d/ Y0 W/ Y1 [1 P
try {0 m5 Y2 n- ~" W: k$ d
Heatbug proto = (Heatbug) heatbugList.get (0);# T& j) |; `1 R' O. ]. Z9 c U
Selector sel = % K1 X: a4 A' ~1 P
new Selector (proto.getClass (), "heatbugStep", false);1 c- z/ m' W* G: M
actionForEach =
# v" g' Q' b' ~* J modelActions.createFActionForEachHomogeneous$call: n0 G5 V" x5 p9 W( w' W& P+ j9 {
(heatbugList,
1 L4 J& ]7 u* \+ r1 S8 L* d new FCallImpl (this, proto, sel,( U& c1 Y u& O) E' T& J3 x
new FArgumentsImpl (this, sel)));( |/ d! `% e. [* l: u
} catch (Exception e) {/ g- Q9 W9 l8 }. i
e.printStackTrace (System.err);
@. U9 X$ X/ V* V/ m4 Z" v3 | }
6 X9 I. ~! s7 E ; c8 n# U3 {) {
syncUpdateOrder ();1 B% c* r$ w3 d: z8 y9 G
% ^+ e& v S; \6 I
try {
6 Z/ s _) F. S! w modelActions.createActionTo$message `* g6 ]) ?: a+ a) _3 y2 g# Q6 s( x
(heat, new Selector (heat.getClass (), "updateLattice", false));
1 d! W/ I8 c5 N. [2 B- n) ^ } catch (Exception e) {
. E3 p6 o& h9 |6 I: Z, Q# ]2 h System.err.println("Exception updateLattice: " + e.getMessage ());
( z& e$ c8 r3 @( O z, Y# m }$ ^) q6 W1 k0 j: C' b& h; L9 t
" i$ k5 A, Q: o' p; x `- F( L
// Then we create a schedule that executes the- v8 b: S; d+ l; B8 r/ ^0 P
// modelActions. modelActions is an ActionGroup, by itself it
/ g0 O9 g: [3 ?/ e: P+ a, o, k // has no notion of time. In order to have it executed in
& x4 _5 _' i. D; H // time, we create a Schedule that says to use the, T3 E$ A" K( a. Q
// modelActions ActionGroup at particular times. This
4 u; _) M/ Z6 T" z! t( n // schedule has a repeat interval of 1, it will loop every, n8 J( u% r1 t# A( L
// time step. The action is executed at time 0 relative to) V% Y/ {+ `2 G* z5 x% O
// the beginning of the loop.
. ] O, b" d% u+ i0 q, D V2 r: |$ @( h
// This is a simple schedule, with only one action that is; ^: u3 o' c$ C5 H3 V
// just repeated every time. See jmousetrap for more+ ?' W2 V- q t! ], J* \7 W% c
// complicated schedules., F, o+ H7 |0 A* I
7 T. r$ a7 W& H" M
modelSchedule = new ScheduleImpl (getZone (), 1);
; D/ l. y1 K( K2 c3 ^ modelSchedule.at$createAction (0, modelActions);- r8 d* p! Z3 n5 M; m4 J( r
( w8 J" X2 i$ h# f& I return this;( i. Q i5 _2 h D! B7 J: Z
} |