HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( ~ A, } T9 @4 t }$ W6 f
( s3 N* x; A2 L" d% v1 T; T9 Z public Object buildActions () {, H* ?/ p |! t7 F& D6 f7 k/ Z# B
super.buildActions();
5 K+ f8 L& O4 b: j8 I3 P6 w/ r7 g
8 w L% h& }+ S4 ?1 ^$ J# h: ]' \6 D // Create the list of simulation actions. We put these in/ U; e4 b) F! A: `
// an action group, because we want these actions to be
t6 g4 T; _7 V' t( q // executed in a specific order, but these steps should' g% a% C9 N7 \5 y! v
// take no (simulated) time. The M(foo) means "The message
4 [3 V1 k; m* l% o& m // called <foo>". You can send a message To a particular
! U/ e6 d, s9 ~7 A8 u3 N // object, or ForEach object in a collection.8 ~4 A+ m( }: y- {, N- V
5 O. ]" N8 v+ Q
// Note we update the heatspace in two phases: first run7 E8 @8 h0 Z7 [8 \
// diffusion, then run "updateWorld" to actually enact the9 G6 e p% A" j$ D2 U& y
// changes the heatbugs have made. The ordering here is
) y- e8 s! @- y5 T- \ // significant!* s$ n% I, M3 }6 z
" j* a f5 i, K; K // Note also, that with the additional
# D' C) Q2 g' z2 ^: L$ d6 [& u/ q // `randomizeHeatbugUpdateOrder' Boolean flag we can: d" T& b- A8 W: c& T9 X; m
// randomize the order in which the bugs actually run8 N, ~8 x4 t( q4 b" C2 H' N
// their step rule. This has the effect of removing any
) T( K7 C- m6 O0 D% x- G // systematic bias in the iteration throught the heatbug* `) V( J, w- P/ ]0 O4 P' K
// list from timestep to timestep
% m( ~: c' r) Q0 m 7 n: | I& o- `; e( s
// By default, all `createActionForEach' modelActions have' H$ W' w+ p" h: E( D
// a default order of `Sequential', which means that the8 Z0 H9 N K& {
// order of iteration through the `heatbugList' will be$ T! K9 K, r" L* m3 \
// identical (assuming the list order is not changed
2 L1 z& `, o! a: z8 K, }. M // indirectly by some other process).0 ^' F- s1 m8 r
: m# g! Y W5 e1 e% Q4 j( d2 D modelActions = new ActionGroupImpl (getZone ());
) R7 h8 v; I" x* S q" o, [
, z1 ]" w! h3 s3 m' }0 F try {
8 ^1 ?4 g8 u2 k$ b; E, g& `. V modelActions.createActionTo$message
% D U/ ]. H% w1 n (heat, new Selector (heat.getClass (), "stepRule", false));: a# [% s2 S! I% w
} catch (Exception e) {% ]4 Z2 T P8 u9 x: a- `4 }0 v3 e
System.err.println ("Exception stepRule: " + e.getMessage ());
9 \! O2 I4 A1 U) C6 B6 x2 E5 l }# ~, w; I2 a+ N8 a% [5 @
2 ~6 _8 B m* z
try {7 N) _9 E5 ?3 U3 L; N6 F
Heatbug proto = (Heatbug) heatbugList.get (0); ?$ L! ?% m. u* z; Y; W% Z
Selector sel = 6 u) b( V" f3 W; X e: C8 l
new Selector (proto.getClass (), "heatbugStep", false);
% Y( `* Y. s+ Q2 |7 X5 }; x5 x' N actionForEach =
- A' q4 s, G) k& l! L- W" z0 i' T modelActions.createFActionForEachHomogeneous$call _& N! J, n1 @# v4 p9 P
(heatbugList,; d+ Z; J' m8 i" r+ o5 C" X$ Q
new FCallImpl (this, proto, sel,
& Y1 s- m r& a2 b$ v9 R: Y new FArgumentsImpl (this, sel)));
3 h- V; @) o9 Q8 V } catch (Exception e) {9 V( L+ p" e) e4 w A& K
e.printStackTrace (System.err);
1 F5 V, c1 @, T6 ?5 Q P; M- | }
1 |/ e6 U- B( u, q1 b0 R" E Q 5 F7 B% c7 I6 ~, K$ |
syncUpdateOrder ();
+ C# u% N) l6 u" s* C+ q3 D4 Q: ~8 r6 v t
try {
# D3 m0 T+ S9 k7 M modelActions.createActionTo$message
* N! r3 s* l( j& }. t+ k (heat, new Selector (heat.getClass (), "updateLattice", false));
9 ]3 Z& d6 r) G4 z) Q/ a) L } catch (Exception e) {
7 s4 ^9 y7 Z6 X8 h7 l. P+ q System.err.println("Exception updateLattice: " + e.getMessage ());
( g2 p2 r$ K2 Y, ?* | }; h6 }% e" M2 ]& l
" s. X: Z* @, P& f
// Then we create a schedule that executes the
# {; u1 |: w" n k% \7 n; B+ a // modelActions. modelActions is an ActionGroup, by itself it, g/ w9 m# }4 Q8 J1 E
// has no notion of time. In order to have it executed in$ p4 F2 i; _* I) [ u
// time, we create a Schedule that says to use the: ?( o& u& d! L& K
// modelActions ActionGroup at particular times. This
' w$ I3 t8 V! i0 C, O+ f // schedule has a repeat interval of 1, it will loop every
( F i3 ?. ^. v$ M# C4 R // time step. The action is executed at time 0 relative to
7 D4 [4 h n5 `% P3 ] // the beginning of the loop.
( X" }$ a5 _+ ~" A9 m+ }6 \3 R( \% ^4 s+ \' N/ S
// This is a simple schedule, with only one action that is
6 j, @2 u* N3 x4 c // just repeated every time. See jmousetrap for more& N3 F$ S$ r1 G5 p
// complicated schedules.' T) N- }% Y/ v; P! q$ q& R4 D4 L
0 X: N$ P# L( ~ modelSchedule = new ScheduleImpl (getZone (), 1);
3 x0 N: b% w, V9 P8 X$ f modelSchedule.at$createAction (0, modelActions);/ L3 z* @8 z W+ G
2 Q% E* J% y4 L4 T9 A+ b6 \ return this;7 v: g1 l! a+ r1 D/ f
} |