HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; y+ [. z' A4 ^& O* }4 s
; }7 P, E }0 l b# _
public Object buildActions () {+ Y( g) B- c4 b" n* y4 B$ h; f
super.buildActions();+ L! x* D( Y; J# w% I- m6 O
% p$ D2 F' N" C. U) \ // Create the list of simulation actions. We put these in
$ d. x6 d7 `* i5 e8 i* O k // an action group, because we want these actions to be
& P2 r! f* p/ G+ ] // executed in a specific order, but these steps should
2 G/ i+ [5 j' Z4 S7 L P // take no (simulated) time. The M(foo) means "The message. y" H( c. @2 Z" O9 D$ }- T
// called <foo>". You can send a message To a particular* m! w! v( d: y/ l' v X
// object, or ForEach object in a collection.7 \1 m" Q7 @* g" k1 r% M% F
) u* q7 u8 h% u6 S* Q# O // Note we update the heatspace in two phases: first run
+ _' z Y9 M. x/ Z // diffusion, then run "updateWorld" to actually enact the3 _# ^7 |8 W5 s( D& w
// changes the heatbugs have made. The ordering here is1 f( }- t$ H2 F% O# F
// significant!
' D3 H) k. I# M$ V! X8 n
L+ n/ n9 m, m+ p // Note also, that with the additional, o+ Y* C6 G/ g3 D
// `randomizeHeatbugUpdateOrder' Boolean flag we can
; V% V6 s- K8 e! c! d // randomize the order in which the bugs actually run
1 @0 B9 j6 p/ a, j2 b/ i // their step rule. This has the effect of removing any
4 D: ]7 i( j6 T. C/ Y# Q9 ]& K; m( ]0 J // systematic bias in the iteration throught the heatbug4 g7 b5 G9 t+ }2 ~# G
// list from timestep to timestep, M7 @& N' C( w) n' Z% _2 d' ~6 y
/ ?% P5 F% \2 a2 g // By default, all `createActionForEach' modelActions have. p9 @4 _' A" h: d
// a default order of `Sequential', which means that the
9 S( \$ S4 w9 n# B! { // order of iteration through the `heatbugList' will be
: p+ x8 l0 a! B- ~ // identical (assuming the list order is not changed
- T0 @. C0 x( T( y // indirectly by some other process).' b/ y" j; E" ]# T1 j
- \2 Y8 J+ F& y% w0 C# q4 B3 W
modelActions = new ActionGroupImpl (getZone ());& U( S1 Q9 E& B9 M
A; W8 e7 v; Z$ G$ I V
try {
+ i4 x% B4 q( x& A/ u( z0 t modelActions.createActionTo$message& Z" ?* H% a5 n
(heat, new Selector (heat.getClass (), "stepRule", false));3 F( g1 V$ U3 ^" }- d. N8 }
} catch (Exception e) {" T p* d m! W( m4 E
System.err.println ("Exception stepRule: " + e.getMessage ());+ N/ R; X0 D/ _! I p2 k
}- G% O( Y! o; K
v9 ^, Q6 Y, G( }/ D try {
9 m3 H* i( C; H+ M# s* ? Heatbug proto = (Heatbug) heatbugList.get (0);3 E! x8 W% r6 W/ d
Selector sel =
+ W5 P% L3 ^8 H new Selector (proto.getClass (), "heatbugStep", false);5 ]0 j. j8 u! w; l- F6 ^, N
actionForEach =9 `6 y& O4 `) b: }
modelActions.createFActionForEachHomogeneous$call
9 D2 O2 C) h- k3 m (heatbugList,5 d; V2 L" u" ?: d; W
new FCallImpl (this, proto, sel,6 \& G# {# t! Z0 x
new FArgumentsImpl (this, sel)));5 x6 `; h% d9 x8 Y) }. `
} catch (Exception e) {
) l3 h" o+ s) @6 @6 K4 x! N e.printStackTrace (System.err);6 }+ L9 r) P" D0 g! b, F
}
) w+ D/ i0 F* Z5 N
, S0 R* y& I+ W syncUpdateOrder ();
0 \+ i# P+ l+ c$ P6 N$ B+ g9 u# K0 R4 L: u
try {3 l# [1 T( N8 k% A4 `* I0 X, u8 j$ h
modelActions.createActionTo$message 8 V1 I6 C9 J% u
(heat, new Selector (heat.getClass (), "updateLattice", false));
3 u' |: j( M! C } catch (Exception e) {) R3 F3 b, b) {- M
System.err.println("Exception updateLattice: " + e.getMessage ());5 X5 ^8 z1 C# G; c1 |# u7 P: C
}
: Q* l4 `$ { ?
; e/ y& d% ~8 P& l. {, r // Then we create a schedule that executes the
& S9 L$ @2 d$ P" B4 a; c& Z3 w // modelActions. modelActions is an ActionGroup, by itself it
3 N1 S3 \/ I( \7 @9 b // has no notion of time. In order to have it executed in* y& E1 u' z+ `8 Y4 S" T- V% @$ R
// time, we create a Schedule that says to use the' m. g% _$ Q, `! u4 M' z
// modelActions ActionGroup at particular times. This7 M! x! {9 r4 c! |& \' F( k
// schedule has a repeat interval of 1, it will loop every0 S- w# I6 ?/ i, X; N4 H3 n- b' q4 @- L
// time step. The action is executed at time 0 relative to
" `! n- p" d9 s, _. ^ // the beginning of the loop.2 ~7 G: X3 [- X
; _) g) ] Y5 z, M
// This is a simple schedule, with only one action that is
* R% C& t1 O- z // just repeated every time. See jmousetrap for more
1 U$ |# j# c/ y( T" | // complicated schedules.
$ ]/ t' `7 X) D2 f- v % X" U1 H5 c: S; c
modelSchedule = new ScheduleImpl (getZone (), 1);
: y/ D: b/ j# L0 k5 P- {$ a4 A modelSchedule.at$createAction (0, modelActions);
' M; O+ O/ ^; `; X
( p- Z0 H1 B+ _4 z( q% [* r3 V return this;8 H: H3 G- @) T4 }( B$ @
} |