HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:* A' ?: p) _! K7 J1 \
& ?6 Y& h! x! w; p/ r' A; z public Object buildActions () {' |/ |6 z1 X3 z6 x6 M
super.buildActions();$ `3 S0 Y3 B3 G7 V) \+ i! ]
7 ]- I3 m7 j4 \ `' ~! f
// Create the list of simulation actions. We put these in
# q: z3 U3 E G' ^; \ w // an action group, because we want these actions to be
1 k9 N; l" m% F. L; t, d // executed in a specific order, but these steps should
3 H$ `. v. G$ P: O a // take no (simulated) time. The M(foo) means "The message: ?* o* e6 _! p; i, e [ R. W
// called <foo>". You can send a message To a particular
) T& s8 `& d0 l3 a7 t. r // object, or ForEach object in a collection.
# O, w; p- E' z2 t7 j
9 E0 H) i- U- g& f // Note we update the heatspace in two phases: first run8 d5 R; B; G/ A% _& n
// diffusion, then run "updateWorld" to actually enact the1 w$ V. ^; [/ r! [$ w; S i
// changes the heatbugs have made. The ordering here is
* D1 B; B1 o/ i6 @& U // significant!5 ~7 o+ E4 o6 l% e" c+ d/ Y
& h% _# l2 o4 `% u' v. q // Note also, that with the additional
1 ~0 n( F; Y6 P, d+ {& d // `randomizeHeatbugUpdateOrder' Boolean flag we can
+ P& w X; M5 [: P& N // randomize the order in which the bugs actually run
4 M9 O3 ?# e5 k: h' D2 b0 n // their step rule. This has the effect of removing any
3 s4 a4 z% f# a; j // systematic bias in the iteration throught the heatbug& @% @2 R5 L$ d: ]
// list from timestep to timestep1 z) l" ~& i# p- z! K; [
& l0 [# [* ]4 Y0 \$ h( w* @ // By default, all `createActionForEach' modelActions have
; }+ r3 w6 y8 ]. b5 M6 t& H // a default order of `Sequential', which means that the
! O1 _, I& l/ q3 c // order of iteration through the `heatbugList' will be8 z4 t7 _9 t5 G; g+ X' _6 E" S
// identical (assuming the list order is not changed3 M9 _0 D: o' G# _/ Q/ y
// indirectly by some other process).- h& h# M) M" c7 K) ?3 }% q* i2 t+ `
' X* x/ X- p; E" a5 d1 @% E1 l modelActions = new ActionGroupImpl (getZone ());2 [/ K7 _3 ^1 q4 u
6 G+ C: R8 G: j try {
$ t8 k8 O5 n; @" i modelActions.createActionTo$message
5 i& H* |8 D' m* [ (heat, new Selector (heat.getClass (), "stepRule", false));
5 P# g& T8 r+ s! S } catch (Exception e) {
7 f& _' }3 ]& D& i2 C" S System.err.println ("Exception stepRule: " + e.getMessage ());
7 N+ L) P5 M. \; S& o4 e }$ r* @& D! j3 p* L7 _' Q$ z
; D- q! E+ A4 Z. A y2 I( T
try {" I' ^4 w3 N9 F1 F
Heatbug proto = (Heatbug) heatbugList.get (0);5 p5 f) y2 p9 O; {3 o
Selector sel = / e" l, t# O! a7 n
new Selector (proto.getClass (), "heatbugStep", false);
, P; n! j w$ ^4 l; G& J% p actionForEach =
, K& h3 B4 z6 i! C, v modelActions.createFActionForEachHomogeneous$call* d- n; F. `9 v
(heatbugList, D) Z1 |$ L' E
new FCallImpl (this, proto, sel,
3 Q% K# G7 u" b& p1 j! c) j new FArgumentsImpl (this, sel)));% }1 v" V4 y4 J$ Z$ O2 L: t
} catch (Exception e) {
. B7 r1 ~' ^* N ~ e.printStackTrace (System.err);% g4 Y c' ?# O7 @1 m
} b/ T' ]0 ~5 a5 O
8 M2 ^& Z5 L I syncUpdateOrder ();
, t2 U; ]- {9 |6 @6 x9 {- n( c1 V. D( f$ ]8 v/ ^! G, e
try {
7 ^. D h3 @( j+ q/ w modelActions.createActionTo$message ! J* B' B! j2 K# T
(heat, new Selector (heat.getClass (), "updateLattice", false));9 I I8 Z# s( _. x7 x% t: w
} catch (Exception e) {: U$ p Z: E* u% T, R2 c6 e9 f' H: Z
System.err.println("Exception updateLattice: " + e.getMessage ());% p: o0 m \9 u; r
}
. Q& B* }# U* X$ [9 } # r1 l0 b- z! h& `% M% n
// Then we create a schedule that executes the
# F4 ?! I; a5 |3 R8 n // modelActions. modelActions is an ActionGroup, by itself it% R2 g6 N' G$ f7 u& {4 p' T
// has no notion of time. In order to have it executed in; O& w: B/ g8 L! A5 r4 |% T
// time, we create a Schedule that says to use the
8 X* A- L, t" j0 F // modelActions ActionGroup at particular times. This$ z! e) q4 h w. D" R% E/ B
// schedule has a repeat interval of 1, it will loop every) Z! {. G; c$ l6 n1 ?# w) x9 o
// time step. The action is executed at time 0 relative to
+ }( H( ~; I, P' z( D // the beginning of the loop.9 Z% [6 [8 l7 B1 O
5 ]- c& m- v2 ^( r: s // This is a simple schedule, with only one action that is% p' B) u: p8 _3 m& V
// just repeated every time. See jmousetrap for more
8 k' z6 T7 \/ i! {8 ?+ c$ O" ^+ {* l // complicated schedules.
- h! {9 f- N& K
( [2 ^" O; X0 l0 T6 P( K' J$ @( m9 c+ [ modelSchedule = new ScheduleImpl (getZone (), 1);
8 V, Z8 V' Y2 N4 i3 B! l. z modelSchedule.at$createAction (0, modelActions);/ N1 G% G9 C. E6 U
! P) Y# R) ~0 B* H2 f
return this;# N5 B( f5 d+ F5 x; \! d
} |