HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 L1 { j. V- M! s% I7 |" Y" y7 E! |. @- U- D1 O3 Q& p
public Object buildActions () {
# l4 i$ v( S! U super.buildActions();8 `% A# T8 M( @+ p! d& g: M
: ]( X9 q: O* y4 ~ K. r2 q
// Create the list of simulation actions. We put these in# l( i! n6 o% l
// an action group, because we want these actions to be* m, g' j* P3 G" U- w E( H
// executed in a specific order, but these steps should: i2 g/ x* O# |& \1 E" Q6 N2 y
// take no (simulated) time. The M(foo) means "The message
, d) R: D1 b# f- [" e# W' E // called <foo>". You can send a message To a particular
+ o$ X* T' t& v* y" F2 } // object, or ForEach object in a collection.) l5 E' x3 J" z) X4 Y: g- H2 w
0 i5 R; H: @3 O( p" J0 o6 u7 @" W
// Note we update the heatspace in two phases: first run/ Z0 u+ m3 `: F9 Q5 c& H
// diffusion, then run "updateWorld" to actually enact the
8 t# U9 I- R3 O2 X. q // changes the heatbugs have made. The ordering here is
. z% S- S( l9 K6 a; q* r // significant!, F C" E Z% _4 G5 Z$ Q( ^
( M3 C E9 I1 [! A: H
// Note also, that with the additional/ W. D) f) v2 c! P1 @# p( d# T1 t
// `randomizeHeatbugUpdateOrder' Boolean flag we can; n) |8 t% S; H$ N
// randomize the order in which the bugs actually run
! D& H' {: G S // their step rule. This has the effect of removing any
5 w0 ]$ E% ?. G |! } // systematic bias in the iteration throught the heatbug6 z; m4 V2 I2 c2 {* p6 }$ n' a
// list from timestep to timestep4 Z' Z; Z0 A% I- _ M7 C0 `
- C) t7 P! I5 k- {9 U // By default, all `createActionForEach' modelActions have5 ~3 O( a# B$ z
// a default order of `Sequential', which means that the
! G! ?3 n1 r" n: E, y // order of iteration through the `heatbugList' will be
0 I6 d' G7 n; x' |, h; X$ ^# l // identical (assuming the list order is not changed8 I& N# z0 A7 H, K' w) \, U( q
// indirectly by some other process).
4 M0 i) U. F5 l& V2 k- E* [
3 k) c' v9 N' o5 E% U5 _ modelActions = new ActionGroupImpl (getZone ());
$ M/ @0 r' E9 v8 \: |' A& o" {( j8 I U% A/ B8 F' W
try {3 C( Y7 N' s ?+ ], b
modelActions.createActionTo$message
% V7 C# n0 L2 v2 S6 b1 E4 ~ (heat, new Selector (heat.getClass (), "stepRule", false));
5 p- V6 U- O$ D } catch (Exception e) {, q% M$ |- P8 f/ N+ b; G8 D
System.err.println ("Exception stepRule: " + e.getMessage ());
) d& m9 ~ Y5 j3 Q0 @! z7 H }" T( V3 y, @$ G9 q! a% w
) \2 W. I) N* H$ M. c# f, R
try {( O. v9 m4 S4 K, `
Heatbug proto = (Heatbug) heatbugList.get (0);
) g! L, F S) y Selector sel =
, S/ b; m/ ~9 j9 y, j new Selector (proto.getClass (), "heatbugStep", false);$ U5 C9 Y8 F G- R) h& h: ]
actionForEach =/ B- F* m2 V# `' G# @- X
modelActions.createFActionForEachHomogeneous$call
* ]6 Q2 n# w" Y (heatbugList,) H, I) S1 R3 {
new FCallImpl (this, proto, sel,
& d" y% m2 C R& s new FArgumentsImpl (this, sel)));
; o9 y0 ]9 E$ N) C: [ } catch (Exception e) {6 s8 @" [' v0 s/ e) Z5 p
e.printStackTrace (System.err);
5 g( N0 Z" s8 W( F' f }9 e) ]- Y- l: z. d, o# ^- {3 w+ q+ _
, ^3 ?, x w- s' Y
syncUpdateOrder ();/ T9 ?" S5 O ?9 o9 P
* W) r1 U* s) J& e* u d& C
try {( f' U$ [3 }% ^( v% C5 p) Z
modelActions.createActionTo$message
+ z" X9 @: s* T d9 t, A" S1 v5 O (heat, new Selector (heat.getClass (), "updateLattice", false));6 z( f1 R6 [# d
} catch (Exception e) {
. L8 X! o- w0 L- c System.err.println("Exception updateLattice: " + e.getMessage ());
# }' Y& P h; y3 e- K* g( J }$ h0 ]9 E: H% ~( ?& M7 M
! Z" X [; S$ S& z& C // Then we create a schedule that executes the
1 K9 N% Q( R z( j3 r5 {! S, Z // modelActions. modelActions is an ActionGroup, by itself it
7 O' K3 O8 J P& D4 q* w' h // has no notion of time. In order to have it executed in
6 \, q4 o& a \" ^$ t! P; K' h // time, we create a Schedule that says to use the, A' g5 L( R3 S* S [7 e$ Z
// modelActions ActionGroup at particular times. This8 K) v( j6 _$ Q& N
// schedule has a repeat interval of 1, it will loop every& S$ L" B7 G5 o( L; ]' p
// time step. The action is executed at time 0 relative to: B. \* K) [5 r6 g. U: m. B
// the beginning of the loop.* r. G( l, X7 l4 E
' x; X; E! x, t- S, K4 ^4 T
// This is a simple schedule, with only one action that is; k% o; |3 p$ w1 R/ I8 N) C; m
// just repeated every time. See jmousetrap for more* R/ } v, K& g
// complicated schedules.
) w9 F' z, i7 S6 G5 o$ P) ^
6 o! k1 b X/ |" D modelSchedule = new ScheduleImpl (getZone (), 1); f5 f3 f; \1 P
modelSchedule.at$createAction (0, modelActions);; D {2 {* k: L7 v) y- f4 B7 h) |
5 [7 n& l5 t7 I$ p# E return this;
2 i* ]' ?4 _. }) Z } |