HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; z3 U( V' V/ U4 e
, h/ w4 I2 y0 |& P" w. f: u public Object buildActions () { L3 Z8 w, V$ I1 Q0 Q& F; V! H! f
super.buildActions();
* X; q: D% a6 g
1 Q% c$ r; I2 q$ C // Create the list of simulation actions. We put these in( h6 ?9 f/ Y9 y
// an action group, because we want these actions to be. j3 |* Q+ c- ^, d1 G' Y7 X6 {( U
// executed in a specific order, but these steps should) z1 e! N6 s. j+ t
// take no (simulated) time. The M(foo) means "The message
- K, N H4 i+ ?/ B* f# A // called <foo>". You can send a message To a particular& p! x( i* F7 c' N; d
// object, or ForEach object in a collection.& {0 v4 j! v- j8 m
4 U1 h/ d& ~' ^( L // Note we update the heatspace in two phases: first run
6 t% p t/ a& Z6 V+ A8 i: M // diffusion, then run "updateWorld" to actually enact the, E& O: w, G( M- G7 ^& \+ o+ Z
// changes the heatbugs have made. The ordering here is
7 C5 ~+ p* \& x& E9 s // significant!
6 P) b9 [* V. r9 i+ S 1 T# ^& n+ m! a/ Y1 } d# ?
// Note also, that with the additional
: S) L1 l# ^3 _' C- L8 m* l5 I$ j; _ // `randomizeHeatbugUpdateOrder' Boolean flag we can* Y9 G5 G% t' f6 D
// randomize the order in which the bugs actually run0 ^ Q! w4 O3 q
// their step rule. This has the effect of removing any" t$ k( f2 ?( R: ?$ Q+ @" r o
// systematic bias in the iteration throught the heatbug+ |& O, {+ N- j) w, r+ j" l$ v: l
// list from timestep to timestep
2 u( }7 J* _. d# y( y2 `) i" n
" f3 C( A& A2 L4 }0 z) b5 J // By default, all `createActionForEach' modelActions have+ \" h, M0 `& e1 u+ I& [7 r; C
// a default order of `Sequential', which means that the
& [0 L0 J) n, a! z+ \, A0 O+ Y+ ~0 J // order of iteration through the `heatbugList' will be
8 x; \. V. j- e1 s; b7 Z( X8 q6 R // identical (assuming the list order is not changed
$ g6 [8 \7 H; W( i // indirectly by some other process).
2 m/ i0 B$ a7 n" O 1 c7 E, q% }) _7 ~3 i3 q) i& Q1 _
modelActions = new ActionGroupImpl (getZone ());
& c8 K4 D# V# g9 M; Z: j1 {+ U: y, E( p; \4 d
try {+ H e( F y" }3 y2 q+ t
modelActions.createActionTo$message9 w& E( ?* ?+ S6 h+ I" v
(heat, new Selector (heat.getClass (), "stepRule", false));
+ L# |) A; z- Y% } } catch (Exception e) {
% {( h7 g# }5 e. C- y) C8 O System.err.println ("Exception stepRule: " + e.getMessage ());
- x. J' \8 S/ L* ~0 { }
( Q7 p) Y! P! Q4 s' a( Z, A+ f6 g9 P# @) b/ M
try {
' A% u+ N# T2 I% a" R Heatbug proto = (Heatbug) heatbugList.get (0);
5 o5 ^- u# B6 U4 [4 t7 c/ g Selector sel = ; s8 @, x4 ?8 S/ A Y
new Selector (proto.getClass (), "heatbugStep", false);
, n7 h; X, Z" M) r' \7 w actionForEach =
- l7 l& F M3 Q modelActions.createFActionForEachHomogeneous$call* b1 B( M7 I2 K! ], j: S
(heatbugList,
8 B h6 ~# l$ N& l) } new FCallImpl (this, proto, sel, b( }1 i" R0 I
new FArgumentsImpl (this, sel)));3 q, p$ h# t2 V& a, A; T, G
} catch (Exception e) {
|2 i' s/ b7 }3 ~ e.printStackTrace (System.err);
& F- S6 E7 f5 g# p }
) n0 [6 k- J( V) i3 V" x3 h & j- A! A0 m- x7 y( l% ]
syncUpdateOrder ();: m& J: T6 e" j, `, W) k/ m
5 e7 S& Z9 c6 C( }" `% c9 ]
try {- s8 V* [! Z, |
modelActions.createActionTo$message 6 {, j" v0 P- _" Y( ]6 R; n
(heat, new Selector (heat.getClass (), "updateLattice", false));( r/ x4 x! P8 g
} catch (Exception e) {' T% H) k, ?3 F7 j% Z& T2 {
System.err.println("Exception updateLattice: " + e.getMessage ());
4 b* i7 n6 v1 n- Y9 E }) N. x! A- q& ^ m$ k) A, c
^. D" Y2 i" }. b; ] // Then we create a schedule that executes the' Q, f$ |6 r! y7 {5 Y& l
// modelActions. modelActions is an ActionGroup, by itself it* U4 R4 Q1 R5 Z2 e. B4 Y' H
// has no notion of time. In order to have it executed in; {3 k& S8 a7 @3 T# N) z$ ^4 o
// time, we create a Schedule that says to use the
, G& Q9 G- [4 L, t9 ]) L# [ // modelActions ActionGroup at particular times. This; H+ p7 p# P" F# ]* u. o
// schedule has a repeat interval of 1, it will loop every! {( b( @, m, H; N4 D9 `) l
// time step. The action is executed at time 0 relative to
( Z3 ?( y4 N. R // the beginning of the loop.
4 C4 A6 X2 T7 }7 d, T* b0 m) P0 W Z+ @! b. D( g
// This is a simple schedule, with only one action that is
/ k' |" [; O# ^8 {( O+ J // just repeated every time. See jmousetrap for more
% P* p3 ` f( q // complicated schedules.7 C/ s1 _' m2 R2 a4 }, o" T
$ ]0 `8 Y1 e% f& \( n modelSchedule = new ScheduleImpl (getZone (), 1);
: `9 S/ p+ K7 A4 w9 [; X modelSchedule.at$createAction (0, modelActions);
]# ^5 @) @( U/ o$ I / k" c: g8 q3 O! N8 {
return this;
P; f0 P! P% k1 B$ q* c" p9 Y) N; t% b } |