HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
f' W4 E( h$ G ~* U+ a5 R0 P, U+ p d/ h( C& @
public Object buildActions () {5 e4 k; P) q- j" E
super.buildActions();' i& G8 `) B! n7 ~. n( w \
$ {/ V# @' O* X) Y
// Create the list of simulation actions. We put these in
. C# L& l. Y0 { X // an action group, because we want these actions to be
8 t% s. Z V9 L( F0 o1 P4 ~2 u // executed in a specific order, but these steps should
) _, u) _3 k& L1 Y$ n9 T6 | // take no (simulated) time. The M(foo) means "The message, }) E4 z1 T+ [+ }( C) K' O
// called <foo>". You can send a message To a particular" g {. u4 E, x# Z6 f2 u' _0 f/ f6 h
// object, or ForEach object in a collection.
b& g1 J- }1 g# D 3 |; ^3 e I' H3 k" i+ d9 {
// Note we update the heatspace in two phases: first run1 G! [. W9 d. O1 ~
// diffusion, then run "updateWorld" to actually enact the% E x" O. Y& o2 I7 H# |# @
// changes the heatbugs have made. The ordering here is
2 x5 O0 @0 L' `4 Q2 ~ // significant!$ X v- C4 `/ h6 G
) \# V6 p; j2 g. u3 y
// Note also, that with the additional$ S7 k2 W" k' `$ l2 O
// `randomizeHeatbugUpdateOrder' Boolean flag we can) L L- l) C. @/ F9 O4 X
// randomize the order in which the bugs actually run
0 \$ c/ y2 V$ Z! y6 E // their step rule. This has the effect of removing any
. |% |, q% S0 {7 d+ u) o4 i, |" @ // systematic bias in the iteration throught the heatbug
8 C0 A7 U! f& R) U6 T3 \: _ // list from timestep to timestep
& C: a$ y' ?, B6 R
9 [5 M) T$ |2 d" S! X' D) Q) Z // By default, all `createActionForEach' modelActions have4 ?. g! U5 D; p0 V7 V
// a default order of `Sequential', which means that the
$ z# ~, g3 ?) S7 U9 W C9 l // order of iteration through the `heatbugList' will be9 ~' q; {5 Z' Q& W; e- |6 Z
// identical (assuming the list order is not changed, w8 [. h: e+ ~# m) I; }
// indirectly by some other process).$ M6 }7 B; l6 T8 ~, E6 c
, Z$ s1 F4 P9 E V, S9 n- w
modelActions = new ActionGroupImpl (getZone ());
4 I1 ~4 w! Q" i. J0 C
1 R2 q2 V5 x: p4 o try {3 A$ f" T" P4 G6 L4 R4 f# u7 |
modelActions.createActionTo$message
! w; d/ T' B; G9 H, E6 _4 X) A1 L4 g (heat, new Selector (heat.getClass (), "stepRule", false));* y* H1 i3 b: s1 u
} catch (Exception e) {
6 S2 _5 k5 b0 [) q) s6 L1 F9 P System.err.println ("Exception stepRule: " + e.getMessage ());& b3 _# W2 e3 Q& M- s# I- R4 A
}
6 ^6 l! w; g$ P; R1 \1 d( U$ q- n4 u u" x; A8 u# m4 f
try {9 F e7 e9 C" Z: w5 T+ F# R
Heatbug proto = (Heatbug) heatbugList.get (0);
) X* l' t# w4 N* ~ Selector sel =
6 l) O" @$ f1 g$ i$ k" g new Selector (proto.getClass (), "heatbugStep", false);
- g% ^# L$ W. W3 \4 ?' o actionForEach =
1 m; r9 U5 f" X9 o2 W! ? modelActions.createFActionForEachHomogeneous$call
9 a& p# b+ i2 `+ t (heatbugList,
( b0 N# h. k5 a0 q new FCallImpl (this, proto, sel,
( p8 M, A" t- ? H new FArgumentsImpl (this, sel)));4 O, `& ^. _2 a& p
} catch (Exception e) {
& A6 a. x! t5 C2 K% @5 ` e.printStackTrace (System.err);
) R% C. }5 ? S6 P& ^7 {% s! s | }
/ e0 X- `% M1 n7 N( M . l, z1 F! _5 i8 W4 T6 Y
syncUpdateOrder ();& ^% H* ~3 g9 ]+ g2 g
1 V6 C( F: r4 {( [+ k5 n) a' j# f, u try {
; g$ m+ x' A- f5 s modelActions.createActionTo$message . S* r3 b/ R6 k$ c! Q8 _
(heat, new Selector (heat.getClass (), "updateLattice", false));
' ~! ?$ Z8 i' G1 P. g+ R } catch (Exception e) {
/ P; E6 x; g* l, j System.err.println("Exception updateLattice: " + e.getMessage ());: y6 a( E. _* { i
}
! I: w! U) V) [9 X! v1 c0 s 3 L- f( B) Y6 y7 u" j6 K
// Then we create a schedule that executes the1 \$ A- U/ l' [4 S0 [, y: a! K6 Q
// modelActions. modelActions is an ActionGroup, by itself it
$ a `: k9 U m- Y$ X( y( g // has no notion of time. In order to have it executed in
+ [4 U/ o5 h1 P) c+ c+ z // time, we create a Schedule that says to use the; C R5 ?* v: v- G! E. `0 o8 P/ w
// modelActions ActionGroup at particular times. This
# r$ h8 r+ f v a // schedule has a repeat interval of 1, it will loop every
$ U" b8 L1 q/ R4 b/ E7 G // time step. The action is executed at time 0 relative to. C9 W9 |, H# e* Q; H
// the beginning of the loop.3 |+ _6 @' X, V* G5 O' o# J1 P% `
$ _) s& ]6 c, B3 \* z
// This is a simple schedule, with only one action that is4 i! O- j. ?4 Z, ]! i) C; ^
// just repeated every time. See jmousetrap for more
$ k' P$ f8 M- d // complicated schedules.( z. n. ]0 K' C! j& w: i8 I
/ u" S& X. [. Y. Z- d8 j# H3 V
modelSchedule = new ScheduleImpl (getZone (), 1);2 i c9 N& c+ W/ C
modelSchedule.at$createAction (0, modelActions);1 x, c! h, K" l7 e. T; H
+ x. O7 P* y# e' T8 k6 j. w return this;+ v' Y% D2 l- }# y. a& C
} |