HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 A: ]: O. x3 Z0 r' e7 L3 {) P! ]' V* Z% T. z% ~! W$ m" D
public Object buildActions () {4 D$ \ g- A9 ]* u/ N& m' `
super.buildActions();* H+ l* b* o) f2 x
. r+ [1 s) z# a0 z6 D' d j, `
// Create the list of simulation actions. We put these in- m, @2 n3 D6 B7 J6 e
// an action group, because we want these actions to be
+ e! _- ~4 J% X2 v8 s$ q // executed in a specific order, but these steps should b0 Y% Q$ s: {( G0 B
// take no (simulated) time. The M(foo) means "The message+ n/ w) U' w) i' A
// called <foo>". You can send a message To a particular8 g2 K* b6 ]' b& v
// object, or ForEach object in a collection.
+ _; O( P. ?: m; f- M: O; j4 h' R
- l. k$ A& r; W! E // Note we update the heatspace in two phases: first run
1 c" C) e2 M/ @8 w7 c# f( G. G // diffusion, then run "updateWorld" to actually enact the9 G8 Z& F8 u3 R, `: A
// changes the heatbugs have made. The ordering here is1 ?# [3 q% O1 i% A4 Z% b9 x
// significant!8 a& e& k% e" ` Y: ?6 `& k
$ R/ C0 f5 W. b // Note also, that with the additional7 J g+ I" `, B2 G
// `randomizeHeatbugUpdateOrder' Boolean flag we can7 ^' L7 T8 ?+ y3 R9 c# X
// randomize the order in which the bugs actually run; ~7 s- e- g1 t$ E
// their step rule. This has the effect of removing any
{% Y# e) w* A& P; C6 j // systematic bias in the iteration throught the heatbug$ _7 V9 m$ w8 y: m0 ]; _
// list from timestep to timestep0 n+ C6 T' x8 d* W, g
' [% ], s; R" B T. E
// By default, all `createActionForEach' modelActions have7 V) L0 c3 C9 Z6 v
// a default order of `Sequential', which means that the
/ m6 ^5 q1 r& H! x // order of iteration through the `heatbugList' will be [/ E6 {- a6 r) i* B
// identical (assuming the list order is not changed) Z2 z, q$ y& ]3 _
// indirectly by some other process).
# l" \/ ]# h7 i* v5 J$ G) A) Q 6 b; \' c0 u( C: ?0 Y
modelActions = new ActionGroupImpl (getZone ());2 L4 W0 D" M7 l" F% T. ?' ~
: F' _0 S* I1 {+ E4 l) T
try {. T8 T- D" W) R2 ^; p A# L
modelActions.createActionTo$message! S, q* v% `6 E
(heat, new Selector (heat.getClass (), "stepRule", false));
; m7 y$ h/ [+ w. B5 J7 d } catch (Exception e) {4 u2 Z( t% ~5 V1 Q
System.err.println ("Exception stepRule: " + e.getMessage ());
( i w! u- L2 r5 q* e3 E2 n" b }: r- c. x' N- [+ l$ o3 |+ } K
( w/ V5 X3 Q4 G3 m& @; | try {
, A Q4 ~; k& W) U# T Heatbug proto = (Heatbug) heatbugList.get (0);$ q" m/ O6 T" y) k
Selector sel =
2 y. j8 \3 O2 |2 M z new Selector (proto.getClass (), "heatbugStep", false);2 ]* n: \8 ?9 l* j, A8 J7 k
actionForEach =6 V6 I' R8 ~9 K, m8 s* V
modelActions.createFActionForEachHomogeneous$call3 s1 {( U, j5 F x/ N
(heatbugList,
- [9 u5 T8 A0 L; G6 T: i9 v new FCallImpl (this, proto, sel,
) E0 A+ C! i/ f! g2 _ new FArgumentsImpl (this, sel)));
) k/ d" |; L& q- E, F$ c } catch (Exception e) {
& `/ a2 [- f/ D e.printStackTrace (System.err);& ]* C# E) [1 N
}
* X2 ]; [, h9 |- u% {
1 R' w7 g! T' E7 f3 I, F, f syncUpdateOrder ();
% X* G( m2 \/ o" [" g: J/ E ? q+ v6 Q, [5 b' z0 ]
try {2 w: C9 w( s7 R. r; ? v* g
modelActions.createActionTo$message ( Y1 h4 L+ Y! x8 k0 ~, ~4 F
(heat, new Selector (heat.getClass (), "updateLattice", false));
1 r/ j; _) h! c) a" j3 S2 R } catch (Exception e) {0 G. a: r7 ?0 _: I, n4 l
System.err.println("Exception updateLattice: " + e.getMessage ());
: }5 I6 D( I- J( K1 f/ ^2 {4 A }3 d, u6 U* `+ H* ]! t( R
4 u0 l7 ~0 F+ x1 B' F
// Then we create a schedule that executes the
$ ?7 t( c2 Q9 t3 x9 W // modelActions. modelActions is an ActionGroup, by itself it4 ]* g: g6 _" Y/ ~0 W) C
// has no notion of time. In order to have it executed in
/ m8 r8 E- E: p6 i \ // time, we create a Schedule that says to use the
, }. {0 D$ |1 k! h' z // modelActions ActionGroup at particular times. This
1 A8 c+ o4 d: o n0 O // schedule has a repeat interval of 1, it will loop every$ U9 Q J( F" U+ k/ c; T
// time step. The action is executed at time 0 relative to/ x" ?* ?. Y: |
// the beginning of the loop., V! u V/ \$ |% ^% m9 ?
. G3 N7 F; f7 Y // This is a simple schedule, with only one action that is' W( g7 H& u7 A# Q4 j9 p2 Y
// just repeated every time. See jmousetrap for more! Q' F1 U8 ]( _+ R6 G7 m
// complicated schedules.& @) D3 u! x9 ?+ C2 m+ c8 g+ Z4 g
" [( |3 c4 n) z+ R. v9 \: g modelSchedule = new ScheduleImpl (getZone (), 1);+ k2 T/ i3 D( f/ z! X6 W' F) M
modelSchedule.at$createAction (0, modelActions);
! E1 i g" D# A6 [ " W1 o9 Q" @, t' x6 M
return this;/ b! Z$ W( M) Y- \4 ?; j8 P. N; D
} |