HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 I0 H/ j& E7 }& P
/ J% q: a' Q) O- N: g$ x public Object buildActions () {/ N$ k# p8 @9 ]3 z/ O& E5 S
super.buildActions();
. y) l. R/ [- c0 C' s8 \0 F, O 5 T2 Y( A& {, o/ p
// Create the list of simulation actions. We put these in
& m! @* E* X x/ |4 d9 _ // an action group, because we want these actions to be
1 b0 I( o, R A" U _ // executed in a specific order, but these steps should. ?* o7 \2 h# H' y! e
// take no (simulated) time. The M(foo) means "The message* K; y i2 a% K; u$ `( ]
// called <foo>". You can send a message To a particular
, E ^2 ]4 | q; K+ Y' }! h // object, or ForEach object in a collection.
5 [! b6 }( _! J6 @
8 L! n" q1 F9 Z+ \ // Note we update the heatspace in two phases: first run
& d" u/ m5 H' g+ A7 g' p // diffusion, then run "updateWorld" to actually enact the2 u2 Q' `1 {$ P; B; B
// changes the heatbugs have made. The ordering here is
0 }$ o$ e8 H& a1 U- |* ]6 ], D // significant!
& R+ M, e v3 c' w% d" J " E1 B+ j4 ~5 K
// Note also, that with the additional* u: C; U# U: e. ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can
* v+ R' o d# r" \ // randomize the order in which the bugs actually run5 d! e) V$ c+ |
// their step rule. This has the effect of removing any
5 F- b: E& _6 r* L8 J7 ~' a // systematic bias in the iteration throught the heatbug
" O4 {0 _4 _: Z. C/ g5 R( M // list from timestep to timestep2 Q1 E6 u6 _& h: l/ H7 n4 B' @2 ^
; u5 k+ x! C# \1 P, V // By default, all `createActionForEach' modelActions have
/ V. S6 G4 V0 k2 b // a default order of `Sequential', which means that the7 V' i% A) L. {- V5 ^' ^/ u
// order of iteration through the `heatbugList' will be. i. ]& H" R( n! v4 Q
// identical (assuming the list order is not changed2 [- m) V* p+ Q' [' r8 @1 N; G* T3 Y p
// indirectly by some other process).
3 j+ y6 i; l3 [! |; ^) j9 Y
, C- _: h* y# `. }- z modelActions = new ActionGroupImpl (getZone ());( d$ \8 d O' Z/ z7 ^2 h
7 c' P3 m9 J4 {9 S2 @$ Z
try {
7 _) U! Z% k$ B' I modelActions.createActionTo$message
1 X7 V" R9 N* c# V5 l (heat, new Selector (heat.getClass (), "stepRule", false));
8 _1 T, f! C& r+ J. U/ C T } catch (Exception e) {/ e$ Q5 u2 t( q% E
System.err.println ("Exception stepRule: " + e.getMessage ());
4 m& y2 F" |* @( H }5 w O. u7 w' k0 `1 h8 _" h
& p( G" e6 K4 D0 O
try {0 k/ r. p' V4 c) {9 @
Heatbug proto = (Heatbug) heatbugList.get (0);' F' e0 Y6 I$ K$ q1 M/ ?4 C' Y
Selector sel =
* y4 b8 J% q( P new Selector (proto.getClass (), "heatbugStep", false);" E% F B' e! t) C+ j0 z! @
actionForEach =' }" m! x' L2 ?; \1 {* [0 ~+ Q5 e |
modelActions.createFActionForEachHomogeneous$call
' O" @) V6 Z% A1 Q2 ? (heatbugList,
, D$ b# s+ R2 ?, t ~, S0 G8 z new FCallImpl (this, proto, sel,' b& K S: t* b8 t
new FArgumentsImpl (this, sel)));7 `; ~6 o3 p7 j3 r4 E
} catch (Exception e) {
1 D5 f5 r4 Q" I1 ?3 z7 h M7 @/ j e.printStackTrace (System.err);3 R' b. R2 B: a* G
}
+ Y: ~! _0 T' |8 _ a9 L , X4 V6 _- o1 A. F6 ~
syncUpdateOrder ();* l1 L1 h; J7 _& w
" Z8 E2 n! L, S( {5 p0 D try {9 w0 L# S. _1 ~9 S
modelActions.createActionTo$message
/ r9 V6 N. u6 C (heat, new Selector (heat.getClass (), "updateLattice", false));
& p2 |0 f" I6 i% Q2 a } catch (Exception e) {1 n" f5 g: }1 _5 `) S9 H) ~
System.err.println("Exception updateLattice: " + e.getMessage ());
* n9 _1 r; F/ k. y& W h/ Y+ ~ }
2 d8 O8 l; ` r9 F1 k. {# l 7 j% R7 ^# K+ M; s9 F
// Then we create a schedule that executes the
; ?2 n4 w8 `# @. F; q: S6 d: q // modelActions. modelActions is an ActionGroup, by itself it9 f1 P* h3 @' R3 O; X
// has no notion of time. In order to have it executed in
, Z+ P1 k; E4 d, z u/ Y6 c' T" d // time, we create a Schedule that says to use the
3 {. D. O! c" ~0 W Q7 s // modelActions ActionGroup at particular times. This2 o) c7 X) e7 m7 Z4 S
// schedule has a repeat interval of 1, it will loop every
6 r( u6 P+ w6 r3 I // time step. The action is executed at time 0 relative to7 \. P9 I9 I/ `; v& f: i
// the beginning of the loop.
8 G+ J+ W. B5 ]6 C' e. [
+ C3 R6 M' b9 M/ y6 [* w3 e // This is a simple schedule, with only one action that is
! K8 L/ k: t1 P8 ^ // just repeated every time. See jmousetrap for more
5 e7 h9 n( Q& ^; [ s // complicated schedules.
& Z4 w! a; R; J9 e t
9 }, r6 M/ _3 X8 i: _; V* y modelSchedule = new ScheduleImpl (getZone (), 1);% v: A% M |- l
modelSchedule.at$createAction (0, modelActions);; |* l) ? V) l3 G; Q
- z( b% q' C: L return this;
0 Y# p% I1 w4 \. U: x } |