HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" S/ V) s1 j `: A4 m& ]
) d( x+ {' T0 N0 a/ U. {, P" | public Object buildActions () {
) D" K0 [' \$ A/ C super.buildActions();/ X. |7 S" M" v8 `; _
{1 Y: O# D9 D$ v3 K T* | // Create the list of simulation actions. We put these in/ S1 V7 w' g: [
// an action group, because we want these actions to be
" `1 q+ D0 I% [3 G# a( P // executed in a specific order, but these steps should! l( [% B/ V0 ~' T( \* R' R( V8 s
// take no (simulated) time. The M(foo) means "The message
9 b) d9 w1 T. g/ b& H3 h: `. c // called <foo>". You can send a message To a particular/ P: Q# P* x; o9 b W
// object, or ForEach object in a collection.
0 @* n: R8 Z8 _2 K 6 ?4 k# V$ O$ ~; V8 z
// Note we update the heatspace in two phases: first run. l" I0 p' @/ x
// diffusion, then run "updateWorld" to actually enact the7 H3 _* \/ D! q9 V7 Y
// changes the heatbugs have made. The ordering here is
. e8 {* ?7 w$ J4 n* M // significant!# K$ ~: w+ c- X8 X5 U1 `
8 K0 }( ?) b5 q9 n/ R, A // Note also, that with the additional
5 x( M6 L, y/ w3 j // `randomizeHeatbugUpdateOrder' Boolean flag we can2 [, m$ o3 z: y( X3 C$ _1 p2 v: h
// randomize the order in which the bugs actually run
( G" Q" d! _; [" P& |: @ // their step rule. This has the effect of removing any) @0 z. S2 R( S
// systematic bias in the iteration throught the heatbug
8 q( L( i6 J( ^ // list from timestep to timestep4 k0 j) F" Q! g+ V {! d n
/ M: d t9 J+ K6 J
// By default, all `createActionForEach' modelActions have
& m Z" u# Z% a) b) k/ U% ?0 ~+ V // a default order of `Sequential', which means that the. [5 N" l. m5 w4 q q; t3 U
// order of iteration through the `heatbugList' will be
1 P3 c( ?- T. U7 H // identical (assuming the list order is not changed
- x8 p+ C z+ y // indirectly by some other process).
6 s, i5 C4 ^& J+ C. S% H3 J
/ z2 u+ ]3 S4 \' Z$ M% F3 T% @ modelActions = new ActionGroupImpl (getZone ());! b1 ]8 t) c* l3 f* @9 Q7 r4 [# t
9 D$ j9 m! v$ }. W. ?9 d7 x3 V8 \
try {% g3 r/ ?: a5 R$ C. Z, ]2 T
modelActions.createActionTo$message
2 I8 M8 f; K- T7 q0 f' j( M (heat, new Selector (heat.getClass (), "stepRule", false));
! n9 x# A& I% F7 d n+ ]% Y, A } catch (Exception e) {) t4 `$ E+ Y( P
System.err.println ("Exception stepRule: " + e.getMessage ());' u" N" o- T. L1 t) u6 U1 g
} @* d* n8 i% ~% T4 Q
: c; e2 D) x% C( U1 }/ t; ?3 U" m* [ try {! Z+ D. y- X7 w3 N2 r! Z. R. s
Heatbug proto = (Heatbug) heatbugList.get (0);
9 x/ t) y1 `# w! s8 }8 X Selector sel = % b9 \: N$ o, ^/ y7 o
new Selector (proto.getClass (), "heatbugStep", false);5 p* A% ?/ i- M; M
actionForEach =6 i' l0 Q# [. d/ _- D
modelActions.createFActionForEachHomogeneous$call
" z h* S6 }& L* p B" V& K. b (heatbugList,9 D; i7 I" \( \7 Q/ q
new FCallImpl (this, proto, sel,2 a- b& c; m5 U# X' U
new FArgumentsImpl (this, sel)));9 Q7 V9 t- X) b. W: X
} catch (Exception e) {' V( @: T8 q; f+ p
e.printStackTrace (System.err);
- m2 ~7 o' L3 b1 k! o$ ~2 V4 ~2 `6 ? }
2 z0 @- |, w% p6 Y $ T; r. i' q$ x1 j3 g) Q: ~
syncUpdateOrder ();5 e8 O/ j9 ]" i% D* b
/ l8 \: A; P& f' l4 U7 g try {
8 L4 A6 i) O; |( s ?8 U$ T2 G5 } modelActions.createActionTo$message $ E9 |! b1 w y/ I8 D6 A
(heat, new Selector (heat.getClass (), "updateLattice", false));: l" j( h. ]7 ^5 ^( c) s9 @ i
} catch (Exception e) {7 z; z% I0 X- y) I- Y
System.err.println("Exception updateLattice: " + e.getMessage ());
1 \, t% \ C6 N2 k5 c }7 g/ r( y( Y7 Q
0 u4 [. D1 ~* r. y. i( { // Then we create a schedule that executes the; L4 g$ }' v; H( Q1 U1 v8 z
// modelActions. modelActions is an ActionGroup, by itself it6 L5 l" V4 _$ ^ u; u
// has no notion of time. In order to have it executed in
7 }* B0 [, {' v. s // time, we create a Schedule that says to use the
& q; ~& L' C2 ^; f& e8 J // modelActions ActionGroup at particular times. This9 S! ?6 v/ Z& ?
// schedule has a repeat interval of 1, it will loop every
( P: C, s8 S+ E6 f // time step. The action is executed at time 0 relative to
" `9 E( S" w! j2 A // the beginning of the loop.3 |3 M7 ]5 ]# U6 S3 \* @- J$ B
( x% Y) a. C7 ^2 t; ] // This is a simple schedule, with only one action that is! W- C" D" k- G$ k4 o
// just repeated every time. See jmousetrap for more
0 R3 A& r% U O2 Y // complicated schedules.
# K5 f, w: o/ n ! Z/ c& z4 M# C6 `/ \9 Y3 w
modelSchedule = new ScheduleImpl (getZone (), 1);
5 _: w( e4 \% M$ v# P4 g3 x1 K8 g modelSchedule.at$createAction (0, modelActions);
! c% s2 n: u0 b. e
; O6 T1 P/ |9 d6 d- s7 e return this;+ O, O) _6 j) |
} |