HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: a$ F+ a1 \$ \. b5 k/ W
4 x; A% J$ e9 c. Q3 x) Z9 O+ B# g public Object buildActions () {
. S; m8 h S4 o# C6 F) D# J super.buildActions();
& F H" W: ]# g* W7 o2 N9 s5 c
2 z" |7 g+ P2 \7 g. J. m! s4 s // Create the list of simulation actions. We put these in
, G6 ?& q! s( S // an action group, because we want these actions to be7 b8 R' M* L* l# g
// executed in a specific order, but these steps should
6 C. C3 M) ^0 {' w/ f5 N. w- Z9 h0 d' \ // take no (simulated) time. The M(foo) means "The message
; h" \! e# Z7 [ |* v // called <foo>". You can send a message To a particular
8 o' h6 D. H N: e // object, or ForEach object in a collection.& ~4 @, M# o3 S1 {& I
) n) E- v: @& H% n+ {( ]/ h- `5 C // Note we update the heatspace in two phases: first run
/ R8 D- O% ^- Y2 j // diffusion, then run "updateWorld" to actually enact the5 O6 u! y8 A3 [( ^* \5 _( [6 q
// changes the heatbugs have made. The ordering here is
. z! P4 R& _3 d+ h( K4 `- j; l // significant!
# z- y- R! y& q$ c ! j4 ~; t( W" o, ?, W: O' [" u$ n
// Note also, that with the additional* g4 n6 U1 n) O. O
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 @! I& a+ j' K. ~4 G* P
// randomize the order in which the bugs actually run7 B: D* O. R. ^7 O; R5 G
// their step rule. This has the effect of removing any% s% K! c5 q8 ~5 p# A% n
// systematic bias in the iteration throught the heatbug+ }% S- l2 L" g; P: Y' w8 n* U! g
// list from timestep to timestep
; y1 k/ O0 ^8 g- b ; @; z" t( M, f7 ^/ z
// By default, all `createActionForEach' modelActions have+ q5 n3 @ Z4 p6 p6 n! Y8 E' c7 _
// a default order of `Sequential', which means that the+ y o% B4 T8 C) {, r
// order of iteration through the `heatbugList' will be
3 g) u. e' c) ?- Z) G; {9 F1 z // identical (assuming the list order is not changed
- G% K6 r; z& E7 N5 T. I // indirectly by some other process).
% Q W$ P3 B" o9 r' G 5 g P$ M" l) C* k0 Z" L. p- v
modelActions = new ActionGroupImpl (getZone ());, k- i' I* T( J& y7 D: l
9 h1 b5 h/ H; U, p
try {
) E9 t' i5 J1 A; ] modelActions.createActionTo$message3 x$ r5 C2 f% w- Y
(heat, new Selector (heat.getClass (), "stepRule", false));6 k( b4 t, F3 F* p8 \6 S6 J- P. f
} catch (Exception e) {3 m/ `0 R3 K. V2 c& }
System.err.println ("Exception stepRule: " + e.getMessage ());
4 G' [+ t- J. n- c6 k+ R }
. Y9 S; X5 b; \$ N7 R7 H/ j6 q* d: S7 F% i2 J5 V* E r( M
try {: C" z4 ~7 g% T1 h! I5 `& k
Heatbug proto = (Heatbug) heatbugList.get (0);
( p! Q! \" D7 J- h) E( s5 K Selector sel = 3 ^- r, y" W' W2 F; G! k' h* R3 j5 U
new Selector (proto.getClass (), "heatbugStep", false);
. _" D9 S) k# T+ h/ u4 @$ W actionForEach =
- C3 S% \+ }0 @4 ? modelActions.createFActionForEachHomogeneous$call; t1 W7 n0 @# L9 @ |: n
(heatbugList,6 A. v9 `% ~4 K# j" f! d. Y
new FCallImpl (this, proto, sel,9 e; E) O* x- X" P% L# h
new FArgumentsImpl (this, sel)));
% K4 _: X- ^& e! y' \' F } catch (Exception e) {9 K j! e) U1 `2 \4 a0 a3 L
e.printStackTrace (System.err);1 [0 H! `! |! _5 N' @
}# H f4 O" ]+ S7 n. E
3 x( w0 X: r% z$ j/ h syncUpdateOrder ();- V+ S1 Z: {( e s: }
- G% w7 x, }/ Q/ X2 Q' G6 u try {4 W6 i. U, Z% m' G+ R0 q
modelActions.createActionTo$message , Z1 Y' R6 m; t" ?
(heat, new Selector (heat.getClass (), "updateLattice", false));
! g; M4 z0 Y% A- F1 ^ } catch (Exception e) {
/ v5 t# R* r& N System.err.println("Exception updateLattice: " + e.getMessage ());
- ^; j% c5 \ J2 ^6 L$ I$ a8 i }0 P9 _& F8 r* U3 I- x/ a
. n; U# `6 W0 u$ e, \. p1 R // Then we create a schedule that executes the$ L0 l5 C- x2 i- x
// modelActions. modelActions is an ActionGroup, by itself it$ n1 ]4 H$ s& H3 I) S
// has no notion of time. In order to have it executed in
) C3 m$ e- u7 r. C% N9 c j // time, we create a Schedule that says to use the! S3 m4 _6 l- N- t9 b* o) W
// modelActions ActionGroup at particular times. This8 |; m9 Y/ ]0 Z8 O" z
// schedule has a repeat interval of 1, it will loop every9 w( i" ?7 m& E" p/ p' ^
// time step. The action is executed at time 0 relative to
9 r# P5 W I" |# ^" p // the beginning of the loop.
$ W% y6 A8 E5 I0 d# t e) i" n/ c- n5 M7 p% G2 P$ v
// This is a simple schedule, with only one action that is
* }$ D x/ v: X& j7 W' X( N0 t // just repeated every time. See jmousetrap for more X4 B- D# x: q5 b+ d% j5 J4 ]
// complicated schedules.7 {/ X4 V0 N/ ^
5 w% n! R7 c4 x modelSchedule = new ScheduleImpl (getZone (), 1);
# e% U- N( T) R% f# X! l3 u modelSchedule.at$createAction (0, modelActions);
4 M; W9 l+ H" A7 A' F0 g. j) M& V
% A! ^/ S9 L+ p" B. D5 u' g return this;
a( o' n# G6 E3 }. Z1 x" r } |