HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 M# b. Y/ E/ ?" D$ C( r
' }# D! f1 u0 b1 u# e public Object buildActions () {: e' j, c1 Q0 n, f+ E
super.buildActions();
% K7 ]% [' a. s
) F" |3 R; ~' s0 i( W* _* _ // Create the list of simulation actions. We put these in
k X8 g1 `% r2 i) Y9 K# Z // an action group, because we want these actions to be' D* [7 s/ a7 Y' y
// executed in a specific order, but these steps should
4 }6 w( h5 h; n: n: A- N // take no (simulated) time. The M(foo) means "The message
/ p* W9 X% F6 [3 s: i // called <foo>". You can send a message To a particular3 D3 m2 B1 N9 t j
// object, or ForEach object in a collection.1 e' e% `8 `! B$ S9 D, t8 N
) Z; G5 V0 I1 s9 a0 H b4 c
// Note we update the heatspace in two phases: first run
; Y# }$ y8 t: z- l // diffusion, then run "updateWorld" to actually enact the( ? R7 K3 c; [" s/ I3 b' D0 c- a7 x
// changes the heatbugs have made. The ordering here is. p9 I) B+ C: ^' {
// significant!
" N1 A# S/ F# r
# n7 R \8 @& f. r8 q // Note also, that with the additional
x! A5 O$ ` Y9 \( E, t // `randomizeHeatbugUpdateOrder' Boolean flag we can, i4 v |' ?9 Y8 t
// randomize the order in which the bugs actually run9 Q* {' F" z2 ^' `0 ^
// their step rule. This has the effect of removing any
4 e9 ^& i7 C/ U. _0 ~/ X7 | // systematic bias in the iteration throught the heatbug3 f3 M& \" p2 T1 ?, H
// list from timestep to timestep# F5 n4 Z& ^/ F; K
2 ^$ C0 z6 ?, P5 \ A9 E // By default, all `createActionForEach' modelActions have1 S& }' J& x e0 x1 D6 P
// a default order of `Sequential', which means that the' v5 P% N0 s! m: e3 J
// order of iteration through the `heatbugList' will be
5 j' Z1 R# h- H5 S B* v+ S // identical (assuming the list order is not changed3 W6 g2 m% P& P: r1 j
// indirectly by some other process).& A' Q3 v# \5 a4 K! I* F
8 E& `2 O8 ^* z" _: G5 ] modelActions = new ActionGroupImpl (getZone ());
+ M; J0 a5 }0 X1 p
. G4 V. |! Z5 E( a F" k/ a try {0 I2 V* \1 t: k+ Z4 W) P
modelActions.createActionTo$message+ \; Q9 S1 Q; n* c2 ~* X
(heat, new Selector (heat.getClass (), "stepRule", false));3 o8 M9 v) _- b3 s
} catch (Exception e) {% L/ h0 R# T. J8 F5 B$ H, ~6 |
System.err.println ("Exception stepRule: " + e.getMessage ());2 T$ G6 s& ~" T% S" X3 \$ X' J
}9 a( \- ~3 z) ]
4 M/ X& m1 e- |
try {4 O1 d) X( c v5 i6 \
Heatbug proto = (Heatbug) heatbugList.get (0);
% [, b, l5 G1 r V Selector sel = * `( X& s4 j; w0 c8 x" k& m* G
new Selector (proto.getClass (), "heatbugStep", false);9 n5 v. y- C* ^' ~; ^
actionForEach =, b- L. s9 B) x- y" t, v* D- o* b) G
modelActions.createFActionForEachHomogeneous$call
% Z$ t# u ?# ] f' j$ ]- u (heatbugList,
) T+ a4 B o8 ^7 {) q# p e9 e new FCallImpl (this, proto, sel,. q# o4 l' I$ i& P9 O; H
new FArgumentsImpl (this, sel)));6 K, g4 ^2 k; ` E. ?4 k; a8 ^* F( L
} catch (Exception e) {
! C* E) k1 j \ e.printStackTrace (System.err); P& u9 Q! X# D$ h: o. k
}
4 x p, Z; q5 ]. K1 u4 i
, v' x, ?8 _+ w: V! @5 O1 R+ D syncUpdateOrder ();
8 B2 j" z* k( e* i! c) B. U) U" o3 @+ n( E4 J
try {9 [4 V8 l; a6 A- U6 e% {8 Z) a3 @
modelActions.createActionTo$message
& `* j% Q% ]3 R+ V& P- d8 J (heat, new Selector (heat.getClass (), "updateLattice", false));
/ Y% @" Z6 i8 v r6 T6 D } catch (Exception e) {
, {6 K5 W/ [/ ]; u5 ?3 e5 s System.err.println("Exception updateLattice: " + e.getMessage ());
7 d; Y6 I4 I- }, |. G }2 |1 C+ L5 O2 y1 \4 }
# K5 _8 g3 O4 J% g // Then we create a schedule that executes the) b+ I. R! q$ G4 j5 v
// modelActions. modelActions is an ActionGroup, by itself it9 ^7 W% k' n1 Q4 N4 u" G; V
// has no notion of time. In order to have it executed in+ _0 u8 w' t& J; m7 G( t
// time, we create a Schedule that says to use the% o% U/ a; S: T* {; }
// modelActions ActionGroup at particular times. This
+ C) `2 o' f7 e* Z2 w5 d // schedule has a repeat interval of 1, it will loop every
t2 C/ ]4 {2 }& n // time step. The action is executed at time 0 relative to
- n5 F2 w7 x* l& v! D' `) g // the beginning of the loop.; Q1 _& M1 I. l7 B
/ R- T& ^) ~2 R U; h, d // This is a simple schedule, with only one action that is( ^' I) ~2 q. T7 \$ `
// just repeated every time. See jmousetrap for more
' L; P) J- F$ U. s5 \* P // complicated schedules.
5 V* w6 `- R' D& m5 n* a 0 W% q8 d! @: Y: a% w& m" L
modelSchedule = new ScheduleImpl (getZone (), 1);
: q" f, R! y1 \& x6 I- l @% e modelSchedule.at$createAction (0, modelActions);
5 p; c) ?6 Z$ @! L" }/ o9 z1 _
* ]+ C H u/ u2 C, {) D4 J' S return this; a% K0 D3 K( b
} |