HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 K% z0 e# h4 E$ |9 P0 A+ t& J
3 N1 E9 H* q. g public Object buildActions () {4 t6 f6 ], g5 u" E
super.buildActions();1 B2 M. g9 S; n% P
' D9 d8 g0 h5 z" a; ]$ u! r // Create the list of simulation actions. We put these in
9 f0 Y. o# a. @) P9 g. h+ p( m // an action group, because we want these actions to be5 V8 J! l2 E, q a p" C0 }
// executed in a specific order, but these steps should/ G- B. B* s6 i, v
// take no (simulated) time. The M(foo) means "The message
, y: E r/ H4 j+ {0 \+ l // called <foo>". You can send a message To a particular: E5 v& C* Z9 F( k2 @, o+ e6 h
// object, or ForEach object in a collection.
7 j3 ?, ?3 s' X1 D 5 c+ i7 s; c: {( z# Z) J! _: V
// Note we update the heatspace in two phases: first run+ s: i2 Z4 C8 x. D5 F5 M, w
// diffusion, then run "updateWorld" to actually enact the
# Q9 {" C' a3 ]0 V+ i // changes the heatbugs have made. The ordering here is: {! X- j; N, |( F) e6 o" b: R
// significant!6 F, p& I- j6 Y; M( J4 `, ~
. a, l9 r# l7 F( J# H! T // Note also, that with the additional& T f$ e9 D# u" N3 w1 [( d
// `randomizeHeatbugUpdateOrder' Boolean flag we can
2 ^3 H' }$ ?4 m# u" c, o3 l4 x // randomize the order in which the bugs actually run. \% z2 G& _1 l4 D
// their step rule. This has the effect of removing any
& I% F6 Z! l l9 I$ I1 ` // systematic bias in the iteration throught the heatbug" a$ n7 H! r! K& J
// list from timestep to timestep# l9 c. U. ~* B8 g
* }$ T. v0 b' i; e, @0 p' ?8 d // By default, all `createActionForEach' modelActions have
" B7 n5 K/ h# j' D // a default order of `Sequential', which means that the
) ?7 K7 {; G* ?+ U. Q9 o. `# t // order of iteration through the `heatbugList' will be, _5 K6 f1 y9 L/ Z* u$ U
// identical (assuming the list order is not changed
9 x v2 d( B, S+ a // indirectly by some other process).
- F2 j2 G2 G: p8 v 2 i4 l/ B3 p6 F9 O7 X
modelActions = new ActionGroupImpl (getZone ());
6 F8 @6 l. a( l; t6 [: }6 ]; L, Y0 @# J
try {. v& e. Z. {4 ]1 N/ F! H0 `
modelActions.createActionTo$message
@; Q! r1 ~, y$ a4 u& w' s( _! `7 j (heat, new Selector (heat.getClass (), "stepRule", false));/ K; W* l% Z5 i& m) O6 L2 Y: @
} catch (Exception e) {- I% @* o8 Z4 |6 _" z
System.err.println ("Exception stepRule: " + e.getMessage ());* a; H7 {0 ~) r1 ^
}
7 o0 {/ ^ J3 I- e. O
' {: X- `' ~" N4 v try {. d1 P' w! R% m7 X" N* @
Heatbug proto = (Heatbug) heatbugList.get (0);) H' t& y; O0 g! V$ b
Selector sel =
$ `/ Z" d. @7 ]% L; h new Selector (proto.getClass (), "heatbugStep", false);
8 `% H1 _4 C- E$ ?4 G+ G$ M actionForEach =
( j- X4 i6 M1 t! `1 T modelActions.createFActionForEachHomogeneous$call3 ^: D5 ?& e0 N$ M
(heatbugList,4 e: ~1 d* f' \" p8 `
new FCallImpl (this, proto, sel,1 t) B( X7 ], Z- E. M
new FArgumentsImpl (this, sel)));. L1 E+ f* \6 G# |, P
} catch (Exception e) {
6 Y4 B- }+ A9 o& v e.printStackTrace (System.err);
3 S# _3 u' B2 C( g/ D }3 i0 @2 ]6 J b% \0 G9 u
' D7 @ L4 W: K) g% \, X
syncUpdateOrder ();: P$ X, J3 O8 P, J$ H6 q
) A2 Y. g5 O, A: O. P try {
9 `8 t, v1 O; J+ W" P& n6 q modelActions.createActionTo$message & ~7 E& t2 \" C# i$ Y+ T
(heat, new Selector (heat.getClass (), "updateLattice", false));
) @) b6 f9 m! R: j+ F( T8 C } catch (Exception e) {" l2 ~3 A+ Y. V" k
System.err.println("Exception updateLattice: " + e.getMessage ()); I* t3 I, i$ B% i& a: C
}% `' @% V( `6 } B1 M& ]$ }
* D8 L0 T( x( A0 Q2 E
// Then we create a schedule that executes the
, n! X$ b7 \1 Y' z n, A // modelActions. modelActions is an ActionGroup, by itself it7 K; K7 T5 {+ h
// has no notion of time. In order to have it executed in& k E9 F5 A$ D$ s8 c4 |
// time, we create a Schedule that says to use the9 x7 N. u: W' f5 m. p; U9 d
// modelActions ActionGroup at particular times. This% [# n5 b6 f$ f& M3 T
// schedule has a repeat interval of 1, it will loop every
4 A- y* P5 F6 A# f& s* l/ o // time step. The action is executed at time 0 relative to1 C! z. S6 j2 J/ N* ]8 i& k) C
// the beginning of the loop.8 |( a' h4 o6 d1 b: m9 A$ P
8 V9 L8 j% [; N. R# B' I% h1 W // This is a simple schedule, with only one action that is8 M5 n9 P+ g+ d; G' b
// just repeated every time. See jmousetrap for more! e" Y4 X2 z& d; e# N$ | |( e
// complicated schedules.
- I4 G3 n. z h, J8 g9 z, _% |1 M4 ?/ l3 V
+ f/ z! n2 ?, g+ ` modelSchedule = new ScheduleImpl (getZone (), 1);5 i( M& Y' l' |" J P8 A* J; ~
modelSchedule.at$createAction (0, modelActions);
3 V9 T( d: E( Z; o. f
7 s" a" ^1 K/ D J( a; t( j: ~ return this;( G1 G F9 j; L e: g9 s3 j
} |