HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( Z2 r9 ?$ W: n9 x. z0 y/ M. S8 K
+ X, H4 p- Y" I! P; p public Object buildActions () {
4 I$ \0 t$ r+ s8 a, x! |- H super.buildActions();. N; f1 W ^ i2 a s0 _
; r5 b7 Y9 _5 W; M4 k" u8 ~
// Create the list of simulation actions. We put these in
- s! [0 Q2 d7 N, Q5 a // an action group, because we want these actions to be% c0 t" F1 D$ [4 `
// executed in a specific order, but these steps should
7 b! `; p3 l, l' } // take no (simulated) time. The M(foo) means "The message
) g! B+ p9 C7 c! i // called <foo>". You can send a message To a particular3 x' h% {7 y; J" W4 X; w' i
// object, or ForEach object in a collection.; W+ p7 q& j. s0 B
" H2 Z: A; R/ e) Y; l6 c$ L
// Note we update the heatspace in two phases: first run
( x" o- ^- Q/ p1 S0 f! M# ] // diffusion, then run "updateWorld" to actually enact the! c$ k- @7 S9 ~: H ~ ~) K( _* v
// changes the heatbugs have made. The ordering here is
# k" T4 H% L% U1 V9 c // significant!+ _/ ~3 r8 @( r f% g. y- N
& W4 U- i& f+ \1 f" |- E
// Note also, that with the additional
) s) i. h5 N4 { // `randomizeHeatbugUpdateOrder' Boolean flag we can6 I. i. v3 |" M4 }( n4 U
// randomize the order in which the bugs actually run
+ w6 o5 C# N# l' A // their step rule. This has the effect of removing any' O- A% ?) R, F# w
// systematic bias in the iteration throught the heatbug
1 Q8 ], l4 d( S8 b0 O* p // list from timestep to timestep
" j* o* k( @. L8 W, g( N, q: M
! F1 _- b- e. }- M' ^; L6 n5 E // By default, all `createActionForEach' modelActions have5 O$ d! c' }/ Y% F& s
// a default order of `Sequential', which means that the, h# ^6 S: F; U% E" T# O5 m+ m
// order of iteration through the `heatbugList' will be& g$ d* ^- M- t3 Y+ W
// identical (assuming the list order is not changed2 T: Y. O9 @# w9 j$ q9 m
// indirectly by some other process).
0 b, O0 t$ D* C; K; B/ G8 g ! T9 x8 J a z
modelActions = new ActionGroupImpl (getZone ());6 y+ t( L- u- f- [5 x4 \; h3 j
/ ]$ T. }0 B; N! p1 _, k$ |, w
try {
! h+ m" _8 C* S3 ~8 t4 r3 W2 Y modelActions.createActionTo$message
( P* }$ f5 G2 f (heat, new Selector (heat.getClass (), "stepRule", false));; q: v w, i& B( y
} catch (Exception e) {
% \5 ?$ L+ [% J9 a( ]5 i System.err.println ("Exception stepRule: " + e.getMessage ());$ m, Q& P! B' @2 k a$ O5 s6 x
}9 o# }0 l. F( G1 i+ M: F
" R. T$ p1 r: j try {
. n5 S+ }* J! Y' t7 J- Q1 V- ?4 W Heatbug proto = (Heatbug) heatbugList.get (0);
: j5 B* x; r9 c Selector sel = 0 j I/ @2 D% C( r
new Selector (proto.getClass (), "heatbugStep", false);; A" v, M" n5 ^7 \, k+ Y( V
actionForEach =" L% }. s$ C A, K
modelActions.createFActionForEachHomogeneous$call
9 R0 S# \' r/ j, j2 F0 ? (heatbugList,
$ @. E% ]8 }* k9 } new FCallImpl (this, proto, sel,/ Z' ~ n! S/ s
new FArgumentsImpl (this, sel)));
; S1 t k! ?! ~5 H } catch (Exception e) {
; X! [) ]0 t3 a; G; F* a e.printStackTrace (System.err); H* a: [( l5 e+ h5 P; {7 @. f4 T6 m
}
% h: }7 R8 P6 p6 g3 S4 Q8 H
: ^. ?# Y' H) f; O syncUpdateOrder ();
* `3 p+ o3 z( ?: |5 |! F5 ^+ B; A( q; }% H
try {1 Z( w: s' K; O V- R- T! L3 B" }
modelActions.createActionTo$message ; P& L5 u1 T" m# m9 S3 B; h
(heat, new Selector (heat.getClass (), "updateLattice", false));5 s: {9 d. v! A4 |2 Q
} catch (Exception e) {
/ L% \/ E7 T0 J6 ]3 u% o* o System.err.println("Exception updateLattice: " + e.getMessage ());
3 ~: z- z0 o4 T7 C- e3 V }: y3 ^0 ~6 Q; ]% N5 [# W5 ~4 j# k
6 s& O- ~) ?! J4 S: e // Then we create a schedule that executes the
8 \, _* z8 f' c7 R! r( x // modelActions. modelActions is an ActionGroup, by itself it+ z$ n* |; r3 Z
// has no notion of time. In order to have it executed in. @* X- n+ G5 L9 g
// time, we create a Schedule that says to use the
9 v/ F# ~2 s" Y) i // modelActions ActionGroup at particular times. This0 P6 f# {5 Q) Y* H2 w3 p& g
// schedule has a repeat interval of 1, it will loop every: L$ N* D' r5 Z- w
// time step. The action is executed at time 0 relative to) L4 \1 P3 e$ ]
// the beginning of the loop.
3 _ d0 R: N, w2 j' O" E/ D/ b" C: g! @% L/ Y5 l E/ S$ g
// This is a simple schedule, with only one action that is) ~& [7 R8 a5 R3 ], g+ S
// just repeated every time. See jmousetrap for more
' w- v9 \& l; }5 G9 Y // complicated schedules.
, S5 a# t5 v2 c# h3 _
8 x) @. u% G' r( u w" N/ ~ modelSchedule = new ScheduleImpl (getZone (), 1);" k1 i- [& [9 ?) d. Z( S
modelSchedule.at$createAction (0, modelActions);: Q! Z2 m1 L$ j0 ^7 P+ q4 a7 j
& i- T: @# M( |
return this;! g8 ^5 `2 ~4 [3 |. H9 |
} |