HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:. J. X9 E! J4 ?
/ ?- O' y; d! ], ?! ?& F* Y0 f, U public Object buildActions () {5 U8 l/ {( g2 s" b) b u* \' V
super.buildActions(); i9 P8 O( T4 I( G$ R
1 v y& K' n j/ R2 p4 V // Create the list of simulation actions. We put these in2 ~4 n5 Z9 f+ s: b8 Q- E/ s9 J
// an action group, because we want these actions to be6 ]. U* P5 X$ S* z
// executed in a specific order, but these steps should
6 L/ P2 s" k" @8 l/ S/ \ // take no (simulated) time. The M(foo) means "The message4 C x8 z( Q) f& b+ p) k' Q
// called <foo>". You can send a message To a particular5 n" y; |, B5 R# s* G
// object, or ForEach object in a collection.
" ~2 ^' B* g7 L6 F
. o% a! \. t; l6 ~. E // Note we update the heatspace in two phases: first run: E2 T4 m+ Y9 B- X3 t
// diffusion, then run "updateWorld" to actually enact the
) X0 M. h& t1 @) w% `/ W // changes the heatbugs have made. The ordering here is
0 a% \) [7 K2 P# C; _$ f% w% @ // significant!
! v; v* ~0 F: ~+ C
6 C# Y# e; c4 ~2 k( P V" ~: \6 o3 l // Note also, that with the additional6 U# r+ a2 {$ {
// `randomizeHeatbugUpdateOrder' Boolean flag we can! a4 T% x* d `( ?
// randomize the order in which the bugs actually run
# Z+ ~- ^# v# I& Q/ i: B // their step rule. This has the effect of removing any/ f, B: D0 h$ G$ R& b% D' i
// systematic bias in the iteration throught the heatbug
6 u. r: C* R) h" t5 o: z6 b% J5 P // list from timestep to timestep# u# r2 O4 {8 J7 N
0 y+ C2 v: R4 i, b* @9 _% e8 b // By default, all `createActionForEach' modelActions have
4 @: f2 Z! Y) L3 O. i // a default order of `Sequential', which means that the3 W6 w) Y C, D" ^+ w- q
// order of iteration through the `heatbugList' will be
$ R+ K% v! w4 F+ { // identical (assuming the list order is not changed$ m* C) G& N$ |5 E$ b' {5 @
// indirectly by some other process).
, l: A1 w9 {" r+ M& Q O/ p
: y Z2 ~; J; v0 p C$ L E' N modelActions = new ActionGroupImpl (getZone ());, b8 M) t. u" e8 l" j3 ]' Y
8 g+ \+ U- t6 b0 B7 Z) ~ try {
; q/ f" A8 M1 j modelActions.createActionTo$message( n; a9 u# J& }" I0 k0 f) l1 D! T R2 ?
(heat, new Selector (heat.getClass (), "stepRule", false));
3 Q/ ]# g* c& n8 ]( i } catch (Exception e) {4 `. p+ ]' O1 }( o! D
System.err.println ("Exception stepRule: " + e.getMessage ());
7 `: k. j s: D8 v( [ }. ~. o; b- _4 ^
- x- z9 ^/ I8 G
try {1 r, n. _& \9 w8 A; ~* }2 i
Heatbug proto = (Heatbug) heatbugList.get (0);" [) q B/ A% O7 l8 x9 `( W" R
Selector sel =
, M5 @) o3 o5 b6 F8 M new Selector (proto.getClass (), "heatbugStep", false);2 _9 u6 `. o2 @
actionForEach =
8 D/ P4 P5 [7 Z! B. F/ @ modelActions.createFActionForEachHomogeneous$call' {4 H) j! X0 {* _8 v' A* P2 K, ]
(heatbugList,+ G6 W7 j% a, H. u6 z
new FCallImpl (this, proto, sel,
. \7 }' B$ K1 V" j4 y new FArgumentsImpl (this, sel)));, |: a) T/ ?1 z4 S, d9 B. ^# i0 J
} catch (Exception e) {# T4 [$ d X# N, Z
e.printStackTrace (System.err);
# U; d5 t1 ]* Y2 z9 S! o8 y& q( @ }; s; `& V: h" X7 g6 N- W' [
+ ?+ }; I ]1 S6 j. J a
syncUpdateOrder ();* T4 p( y* @0 B( Q* D
6 f' ~0 n& o0 V
try {
% }+ N1 B; D# Y% {; d7 ?5 y modelActions.createActionTo$message " b$ S# h( M7 m" B2 s
(heat, new Selector (heat.getClass (), "updateLattice", false));3 M& f9 n! R8 v4 v3 [
} catch (Exception e) {7 ^% [ A# q$ Q6 c+ ~2 p7 ?
System.err.println("Exception updateLattice: " + e.getMessage ());
* w. T0 m8 E+ j# u$ V }3 N) ~9 u7 b) ?9 @
8 \. O* a; w( {+ j& Q+ I, U4 _% a
// Then we create a schedule that executes the/ ^* X" F* K) ]* ~# d1 u
// modelActions. modelActions is an ActionGroup, by itself it3 a* b8 c5 J/ @
// has no notion of time. In order to have it executed in% a+ ~! a# E3 W: }( y/ b- p
// time, we create a Schedule that says to use the
/ |% a* c K* s0 [# m // modelActions ActionGroup at particular times. This
* W+ h- q9 T$ |: b$ e // schedule has a repeat interval of 1, it will loop every
6 e& l, L. g3 M6 f // time step. The action is executed at time 0 relative to4 u* h* [+ T! L8 ?
// the beginning of the loop.# h3 {7 c* v) ?
4 u8 E# C2 U( v$ n! J // This is a simple schedule, with only one action that is
& ^$ Z. \1 w3 ?/ w' p8 Q9 o // just repeated every time. See jmousetrap for more
* k2 a; c! \% ^0 M0 z) ^ // complicated schedules.! s& K4 t% u) ~2 x3 n
* `1 v' K" u' o* d* H3 w* c) v2 ?
modelSchedule = new ScheduleImpl (getZone (), 1);& P' l8 c/ C2 T5 J5 _( v! `( H! o
modelSchedule.at$createAction (0, modelActions);
" `& O+ v i. i! f t K6 B3 u" R# A) }7 j) W+ l3 C
return this;$ Q& y6 T& Q$ W4 V9 _" f4 K
} |