HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, E! D9 G" g b8 A/ [2 `
/ d: a- b# R; G) k' L8 j public Object buildActions () {
( C4 P' j" y0 T% |6 u+ T super.buildActions();, S, {& \) L, h+ O7 b
( h! q6 p7 s% H& k' W2 Z5 {4 I5 e
// Create the list of simulation actions. We put these in3 J0 o6 N/ ^5 @& _0 u6 U
// an action group, because we want these actions to be
6 b0 K' `3 O$ m: N. ] // executed in a specific order, but these steps should+ u7 ^- p5 |( J9 j
// take no (simulated) time. The M(foo) means "The message
2 o, ~- X% L! {3 @ // called <foo>". You can send a message To a particular
5 {) f: y M* e: I- S/ s // object, or ForEach object in a collection.! A; I) |: q+ Q' s* e/ c y
' V" M: p- D2 V7 D2 J // Note we update the heatspace in two phases: first run
`, K- s6 {0 p& C; c // diffusion, then run "updateWorld" to actually enact the5 r+ o; Z9 I& ?. p( k
// changes the heatbugs have made. The ordering here is
* f% A! R3 Y6 Z, J1 P9 K W& _ // significant!
9 }. ]6 |. {5 e6 z$ ^" l5 g' l& } * w( _) l' B, R; h3 U
// Note also, that with the additional4 a- }% U- e; b9 K, U8 S4 E! h
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 W- k6 k6 O, l l& D8 M& k
// randomize the order in which the bugs actually run0 @6 F0 ?; |9 s; }3 _% ^
// their step rule. This has the effect of removing any
0 y# S$ {! Z! W% d% @ // systematic bias in the iteration throught the heatbug$ x: y8 n2 j: |, ?3 c+ ^1 |4 t+ p) t
// list from timestep to timestep# V: y6 s7 p' h+ C0 t+ [
6 a, z3 V- w) ]5 ?5 {* V+ n6 l // By default, all `createActionForEach' modelActions have4 h2 W0 s/ e4 [' \( F# @1 N
// a default order of `Sequential', which means that the* v. _5 c7 F" V6 r) q5 x9 u1 j" a8 h( x
// order of iteration through the `heatbugList' will be. s$ @# k: Z8 ]4 i% [7 u
// identical (assuming the list order is not changed$ ?# h6 }4 P d/ {/ b7 |
// indirectly by some other process).
) p) N& v4 K% F: ~& \
6 j- U% ?( k) I6 i: s modelActions = new ActionGroupImpl (getZone ());- o. I" y/ B6 G! L, g
! X. d, Z$ I% i
try { d7 g1 q2 g. y; r# Q
modelActions.createActionTo$message
; I3 E5 I% L" |$ l* Y6 X (heat, new Selector (heat.getClass (), "stepRule", false));
7 ~1 v; ~) N2 d+ { } catch (Exception e) {' y( ?8 x" V. x) m v
System.err.println ("Exception stepRule: " + e.getMessage ());! R7 b9 U3 \% K9 `, Q
}$ u& e/ j2 h; n
( o9 k0 f( v+ C W+ w try {6 K: e: I* p4 t
Heatbug proto = (Heatbug) heatbugList.get (0);
( n& _8 p7 U& n0 c8 }9 U. _. z Selector sel = $ G4 ?, L- P- ^+ i3 w) Q2 s
new Selector (proto.getClass (), "heatbugStep", false);
) N/ L5 Q* S4 `1 A! Q; N actionForEach =- A7 x) @* R# q
modelActions.createFActionForEachHomogeneous$call- ?* Z3 H, ?% Y; c9 L- Y" t& O
(heatbugList,# g$ w1 R, ?% S
new FCallImpl (this, proto, sel,
7 F1 l) F# \ |0 r5 |. E new FArgumentsImpl (this, sel)));
/ s( J c* T2 W& H' U } catch (Exception e) {6 X& I8 y. s/ D! i
e.printStackTrace (System.err);5 \7 a+ | S/ j' ~. A: s
}
2 P$ q8 O/ \9 M0 c9 }. S
" y7 Y ~, c- e& Y syncUpdateOrder ();/ w1 n/ R7 i1 s1 F8 V
& O9 I: `9 W" y5 z8 n" r# |
try {+ h! C N; a9 R
modelActions.createActionTo$message
- s' t) A" A$ c B, H (heat, new Selector (heat.getClass (), "updateLattice", false));% u3 g; O3 f. }
} catch (Exception e) {
! R$ e0 ?; g6 P7 s& X2 k& r* I System.err.println("Exception updateLattice: " + e.getMessage ());) |) s1 j! A: u* x: T
}
8 o: ~1 N" L* z# N9 Z* t
( }+ ?9 Z+ Z. K // Then we create a schedule that executes the
$ K* p, K4 f2 E+ Z // modelActions. modelActions is an ActionGroup, by itself it
7 M' m+ q, b2 V // has no notion of time. In order to have it executed in( L+ N. C/ j, |% W/ `0 q
// time, we create a Schedule that says to use the
/ f7 E. F, y2 M // modelActions ActionGroup at particular times. This& v6 o3 r6 }1 }4 x; K) \5 Z# I0 E
// schedule has a repeat interval of 1, it will loop every
/ Y; t' _, o( N' ]4 ^ // time step. The action is executed at time 0 relative to
' s, n$ J5 V2 ]6 r // the beginning of the loop.
: R- R* h! N) U1 i- {$ `. h% M- b' P% }5 ~! \) ]1 b/ t; B
// This is a simple schedule, with only one action that is
5 y! Q/ B+ ^9 G. a1 N. d2 F // just repeated every time. See jmousetrap for more6 _% _8 Q; n: E9 e8 `
// complicated schedules.
5 ^# H# r3 Z3 `0 N" |9 p2 {7 d
8 p/ J2 j9 }9 @! s* P% v( c modelSchedule = new ScheduleImpl (getZone (), 1);
' n5 S' L% w2 ~. m" ~; h' x modelSchedule.at$createAction (0, modelActions);
5 ~/ A7 b0 N" G! E 8 n) s8 A/ _" l# s
return this;6 g4 _" ^6 K( f8 n( v+ y
} |