HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! Q6 Y. G8 e) A5 u7 i& c& Z6 i2 u
% Q+ r& Q+ A ^7 n9 f public Object buildActions () {
- j, `5 y5 `4 I1 B8 a super.buildActions();( t0 J) Z( K7 b! }
4 H& s9 Z) P8 U7 {. @ // Create the list of simulation actions. We put these in" _3 }) b7 ^( \1 G! e
// an action group, because we want these actions to be
4 K4 L. }5 J4 e3 p // executed in a specific order, but these steps should% F x6 H- R0 Z" k2 W# w
// take no (simulated) time. The M(foo) means "The message; K+ }, V: x' T1 P9 z( F! ~. _
// called <foo>". You can send a message To a particular
9 D) t9 }' S! Z // object, or ForEach object in a collection.( B7 F3 d, W9 I: D: g2 X. W
# D" ~6 Q& n$ y: G // Note we update the heatspace in two phases: first run8 I: F$ k* Z9 W8 v' I
// diffusion, then run "updateWorld" to actually enact the7 J4 g0 `4 a6 [4 B+ L1 x
// changes the heatbugs have made. The ordering here is
2 y' a* t) P3 V" k$ M: f# Q // significant!
& ]9 g3 P$ A& v; _! B# k- ]
3 c2 S' R. W5 I% F9 g( L q // Note also, that with the additional
G, }( {+ i" t! s2 z# ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can8 d+ t; {* m& ]" j- X% Y1 ?: T
// randomize the order in which the bugs actually run* B$ r" y ^0 [: l, ]
// their step rule. This has the effect of removing any
. ~5 ?. D2 g6 h8 [0 L' j; ~. m8 I // systematic bias in the iteration throught the heatbug
8 c# L# N! S+ G* v# S5 V! b // list from timestep to timestep
# \; c, |* F7 A1 L9 ?% ^* E 3 x! O L. `8 c1 o. \0 p N; w
// By default, all `createActionForEach' modelActions have
% i0 L0 @, l1 K% \ K' c' ` // a default order of `Sequential', which means that the
- h# n" ^( ^& h, S; S7 e1 J // order of iteration through the `heatbugList' will be9 X! b b: o8 q; s4 j; S2 N
// identical (assuming the list order is not changed
' \. D8 [' M( A/ p // indirectly by some other process)., A' U6 s( @+ @
# ~; C; y% V8 D$ b- R modelActions = new ActionGroupImpl (getZone ());2 i9 ~& S7 U6 V
$ `% t( b' X2 y* ~2 p
try {
" m! Y- j5 M: u: s7 L modelActions.createActionTo$message6 F; O& r; s$ \! u- P" [
(heat, new Selector (heat.getClass (), "stepRule", false));
3 k2 S2 c h1 [, P; P& I, D } catch (Exception e) {
% ^8 b+ ~" ^/ H+ F! ~% T System.err.println ("Exception stepRule: " + e.getMessage ());
) ~$ ^9 Z' i7 R& t/ T/ D }( b0 p1 K2 O2 q' Q& e+ Q8 c% D- O! M
( B9 b( e% [; O0 M+ ?! ~! J
try {
( C4 Y4 m& j6 `, ~" ~) N9 u1 n Heatbug proto = (Heatbug) heatbugList.get (0);% U! _% y- Y" Y
Selector sel =
- l/ G# ]" v9 [6 Q8 w new Selector (proto.getClass (), "heatbugStep", false);/ c+ `" Z& O7 J( |
actionForEach =
% z3 d8 v1 B1 Y# v' e" Z modelActions.createFActionForEachHomogeneous$call
" F9 m6 l" A4 o: l# z0 V! m (heatbugList,
+ E5 F( J3 I* F7 S& H! w$ y new FCallImpl (this, proto, sel,
6 N! J; r: x. m# `2 ]9 H) R9 h new FArgumentsImpl (this, sel)));
+ e8 x0 S. k( @' u, q } catch (Exception e) {9 f9 K' b: }& W) y. C9 j9 r: c
e.printStackTrace (System.err);- h1 h# v4 F8 d$ N
}
% N: F. @4 h; u; K% _3 j
% a, B- i9 V- K8 m% p) K syncUpdateOrder ();
: E5 p: U7 f9 L: i' p% q( b# I9 l& I9 {5 x3 N4 _
try {
; [8 ]+ t5 F* Q" l0 U$ R5 C modelActions.createActionTo$message
1 |3 f4 A; l8 |, a; K (heat, new Selector (heat.getClass (), "updateLattice", false));
+ I: K- ^4 F2 R3 O } catch (Exception e) {
% g4 [, y9 l g1 J3 o System.err.println("Exception updateLattice: " + e.getMessage ());6 S% _3 P/ }7 D, p9 h2 }
}
f* O! m. Q- \9 s6 R. B
3 n) ?' |, J0 @5 p // Then we create a schedule that executes the: f8 K8 e' M+ }
// modelActions. modelActions is an ActionGroup, by itself it9 W: `2 h% b' N: M( Z; m
// has no notion of time. In order to have it executed in
" B3 I9 `; _. q1 w3 ~4 z2 r // time, we create a Schedule that says to use the
2 {+ f" C1 q' ] // modelActions ActionGroup at particular times. This
" m- A4 u' d6 D2 f% | // schedule has a repeat interval of 1, it will loop every
& {% |. l1 r$ C- B& p5 r' }6 m // time step. The action is executed at time 0 relative to
; o4 w5 C) ?- A" ] // the beginning of the loop.. Q) A6 K3 ~2 h( }
. ^# s5 d1 p* c8 s // This is a simple schedule, with only one action that is4 c: C3 P q+ |) j
// just repeated every time. See jmousetrap for more; {( `; S% y$ F& X
// complicated schedules.
9 f/ \8 x1 _( A% W9 F 9 R- K7 O6 y+ q7 m& a8 U5 j
modelSchedule = new ScheduleImpl (getZone (), 1);$ k5 m/ `+ s' ~9 k0 k) R: k
modelSchedule.at$createAction (0, modelActions);0 g5 c8 D. |. T# `8 ^- O2 P; K% V6 r
; {# P7 N, u4 m3 @( ~3 K4 Z7 A& z
return this;( |$ K K4 K) |5 D" `6 B
} |