HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* W4 Q! B7 {! ~# T: U5 t1 O/ z5 K; y8 K$ J+ J7 _/ n3 }/ J/ e3 i
public Object buildActions () {7 N4 ~- J5 C6 H- z% G
super.buildActions();3 T9 E/ J% E4 O: F+ \# Y2 _! w
. w3 O- ?4 ^) E, J // Create the list of simulation actions. We put these in
6 Z% F& Z9 Y8 _ // an action group, because we want these actions to be
9 L( v$ t( w3 Y1 ^3 U) g1 @3 f8 I // executed in a specific order, but these steps should( R( @% N5 q5 W5 M1 ?
// take no (simulated) time. The M(foo) means "The message" m3 e% Z6 |5 K* \8 L+ V
// called <foo>". You can send a message To a particular' N1 e$ j$ b8 t3 |
// object, or ForEach object in a collection.( ]: A0 R* }8 V
% H7 O+ a ]! d5 \" f9 m // Note we update the heatspace in two phases: first run7 s$ Q' j% P$ N' U [+ z
// diffusion, then run "updateWorld" to actually enact the8 G; g4 d. @5 W" H1 d& S! e8 l
// changes the heatbugs have made. The ordering here is
# y6 B$ v4 Z$ |. X. ^0 U // significant!
4 Y) b2 i7 w* j
) W' @# r& K. Z# o% ^( {% d/ P) ? // Note also, that with the additional! y- S3 ]. J) L$ W
// `randomizeHeatbugUpdateOrder' Boolean flag we can
6 {& r( M) I/ v0 T! G% | // randomize the order in which the bugs actually run/ Y) X) i1 A) ~' p3 W1 f# z
// their step rule. This has the effect of removing any+ q O$ {: q5 n0 Z" `
// systematic bias in the iteration throught the heatbug
, d- q; x' v8 P# ?! W8 K* Z // list from timestep to timestep* C) @+ K( J; a% _. ^# x
7 \' d- M1 S7 @1 B# J, D
// By default, all `createActionForEach' modelActions have
- h2 o. O2 h! c // a default order of `Sequential', which means that the
9 ~& Z/ s7 E( a3 V" U3 C x // order of iteration through the `heatbugList' will be& r$ |* L0 U3 J! F
// identical (assuming the list order is not changed
" v0 G! H$ l( ^# F& p // indirectly by some other process).6 c8 `3 v% A2 o( l
5 T3 e" L. U6 O; B: d. B
modelActions = new ActionGroupImpl (getZone ());) j; @* G0 ^) d* g
g; D y/ A1 K9 [" E" r; ~; U try {, g* d1 J1 G0 Q( @7 t3 ]
modelActions.createActionTo$message
t0 E8 g1 G6 l1 d, B& l (heat, new Selector (heat.getClass (), "stepRule", false));# K( H5 ` R5 |* `. q
} catch (Exception e) {# v9 K2 ^# W* m+ `+ Z
System.err.println ("Exception stepRule: " + e.getMessage ());$ R% ]4 A: }" \! b' U0 p& {( ]
}
; W; `$ u, {' t3 Q: e- v9 |
% I; C4 o' t& Z2 a \; C try {( X* i) ^$ k$ Z, N1 y
Heatbug proto = (Heatbug) heatbugList.get (0);: l3 p9 V, J3 F5 {& ^1 N3 d9 q
Selector sel = $ G6 U* }$ }# ]8 `, _: U
new Selector (proto.getClass (), "heatbugStep", false);
! n0 H% i Z* G8 o/ J7 F actionForEach =
* R+ _3 I9 ?, R6 k9 ^ modelActions.createFActionForEachHomogeneous$call+ L! c) d6 g: f5 w+ O) _2 Y* L
(heatbugList,
2 C% U$ K$ _* u) o6 v" L' g/ k- b new FCallImpl (this, proto, sel, |+ t S3 k: A4 Y R; S$ m5 @, _
new FArgumentsImpl (this, sel)));
/ P' n- S# G8 u% q; v } catch (Exception e) {* y8 I( G5 x- ?$ H, }
e.printStackTrace (System.err);1 n8 R4 L$ P/ A- e! b, ]
}
5 R! j9 f$ Z0 d: ?; { 3 R& n( ^! S* A' L2 t+ o% ~6 X
syncUpdateOrder ();
+ V- q# o* c1 r3 `& e, B* E3 n" ^3 O d" x: Q. s) E: Y8 C
try {7 {, _" ^# J2 N$ l f; D
modelActions.createActionTo$message : T7 K& ~7 q! m; n; N
(heat, new Selector (heat.getClass (), "updateLattice", false));1 r; ~& A, W0 d# x5 H1 e$ L* G, |
} catch (Exception e) {
7 C) F; S, I }, |8 e( t& j System.err.println("Exception updateLattice: " + e.getMessage ());
9 g! a2 s6 C: B# d }- R( e( l1 d0 s, Y( a9 D
" r! I. c: j8 b
// Then we create a schedule that executes the
/ n+ K0 E n- V: z9 w+ S$ [6 z // modelActions. modelActions is an ActionGroup, by itself it# ^, b; d$ X2 Z( p
// has no notion of time. In order to have it executed in
3 I7 n) n( B+ O: y9 a C# }/ k _* x // time, we create a Schedule that says to use the
Z/ Y0 A! f6 K' x // modelActions ActionGroup at particular times. This
% r8 x) c% a0 k: P! U // schedule has a repeat interval of 1, it will loop every
8 f+ ]7 [" \& e: m: P6 Q // time step. The action is executed at time 0 relative to% d6 }$ b! E5 {. ^3 s6 S
// the beginning of the loop.% s6 a6 b* S8 M, ~
. N7 E: H) z8 {, }7 G // This is a simple schedule, with only one action that is
4 B5 }: h. O$ \9 i // just repeated every time. See jmousetrap for more2 w6 n$ Z) V _8 L, z) J
// complicated schedules.
6 n1 `& v+ t9 g$ \6 L1 i
6 A: J4 O4 ?9 @/ { modelSchedule = new ScheduleImpl (getZone (), 1);
' H2 D! ]! {$ Y6 [ \" d7 g+ Q# S modelSchedule.at$createAction (0, modelActions);
1 Y" |7 M7 i' C: V8 o) E
) b, I$ \0 t9 x return this; I4 Y* { [4 o5 f- f" ]$ x7 V
} |