HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:$ H7 e9 ?" a0 A T& Z7 w0 |
- [' _, S# E8 E% r% w- Z* @6 J5 O
public Object buildActions () {9 T" }6 {# p$ f: [8 y
super.buildActions();
# d" D, ?' b( Y/ k
: f8 ~2 i7 {" b // Create the list of simulation actions. We put these in
+ j8 B. V; J/ z( I) q7 D6 L; g // an action group, because we want these actions to be, v& m' [3 P; D" l0 M/ |, m
// executed in a specific order, but these steps should
( [: @1 {8 ?& [8 H* T( c // take no (simulated) time. The M(foo) means "The message7 x; p2 m) I1 E$ J4 q+ g' f$ v v+ Y
// called <foo>". You can send a message To a particular7 M$ T. J7 U6 X
// object, or ForEach object in a collection.8 W: Q: F2 C8 b) K/ t/ u
9 A; l z0 S3 ^' v% `8 z0 H2 R- |" a // Note we update the heatspace in two phases: first run
$ o6 `; m& P: [, ]! ?# { // diffusion, then run "updateWorld" to actually enact the2 w. x+ a5 w, M1 `: _
// changes the heatbugs have made. The ordering here is
/ X1 S/ D- t3 `2 |3 }! A# ? // significant!
9 N, \6 x9 v" f7 Q# O / R7 C l! n9 c
// Note also, that with the additional
8 X' J9 m. ]7 M* U; g // `randomizeHeatbugUpdateOrder' Boolean flag we can4 U$ n- ?) o3 e* t. J. ^
// randomize the order in which the bugs actually run
j1 s" k! ^4 w- f7 ? // their step rule. This has the effect of removing any
R( Q/ C* J( F9 V1 h // systematic bias in the iteration throught the heatbug
# q( y; W6 H. s! S _ // list from timestep to timestep# j; A3 b! }0 l! |, h) h
3 h; Z7 H [2 Y0 j0 A
// By default, all `createActionForEach' modelActions have5 O/ S# d, y& v- R% S/ x0 G
// a default order of `Sequential', which means that the1 I# I# m# F( [3 }! x
// order of iteration through the `heatbugList' will be
/ F- |& }5 U8 F+ | // identical (assuming the list order is not changed8 h0 J+ W3 e6 \* w
// indirectly by some other process).: J* z4 ^" ^1 X% m) h0 h
2 g4 j) d2 n( I modelActions = new ActionGroupImpl (getZone ());
) i/ |# [& W& P: P$ s7 [4 C' D3 k: H, I6 W- C
try {
( o& i1 f, B. r6 \) X' j! }7 { modelActions.createActionTo$message$ ` \$ u! v; E- ^) E; j: n
(heat, new Selector (heat.getClass (), "stepRule", false));& N/ z1 s+ ?. [) S1 r
} catch (Exception e) {
1 U( ]2 E1 K6 Z" o5 b9 {, K# O$ U System.err.println ("Exception stepRule: " + e.getMessage ());
* J" ~) F, E) O3 J H }9 k# I7 i R# F$ r( \
3 N$ N4 n% g- Y+ @. }/ @" `1 Q
try {: @" L; u5 y, d7 e
Heatbug proto = (Heatbug) heatbugList.get (0);
! H* A2 G% |$ d Selector sel = 6 P' J0 i0 h6 o2 ?# v. l" s+ [- Z8 W
new Selector (proto.getClass (), "heatbugStep", false);& B; [4 N3 M. h# E" }! u' u
actionForEach =
, ^7 c8 P7 ?+ w+ @# V$ L- w2 q' @' E modelActions.createFActionForEachHomogeneous$call
5 b' v. s9 P# H+ [2 N' }5 ] (heatbugList,0 L3 y0 z8 a8 S' l' n# p
new FCallImpl (this, proto, sel,' n: w0 E; j) L
new FArgumentsImpl (this, sel)));
# d4 e' t) u6 P1 n } catch (Exception e) {
/ ?/ E* ]) t: S5 E! W1 F: y e.printStackTrace (System.err);/ w$ q. I, c* F) V
}
9 X% L' W4 D" ^% ?$ G/ ?1 G7 o! R - s% F4 K; Q- z1 E$ |: w
syncUpdateOrder ();$ a. G- j: R N0 W5 j+ S
' S7 @3 c0 t8 a- b3 `: p try {6 ?" u7 s N% b# ]' W2 {5 N
modelActions.createActionTo$message , y x4 v% n3 ] |
(heat, new Selector (heat.getClass (), "updateLattice", false)); d7 i- r# d% @/ ]" \
} catch (Exception e) {, G5 x2 {/ J$ ]# y% K1 M9 f
System.err.println("Exception updateLattice: " + e.getMessage ());
6 U% Z4 |$ [8 m- p4 V+ A }& K% w6 Q8 X9 x0 J
8 V/ {/ A) C1 s( @( j& w3 y7 |
// Then we create a schedule that executes the
$ }+ T# Q4 b, m6 H1 h; X // modelActions. modelActions is an ActionGroup, by itself it( W6 |+ l7 x; R
// has no notion of time. In order to have it executed in
& B2 y M2 c/ r/ P& }1 x7 N // time, we create a Schedule that says to use the# N1 H1 `0 p+ s T
// modelActions ActionGroup at particular times. This
: m4 H8 G3 Z6 W6 Y& I# u' p2 T/ ~ t // schedule has a repeat interval of 1, it will loop every# k- k0 K& I6 ]
// time step. The action is executed at time 0 relative to, c! g7 `* I$ y0 W4 D. O
// the beginning of the loop.' x( q* ]# p1 l( _4 y W
) b/ S3 C3 j7 H2 ^+ a // This is a simple schedule, with only one action that is/ ^* l5 ~: A; u) A5 f; w# [- [
// just repeated every time. See jmousetrap for more
6 I( R, E0 ?% m: K$ |# h0 \( k' H // complicated schedules.( U) W7 @) f+ L- _) L& s9 U
% {5 @4 e% j: {( A& B
modelSchedule = new ScheduleImpl (getZone (), 1);
& t: o2 w& _& u2 \7 e0 m, M" D modelSchedule.at$createAction (0, modelActions);6 {' Y2 Y. Z$ R g/ S7 h0 \* h$ L7 u
$ b! K! ?5 _2 T6 j( } return this;
) x6 U$ p4 b' A$ g: O& |" s9 d7 M } |