HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 g* }: t) G* J, T; O$ A
" D& m) Q5 H% J: R# A; |! q
public Object buildActions () {8 g2 N9 y6 _# L) F& I
super.buildActions();1 k! |" [4 N- @3 k) c
# r) h( G% u3 k8 @; R
// Create the list of simulation actions. We put these in o7 B& B/ d0 r4 |/ X
// an action group, because we want these actions to be
2 U- C H# k) D% m // executed in a specific order, but these steps should0 U9 G" e# O ?/ v: k3 m7 t
// take no (simulated) time. The M(foo) means "The message1 K/ @; _, ]& b# T
// called <foo>". You can send a message To a particular
: Z) Q5 ~: ~0 ] // object, or ForEach object in a collection.
5 d' V, r. v, S Z/ y 3 [7 r; \! H; R( V+ J) O" D$ v% B0 [
// Note we update the heatspace in two phases: first run
+ v# W; W$ C+ g // diffusion, then run "updateWorld" to actually enact the
1 d1 r9 O/ P) J: J1 H // changes the heatbugs have made. The ordering here is9 f2 c3 e8 ~: m9 e, ?
// significant!
$ H+ b# U1 I) L5 w& ?& H/ q
% ~9 Z+ p. y" h: p \ // Note also, that with the additional! b' U2 {" r' P# _1 J5 S
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# {& P3 G- C! {6 b- h // randomize the order in which the bugs actually run
+ U" Y5 R) J$ }/ J, G: Q( Q1 y // their step rule. This has the effect of removing any, M4 v" Z# D7 O
// systematic bias in the iteration throught the heatbug
+ T9 I" K+ p4 A // list from timestep to timestep3 M; g0 S; \( O. V% i
' p4 B3 X, e. E! `% U0 R' x1 G# |
// By default, all `createActionForEach' modelActions have; n* Y3 E/ T* y! Y. x' { o7 e( B$ D
// a default order of `Sequential', which means that the
* _' s+ f) N( ] // order of iteration through the `heatbugList' will be% |6 n# c2 ~+ ?+ o7 a
// identical (assuming the list order is not changed$ b6 {/ V' W5 C
// indirectly by some other process).
+ R5 o9 e' r9 @6 V) i) s, S
$ e3 A" i L8 e2 G; K* n6 f modelActions = new ActionGroupImpl (getZone ());
0 X0 M# }1 A% b
/ I8 k1 R3 C# U/ L# H% L try {) e, _ W) P+ f9 x2 _: O& e
modelActions.createActionTo$message& B$ F! W& [9 Z: J" g
(heat, new Selector (heat.getClass (), "stepRule", false));
3 ]1 d$ B( U4 H } catch (Exception e) {
A5 ~; R3 H. f6 M9 L8 y System.err.println ("Exception stepRule: " + e.getMessage ());
6 [5 L7 [8 m% `) U w8 F/ ?5 [+ H9 [ }4 Q I. ^0 D/ h! s2 r
* t/ t5 ^' X }0 y% R5 z& y
try {6 g6 ^& d, g% ?. Q! Z* R
Heatbug proto = (Heatbug) heatbugList.get (0);- q, y5 H: U1 F
Selector sel =
Z1 x! I0 w$ k5 m9 R- l2 { new Selector (proto.getClass (), "heatbugStep", false);
% p' q( Y; X/ ~1 v actionForEach =
! |& e# n. c9 L$ H, J modelActions.createFActionForEachHomogeneous$call/ E! _" t" x1 x) p3 x& z
(heatbugList,) y3 n. T4 U0 ]( |# O* k! ^; V
new FCallImpl (this, proto, sel,& q2 ?7 c$ }; }: q; _! y$ o
new FArgumentsImpl (this, sel)));
. o7 j' f7 i" f7 k @ } catch (Exception e) {+ E" F3 T" l' F8 l3 X3 u
e.printStackTrace (System.err);
5 G2 d0 W4 T* r" R/ F }
* _' n( t# A. a- ? 9 }4 L7 ?* O0 c* J
syncUpdateOrder ();2 j% A1 Z* {* B9 j
z, J' p b6 E/ U* `& |
try {
8 t# M6 [3 h0 c! O% U8 g modelActions.createActionTo$message $ N4 d/ `. i9 i0 P2 I W
(heat, new Selector (heat.getClass (), "updateLattice", false));# K% l7 d5 Z) e3 p3 V/ l
} catch (Exception e) {
! E% j1 K) C; z2 l$ p0 w System.err.println("Exception updateLattice: " + e.getMessage ());6 ~7 ?. v. j& a6 z7 s: d, _0 f! A, m$ \2 X
}
* Y- Y N( |$ _9 B: G" U
* U+ g' ]1 H8 H& X! W // Then we create a schedule that executes the
/ e8 t2 q: w5 e5 T, X // modelActions. modelActions is an ActionGroup, by itself it0 F% c) r- P* ^4 q: t9 W
// has no notion of time. In order to have it executed in0 x/ t. Z+ ?* \
// time, we create a Schedule that says to use the- Y! V2 ] @: F8 M" Q4 ]; \* t0 k) Y1 U
// modelActions ActionGroup at particular times. This' ^, B# O+ u7 N: @
// schedule has a repeat interval of 1, it will loop every
! S: K& `) ^" B( ]( e# g: y L // time step. The action is executed at time 0 relative to
7 ]0 `; \' B K' f& G) Y# D6 J0 ]$ f // the beginning of the loop.% k) j& [, A" A( }
$ d7 @2 C2 U2 [( T& ^ // This is a simple schedule, with only one action that is
( C% g$ S0 R( o# D6 g4 h // just repeated every time. See jmousetrap for more+ J2 O1 Z3 H$ i* ?, Q p& t
// complicated schedules.
& C9 s2 h) h% ] ~! q. C7 _ " h7 S8 K8 D) k3 d; p8 T/ N
modelSchedule = new ScheduleImpl (getZone (), 1);' i; s1 {) U) E( J) U, x$ F
modelSchedule.at$createAction (0, modelActions);0 c3 d p7 H. V" _% H' E
4 w0 M: l/ Z0 R
return this;
/ A4 m9 R$ `- C7 a" _% W2 a } |