HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% H$ n& z) y3 f) }
/ ~' X0 n( h( t! | E P public Object buildActions () {
9 j4 h7 [) |' q6 m# I x, z7 i super.buildActions();
) V! J! \8 j+ F. B/ ]: v3 v 5 @( E& t5 c: Z- Y2 T6 c
// Create the list of simulation actions. We put these in
1 H" L2 v0 ]' d5 |" x* C // an action group, because we want these actions to be, ]7 c* q: y2 k; o. V; V0 h {
// executed in a specific order, but these steps should+ e% }3 B* c" y% `" E* E% \: z
// take no (simulated) time. The M(foo) means "The message3 n2 K* ^( ^& Y+ @( l
// called <foo>". You can send a message To a particular
( V" D+ @4 w' [ // object, or ForEach object in a collection.
* u. W6 `1 S2 B% [5 j4 d 0 F$ R9 b& C) a! E
// Note we update the heatspace in two phases: first run
5 i6 U V2 H# N4 H0 `3 F // diffusion, then run "updateWorld" to actually enact the8 d0 y5 b( p' ?5 S( G
// changes the heatbugs have made. The ordering here is }" J! j+ G* `) H9 U3 x) D
// significant!
% B4 Y/ R+ x1 K0 N 5 ~5 l& D& H) P- D4 ^
// Note also, that with the additional! c7 k% n) n# x9 N
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 b h" \7 C$ X' T- h8 H; H // randomize the order in which the bugs actually run" z$ T; v; O/ ] d) g- Z! B
// their step rule. This has the effect of removing any- l7 i: q; \. w* S
// systematic bias in the iteration throught the heatbug j% N7 Z6 B* X0 T, G
// list from timestep to timestep4 J6 d2 B L# p
- c! U& ~% g. f/ S C
// By default, all `createActionForEach' modelActions have
8 I4 T. [. y p, `/ N9 M // a default order of `Sequential', which means that the4 M+ `: L) V7 q
// order of iteration through the `heatbugList' will be
, q+ o" f3 e+ b& t. S5 l3 }$ l' h // identical (assuming the list order is not changed
$ b% _: Y# q) [- i) P4 r/ p" z // indirectly by some other process).$ Z0 G v" |% d! i! Z' S* w
" a& o3 E, [3 D; W3 v modelActions = new ActionGroupImpl (getZone ());
7 C6 }8 {& Q |7 Z, E8 P$ o5 u7 n q* C; t3 k& P
try {6 v( M1 ^; w; \ p/ I
modelActions.createActionTo$message
/ B# `7 B: z+ u) e (heat, new Selector (heat.getClass (), "stepRule", false));8 X" F# ^( H; p3 a7 g2 g6 ?
} catch (Exception e) {
- |: u9 a$ @0 M2 W' c System.err.println ("Exception stepRule: " + e.getMessage ());) Y! i0 \: L5 S/ s
}# `& `- I- n6 J! @! E* ?
0 L7 I3 a0 _' m: Y9 ~ try {! j4 ?3 Z# O/ _2 Z5 r8 Q
Heatbug proto = (Heatbug) heatbugList.get (0);
2 ?5 C7 d# G, Q3 T1 J- j* V0 o( M1 a Selector sel =
+ ]5 H" S) H/ G3 `2 M4 k new Selector (proto.getClass (), "heatbugStep", false);
' a+ u3 X5 m9 Z& i$ \ actionForEach =: D J) D. C$ H1 F7 g: _, }2 M
modelActions.createFActionForEachHomogeneous$call: j6 h ~$ q6 v
(heatbugList,$ k {' J' Y) [2 Y
new FCallImpl (this, proto, sel,
0 A7 `5 H. M6 ^9 P- F new FArgumentsImpl (this, sel)));! M5 ?. U7 `1 c5 r! r$ A) i5 j1 Y
} catch (Exception e) {5 C7 [, J( ?1 j5 v9 ?0 V
e.printStackTrace (System.err);: F4 n0 q) K$ q5 z1 U( @
}- p/ Q' U/ \4 E( [ C' V F. i
3 k# o9 C4 n0 B8 A f; O syncUpdateOrder ();& ?, m/ ]+ l' g3 H
7 G, @# q4 K% }5 r( J
try {
* c8 \6 ?( S3 J# W modelActions.createActionTo$message _2 C7 l) k5 F/ @% G
(heat, new Selector (heat.getClass (), "updateLattice", false));
! f4 I* e. @8 ]- }( y6 h9 b2 J! d } catch (Exception e) {/ }# ]4 @0 O% `( S d
System.err.println("Exception updateLattice: " + e.getMessage ());
# T# R% u8 S1 F- y! A0 y5 c+ Y9 T }; K2 G. t& W ^5 O# s$ J9 R) ]
% g$ z2 |5 J5 W# T, }; I // Then we create a schedule that executes the
+ b2 T9 N8 B0 F8 ^, L // modelActions. modelActions is an ActionGroup, by itself it
! h* H3 c" h w5 |9 S // has no notion of time. In order to have it executed in
; P" x9 j# d0 f3 J // time, we create a Schedule that says to use the
; y$ o% v2 z; q. g# ^) Y6 q4 G7 {/ y // modelActions ActionGroup at particular times. This, c0 m5 `7 a" x- z, V
// schedule has a repeat interval of 1, it will loop every
) ^: z) Z9 \8 y& C1 ?/ z // time step. The action is executed at time 0 relative to
* I6 o; p$ H% ]4 B0 A+ c9 e // the beginning of the loop.% \' A. S" K0 I& t J s
( P' d$ x" _& p9 z1 H8 X0 J // This is a simple schedule, with only one action that is
9 T" V! X( Q0 w+ T // just repeated every time. See jmousetrap for more4 {7 ~: W* ]( o2 z$ H* l3 O: L
// complicated schedules.
5 Z+ ~( h4 I$ R% V. ^9 Z$ [ - G2 G. B D: @! k! y" Q
modelSchedule = new ScheduleImpl (getZone (), 1);
2 U! K& F+ d% v8 x5 _ modelSchedule.at$createAction (0, modelActions);
! k7 d) j- @! r( H
& d$ j* } ^5 y1 v7 _ return this;
! l* n$ s( f8 a' x. J$ Y Z } |