HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; e6 R, }4 h. R6 x$ s* f
# D" k& l0 Z/ t- W7 Z& H% M, D7 k- ^
public Object buildActions () {" U! ?# f# D! J W0 L2 c* {* u
super.buildActions();2 s2 H! }) j O( }* A
3 B3 H' ~' N, _' g H
// Create the list of simulation actions. We put these in+ j5 W: m/ X2 {1 k `! L) V( \0 ]
// an action group, because we want these actions to be
: k; ]: W. l" e8 b( d/ u // executed in a specific order, but these steps should
* L. ?% i2 f4 @9 W$ z // take no (simulated) time. The M(foo) means "The message
+ R% u9 j' l2 v: L# m2 R // called <foo>". You can send a message To a particular
+ s$ e, m% ^" o- X" y T' q! N // object, or ForEach object in a collection.; q% }# t+ T8 l. P* q% V
( b5 @; C/ u2 y$ c" x5 K
// Note we update the heatspace in two phases: first run7 O: h) G8 H, F2 U! q" ^
// diffusion, then run "updateWorld" to actually enact the
, G* R" f6 t/ {; l* U: H // changes the heatbugs have made. The ordering here is) Y$ p( c; Y! @1 g) B' D
// significant!
( x" g% z; R. [1 ~7 _
$ A9 H6 t& s7 u' F, ] // Note also, that with the additional9 ]# K) b/ K" p2 X* L E
// `randomizeHeatbugUpdateOrder' Boolean flag we can
4 Z! l |. d/ v1 q; d0 ^" K // randomize the order in which the bugs actually run; A& O& t: Y& p( Z4 C! ~
// their step rule. This has the effect of removing any+ n; b9 m; V6 f2 ^0 u
// systematic bias in the iteration throught the heatbug, b/ ?! ]6 y# w9 U7 b9 J
// list from timestep to timestep2 h8 Q* N _- r1 _: V# n/ ]
+ {1 [" L/ l* ~8 V
// By default, all `createActionForEach' modelActions have
4 v7 t, h2 O/ L4 [( U // a default order of `Sequential', which means that the& h# q$ c4 b9 G9 T
// order of iteration through the `heatbugList' will be
' x# q" M* b5 |0 V3 [- ? // identical (assuming the list order is not changed
& q0 p ?, P' c# q; \ // indirectly by some other process).
2 U/ y9 @# Q( Z: R: M3 F
% T4 b5 C2 e# d5 K, e modelActions = new ActionGroupImpl (getZone ());8 x/ W+ J( k) g& n: @8 h) J
: I+ i9 v* z2 i: s q$ l, Y
try {
' N2 [" f3 h) x$ M4 m3 X modelActions.createActionTo$message. Y7 ~$ |! M( p
(heat, new Selector (heat.getClass (), "stepRule", false));
9 r( E0 v6 \5 \2 u. g: i# ^) r } catch (Exception e) {
- Y* S8 Z# n3 C7 i# P' I( A1 D- h System.err.println ("Exception stepRule: " + e.getMessage ());) ?' V5 C) B4 U' Z1 r* {
}' i# ^) B5 [$ g* F9 q; ~+ t! R
( S; v" z* `2 [' r" g; G# O try {% V/ j+ t/ o8 N& d' _2 B% K* B+ Y
Heatbug proto = (Heatbug) heatbugList.get (0);- G% j* \9 U2 Y" k# o
Selector sel =
# B' p; @3 K) j; u3 l7 P7 U3 Y, K9 d new Selector (proto.getClass (), "heatbugStep", false);2 |& S Q4 e# B! `' f" n6 `/ J
actionForEach =+ F! {0 V8 k* P
modelActions.createFActionForEachHomogeneous$call I) s, l+ c, ?# Q+ h
(heatbugList,- h7 C4 p d: e$ n' B
new FCallImpl (this, proto, sel,
& \; n; ^8 H& X* w* `8 i new FArgumentsImpl (this, sel)));
2 o& V+ s6 Q: q8 a$ U! o8 H } catch (Exception e) {
1 t" B; B1 s% E0 S$ b e.printStackTrace (System.err);! `: s9 B" p; z& f" m1 {
}
5 i3 `8 t1 f5 e, u# [( k
% n/ V! H" M- @; f6 E! L. H7 u0 S syncUpdateOrder ();& e2 [9 u( ]0 l3 n ^0 h5 W
7 H [- N& G, U9 K0 L) {+ ] try {' P8 u! x" G% b- E* f3 R$ [) C
modelActions.createActionTo$message ! R" z) ?& t; Q
(heat, new Selector (heat.getClass (), "updateLattice", false));( @8 }; m* U' d% M
} catch (Exception e) {
. s! S$ L) I* V+ W' u, f System.err.println("Exception updateLattice: " + e.getMessage ());
, o0 m# G5 i! N: e( r }# h h4 @% b! N
8 e( T; }; Y, J* S$ r0 ]8 ]
// Then we create a schedule that executes the
+ P' v- N" {2 D1 B% |6 e" H7 i // modelActions. modelActions is an ActionGroup, by itself it% R5 c! ^9 R0 [: m
// has no notion of time. In order to have it executed in
( l- U; p" v' d- _9 ?6 P6 s6 D // time, we create a Schedule that says to use the
8 A6 G3 w6 z- L5 ]. S6 k // modelActions ActionGroup at particular times. This
8 t, C6 M6 b' f: [' S, J // schedule has a repeat interval of 1, it will loop every
4 }. ~/ L0 I6 H5 Z' n! W // time step. The action is executed at time 0 relative to8 F, C. b' W- J) Y
// the beginning of the loop.$ v3 @" x! `9 [0 I/ l2 k, g" P
" b5 ]% k9 _# @ // This is a simple schedule, with only one action that is3 T% V5 V; ] q6 ?- Y1 J( a% m
// just repeated every time. See jmousetrap for more5 X B2 a+ D) P9 n* d
// complicated schedules.
! ? ]# a8 N9 a2 G1 g
* Q' K5 g- H0 g3 i, G+ w modelSchedule = new ScheduleImpl (getZone (), 1);
1 a+ C7 v B2 M% I6 a W modelSchedule.at$createAction (0, modelActions);
# T8 b& @, q' o* A " N; D9 a5 h- B; M
return this;- H3 D, P$ u5 F
} |