HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 G7 m4 Y$ j3 H! i, v
$ P K) `- M% |5 E5 F* k, q
public Object buildActions () {
% g, G" z% f# s super.buildActions();
" f) Y4 y8 p) d6 G, M& H - l" _" @( t! ?7 Z7 ~0 Z/ D" B
// Create the list of simulation actions. We put these in
7 U, k& l y0 ?; H // an action group, because we want these actions to be! j3 P% y) e5 r0 F- e1 m
// executed in a specific order, but these steps should4 F6 R. @8 j. z1 `% f* H
// take no (simulated) time. The M(foo) means "The message1 v( e7 C, @( P& l% Z$ z
// called <foo>". You can send a message To a particular
0 o2 y5 B, U% g; |4 ]4 A // object, or ForEach object in a collection.9 U- i3 j( z! h! J$ R2 p' |' S
/ Z. h5 D7 }4 G3 ^' b. a' I // Note we update the heatspace in two phases: first run; w2 |. p! Q/ Q9 m) c `0 A* K
// diffusion, then run "updateWorld" to actually enact the2 K) s+ _' e* V- g0 J' b/ s( v
// changes the heatbugs have made. The ordering here is ] V5 @ f& s/ b3 ?/ z2 O
// significant!6 \' C" H' W# ?- N
; p1 E6 I; S. w3 W6 D
// Note also, that with the additional
7 K5 z' e5 m j9 i // `randomizeHeatbugUpdateOrder' Boolean flag we can% b* `9 k1 S$ s' R& i
// randomize the order in which the bugs actually run- L2 |. \ e- r6 W+ Z4 U) f
// their step rule. This has the effect of removing any9 J' w4 \3 Q* a; }
// systematic bias in the iteration throught the heatbug D$ c$ U9 Q( |3 c. e
// list from timestep to timestep
* X9 w+ Q; y2 ]8 p
}" q1 c* t/ u- b: S // By default, all `createActionForEach' modelActions have- |) V/ x8 R0 ^) P
// a default order of `Sequential', which means that the
3 T2 t0 ^% @' J9 z // order of iteration through the `heatbugList' will be
3 F3 ^1 i8 L- a; z' S // identical (assuming the list order is not changed
6 y C0 a9 ~) ^$ M* |- d // indirectly by some other process).
& q5 O( R. J# }7 t- s 5 Y; m o+ |# v" \7 z! m: q. L
modelActions = new ActionGroupImpl (getZone ());
, {+ |2 D4 C ~ }- u' `- V7 K9 H6 Q
try {
, A+ y; q& A" q* L E modelActions.createActionTo$message
1 e1 I- u6 |: c/ \ (heat, new Selector (heat.getClass (), "stepRule", false));
/ u4 v" S8 n3 n, g8 O } catch (Exception e) {: O' S* m3 ?+ [5 J( y- c2 b% S
System.err.println ("Exception stepRule: " + e.getMessage ());
4 J% n* F8 }! v8 T }
2 q; ^1 q* R2 H5 I
. M2 G9 u. T- _: `( l1 W7 ]5 ] try {) o4 v# C& y- z h0 ?
Heatbug proto = (Heatbug) heatbugList.get (0);: [5 D( J1 n$ k( V- q3 H( d
Selector sel = / c9 U7 Z5 A- l8 F) d" e" L
new Selector (proto.getClass (), "heatbugStep", false);' {+ e3 t3 J! t7 s$ [9 E) o/ m) |6 ^
actionForEach =
* n3 R- {5 B5 K4 M! B7 }$ S9 Y& F modelActions.createFActionForEachHomogeneous$call7 d* d. g5 b( ?- K1 C( F3 z
(heatbugList,0 e) [# F/ A/ S' ]& o) U
new FCallImpl (this, proto, sel,6 T, [' h$ |* u" M' Q- ~
new FArgumentsImpl (this, sel)));
! r- ?- v& C1 Y& S. h } catch (Exception e) { X; U2 W5 i" V* W) `+ @% p4 }- ]( i
e.printStackTrace (System.err);
. N( i, A# _: i7 c } t/ L, }0 m' Q5 v/ g
7 R. m0 f# P- w( } S/ [" D
syncUpdateOrder ();( R! o8 H0 D( D# G6 Q; ^: K
2 q. t+ T7 H3 q/ l" w- Y6 W$ ?7 b
try {
( q# G3 L# } {2 M* r) s8 ` modelActions.createActionTo$message
' l4 ~$ N' |' S5 l" d: Z (heat, new Selector (heat.getClass (), "updateLattice", false));8 w& i; T; Y9 T. a
} catch (Exception e) {
6 z0 U9 y2 {* T' o/ S6 R; A6 B System.err.println("Exception updateLattice: " + e.getMessage ());
$ C3 N# `& w5 W }
3 o8 U" h$ M% L7 X# D) t7 K: q. I1 Y& I 2 Z0 X1 b; S0 ~# D3 j) ]3 F9 T
// Then we create a schedule that executes the
2 i6 P0 x U ~2 H3 Y# m* j. a) [1 F4 ^ // modelActions. modelActions is an ActionGroup, by itself it7 U0 w4 F6 G0 z1 \
// has no notion of time. In order to have it executed in+ P2 I2 x3 h* U8 j& Q- ~
// time, we create a Schedule that says to use the" R* e( M' P# j4 C0 m4 h" Q+ f" F4 p
// modelActions ActionGroup at particular times. This
% I! F5 ]$ ^" D: a p // schedule has a repeat interval of 1, it will loop every' k0 C+ c& {/ T6 T8 t% x+ [: z
// time step. The action is executed at time 0 relative to& Q+ V( ]. R4 o: y3 \) c' d% G) B8 U
// the beginning of the loop.! t8 B, M" |- u$ C* r, S
4 o- G9 e# R0 f8 v/ c- o( f, `( d* q
// This is a simple schedule, with only one action that is" @1 h% y3 `' m3 }' U
// just repeated every time. See jmousetrap for more& C% I' s! z. n7 k$ i; N
// complicated schedules., f6 x; B. m2 Y! ~- U( l* k! x1 }' z1 @
. Z9 t" G6 U. m: s# r
modelSchedule = new ScheduleImpl (getZone (), 1);! E1 n$ ^3 a1 S2 o
modelSchedule.at$createAction (0, modelActions);
( t& I% R1 |% A
1 f$ _2 z) `: C1 y, Y7 }2 S8 P# Y return this;; S0 c [# r) U3 Z
} |