HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 H" K7 g9 B! g" L5 T$ y% ?
' f6 `6 S' `$ }9 I: B* Y public Object buildActions () {# p. h; i0 v& S# T
super.buildActions();
5 `+ H8 {, j" }3 q2 ~$ @$ B
* t' I' q% z. p B // Create the list of simulation actions. We put these in
+ C( C- \* ?1 G, [ // an action group, because we want these actions to be5 V% y% e4 G% S6 Q/ n, d) V
// executed in a specific order, but these steps should
# s6 ?5 \' y+ ~$ p1 D // take no (simulated) time. The M(foo) means "The message+ x( f% e* p: O3 |0 Y- d
// called <foo>". You can send a message To a particular5 o/ P0 R! Q! }) l
// object, or ForEach object in a collection.
+ o i; U) k8 C/ d% Z O, t ! N1 b2 v7 O$ p; s! b# M
// Note we update the heatspace in two phases: first run
2 ~! ?% z% E& J* b4 v; D2 W // diffusion, then run "updateWorld" to actually enact the0 w5 c) Z H) u8 S6 q. K9 j. \
// changes the heatbugs have made. The ordering here is$ E* S/ A* t" ?7 C( \, Z' N
// significant!
$ b6 `0 T, s+ \. F
& E/ p7 v! G, {1 a8 p$ W% F! y // Note also, that with the additional
7 F$ ^: e8 {6 K$ P, J8 \ // `randomizeHeatbugUpdateOrder' Boolean flag we can9 B( Z3 J$ S5 {; f* T- a
// randomize the order in which the bugs actually run$ h: V* _7 P3 e B! M) x B
// their step rule. This has the effect of removing any Y; D) ^, [ n. j, t2 G
// systematic bias in the iteration throught the heatbug
! O& U, Z9 L% Q" D% r. D* ^7 l // list from timestep to timestep5 p; ]- V3 i; C; d
. h3 F3 d( J; n& ?
// By default, all `createActionForEach' modelActions have
" r/ H* b8 r$ D // a default order of `Sequential', which means that the/ Z) m& J2 ^# c! x
// order of iteration through the `heatbugList' will be$ A _, \. ?. y( G8 G, o- B
// identical (assuming the list order is not changed$ l0 N0 ?# Z1 A
// indirectly by some other process).9 e& O, t/ s- h& ^( {& j! b
+ g- j/ S% |6 m+ J1 g* Q
modelActions = new ActionGroupImpl (getZone ());
9 Q9 t [) j* v4 A
5 ^ k! {- |" w' x( t# ? try {7 H0 z3 Q- `$ c4 j( H
modelActions.createActionTo$message
- _8 V% f; ^# h3 Z, I- ?; F4 F (heat, new Selector (heat.getClass (), "stepRule", false));5 m4 d# x j0 w7 W2 H
} catch (Exception e) {2 @& b8 F" h' c; w& d& a# U( l- U
System.err.println ("Exception stepRule: " + e.getMessage ());% ]0 ^( w( B7 Q8 Y" p+ d0 b
}
- G6 Q' C z l* P$ T( Q3 P% ~( u% q5 y) t
try {
. G6 Z# O) H3 _& N) x1 y Heatbug proto = (Heatbug) heatbugList.get (0);
. e3 N5 f$ D, x, O6 |, i& `& ^ Selector sel =
# N7 a5 b9 X6 \5 y new Selector (proto.getClass (), "heatbugStep", false);
* [( m0 X( P2 H% i3 }+ k( @2 `' t actionForEach =
# C/ w, T' E( d% U2 L modelActions.createFActionForEachHomogeneous$call
* e5 S& M7 y, v3 ~ (heatbugList,; {: W) U& d* M1 B
new FCallImpl (this, proto, sel,- f* M$ a4 K+ J/ Y0 n
new FArgumentsImpl (this, sel)));
/ Z7 d- R! q8 J" c5 k/ x } catch (Exception e) {
1 n5 L2 s+ O1 K" @9 R/ D e.printStackTrace (System.err);
0 X! b" M# ? n4 s }
4 k! I. a& y: c" b% H v: s( `7 V
9 t( _9 C9 j" g3 r' w, ]5 J syncUpdateOrder ();
: X) t; C$ U. O; s5 G
$ ?* a3 j% ?9 @3 \ try {- ^2 V* e& B8 z+ t# ]3 N
modelActions.createActionTo$message
0 k7 W% S( ?( @: Q$ d" k, P, E (heat, new Selector (heat.getClass (), "updateLattice", false));3 ?0 V/ H* Y% V" b3 G; l
} catch (Exception e) {
' x) u6 E7 d) i( Q4 x* K. u+ b System.err.println("Exception updateLattice: " + e.getMessage ());
4 d. L" Z& H" w& r }
! G8 w$ e' t2 `9 Z
" E+ }: A4 I% C8 B5 b# i" |+ p+ Q // Then we create a schedule that executes the0 k1 z+ H* r! n
// modelActions. modelActions is an ActionGroup, by itself it( I1 H# Q) A/ \, [/ X" b
// has no notion of time. In order to have it executed in$ M( U3 s4 I+ \- Z2 R, J5 p
// time, we create a Schedule that says to use the
! S+ _& }7 Q! N) f" ^ // modelActions ActionGroup at particular times. This
) ]7 E% a& x/ W6 Z // schedule has a repeat interval of 1, it will loop every
- ~! ]4 I5 t$ j U) q% n // time step. The action is executed at time 0 relative to
; B1 G" }" U M) g; b3 _ W // the beginning of the loop.
0 v% [4 Y: B- \* J8 U6 ?5 a _. ^ f, Z5 T% _
// This is a simple schedule, with only one action that is
+ l" Y5 P) y( ]$ A: j; j // just repeated every time. See jmousetrap for more% Y, L. [$ y# @+ ^; ]1 c4 P
// complicated schedules.% i# C& k0 ?; s, U
! S. o" D5 E3 s6 _
modelSchedule = new ScheduleImpl (getZone (), 1);
( r" E8 F& e0 D2 \ modelSchedule.at$createAction (0, modelActions);# C3 k4 }; h* @( w! v
! T0 F/ ` Z) d. }- k% p- C return this;
2 g! C7 F N! F8 e- K' S& K/ E9 H% x } |