HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% q6 e+ u. @7 p& r; l
- L6 W4 }0 v3 F public Object buildActions () {3 ^2 ?4 C8 _2 ^, r. }, V( m
super.buildActions();' E7 i$ ^0 r- \0 `4 q5 n
$ o1 [6 e. S0 x // Create the list of simulation actions. We put these in
9 N8 q I. ~8 k ? // an action group, because we want these actions to be' L1 X: n- Z& j3 ~! s- D
// executed in a specific order, but these steps should. K+ k" a$ B, q* S1 S7 p
// take no (simulated) time. The M(foo) means "The message
8 `6 C( {: T. h d/ t0 ^% r( ?' b // called <foo>". You can send a message To a particular" |2 S0 f9 V% v
// object, or ForEach object in a collection.% X% W$ }1 D, l8 Y
$ G4 S/ a9 I1 r8 W2 i2 q // Note we update the heatspace in two phases: first run
0 h: ?- y# p! C Q" d // diffusion, then run "updateWorld" to actually enact the
( d* ], H) d# P h // changes the heatbugs have made. The ordering here is
7 m% N+ _5 s+ V6 _ // significant!( B9 C/ V' g! p6 k
, v5 x: i/ }" }+ ^
// Note also, that with the additional
. i7 M# o% E8 u5 p' N7 c // `randomizeHeatbugUpdateOrder' Boolean flag we can
5 Z* B! j; R* Z3 _2 m* ~" z // randomize the order in which the bugs actually run
# p. @4 N! w0 o0 ^9 ~ // their step rule. This has the effect of removing any
* h- ~' l/ t* w0 M) }. L! `9 {7 Q // systematic bias in the iteration throught the heatbug% C; r j* J3 A X) i- Z# p; {
// list from timestep to timestep
$ N' c7 O+ h: P. {5 M+ O ' `; y# S8 m2 o
// By default, all `createActionForEach' modelActions have: j+ ~8 ?+ Y8 i' R2 }
// a default order of `Sequential', which means that the. a* d T8 k8 s) o' b ^
// order of iteration through the `heatbugList' will be
5 Y* g3 f/ Y% X9 s // identical (assuming the list order is not changed
* p- j9 ^# [3 K0 i5 m* i" N0 j // indirectly by some other process).% h9 L9 S. T+ s$ ]( |
8 m- r4 b1 B4 |* W* D
modelActions = new ActionGroupImpl (getZone ());
. L0 x1 V, M" J' r# {
7 I; r$ P) y7 @6 P o% Q1 o7 K try {; W, a* M' R1 T) z
modelActions.createActionTo$message
1 r" x' }5 l X. p7 a7 A" k (heat, new Selector (heat.getClass (), "stepRule", false));2 e& S% e% B/ N% c4 v& P
} catch (Exception e) {) k0 u/ x' Z8 Z4 n! [. t
System.err.println ("Exception stepRule: " + e.getMessage ());
+ I+ \6 i4 ?/ b9 q( Q+ \ }& |+ S% P6 S! Q3 j7 T
' f9 Y1 U* O; X& x$ K9 }) H% A& a try {
2 d2 a1 b1 t- V6 O0 x Heatbug proto = (Heatbug) heatbugList.get (0);
8 ]9 c9 N# t# ]- J( J& V Selector sel =
! g _' Z( S5 e+ \ new Selector (proto.getClass (), "heatbugStep", false);
9 Z5 c7 a$ d9 x/ k, X$ C5 ] actionForEach =, C8 i7 X7 p7 d8 A
modelActions.createFActionForEachHomogeneous$call+ H0 }; R5 k- e0 j" p
(heatbugList,7 ~8 E6 f2 D% C" A
new FCallImpl (this, proto, sel,
. D0 L# Y: X, M. p+ P7 K' S new FArgumentsImpl (this, sel)));
' f/ J* N% e+ q5 n; w } catch (Exception e) {
6 i. l: v. T8 m& Y- v e.printStackTrace (System.err);
3 I* P* m) T$ L% B2 `* u }0 U4 v% }. ~1 w7 a7 ^# N
0 z8 u4 H$ r% }5 ^# P ^ syncUpdateOrder ();
/ |& }' h! {2 s# M
2 m( t9 ^" i2 I try {# d7 L+ W( N" U; g7 V; m+ F
modelActions.createActionTo$message
; z8 _( C. ]$ m y# t6 f. f: h (heat, new Selector (heat.getClass (), "updateLattice", false));! M6 v( I& {+ P0 b
} catch (Exception e) {
; k& r P' L+ }( x System.err.println("Exception updateLattice: " + e.getMessage ());
: J$ j' T- `4 ~1 X }% y" d0 U' ]1 S& C1 j
+ g& B6 x# T7 q7 Q4 v& n; H, z
// Then we create a schedule that executes the# e) N% t7 i2 d$ H
// modelActions. modelActions is an ActionGroup, by itself it
0 V; f9 ^8 A$ M7 o# |- j // has no notion of time. In order to have it executed in
+ V ]+ W$ K5 [3 m3 @6 e6 Z! \6 a) d // time, we create a Schedule that says to use the
6 `; F# I2 u- s$ l/ C! I6 W1 S3 m; i0 { // modelActions ActionGroup at particular times. This# P# R8 h6 Q1 Y5 j# n# v' w
// schedule has a repeat interval of 1, it will loop every
+ r. p, r! m: P // time step. The action is executed at time 0 relative to( M2 ]% V4 q, j, ~5 O- x
// the beginning of the loop.
- E4 M7 U# ^5 l3 m9 Y8 `% ]4 D7 m- A1 K" |' P# ]0 I* o; }
// This is a simple schedule, with only one action that is$ ` r+ A4 \6 K/ o; u* ^# S
// just repeated every time. See jmousetrap for more
1 q+ [5 u/ b: ]1 w) _ // complicated schedules.
\: t8 L. O, y( N, A1 n
5 Y+ {( p2 [" g! K modelSchedule = new ScheduleImpl (getZone (), 1);
+ M: r. E( g- u! X" U+ U6 G modelSchedule.at$createAction (0, modelActions);
! m9 m& S( G- Q. m
8 e/ F' V+ P4 I! D. Z& i. Z5 q return this;
B, i& P% k4 N" ^( I) D, }: ?6 l } |