HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* o$ f: M* _9 S+ J. U+ Q
2 m! o. W H' C! W) ~ public Object buildActions () {0 r& C# o: F6 b- o
super.buildActions();
+ q3 t) q2 h I7 d! q , a8 U) _# w. A! Y: C
// Create the list of simulation actions. We put these in
+ F1 Z9 c+ i5 i7 e* Q // an action group, because we want these actions to be
* t# @) A8 F, |; } // executed in a specific order, but these steps should
! G! E/ ^6 @6 |2 O // take no (simulated) time. The M(foo) means "The message7 y$ p: l% J' F, G
// called <foo>". You can send a message To a particular- K: @ @. n7 P1 n% x' N- h
// object, or ForEach object in a collection.
' n. |# f* c+ e- c, K# R
4 [) L/ ~2 w: q" j // Note we update the heatspace in two phases: first run
2 e& h1 g9 s9 @: b3 M; B3 f3 z // diffusion, then run "updateWorld" to actually enact the
Q- R! S3 p/ R- ^5 Q // changes the heatbugs have made. The ordering here is' O f, h) u% l! D
// significant!! y% ]9 W3 n0 Z. i+ [( C( t2 H
& ^7 A9 H6 A* [, w: G5 R4 w: v, r J // Note also, that with the additional% C; j9 {1 U4 x5 Z# D
// `randomizeHeatbugUpdateOrder' Boolean flag we can# B9 s) S4 x. Q; u# S) b2 { x5 V
// randomize the order in which the bugs actually run
* ^9 ^! V$ A' s // their step rule. This has the effect of removing any
. K* \' |' T3 q3 Q. b( t // systematic bias in the iteration throught the heatbug
$ m9 s% T: C$ k6 @+ }+ O; b/ l& D // list from timestep to timestep
) S6 l) W8 n* l/ A: F + ^7 S6 x* R0 O( X* p+ S
// By default, all `createActionForEach' modelActions have5 `3 a8 p" x# U: F# j/ s \6 \
// a default order of `Sequential', which means that the
" m {7 Y; \& @. M- L // order of iteration through the `heatbugList' will be
+ [* k) p; Y: b* E+ Y ~2 s // identical (assuming the list order is not changed/ c3 d8 o& f0 V( W' {" D' q6 \$ T
// indirectly by some other process).7 \8 i! A( ~ T
% W( S( c! `# ^6 P
modelActions = new ActionGroupImpl (getZone ());3 Z- y- y @' j) I
( d- Q' L1 \% B% [ try {
# ^. J3 x4 r3 ^1 ]" | modelActions.createActionTo$message+ f8 l& p5 g; u$ E7 s' W' M
(heat, new Selector (heat.getClass (), "stepRule", false));
4 A' U& g6 T9 Z7 v$ [. {9 X: w' c } catch (Exception e) {2 H/ w) D3 C7 O \5 T" B
System.err.println ("Exception stepRule: " + e.getMessage ());6 s) D* @7 v7 n& ?5 G9 ~
}7 D, S" |; v; l" E; ]6 H
7 a$ b6 A$ P. z try {
: u( l2 M& u6 {( L Heatbug proto = (Heatbug) heatbugList.get (0); x9 k, Y$ D0 N" J5 h
Selector sel = * h. ]3 d4 s- c$ l5 I9 U1 n9 {
new Selector (proto.getClass (), "heatbugStep", false);
( g6 J4 P! k R5 { F H. w7 g" k( | actionForEach =
9 Z! `5 V& Q1 W* j modelActions.createFActionForEachHomogeneous$call$ z& }% V! x: }% ^: j1 w
(heatbugList,
Y& I# Z$ Z% R2 x! G3 K$ V new FCallImpl (this, proto, sel," R8 D. y: ?/ O; g
new FArgumentsImpl (this, sel)));; H% R! S3 |8 \7 v& H
} catch (Exception e) {% R2 Y ?: S2 _3 L
e.printStackTrace (System.err);
/ \4 g h F- H4 K+ E" D }# r6 }0 T0 p) C5 M
0 |8 Z0 |2 u( n$ N# H( V syncUpdateOrder ();
$ z0 }1 L( }/ I+ L; k" t
$ m0 S% S! V, l! ?6 N# {$ C2 [0 L try {
7 U0 z( w% S0 Q! d modelActions.createActionTo$message
9 t9 @' E5 \7 G3 m* v0 ^) O) o/ h I (heat, new Selector (heat.getClass (), "updateLattice", false));) B& n( V r- r5 V0 n. P
} catch (Exception e) {
, X- z# Y' a! z1 Z8 s/ F System.err.println("Exception updateLattice: " + e.getMessage ());: [5 j1 s: E* p/ q3 W# P, Z$ Y
}. \) ]; v: h/ B/ r1 O* G, Q6 n
' b+ @! a+ B- d5 z. N. A // Then we create a schedule that executes the
1 X8 u3 B0 a. z+ ?& q6 c5 {: l // modelActions. modelActions is an ActionGroup, by itself it" `6 o% M% G) o! s
// has no notion of time. In order to have it executed in6 `% I5 f w( ~- J
// time, we create a Schedule that says to use the
: |/ I8 \3 E& L l+ S4 Y. f8 N // modelActions ActionGroup at particular times. This
6 \1 Q% r; Q5 r+ d& b9 x9 U. H8 I // schedule has a repeat interval of 1, it will loop every3 B9 S4 @7 r5 X5 A
// time step. The action is executed at time 0 relative to
4 D6 H' k1 M( P( q // the beginning of the loop.
5 [3 F. u" k1 i5 ?% `+ }: B
$ Z( M4 ~4 B$ k% Z) w5 @# g& _ // This is a simple schedule, with only one action that is
8 ~: Z2 d! c# q7 q+ f/ n* @ // just repeated every time. See jmousetrap for more
^$ F9 F/ O: |$ m( ~ // complicated schedules.
, k; O; B5 R# c4 m1 p5 g: d 9 v7 d; ?+ i3 U+ a# H4 I4 J7 a2 y
modelSchedule = new ScheduleImpl (getZone (), 1);3 j% L( z) G' ~* O( i' n8 Q
modelSchedule.at$createAction (0, modelActions);
2 s' h* A0 p+ Y' R& E& }; n
; v1 v% T% y! N+ M* ~ j2 N return this;! u% a7 T2 W2 M
} |