HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 r l8 g! l4 m# k( {8 h8 Q$ s! P# s4 G5 I* {" f
public Object buildActions () { Y3 u% ] ?/ L
super.buildActions();+ k9 z/ ?3 Q0 z8 H; ~9 b
1 e" g* D0 G. ]% l // Create the list of simulation actions. We put these in
; U1 R2 A) Z3 h( @7 { // an action group, because we want these actions to be
3 b& G7 L3 K6 q6 m q1 t% g0 } // executed in a specific order, but these steps should
6 u0 G) X3 K- q6 D' I* b; w) ? // take no (simulated) time. The M(foo) means "The message
' O5 w" o- o& L1 \0 W // called <foo>". You can send a message To a particular6 `0 Q: O: Q; y' p7 `
// object, or ForEach object in a collection.$ g4 B4 d8 j v1 l9 J3 Z
! T: u) l8 @. Q8 j+ U
// Note we update the heatspace in two phases: first run
! H! a4 {% R/ n$ d2 u) e- h0 F% C // diffusion, then run "updateWorld" to actually enact the3 S( q2 G- C" e3 B* n
// changes the heatbugs have made. The ordering here is
t2 G7 i8 \2 B) b6 b, [ // significant!8 \! e3 x8 D2 M4 m; y3 M
; t4 X+ {. `7 ?- S7 J8 q
// Note also, that with the additional& a4 N9 F5 c' R* R
// `randomizeHeatbugUpdateOrder' Boolean flag we can
# O1 S: J g2 m3 m3 c, V // randomize the order in which the bugs actually run
3 y3 I2 u0 [' k; o // their step rule. This has the effect of removing any) ]* _9 _* h, a& d8 r
// systematic bias in the iteration throught the heatbug- i! Q4 C! V7 P( z% i
// list from timestep to timestep2 E) r% c: v3 W1 S/ Z2 v: I- h
/ a1 K9 X4 Z4 ~3 G1 o" V/ R! Z3 |
// By default, all `createActionForEach' modelActions have8 {+ F& G& B4 P
// a default order of `Sequential', which means that the
' O9 z3 ?' a% H3 K* m) d' V // order of iteration through the `heatbugList' will be
2 p$ p# [) u# G2 L6 u7 R [8 H // identical (assuming the list order is not changed
3 z" P5 `1 V+ ` // indirectly by some other process).: {; z1 M, H* C# z- t
+ f0 j! V4 w4 i5 {+ K modelActions = new ActionGroupImpl (getZone ());% x+ B% B: b2 V$ E; z% P# D9 @
. u+ w; q# D: d try {! Q. ?' ^: O4 J8 g* r+ U* ?9 x; U
modelActions.createActionTo$message+ |, ?4 Q' p- [ F: t
(heat, new Selector (heat.getClass (), "stepRule", false));
/ @2 E$ p! q8 B( }- g% x } catch (Exception e) {
0 ^, w% E* q7 t" ~; q System.err.println ("Exception stepRule: " + e.getMessage ());1 _+ l/ `( c+ ]% L. C
}
! S# C& _0 B. ~$ a" F8 F* M( z3 }- S! u0 H" f
try {
9 q& k: A+ W6 |* P. K+ ? Heatbug proto = (Heatbug) heatbugList.get (0);
, \& \$ y+ }8 m o( N* G; w, b Selector sel =
: q2 d% _. g0 m6 y% j new Selector (proto.getClass (), "heatbugStep", false);
* W% l# e6 ]: `6 V( C" E actionForEach =
4 ^0 C# g T1 \$ \7 o. { modelActions.createFActionForEachHomogeneous$call
! }3 ?4 |' ^4 h' p1 @ (heatbugList,
( M8 X$ l5 R2 r new FCallImpl (this, proto, sel,
& D7 T0 S9 H5 `) o new FArgumentsImpl (this, sel)));8 a/ d/ E$ E$ N! t" b# q
} catch (Exception e) {1 `; a; P+ m; @/ J/ _: s' z% u: [1 a
e.printStackTrace (System.err);' l# Y* |' H+ z* A
}
4 m0 H0 N; Y$ [7 ~5 `1 ^ 5 t' y$ M# [, C7 j1 m
syncUpdateOrder ();
# w a5 Y2 P' x+ a3 r8 }
* n" p! V# c0 u try {
" |* Y0 X/ u/ L1 v. G+ f3 [4 c modelActions.createActionTo$message % U3 F. e; ~ d, H5 z: J
(heat, new Selector (heat.getClass (), "updateLattice", false));
; l% I6 J2 z- O7 b" k2 k. q } catch (Exception e) {; ?/ H$ w. N1 Q
System.err.println("Exception updateLattice: " + e.getMessage ());4 P( z9 o+ y# c7 v
}
1 M% p, J* S$ _" n$ k
& \+ t& a- o: K" x' [$ u // Then we create a schedule that executes the; s; C8 a- Y5 R8 n7 \6 _
// modelActions. modelActions is an ActionGroup, by itself it
0 c. t4 ?. t" z$ M2 S // has no notion of time. In order to have it executed in
# i3 q: j7 w1 G$ v& E // time, we create a Schedule that says to use the
2 I6 G. U( s9 ` // modelActions ActionGroup at particular times. This
$ c! _: u: A9 E6 A; D8 H // schedule has a repeat interval of 1, it will loop every/ H) M8 \& K- _: d/ p( r! {
// time step. The action is executed at time 0 relative to
, _$ [1 M, Y' c4 p/ \! [ // the beginning of the loop.0 `, n( a# g* x7 A& S0 V; g, O7 I
: L) v9 l. G T6 T, d" J( h // This is a simple schedule, with only one action that is
9 ]. c0 b. W _/ }6 d // just repeated every time. See jmousetrap for more
' f g1 P0 |$ f/ c% w // complicated schedules.6 `$ } g+ [' k' o
1 p- Y- C) @! X* q modelSchedule = new ScheduleImpl (getZone (), 1);
6 a6 h0 a2 q! m. t+ d2 x modelSchedule.at$createAction (0, modelActions);
/ g5 A# I: a l 8 |1 W4 S$ q4 c9 r
return this;
. Y. H) J* W2 j5 m+ N- b } |