HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 e# Y* A7 H! B& f
5 M; B: l, W; ?3 X) ]* a public Object buildActions () {
8 B& E5 A) H# ~0 _" N+ s super.buildActions();% B0 r- g% a- o; {6 c) {1 Z
' D/ U$ h7 F, b/ _ // Create the list of simulation actions. We put these in3 P, ]* @* S7 l# M: K
// an action group, because we want these actions to be
, {7 C/ S3 q: {# k! }) T' r% O // executed in a specific order, but these steps should
0 y/ b& M+ u1 Z" Z8 d5 P# g- b1 t, L3 ~ // take no (simulated) time. The M(foo) means "The message
$ q; v# O" c( {; L$ V$ U' p // called <foo>". You can send a message To a particular! d( E$ t$ S. O5 `* a @. P
// object, or ForEach object in a collection. L5 S2 q& k6 P4 \+ y; \0 |( ^
2 w( X7 I( U/ u! P2 n3 H d" j( y( Y // Note we update the heatspace in two phases: first run
% f4 | [1 P7 M* y* F* C5 L6 \- ^ // diffusion, then run "updateWorld" to actually enact the
+ Q9 b* d I$ x // changes the heatbugs have made. The ordering here is9 Y/ D9 f( I9 T* z c
// significant!- i4 ?$ L0 b8 d. m4 t" J2 V
: m K9 o) n* P w/ ~( m // Note also, that with the additional$ l# q# a% G( \. G F0 S1 U. v- `
// `randomizeHeatbugUpdateOrder' Boolean flag we can2 \$ p6 |+ R) M- u8 n
// randomize the order in which the bugs actually run
" d! ]+ o5 b, Z+ U7 O: y5 ?) Y // their step rule. This has the effect of removing any
0 T# e% o% s0 E, N! v8 G0 L; B# ?: l // systematic bias in the iteration throught the heatbug+ y8 I" H9 m3 M
// list from timestep to timestep$ e- w) B/ F" o6 p0 ~+ s/ I
. \; I, M! w* M0 F // By default, all `createActionForEach' modelActions have0 [8 v+ ?) q( f2 @+ P- r+ P
// a default order of `Sequential', which means that the
$ {; I" E2 B2 N // order of iteration through the `heatbugList' will be/ f# ^; t6 K% I; \
// identical (assuming the list order is not changed( h) w+ W4 g, H$ K! |6 \: m) X
// indirectly by some other process).
/ R: Z3 H8 g' d0 y7 N+ b
/ a+ r V2 K! S9 {: }8 }& J2 E modelActions = new ActionGroupImpl (getZone ());
- l( a# Z- T) p; P! T' ]- d. x+ P+ O( c' I
try {, }4 e, M1 G" o, F6 i
modelActions.createActionTo$message
' O# R# X8 y2 Q6 n5 ]; |$ X (heat, new Selector (heat.getClass (), "stepRule", false));/ D. S3 R% u6 @9 W( H5 c) U: `
} catch (Exception e) {
3 R: @6 ]8 M3 q' P System.err.println ("Exception stepRule: " + e.getMessage ());
% @7 n3 F. m! D8 k+ ~! q' Z0 x) T }: R- t1 y9 G- F3 m3 P* G2 V
* ]8 ~ u8 Q$ d+ e* w
try {$ v! q m% M5 X6 H. S. l% `# b
Heatbug proto = (Heatbug) heatbugList.get (0); ^; P8 u/ M r$ \3 G' j# Q, f
Selector sel = 8 r& X9 u7 }& Z: ?0 |
new Selector (proto.getClass (), "heatbugStep", false);
( {' O# k4 j" s$ G actionForEach =
2 x6 {3 g1 T4 h1 d6 a modelActions.createFActionForEachHomogeneous$call
: k3 s2 B% N$ s0 ^. U! D# A0 s0 z (heatbugList,( s- M% Z7 u5 c7 b! R4 b5 _
new FCallImpl (this, proto, sel,
6 j* c- n! j+ m$ l new FArgumentsImpl (this, sel)));" S. ]. A/ I$ A B& B& ]
} catch (Exception e) {
) `! |* Z. m/ W: I7 R* e0 j$ x e.printStackTrace (System.err);% W$ \$ f$ G E+ g' M! o
}8 U2 c0 \( Z6 x7 b2 Z. y! ]
& d) Z, u8 v% h$ w1 _
syncUpdateOrder ();
' C# Z! q6 h" S% Y: n
% M2 G: l6 ? P/ k) {1 z Y. y. p try {, y0 A5 K* g1 Z+ ~7 H7 ]
modelActions.createActionTo$message
1 k1 O; J4 N- h- T" s. B (heat, new Selector (heat.getClass (), "updateLattice", false));/ {; C* k7 z% ]/ ?
} catch (Exception e) {; Y$ Z( V/ V4 K. |, }& ~: q. e- P
System.err.println("Exception updateLattice: " + e.getMessage ());) n; o: Q; {. B
}' ~8 d$ w+ D* W, w/ x7 K s. N' F
3 G, h. f# C* N1 V0 P/ C d9 R6 F( L, l
// Then we create a schedule that executes the
+ l, E7 M! ~ H* U% C( V // modelActions. modelActions is an ActionGroup, by itself it
6 \* n3 T% s+ F r // has no notion of time. In order to have it executed in7 |6 j0 J7 I) G) W! G
// time, we create a Schedule that says to use the: Q7 g# C- I1 U0 K4 V. i, B
// modelActions ActionGroup at particular times. This
) ? R4 b# F6 g _( A/ d // schedule has a repeat interval of 1, it will loop every
9 S# u& |8 s, |! I+ a( d // time step. The action is executed at time 0 relative to |7 w: H9 K( u; X4 `1 @( B! F
// the beginning of the loop.7 \% L; }% w4 j5 E$ J% F" o
( H# s+ I3 l* y+ F2 j$ |' k
// This is a simple schedule, with only one action that is. \2 l( V+ E3 v! ~( k% p6 f
// just repeated every time. See jmousetrap for more
& G& d9 t; E# w( Q+ J. ?" Z // complicated schedules.
4 M8 i8 y) e- A# _% j4 o1 N9 t: k$ P
* W$ d8 j! O% ]. \7 Y modelSchedule = new ScheduleImpl (getZone (), 1);5 i0 T1 y- ~: V7 I$ y* }
modelSchedule.at$createAction (0, modelActions);
5 d5 d4 E; q2 P* T! A5 | $ s% l! _) W: `$ D% q
return this;
* Z% k r: i( g1 W/ f# [" ~6 ~ } |