HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 M* J: w# J6 [4 e3 U: v0 q
+ K! q. j" B! Z u5 s public Object buildActions () {
0 d* W' E# Z5 A3 R super.buildActions();' d& e' E3 e/ _( T/ z# h$ x! k
! R! T4 n5 K; g; g
// Create the list of simulation actions. We put these in
( V4 d2 c( U# S9 Q) W4 Q+ N // an action group, because we want these actions to be
, W& M. ?! u9 C: [' X5 W // executed in a specific order, but these steps should# e# ~. R) g. p5 A2 j
// take no (simulated) time. The M(foo) means "The message
' Y7 ]6 n' z/ |0 X) Q' h7 P // called <foo>". You can send a message To a particular
8 {* J" ~7 `. J. ~ // object, or ForEach object in a collection.
$ {4 X' Q' f# y x) O9 _ . b* G/ Y+ I! y! W- ~* h/ t1 _4 Q
// Note we update the heatspace in two phases: first run1 J1 F( i2 l/ _: F8 K
// diffusion, then run "updateWorld" to actually enact the
7 G( `; w' B6 L // changes the heatbugs have made. The ordering here is9 B( }/ Y7 C& }
// significant!
( ~* }( p, E. Y1 M
8 [' x4 b% J3 y& @6 ^- x3 d1 ` // Note also, that with the additional9 B- R( D8 ]* W3 W
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 Q% u/ A3 d6 u# y // randomize the order in which the bugs actually run% h' z! k9 R$ J
// their step rule. This has the effect of removing any& |+ F* C1 W& @% a& v
// systematic bias in the iteration throught the heatbug
# x2 E7 j0 C+ ^7 ?& N* u! W7 l& B6 j, } // list from timestep to timestep
1 E$ ~' C0 z$ J3 ^0 C- ~# e $ E0 ^5 }8 ^4 M4 [
// By default, all `createActionForEach' modelActions have6 F+ Y) O. b. G* _' d
// a default order of `Sequential', which means that the
4 K7 O. l$ r5 C5 s, U // order of iteration through the `heatbugList' will be
, C1 T: g0 S I // identical (assuming the list order is not changed
* B' |3 V& t+ ]: e0 X2 r* t // indirectly by some other process).
3 K/ `% C4 g0 t3 |) [! D% f
, t0 l+ u* @+ f9 ?7 }; a modelActions = new ActionGroupImpl (getZone ());
$ A+ |$ |- A1 a$ f
9 k0 L5 P2 Y( ?* N a try {
0 X4 ]' g% l3 M C( x0 U3 o modelActions.createActionTo$message
) P# x7 @( W# r* N! K% N; g$ h8 [( f (heat, new Selector (heat.getClass (), "stepRule", false));
) A0 G& @9 V% `; |. _+ ^( J+ m( c ~4 m } catch (Exception e) {
3 D" v% {5 \/ x3 L; S; K' L8 m System.err.println ("Exception stepRule: " + e.getMessage ());
. t' r/ U# ~6 _+ I J5 a4 z }; \% p& }. Q0 g, v* ^5 A
9 l$ \ b6 d) _0 X
try {
+ m. ?& r& D9 b1 }: | Heatbug proto = (Heatbug) heatbugList.get (0); t- X* C5 B- v Z
Selector sel =
8 }" k' C6 M( C! y8 ^% b new Selector (proto.getClass (), "heatbugStep", false);; s9 m, F' v0 ]+ K
actionForEach =
o% ?4 X7 ^, W; t+ } modelActions.createFActionForEachHomogeneous$call) |" F" K. R* m) b. _, ^* W0 A
(heatbugList,, O) L: C T1 w8 m$ P
new FCallImpl (this, proto, sel,, D- i3 i+ _# ]) }/ U: e
new FArgumentsImpl (this, sel)));; g1 Z2 G4 c: X0 T" A9 q
} catch (Exception e) {
7 k7 [9 G# o7 |, i4 b e.printStackTrace (System.err); G$ j# d# G; Q! n7 h w
}4 p: \& T/ a$ j
! G) \3 n! l: f$ M; U |
syncUpdateOrder ();& Q. b4 S. b% m( p
j7 I% u' t5 D' N4 u& _4 q9 H9 i try {
) a7 Z0 N7 x. n1 [( S7 W# s/ B! W modelActions.createActionTo$message % M0 o& W$ h& Y& d
(heat, new Selector (heat.getClass (), "updateLattice", false));
* _0 k' C7 u8 [6 f } catch (Exception e) {* Y5 o7 s4 M% }6 Y/ k
System.err.println("Exception updateLattice: " + e.getMessage ());, }* K& t1 M- X1 }+ @, G8 {
}6 h" S/ s5 T- b# r/ S5 X
; m( V ~# H8 _, a // Then we create a schedule that executes the
, d) J5 d# t' @# |( q" _! Y( {- F4 r // modelActions. modelActions is an ActionGroup, by itself it
* ~" l' L" H$ }0 k0 e0 C+ T+ K // has no notion of time. In order to have it executed in
3 w4 z0 `9 n# v // time, we create a Schedule that says to use the
) {+ {( I' `$ ]# G // modelActions ActionGroup at particular times. This
! _; s* t# q' f, } // schedule has a repeat interval of 1, it will loop every
7 g. q3 z- x/ d( W s ^ O: H // time step. The action is executed at time 0 relative to
6 w! g0 s' o5 n7 X // the beginning of the loop.
3 N, o2 v; l8 S* O) ]! L- S# v1 B6 |: l
// This is a simple schedule, with only one action that is
% k( W$ B# c4 [' q. i // just repeated every time. See jmousetrap for more) q) f$ k. ^. |4 |! c {6 Y
// complicated schedules.
: R+ P! D6 ]0 V/ `( Z7 h7 o. g/ k
9 q% x3 ?* C0 A! ^$ v" i+ @9 m& M3 ?8 ^ modelSchedule = new ScheduleImpl (getZone (), 1);
) ^& w! G9 c# Z( I& \) K A modelSchedule.at$createAction (0, modelActions);+ n" }8 Y# C0 n) T! a) {
: T0 h3 M$ I2 m N9 x" Y! t; Z
return this;
3 Y- t# R5 X3 o } |