HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:- ]# k2 t. N/ ~
& o9 Y t7 w) y: ^4 Q$ I
public Object buildActions () {8 K9 c5 k: `0 N( W$ i q
super.buildActions();
: y9 ^; n2 q! u% j# _- V4 g5 a 3 L3 N0 H& J/ u$ x
// Create the list of simulation actions. We put these in
r/ C# y6 S& _; ]) Y6 t9 i3 v+ o // an action group, because we want these actions to be f; v7 N/ F3 N1 X! t
// executed in a specific order, but these steps should
. I) i, n8 C3 ^; S( n // take no (simulated) time. The M(foo) means "The message6 H8 i3 m/ ]- L- k2 Y
// called <foo>". You can send a message To a particular) x/ e" _7 W9 K/ A y7 d
// object, or ForEach object in a collection.
* u8 X! I; x7 v
9 Q6 g" S3 ]% j' l( r- i // Note we update the heatspace in two phases: first run- d& m' P- [2 g$ u
// diffusion, then run "updateWorld" to actually enact the/ i: b; Q! ~' l7 }1 D
// changes the heatbugs have made. The ordering here is5 u4 D% r& t4 c$ m e6 @
// significant!" K7 G$ h/ _1 S, w5 l9 n( q, S4 W! r
3 t; O' j* {- @, R7 V# }, I9 b& U // Note also, that with the additional/ C* r) U, f5 S+ D
// `randomizeHeatbugUpdateOrder' Boolean flag we can$ ]( F, T. O0 |. J: w
// randomize the order in which the bugs actually run" j& q* b9 m) E4 {- u
// their step rule. This has the effect of removing any6 i# s' o5 M8 E) ~* L6 ]
// systematic bias in the iteration throught the heatbug$ P7 n8 U% r: A4 E3 K- v
// list from timestep to timestep3 a- b/ v$ P) e. x: X1 }
# q+ N ~) D1 N' `8 \: D) L // By default, all `createActionForEach' modelActions have9 Z# ~: G# S0 p+ U& N+ y; d! \
// a default order of `Sequential', which means that the
* X' b; i! ?0 t8 h; L // order of iteration through the `heatbugList' will be
) t: R0 n4 ^: X$ N/ Z // identical (assuming the list order is not changed9 o' n6 H' l5 H8 H* R8 u
// indirectly by some other process).* M# {% S; P& F/ J+ k, t! }* y
) ]" z1 O! a7 P- O
modelActions = new ActionGroupImpl (getZone ());
) O& v' q3 y1 M$ v4 u, i' P' W3 j! Q% n* O% c4 j
try {
" a9 b+ V/ y+ ]: c modelActions.createActionTo$message: g% z P' D+ K$ ^; f. d
(heat, new Selector (heat.getClass (), "stepRule", false));! j2 T: ?# t: p7 p7 c
} catch (Exception e) {
$ ]( s2 T$ S% H3 A8 Z System.err.println ("Exception stepRule: " + e.getMessage ());
* O; J8 R1 z! R8 m9 C }
4 h' z/ m. h6 Z9 z& c& {" z! N" F3 c( A! Q }/ p0 I
try {
! y, q* `& o: c* W Heatbug proto = (Heatbug) heatbugList.get (0);
. `% i0 q7 k5 U: m9 @8 z+ ` Selector sel =
" n" j: x; R- y. f# s# F7 S new Selector (proto.getClass (), "heatbugStep", false);
4 k5 `( ]+ ^ L. o$ S actionForEach =( b( Y r) ?: N L
modelActions.createFActionForEachHomogeneous$call
- i% m5 K+ E. I& M# {/ s3 x (heatbugList,
" p. [: L+ B3 {; F+ x4 v- ^ new FCallImpl (this, proto, sel,0 K, W' S$ i3 c3 v( b( W% \/ x
new FArgumentsImpl (this, sel)));
- V4 z7 W! |. ] } catch (Exception e) {
9 F' P6 v7 R( n e.printStackTrace (System.err);! A d7 O; K* V; d2 c; y% G( T
}, |0 c6 e! C- ^( d. k+ I3 u
5 I' s- _6 Q- s2 b syncUpdateOrder ();
% }3 c& B/ f% I$ J6 n
' r8 o; V1 k; ^+ c try {
" v7 i# T" u% a& L# H6 b2 M, X modelActions.createActionTo$message
- F6 T3 R& [7 ?( g# h$ F (heat, new Selector (heat.getClass (), "updateLattice", false));. V( r4 `4 |! z
} catch (Exception e) {
% @2 I0 P4 A' Z7 k$ b- C8 N System.err.println("Exception updateLattice: " + e.getMessage ());
5 r# y+ o6 X& e" g# j3 i+ [ }3 I: l T+ x, j! t% X
) p- o' \" s6 B6 H
// Then we create a schedule that executes the# R1 G+ o u5 O- \% W
// modelActions. modelActions is an ActionGroup, by itself it
5 }* Y* |5 R! F$ H // has no notion of time. In order to have it executed in; s; D6 Z; ]7 u! Z, o+ B( a
// time, we create a Schedule that says to use the% z& G9 g0 r" f1 a% P. C# n0 U. l
// modelActions ActionGroup at particular times. This2 d5 W2 x* E- G2 | Z& c& O' ^
// schedule has a repeat interval of 1, it will loop every
# v3 O- |$ |2 k- ?6 ^1 p2 e5 n // time step. The action is executed at time 0 relative to; \' e5 h6 H; v; S$ |! L
// the beginning of the loop., A7 J3 A0 g% E q: G3 s; k% E
& q) W0 d. Z$ m/ F // This is a simple schedule, with only one action that is
" d; s" b5 w! X& e // just repeated every time. See jmousetrap for more. l' \; j$ e0 P! P% e# @# c
// complicated schedules.1 i% m+ D# N! O0 E, u
! y3 _4 J1 U! L
modelSchedule = new ScheduleImpl (getZone (), 1);
! v. P- o9 k0 f5 t1 F/ x* ^3 c9 [8 p modelSchedule.at$createAction (0, modelActions);
; E- V, }3 Z- C6 V
& I$ T: @0 @# ?3 b return this;- e1 C* K/ \! q+ {
} |