HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:0 ~. T- o; m" e5 V$ z
# Q5 h! v/ A; V2 i; T. T
public Object buildActions () {8 `5 Y" P5 `/ h
super.buildActions();
+ \9 Q1 |% C, T* j) f
! _6 B8 u% i+ H! J8 K // Create the list of simulation actions. We put these in/ q2 _2 B" J/ Y ~: E! N' X
// an action group, because we want these actions to be* @- Y' @$ `. i0 t, d. X
// executed in a specific order, but these steps should
2 }# b1 T3 ^6 I. d8 Z // take no (simulated) time. The M(foo) means "The message, a+ Y e3 {2 I! W$ j6 q
// called <foo>". You can send a message To a particular
2 S2 G5 i; j) t+ b // object, or ForEach object in a collection.
9 k, G3 q2 C6 p& e$ t
* w5 E% C, _6 @+ f# b // Note we update the heatspace in two phases: first run! @+ m# {4 X" l7 v" \. S
// diffusion, then run "updateWorld" to actually enact the" O+ s3 K6 t6 y) `# f. Y! U
// changes the heatbugs have made. The ordering here is
: Q. x7 T: a& E& s' l3 G // significant!& y7 M; e4 o1 U9 z1 `; @' S( W
% u0 F, U5 s* s5 [4 o2 g // Note also, that with the additional( y7 x# z) m' N7 B
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ r5 f& C* E% ^ // randomize the order in which the bugs actually run
0 n9 H& y/ l F8 q // their step rule. This has the effect of removing any; I# R1 T% b1 m0 Z% F5 H# K
// systematic bias in the iteration throught the heatbug
0 e9 p' O: i3 i. M" l // list from timestep to timestep
# j/ a3 x" c" `2 g
9 q- D5 ?: K0 V ~3 a: y // By default, all `createActionForEach' modelActions have' B, k1 P; e& s3 j: D/ ^
// a default order of `Sequential', which means that the
, E9 l: g$ Q2 _$ h' w. x // order of iteration through the `heatbugList' will be
' ~ o4 _7 f0 | // identical (assuming the list order is not changed
& F3 m9 Q+ j! F- g // indirectly by some other process).. [* o Z# P) }) u. i
) v% ]# e1 b8 ]' O5 R modelActions = new ActionGroupImpl (getZone ());* v9 e* r/ m. c8 v1 f& \$ E/ K( w
* Q2 Y, p! { g. G P$ a; F1 [ try {
" T7 }3 B/ H2 \ `- |; W' G modelActions.createActionTo$message, R- Q2 j# J8 q& B6 x9 B
(heat, new Selector (heat.getClass (), "stepRule", false));
! b9 q) T1 L+ b$ N; D! B+ f } catch (Exception e) {) L2 j" l1 q X0 J; f4 M0 T
System.err.println ("Exception stepRule: " + e.getMessage ());
( i ~0 y/ t3 U, ? }$ Q* C' u% _! u
& A1 A9 w2 V1 q. W+ T9 g2 r
try {- d: ?5 n2 R4 P
Heatbug proto = (Heatbug) heatbugList.get (0);3 K6 l+ d) V# n. c
Selector sel =
5 c% y q& E: S8 o" q& J; r. f new Selector (proto.getClass (), "heatbugStep", false);0 i* p- ]$ E9 p4 d7 L
actionForEach =( {7 [" r) w) b6 w L2 k
modelActions.createFActionForEachHomogeneous$call) T1 m0 a( L" {" y
(heatbugList,
F2 ~: \$ s& D new FCallImpl (this, proto, sel,7 p( ]4 |. B3 ~3 v: d# c: I! ~
new FArgumentsImpl (this, sel)));- P$ u0 u* D5 ?, ~& H! Z1 T2 p8 W) S
} catch (Exception e) {+ L: z" a2 ~# q) a
e.printStackTrace (System.err);. M) n4 B: y8 a1 y" m' v
}! D5 _' f4 i4 g+ u
. a! z1 Z5 V2 {: _2 S
syncUpdateOrder (); L' \2 P/ Z- S/ }5 B, p& ~) Y$ G
# W6 w f8 W( D try {) m/ u6 i4 m4 q/ I) \' [
modelActions.createActionTo$message : C2 g! ~ Q2 c6 v: J3 C/ S
(heat, new Selector (heat.getClass (), "updateLattice", false));2 ?! n8 W, `- [9 y1 F- `: ~8 B5 d
} catch (Exception e) {
6 D# E2 J) f4 G& Y$ [% E6 Y; w8 E System.err.println("Exception updateLattice: " + e.getMessage ());
$ y M, i8 @0 R* Y) ]: J8 O }* b$ ~; C8 V+ }- }- t @
3 \$ e% I- Y, Y8 i$ `1 C
// Then we create a schedule that executes the/ R' t) U7 c) U$ Q6 E# n) S z% h! a
// modelActions. modelActions is an ActionGroup, by itself it
; L. E/ o- A, [4 u$ _ // has no notion of time. In order to have it executed in
! `% z" R" G# R/ t3 h // time, we create a Schedule that says to use the4 y0 u+ \9 a0 h# }4 z
// modelActions ActionGroup at particular times. This+ S7 G1 |" l5 c4 X- R6 I, u8 K; Y- J5 t
// schedule has a repeat interval of 1, it will loop every; y5 Y2 v& d% K
// time step. The action is executed at time 0 relative to
0 T- c" @- M6 p* W- i // the beginning of the loop.
+ F5 [& Q8 p; _+ [1 e+ q* v% f- v2 a/ x% P1 }8 }" d' I
// This is a simple schedule, with only one action that is& c( V/ T/ j' a7 x& _: K' d
// just repeated every time. See jmousetrap for more
0 G, O) t o- t // complicated schedules.3 ^9 E* d$ d5 j- B0 r
& W5 S6 }' u. x5 l modelSchedule = new ScheduleImpl (getZone (), 1);# _& B1 ~. A2 q; n( s; L
modelSchedule.at$createAction (0, modelActions);
7 K4 G4 o- L: p& G9 T5 {( P% \& e
; @2 F# T4 [" [6 O) q return this;
! c8 ~) n) X. c, Y } |