HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 m# K7 I1 Q9 X1 I
3 W0 k7 F3 U6 h, @+ P1 I, N0 A public Object buildActions () {
- n1 Z( w4 T' { super.buildActions();
8 m/ e1 ]' i, r; A c) t ! {, U, J, I% ]# `* i
// Create the list of simulation actions. We put these in
2 G1 }/ Z1 u- c // an action group, because we want these actions to be$ f( E: e+ K2 F; ^6 t, B: o7 u
// executed in a specific order, but these steps should
+ [: \& g0 S5 H- y9 z/ y. a/ i, |$ ] // take no (simulated) time. The M(foo) means "The message
0 s- n0 y5 H: S1 ]7 S% K/ ` // called <foo>". You can send a message To a particular
2 R# c6 \" H' T7 h; T0 t! v- u: d // object, or ForEach object in a collection.
# \( D2 ]" i. O' U
* O. J, [2 {+ e4 @! y# L# d. Q' J // Note we update the heatspace in two phases: first run& d5 D* q3 Y9 I# }- U
// diffusion, then run "updateWorld" to actually enact the
4 u4 Z+ Q7 @/ ]: y- u0 ^& \ // changes the heatbugs have made. The ordering here is; F2 J$ x& k/ w
// significant!
) i. ~# T) D& ^4 v* o " o4 g3 E( L0 L! x$ R4 g$ O4 s# G
// Note also, that with the additional
& m7 g4 r, Q5 I# V- [& w // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 Q# V% K$ ?0 j- T1 S/ Z+ n0 B! M/ y // randomize the order in which the bugs actually run
' o# e8 y. f3 H6 y, C8 }% l" N7 w! @ // their step rule. This has the effect of removing any
! @3 W. W/ f' O3 ]* C$ g1 m. @ // systematic bias in the iteration throught the heatbug' a8 [6 {/ F# K! z4 F9 O
// list from timestep to timestep! J; n4 j( I2 F7 ^2 B; ^
: n+ q" B, `8 R+ [6 M) U% d" d8 r // By default, all `createActionForEach' modelActions have
& V, h @/ v/ ?1 C) Q // a default order of `Sequential', which means that the
4 n4 I9 v; k7 s R( v' Q6 y8 ^# d // order of iteration through the `heatbugList' will be
& a6 w! [: `! ^) W2 N // identical (assuming the list order is not changed
' C) K& ~$ d# T* q8 J // indirectly by some other process).
0 A- o! [, E3 R7 t" `/ r. m # y2 Y6 z* j; j* t) g, I: f) N
modelActions = new ActionGroupImpl (getZone ());2 D5 X5 \! }% D; A8 P; S
0 `# z' |' ?5 k, g
try {7 {2 ]3 y7 ~& x. e. ~. } j3 r% D! s
modelActions.createActionTo$message
0 H& ?7 \8 R; Y0 H+ S8 j3 q' k (heat, new Selector (heat.getClass (), "stepRule", false));
# U0 d, U0 F0 C9 Q5 N( x5 E } catch (Exception e) {
# y+ t1 \: v$ q System.err.println ("Exception stepRule: " + e.getMessage ());0 G ~. v* q6 d: t4 L4 g8 \
} Z' z9 [ L: f$ M2 ]6 ~* q
/ c/ e) L1 G, v$ e try {( s7 a" X r) z' X. C/ }3 j
Heatbug proto = (Heatbug) heatbugList.get (0);+ U3 T8 K0 o* |( }6 F6 I1 s5 B' ~
Selector sel = " Y0 ]' O4 o. J) g; R1 B
new Selector (proto.getClass (), "heatbugStep", false);
{# H$ a6 {2 a+ @ actionForEach =9 U( v- B, x+ y+ c
modelActions.createFActionForEachHomogeneous$call7 @- p! X% G3 v" V& V k: T9 U
(heatbugList,2 \1 [6 S/ w+ O w, A' g
new FCallImpl (this, proto, sel,
# e F; e& h" S# H: _2 A; z ] new FArgumentsImpl (this, sel)));
' ?! c& {; M! G9 k! V2 ^! A* U } catch (Exception e) {
8 Q' E* Y; \/ F, }! l \( a! c e.printStackTrace (System.err);/ N; W3 [ Z O% [4 m0 M
}* E5 y, S4 ` G
/ H2 i; P: T. l* Q7 [ syncUpdateOrder ();
9 C% ?/ {* u: h( v# i, ]
. \5 D& |5 x5 `0 N: B try {7 [# z) w, Y! N5 s! [
modelActions.createActionTo$message
/ F3 b9 y. x+ Z/ h* x (heat, new Selector (heat.getClass (), "updateLattice", false));5 v( o+ v- k# I6 b2 t
} catch (Exception e) {8 M. U! ^. T j/ S0 G$ f
System.err.println("Exception updateLattice: " + e.getMessage ());) H$ U8 K, U2 V- K6 H# [! @
}
6 a$ e- p" I* W" A# Z4 t0 \/ ? ( R: ^+ ?+ X2 C4 I, e" w' v
// Then we create a schedule that executes the6 w! R, j% V& I5 x( v, w
// modelActions. modelActions is an ActionGroup, by itself it
1 K+ \! G8 ]2 A // has no notion of time. In order to have it executed in
9 j- X6 H) T8 D8 }( T // time, we create a Schedule that says to use the
! L7 `7 i$ a6 ~) e: z // modelActions ActionGroup at particular times. This* M% ?4 ^, v7 y6 A; r+ [
// schedule has a repeat interval of 1, it will loop every" u8 u" R# e* j% _
// time step. The action is executed at time 0 relative to
# L6 {' s9 c0 c- @0 M // the beginning of the loop.
0 @$ J: j& j* {3 b5 o0 T, [ B% P: z" n; X9 j: {
// This is a simple schedule, with only one action that is, Q6 F: I3 A$ P1 Z% ^
// just repeated every time. See jmousetrap for more
A3 D. Y% M5 c: F' `/ q // complicated schedules." {: t3 ]' _, ^% n& F
; h' k% a( E9 _ g8 K6 w( o3 D
modelSchedule = new ScheduleImpl (getZone (), 1);1 X* l* |# i8 |4 T: a# v1 _
modelSchedule.at$createAction (0, modelActions);* N) h$ u& z8 _% z. z; g
0 t/ _( w; s9 O# `' \0 U) C return this;+ ?% N1 T, B5 V
} |