HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" B! s. G% H5 B& N& P2 F
* |) Z) d, y) c' }6 k" x2 S public Object buildActions () {
& u* m: C* x; x% T* L: n8 l super.buildActions();# u1 S; v0 d' L7 ^( H! P
) K0 Q5 A% I' o' R; ]% m. ?/ r- a( z // Create the list of simulation actions. We put these in! }. s4 |7 N6 O9 T' s& R( ^, e
// an action group, because we want these actions to be
! q0 D& X. Q l: }+ u. u+ B( u# U // executed in a specific order, but these steps should! m x0 {* J7 b1 F
// take no (simulated) time. The M(foo) means "The message
% q" U* f- }" t, L, S9 Y // called <foo>". You can send a message To a particular
. P5 E& ~& K6 ?# C // object, or ForEach object in a collection.
% u l1 T* @6 `% ^. F# k! i
1 m4 I D# \9 h' E& q // Note we update the heatspace in two phases: first run( q8 s; a9 O: f& o9 W) r+ P
// diffusion, then run "updateWorld" to actually enact the
8 {' F3 `0 ~2 \" S, T // changes the heatbugs have made. The ordering here is
# \6 C: ?! u$ x5 G // significant!
0 R, p% `/ @1 e0 X ( p3 v, }# z! U# K$ o
// Note also, that with the additional
! _8 [2 B0 M$ @# k // `randomizeHeatbugUpdateOrder' Boolean flag we can6 v2 S9 a1 {8 f+ h9 x
// randomize the order in which the bugs actually run
; j7 p# O. `. H- q // their step rule. This has the effect of removing any5 q3 e# |1 C1 L$ n8 j. w- h1 I
// systematic bias in the iteration throught the heatbug
1 s' U; _# e/ u7 m' R // list from timestep to timestep
/ F: c3 o8 E" n* F* G
( y W g) G1 X' v) J9 _" I // By default, all `createActionForEach' modelActions have G7 B& M6 B. T y, l
// a default order of `Sequential', which means that the
' S0 f: W- ~% l5 d. R' } O // order of iteration through the `heatbugList' will be
* A h" L" J8 ]4 R2 V- |2 V# { // identical (assuming the list order is not changed* _2 f: W. ]9 r
// indirectly by some other process)., {8 u# j$ s% K9 U0 `/ X
4 }( l" v0 {# z4 q4 M% x
modelActions = new ActionGroupImpl (getZone ());
' ?) R! }) P$ }; C, P6 w7 {
& D: a6 a, ~4 f# M* d try {- P; P* a, y7 x& I
modelActions.createActionTo$message
& I, r3 K `. @ (heat, new Selector (heat.getClass (), "stepRule", false));3 i: Y" x0 x, o, n% F5 P
} catch (Exception e) {: ?, c# i2 V5 T- Q9 V
System.err.println ("Exception stepRule: " + e.getMessage ());
# b) @6 Q: H! D; X! ] }& G/ H0 {6 E/ A8 j$ Z4 q2 @4 e
2 ]! {, b0 v" z
try {! R# _# H% l; |. e2 a. W
Heatbug proto = (Heatbug) heatbugList.get (0);
" ^0 i( Q6 W/ v; W/ D* g Selector sel =
" A& `8 W: o0 I' F( H% n new Selector (proto.getClass (), "heatbugStep", false);
# U& j% I# m' t4 d/ i: F actionForEach =
, M: _: l4 k, l/ I# ] modelActions.createFActionForEachHomogeneous$call
% i. e; b0 }" Z( f (heatbugList,
- I5 @7 ^! s, J/ W V new FCallImpl (this, proto, sel, o' t) t R+ ]/ }- A* `
new FArgumentsImpl (this, sel)));8 y/ T: r. u# N5 g
} catch (Exception e) { |; b! M s& A
e.printStackTrace (System.err);( v U5 g6 Y. M( r3 |) b4 \- V
}, U" d1 p% r6 ?$ f
) `0 g% {5 a) M& c syncUpdateOrder ();0 x: G+ x& f* y$ b* G
1 o' J2 n. W6 N- y5 e try {
7 c# W! i5 I8 }) C modelActions.createActionTo$message
& z) i* I& z7 K (heat, new Selector (heat.getClass (), "updateLattice", false));
, [5 K, \! ^# Q6 I* @6 | } catch (Exception e) {5 z! ^7 w+ m( p0 c9 Q
System.err.println("Exception updateLattice: " + e.getMessage ());
9 r4 R8 M8 k7 }" G, I }$ p3 K0 @" y! M: {& m* H+ y, U* G
1 n: M2 H+ s% L+ i6 K // Then we create a schedule that executes the
2 q# U" Z0 f1 k, B t. d // modelActions. modelActions is an ActionGroup, by itself it" P1 u/ Q* N( u
// has no notion of time. In order to have it executed in
- \1 L4 e7 T8 ^: X8 v8 X2 X // time, we create a Schedule that says to use the
9 `$ R: W- ~2 |: E; ~. ]' K // modelActions ActionGroup at particular times. This
8 W& F8 g6 d# I6 H // schedule has a repeat interval of 1, it will loop every% Z6 [) c! U6 n( N: Q
// time step. The action is executed at time 0 relative to
7 \3 _% c" u' f; y: X3 E5 | // the beginning of the loop.
1 E0 ?0 k3 | n! @. A* s6 v6 F% b
% V6 U I7 s5 r* @. L, f // This is a simple schedule, with only one action that is, G3 A7 j( B J# R: G8 f
// just repeated every time. See jmousetrap for more; R+ k# g5 p4 t$ G
// complicated schedules.
6 H' ^( M$ n3 k6 ]8 [4 X9 s . g+ {3 Q, @% H, `7 R6 t4 z# W
modelSchedule = new ScheduleImpl (getZone (), 1);
" g- o4 n/ U6 @% S6 P% R modelSchedule.at$createAction (0, modelActions);+ o! n( W8 a0 h' W) h, P
; I9 E6 ?6 i! K( H' g% Q
return this;8 y( Z: ^5 B, ^: F- u2 u0 q4 Y j" @
} |