HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% R0 R* u0 C+ p$ K5 L6 b# l2 u( Y
% P3 u: k& `7 ]. W! `7 k! }9 X public Object buildActions () {: b3 @/ D" f4 A+ A. U4 n. b+ {& o
super.buildActions();2 B( x0 F, _: l! V F8 T
0 U/ P1 r- h6 J+ z // Create the list of simulation actions. We put these in7 s0 {8 O" Y) c1 V2 `- x7 H1 r% y' @
// an action group, because we want these actions to be
' V7 h3 D! f; A5 ? // executed in a specific order, but these steps should! p* o6 s$ r: ^9 `' v, m/ r
// take no (simulated) time. The M(foo) means "The message: P; u9 c: s2 E& @- _
// called <foo>". You can send a message To a particular4 s% j8 N% t: @7 y; c3 L0 \) J
// object, or ForEach object in a collection.
; M5 K$ ^8 G" ^
% _1 B) D/ w' q, l // Note we update the heatspace in two phases: first run B: G/ B. a; R4 g
// diffusion, then run "updateWorld" to actually enact the
2 n; U( M3 j& p& q3 E, W& }8 a // changes the heatbugs have made. The ordering here is, `* U) j* T2 {/ \' G
// significant!
- i- B+ C, S$ \( e5 Y' ~
6 J+ i9 f8 K7 ?4 u9 k0 ^! B // Note also, that with the additional* {8 l2 l" T" p$ ^9 a0 o
// `randomizeHeatbugUpdateOrder' Boolean flag we can: s U! a3 n, m
// randomize the order in which the bugs actually run/ `; N! Z# Q% G# U0 z
// their step rule. This has the effect of removing any
( z! ]+ R' S: ]/ _ S // systematic bias in the iteration throught the heatbug; a' m1 I) T5 H9 W e; T" A8 n
// list from timestep to timestep
( g4 X: |3 O1 q! K. Y w
M U* H" U6 G" }8 ^ // By default, all `createActionForEach' modelActions have* D# Y3 [/ h! x5 o4 Q
// a default order of `Sequential', which means that the- D- ?# {$ J) Z* O
// order of iteration through the `heatbugList' will be& p4 [( y1 _6 T* u
// identical (assuming the list order is not changed
K- p% _# Q g! p! Y2 w // indirectly by some other process).
$ q3 C- e+ ~( x& a! L
1 g/ m0 D. H! Q! H7 b9 ^* }( z4 w modelActions = new ActionGroupImpl (getZone ());3 F6 e0 C6 W2 z. c' D
1 F& k$ g% Y! p6 a4 h try {
8 r7 T! w: I" X+ j" p6 J modelActions.createActionTo$message% g$ b" n- l/ U
(heat, new Selector (heat.getClass (), "stepRule", false));
& Q) p" L/ d. i' a } catch (Exception e) {
. J6 v6 n8 M; Z/ ^0 L$ c System.err.println ("Exception stepRule: " + e.getMessage ());
3 f$ i; F3 y+ v1 v, W }
6 W" g" C9 V2 ]
& a: s( c" s! l3 G4 m }9 A try {
1 Y- g- c6 v$ R1 @$ U; v" V" i Heatbug proto = (Heatbug) heatbugList.get (0);, z; W' Q9 q/ Y9 [. O. ?
Selector sel = % F+ L/ W5 M+ h( r; [5 C1 ^* I! L# f
new Selector (proto.getClass (), "heatbugStep", false);( O+ T4 y4 p, I8 \# l5 i
actionForEach =, j, D% C0 o) S
modelActions.createFActionForEachHomogeneous$call
' U7 i7 w6 ?) R. u- i (heatbugList,3 P! z# J2 {# g" \2 a3 D9 P% ?8 b
new FCallImpl (this, proto, sel,
: q- A2 e, c1 j8 S new FArgumentsImpl (this, sel)));
4 Y1 Q6 ~2 J5 w; o } catch (Exception e) {
. Z7 C _& i5 L/ {: A e.printStackTrace (System.err);0 H7 l& x# T4 P1 P6 |& b
}3 h% W) r% t* j9 G( X' \2 @2 q
) l/ {" v9 B" Z( b6 f
syncUpdateOrder ();
/ w9 h0 W; n& [" O/ }1 ^, m. [ U- B7 Q5 U2 n) C, f
try {
) N' W5 V" D( A. S. r modelActions.createActionTo$message _9 |; {: n+ t! z- @3 g
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 f* `0 E" `. t8 } } catch (Exception e) {9 r& G* U+ @6 U% k: e' C4 E4 G
System.err.println("Exception updateLattice: " + e.getMessage ());2 |+ Q. ^, k, z5 s* G
}
. p7 G; `6 C- O: M. l7 \ - x3 L( t( o5 N8 ~+ j) z
// Then we create a schedule that executes the
' a, m+ P% _6 o% K+ o // modelActions. modelActions is an ActionGroup, by itself it
0 y. t& \( y+ K$ }, F) y // has no notion of time. In order to have it executed in
8 F9 c) i/ d9 |; N, e9 R // time, we create a Schedule that says to use the
, ?! x, y2 o8 @, Y3 F0 m: f( Y // modelActions ActionGroup at particular times. This. C; x Y9 h; P! ?+ \
// schedule has a repeat interval of 1, it will loop every
% a6 D1 v: p9 L8 {5 { // time step. The action is executed at time 0 relative to
8 C) H! q! _' |. I5 |$ R4 W // the beginning of the loop.
/ y4 h0 f9 Q* ?* u1 f: N) x" \& B6 z7 D8 C9 b% `! q! B
// This is a simple schedule, with only one action that is6 H% P6 N7 h3 s% W
// just repeated every time. See jmousetrap for more7 ]9 L- O! E+ r
// complicated schedules.
+ r( Z% F, t3 B- B/ F # ?2 |5 V0 j9 f" f, ^3 R5 r2 [
modelSchedule = new ScheduleImpl (getZone (), 1);: _4 _6 A$ K+ V' @, ^# A; [
modelSchedule.at$createAction (0, modelActions);
5 t2 M" C+ Q$ |2 f
2 c! @* v3 k+ g return this;
4 K9 S5 w! @( d3 |6 d } |