HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& E+ x9 ]6 Y$ A& P/ B6 w8 M O2 {8 I3 Y. }% k3 \' D- j5 T
public Object buildActions () {4 e; C4 \5 z+ ~! e" j
super.buildActions();) d: c) r: `" N3 X8 k. L! k
5 e% z0 ]2 l% {$ L // Create the list of simulation actions. We put these in3 G" s! Q" M0 Y9 V. q/ I
// an action group, because we want these actions to be3 E; A5 d- {7 N( w" U! {
// executed in a specific order, but these steps should9 h7 N# o" {( {6 J7 B) j0 N
// take no (simulated) time. The M(foo) means "The message7 V6 @% `% U* ^. m; B5 G/ g. d
// called <foo>". You can send a message To a particular0 Z. {. l& u2 x U7 v
// object, or ForEach object in a collection.
- r. D# m; B7 D 9 Q+ d4 x4 ~+ G" c( K
// Note we update the heatspace in two phases: first run
$ ~: \: b6 a/ z7 E W# S // diffusion, then run "updateWorld" to actually enact the
$ X* }+ i3 G* s# Y C // changes the heatbugs have made. The ordering here is1 z8 U$ {, J$ G
// significant!( x; T. q1 ]8 O; K
/ s* Q6 b: V2 T5 S1 A
// Note also, that with the additional
' Z. c' s, ]+ ^4 }: m // `randomizeHeatbugUpdateOrder' Boolean flag we can- ?! M" L( c. O( p
// randomize the order in which the bugs actually run6 y' {8 I8 E9 P% o1 B4 z
// their step rule. This has the effect of removing any! t6 d( c3 B7 Z2 X. z
// systematic bias in the iteration throught the heatbug
+ I0 U8 G8 a S$ p1 u2 y // list from timestep to timestep& c) c& x( C! s; p! c }% k
+ @7 p- J" E5 z2 t // By default, all `createActionForEach' modelActions have, `, f' p; b! S
// a default order of `Sequential', which means that the
, O0 ~: t9 Z# F1 e; m7 E/ j5 H7 n // order of iteration through the `heatbugList' will be
( a* z; x% k0 I# g# H$ @ // identical (assuming the list order is not changed4 Z# T- w3 q' I+ v6 ~$ \
// indirectly by some other process).9 |; }) F! y! e l1 D) l
/ e0 _6 x/ R/ l* p5 f3 W( U, ?" m
modelActions = new ActionGroupImpl (getZone ());
$ Z/ i B9 o. p! ~+ F! M: I" o x% a, v- ~9 [; }! K9 O
try {1 o# d0 g9 ?- U, y9 @9 k
modelActions.createActionTo$message! H# \6 \# g; L# C8 g! l
(heat, new Selector (heat.getClass (), "stepRule", false));
: J1 c7 J5 W9 j$ C! a } catch (Exception e) {
' t- q$ H6 Y. z- R. G System.err.println ("Exception stepRule: " + e.getMessage ());) h) |/ I: G" W6 @, n
}; N+ [# n3 N! p' I% G3 ?
4 i" s" m0 v$ O* k4 X" s. h0 W8 O4 P1 z
try {7 p4 X: R/ e/ [$ W$ b
Heatbug proto = (Heatbug) heatbugList.get (0);
" y- j! e) Z! \+ Q/ J Selector sel =
3 P! i, r8 M, G+ u( ^4 W/ K" U new Selector (proto.getClass (), "heatbugStep", false);
+ ]' l% c6 i: o/ r" e1 c actionForEach =
) Y2 q# O, G1 n" M9 w$ A& H) p1 r( ` modelActions.createFActionForEachHomogeneous$call
9 `9 j, w" i3 t8 O! { (heatbugList,
3 N! p9 E0 h# [8 J new FCallImpl (this, proto, sel,
9 O) ]5 d6 _3 z* \3 n3 ?3 H new FArgumentsImpl (this, sel)));5 w8 {: R. N# ^3 b
} catch (Exception e) {3 E! x- M/ I0 ^
e.printStackTrace (System.err);
- a9 g; E6 A& t- X) R* y8 c }& C C) t% t5 R/ B4 D3 y
# H R" T) t5 M2 q+ ? syncUpdateOrder ();- P4 d6 a4 k0 ^2 Z
2 ?2 r1 C/ _# H& }, u: g2 c try {# l% x' X% X% D* @4 G8 c4 Y
modelActions.createActionTo$message # n" y( {0 z* S7 V7 ~0 e7 g
(heat, new Selector (heat.getClass (), "updateLattice", false));* ^$ _% q9 E' R5 L/ d2 X- S
} catch (Exception e) {
; \" J& J+ ~4 z5 N System.err.println("Exception updateLattice: " + e.getMessage ());" D) x( o* o: G6 V! X- G; u
}
9 N! u# A' C! e$ C1 T9 x
. G9 O+ |6 y& L // Then we create a schedule that executes the: c1 Y2 B d4 C8 `# z" v1 \ S; ?5 J
// modelActions. modelActions is an ActionGroup, by itself it4 G d8 P" f' x n
// has no notion of time. In order to have it executed in1 r; x; W: }: M0 M
// time, we create a Schedule that says to use the
6 V) M1 m! Q/ C+ Y // modelActions ActionGroup at particular times. This
( b" ^+ Q, V' j // schedule has a repeat interval of 1, it will loop every
* b1 n( q- k3 ~. ~" S // time step. The action is executed at time 0 relative to
5 q! ?5 _* |! F% M1 N3 P# w // the beginning of the loop.5 }/ |+ N" A8 d- p/ n* f
/ X" U" |( a7 c
// This is a simple schedule, with only one action that is
$ f+ i! q4 p" ^3 f5 {: u // just repeated every time. See jmousetrap for more
% B n3 j# l" A* @( C( T8 H // complicated schedules./ Z* g: M* C1 C# C
3 x7 r* t5 s( h6 e modelSchedule = new ScheduleImpl (getZone (), 1);
3 |: G) q. U* \! \ x3 d& o7 b2 t modelSchedule.at$createAction (0, modelActions);
1 ~' L( `9 c* c* X 4 t r" p Z& N/ H% ~, p. k0 W
return this;9 w+ U0 R+ p/ W) O
} |