HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 L3 z' y7 Z" e& }% x
& @- c0 l5 ]# Y9 t2 ~$ s public Object buildActions () {: J) ?& m; X% c& n& Z8 {. T( B4 i
super.buildActions();
* D9 k& m+ }0 a- p; D0 A7 ?! e/ E3 n
# {4 t6 x5 g# O I0 c // Create the list of simulation actions. We put these in" ~0 h2 ^% a i0 Z+ u. t* Q# m4 F
// an action group, because we want these actions to be0 M6 a& r) ~: X# o' L
// executed in a specific order, but these steps should
: I0 H8 N! c5 v% e% O // take no (simulated) time. The M(foo) means "The message
( i, V1 B" W7 _0 l# C' M3 F9 e // called <foo>". You can send a message To a particular# _6 N* [, L2 |9 i8 ?
// object, or ForEach object in a collection. F! F+ F( ]% [* F8 [+ F
$ D, W) T4 F, V5 u
// Note we update the heatspace in two phases: first run
& @+ e# }: `7 t1 W! k // diffusion, then run "updateWorld" to actually enact the
+ g0 W( S% R: w // changes the heatbugs have made. The ordering here is/ K4 R9 V% c+ {2 M0 R9 b' j
// significant!6 P/ w, w/ \3 C( {$ Z) M
, O# C, i! U5 O* N7 J# [% E5 m
// Note also, that with the additional
% i6 R' p, a' u2 i // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 V( h |: ]; A // randomize the order in which the bugs actually run0 c! j. |. ^1 i- \8 \- Z) o% e
// their step rule. This has the effect of removing any
9 c ~: w- u% }# d$ n: S // systematic bias in the iteration throught the heatbug
; ` f" O# D0 P/ U9 g' l% J // list from timestep to timestep. W" {) v& ~3 I9 T% h8 S
4 W0 L. Q- i9 g4 k- }
// By default, all `createActionForEach' modelActions have
/ z, u5 D" q$ f7 E6 t. O8 G% j/ M" p // a default order of `Sequential', which means that the
1 N4 D* S1 W& G* U* J3 H) } // order of iteration through the `heatbugList' will be3 b; r9 j6 V) ~$ ]+ a+ S1 L
// identical (assuming the list order is not changed
& T- J; F" f: T$ l8 {) Z // indirectly by some other process).9 G0 g+ M7 a. I9 _) X4 U
2 H/ c4 }1 m- y& O/ @- x modelActions = new ActionGroupImpl (getZone ());/ T5 B5 E* d7 w4 a0 O
4 ~- N7 `2 a( Y c$ T% Y
try {
! ?0 N' o$ r \ modelActions.createActionTo$message- ~3 E" q& K; y& F' p
(heat, new Selector (heat.getClass (), "stepRule", false));
8 q; w' o3 @7 h" Q' I( Q9 \ } catch (Exception e) {
- F. ]& t6 H. Q- H4 U& r" r System.err.println ("Exception stepRule: " + e.getMessage ());8 Y, j: H1 G# Q0 Z; U
}1 M% h2 {4 ~! }: v
+ a! B$ ~( o: s7 |) x try {
/ I* s: J4 S' m: |6 L Heatbug proto = (Heatbug) heatbugList.get (0);
7 u9 B! {) |+ H+ u' ` Selector sel =
+ _+ o8 |" `2 I" C' K' R new Selector (proto.getClass (), "heatbugStep", false);
* O" J$ S4 N' Z3 l5 O6 V actionForEach =
3 p. F3 ]/ F0 c' B% X modelActions.createFActionForEachHomogeneous$call
8 g0 T" z5 ?/ X9 Y* t5 _# i (heatbugList,
: q" |* O/ H8 b* |+ i- }7 j new FCallImpl (this, proto, sel,! `! Q# L; @0 w
new FArgumentsImpl (this, sel)));+ M& e6 c: P& G) s% @" \% D9 j
} catch (Exception e) {9 a3 ~( Y4 t) a3 M1 c0 Y
e.printStackTrace (System.err);
' I8 U" Q# |8 J% `2 R2 L) g }
- l. {7 W j/ u6 ? 0 y& Y8 x; e$ P4 |
syncUpdateOrder ();" Q" W+ L% H! j* J$ w P; |
1 i7 g3 P9 z8 r% \3 t( b try { v4 T+ W2 ~7 f/ z+ O! t% w- Q
modelActions.createActionTo$message
5 Y7 X9 }" M: L+ i/ s0 q$ e (heat, new Selector (heat.getClass (), "updateLattice", false));$ F8 f# n# X( ]- ?
} catch (Exception e) {2 O8 K6 ~$ Z, \" J" I
System.err.println("Exception updateLattice: " + e.getMessage ());: ?6 J r! Y5 J7 S8 _2 z
}" |0 ^7 l9 k {' O% P' t: v
( x$ i% ^4 O2 e
// Then we create a schedule that executes the
' j0 T* ^7 ^: z; a0 A% c& @6 x // modelActions. modelActions is an ActionGroup, by itself it
* n1 [% `( }! |5 C1 ]' t% t5 U3 E // has no notion of time. In order to have it executed in
`* U) p3 _* a. S5 s // time, we create a Schedule that says to use the
; E+ [7 F# `4 u# Y; L( [6 t$ \ // modelActions ActionGroup at particular times. This
% A9 Q @1 A4 O9 f5 ^1 g/ b3 F // schedule has a repeat interval of 1, it will loop every4 U- C* j# G- u0 ]. M
// time step. The action is executed at time 0 relative to; C. V! \ T/ w* U% Z& M
// the beginning of the loop.6 a+ n, E: S' F/ l
# F3 P, m" h! I9 D- G& V) P // This is a simple schedule, with only one action that is }- P& e+ A6 D8 X7 W
// just repeated every time. See jmousetrap for more
/ p# u; d. V: e5 c \- B% M // complicated schedules.
3 V1 o8 H! o- N6 z8 u$ W, R1 x , I8 `" P0 I2 n- _
modelSchedule = new ScheduleImpl (getZone (), 1);
0 E# v" ~2 `; g/ Z# p: W7 [ modelSchedule.at$createAction (0, modelActions);
5 V5 Z$ B9 @% }0 x9 K ) O, S4 y/ P+ f' p. p$ ~/ d
return this;4 N1 J3 z+ {9 _; ~9 V" K! x" d
} |