HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 f' g2 |. f) r" b8 g7 D& N1 w% F
+ P, U1 a {" z; n public Object buildActions () {. s; q1 @' @ q" B+ s9 P; ` H
super.buildActions();
/ m" Q9 N; k2 O5 R
. ?1 g {6 P) c0 Y // Create the list of simulation actions. We put these in# |! w o! I5 L& w. Y
// an action group, because we want these actions to be9 H Z+ |! N- b( z. s+ p! u% x
// executed in a specific order, but these steps should
% @' Y8 d0 P* Q2 C8 ?1 i // take no (simulated) time. The M(foo) means "The message
; r/ u l l; W& B/ X. u) j // called <foo>". You can send a message To a particular
9 R( A/ Q% v" w1 p // object, or ForEach object in a collection.! d( w) _; C Z7 C2 u" V& |
! `8 H( R* b) F; F4 O // Note we update the heatspace in two phases: first run
" V0 E8 ?5 F# v+ K // diffusion, then run "updateWorld" to actually enact the. ^! A4 M, Y2 @
// changes the heatbugs have made. The ordering here is
& [% f/ M+ C" _1 Z // significant!! I0 D/ |5 V3 \) l
. R0 T( n* g$ l1 B
// Note also, that with the additional; O; ]/ R. K# B" c" p ~# z
// `randomizeHeatbugUpdateOrder' Boolean flag we can8 Z5 ~4 \0 M( B7 e+ v# R7 Z" ^
// randomize the order in which the bugs actually run
" a) v4 d$ A( V% j4 e6 a // their step rule. This has the effect of removing any
; M1 b- m z7 c B0 k% r" f# f) g // systematic bias in the iteration throught the heatbug
: f1 J+ k) {5 K8 K4 l% B- |( U // list from timestep to timestep! X$ k, m& N" d
, G) z' B- {/ H# Z, a // By default, all `createActionForEach' modelActions have; t9 z; C$ Y9 d4 P& Z
// a default order of `Sequential', which means that the
" l4 a. |. n% {) g3 p7 [* N0 ~ // order of iteration through the `heatbugList' will be S+ \5 X" s/ b7 ~7 ?
// identical (assuming the list order is not changed
9 x9 ]* V8 o% R. L8 x: @' n8 }. u // indirectly by some other process).% X- P$ [1 a, T# U0 y! c5 [/ P
1 |: T7 B: N6 g
modelActions = new ActionGroupImpl (getZone ());+ j" }, w$ p* G, o
) K5 l$ |7 l1 z( d2 d+ t0 s0 Y$ d try {
0 O: g, D" N M( i# d( F3 H modelActions.createActionTo$message4 }+ {" V% p- u( J
(heat, new Selector (heat.getClass (), "stepRule", false));
9 w- K2 C7 q! A5 n3 c$ E8 h } catch (Exception e) {( W+ J5 z J6 B* z
System.err.println ("Exception stepRule: " + e.getMessage ());. ?' \" q, P( T
}! b" f+ b# w9 ^8 j' H
) T. w& V$ B8 o* l try {
, ?0 {1 z+ F9 B. Y( `: s! B% B; S Heatbug proto = (Heatbug) heatbugList.get (0);- b8 q' R0 I {) b* _
Selector sel =
2 U2 c; W& a0 i" i. | new Selector (proto.getClass (), "heatbugStep", false);
. q+ |% t& M+ `: K; E actionForEach =
7 b/ V: B4 ~* m6 Q. g |& d2 T+ K7 { modelActions.createFActionForEachHomogeneous$call
/ ?; T$ M0 Y/ O" ^1 ^7 G) }" a6 @ (heatbugList,
2 F6 y, x; n2 u$ f; i( q new FCallImpl (this, proto, sel,
9 d' {7 s9 U9 ? [' m" F new FArgumentsImpl (this, sel)));
& n7 ]2 T: Q: ^8 o( \" G7 l& W } catch (Exception e) {
. ^( }0 r1 `2 g, R- G e.printStackTrace (System.err);; \! Z' T9 T8 S2 R
}
1 B$ c; |% b& y% k/ y ! K; A" }. @) F% \( a& R
syncUpdateOrder ();5 Z2 `& m* A2 q6 G( Q
" U" F( A, [) |6 w5 u' L9 \& r
try {
9 _2 K5 T" n# K; D modelActions.createActionTo$message + P. u* s* ~5 j, }4 t0 I
(heat, new Selector (heat.getClass (), "updateLattice", false));' f' q+ L2 j$ O) J$ U' _
} catch (Exception e) {0 M" A( X' a8 c) X) i& ?
System.err.println("Exception updateLattice: " + e.getMessage ());; N/ D: h8 r3 v% u
}
0 K5 M3 H* T( Z5 ]7 X$ ~ * E) c, ^1 z# ^! _
// Then we create a schedule that executes the0 V4 x; r9 L9 b1 d& l
// modelActions. modelActions is an ActionGroup, by itself it
& K6 D/ [" b% }1 e0 }, G8 ~' U7 ] // has no notion of time. In order to have it executed in& S9 p: L, [# |4 S4 M! J
// time, we create a Schedule that says to use the( k) |1 o% y" N6 l
// modelActions ActionGroup at particular times. This
: A) T* A0 i* |6 o4 z // schedule has a repeat interval of 1, it will loop every
. Z6 m8 J g4 K8 R4 F; c // time step. The action is executed at time 0 relative to
* i" V% G$ W! g$ ?7 } // the beginning of the loop.
+ m( ]* ?) ^$ [4 w/ I; L
0 B6 E3 ^) P8 K- ~ // This is a simple schedule, with only one action that is1 e/ d \& b$ [
// just repeated every time. See jmousetrap for more
3 g$ J# @; t9 K // complicated schedules.
+ Y7 I6 Q a1 B g4 Q. h s( t
2 ` y* b9 _, Y, t& d$ b modelSchedule = new ScheduleImpl (getZone (), 1);
0 `4 [9 z4 q1 v6 X) g modelSchedule.at$createAction (0, modelActions);. _+ F( F3 _2 ?: X* k. d( l
0 b! f' F9 u- ~7 } return this;
) x, {* y) ~( H9 y) w. [, a } |