HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ c' G$ N% \! V
0 [. k: s5 i. Q! _$ a public Object buildActions () {
" s$ e$ a4 `& u+ g) V7 T super.buildActions();5 k* E/ k; e' j. P
( L/ d9 w2 S4 O4 y, { k/ \ // Create the list of simulation actions. We put these in! o' Z3 |* d" e/ P# J* E
// an action group, because we want these actions to be
% W: ^3 w' H7 K9 q // executed in a specific order, but these steps should
% ]5 a/ t3 j! `4 b: @! t5 c+ ?5 Y% D // take no (simulated) time. The M(foo) means "The message
, a y4 X1 S- n% q2 O/ D( F // called <foo>". You can send a message To a particular
: o( X2 V! Z6 o& O, b0 E& o9 A+ F // object, or ForEach object in a collection.
* [1 Y: b; v& E' P# ] ; P2 d4 }# t4 P0 v
// Note we update the heatspace in two phases: first run: F: t! }2 ]6 x7 i% v1 E9 e
// diffusion, then run "updateWorld" to actually enact the& @* ]" R5 Z, N. z
// changes the heatbugs have made. The ordering here is4 h$ L6 K. ~* m* b9 P
// significant!
1 V2 }( U2 c, G' Q$ B8 q ; I7 [8 N1 A* l: x7 o; `5 m
// Note also, that with the additional
% G; R1 \; X5 k- S1 a9 \) k // `randomizeHeatbugUpdateOrder' Boolean flag we can+ E ?. t- E8 ?' H' t. b2 K7 a& L
// randomize the order in which the bugs actually run
# |7 u$ t; v4 u' A // their step rule. This has the effect of removing any
$ V) i" L% `. C0 }4 Q // systematic bias in the iteration throught the heatbug
/ X2 L1 T# O7 ]4 r% ^: g) C // list from timestep to timestep" A, ~" x7 s" G* ^7 Z; t
5 ~; x6 a1 a( z+ H // By default, all `createActionForEach' modelActions have5 W5 A5 K3 o2 L$ F% m6 ?* p
// a default order of `Sequential', which means that the" F3 r1 t# @/ o0 s% G$ b5 N
// order of iteration through the `heatbugList' will be$ G; W: U: x8 R$ Y8 h0 D0 G
// identical (assuming the list order is not changed
, {" ~# g a/ W6 N2 A, O# ^6 \ // indirectly by some other process).
! h0 y; K# [+ p `: v
}2 Z2 J4 v( \8 Q modelActions = new ActionGroupImpl (getZone ());, F- m5 k5 @% L
' {6 M8 C4 e; O7 F( H
try {! q6 G, L$ Z/ f. v
modelActions.createActionTo$message
7 D. w" i: F; v (heat, new Selector (heat.getClass (), "stepRule", false));
! ^5 l# T7 v( @" M } catch (Exception e) {
- T6 `& R: i" B+ g% C2 U+ k System.err.println ("Exception stepRule: " + e.getMessage ());& P6 @" M0 x* P7 R4 J/ O0 \
}7 q( `/ Z k" n8 U$ @+ k! K
) l& c9 D! O8 c" s' Y5 E
try {$ y7 V& z3 b) C) R& Q9 ^7 l
Heatbug proto = (Heatbug) heatbugList.get (0);4 c2 _3 L# V0 M. `2 {
Selector sel = ' u1 J8 R; S% K8 A: d0 f3 F
new Selector (proto.getClass (), "heatbugStep", false);6 ]* z6 O; J# }0 E! {1 T
actionForEach =- J2 w! O8 {3 g# ^
modelActions.createFActionForEachHomogeneous$call* W1 E, o2 l1 S8 N8 v
(heatbugList,1 M; g6 p! O% d3 g
new FCallImpl (this, proto, sel," g; @% c) Z+ m# _& N; f0 ?
new FArgumentsImpl (this, sel)));
# M# Q, f. v1 |( \9 F& ]) o } catch (Exception e) {
T# _- {( P( G0 H e.printStackTrace (System.err);
; r' l' P3 R" } }3 ?- O! ]% H# s
& G; Q+ G' Q! U: q+ {
syncUpdateOrder ();* D4 m) Y* A% E; U+ R* C1 @% G
- A, _# M) U& R$ Z7 k" D try {8 w2 S3 l! x# Z0 ?& Y
modelActions.createActionTo$message ; K+ v9 g- q" I0 _: Z
(heat, new Selector (heat.getClass (), "updateLattice", false));* E$ e$ H9 j* q& V4 E" E
} catch (Exception e) {
1 x# m4 k5 O3 y: n. ` System.err.println("Exception updateLattice: " + e.getMessage ());
' \) R" S2 T7 [7 X }2 h- @( a/ {1 m
n p& q ?" D, u. Q
// Then we create a schedule that executes the
& u9 a7 E& k4 M. y+ d( P( w9 o0 Z5 | // modelActions. modelActions is an ActionGroup, by itself it
! {8 h: z; c) J* @7 e0 e5 A. N // has no notion of time. In order to have it executed in6 F+ v0 |& b) g2 ] _6 o8 _" p) k+ `
// time, we create a Schedule that says to use the ?3 [2 w: D) o* j5 B
// modelActions ActionGroup at particular times. This2 x6 {# @, t E1 y( R s
// schedule has a repeat interval of 1, it will loop every# {: [. X- |) X( A0 M
// time step. The action is executed at time 0 relative to
7 A8 A: S6 g7 X, U" O+ Q$ F // the beginning of the loop.
7 F9 Y7 G# E: O9 C/ z7 D" x
% @: ^( V4 u1 z* G6 f4 ~/ e0 U // This is a simple schedule, with only one action that is- @! M+ a l( O
// just repeated every time. See jmousetrap for more4 O8 k2 e1 V! d/ z
// complicated schedules.
% S- y$ N3 ]8 D( q 8 S. w0 z0 M% u3 z5 C
modelSchedule = new ScheduleImpl (getZone (), 1);+ ^; g) |. R2 x" B9 R9 Z1 _ {7 X
modelSchedule.at$createAction (0, modelActions);- X! v) ?+ d5 ?) p1 P$ y+ q8 G
5 S# u$ N1 e% v) n# e return this;
) r/ W& i5 A3 {& B( H8 I } |