HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 l( ?* w7 u2 }: g8 H
/ P( k) Q* H# L- c. M
public Object buildActions () {
1 p; h$ H) p& c0 r3 K! i' X4 E super.buildActions();
8 S* A- m% g$ l; G ' {& w& |1 T1 j" z6 [! y
// Create the list of simulation actions. We put these in6 D5 V# ~1 q3 y# H2 _
// an action group, because we want these actions to be
- b( u5 G( ^+ t" [" j // executed in a specific order, but these steps should7 r. Y9 \: e' Y, W
// take no (simulated) time. The M(foo) means "The message6 f' x* j" [ |+ A4 @- ^2 ~( }
// called <foo>". You can send a message To a particular6 Q* Q: |/ u9 P% H
// object, or ForEach object in a collection.
" f7 N- p- J- [7 q; p # e6 K0 ^) N% _1 r- b0 b
// Note we update the heatspace in two phases: first run8 A% s$ w6 p/ d
// diffusion, then run "updateWorld" to actually enact the! h; J" |9 V0 ^6 d6 b! i
// changes the heatbugs have made. The ordering here is7 Y9 R+ X4 w! w% N$ H' R
// significant!
4 @7 T8 y2 V# c- f: y/ Q - M3 {6 Y( M- o( A2 y
// Note also, that with the additional/ n; [4 x8 s- I/ R$ q
// `randomizeHeatbugUpdateOrder' Boolean flag we can3 [7 q, N3 n% z/ ~
// randomize the order in which the bugs actually run# W! Y: O9 c/ [% J/ _: q# O
// their step rule. This has the effect of removing any
0 ]5 [. H9 w% k! v0 F+ f9 |* p // systematic bias in the iteration throught the heatbug
1 C* z3 ]3 e M9 N3 M // list from timestep to timestep
0 W& H1 k, f2 i7 T
2 {( C n" X8 D. c: X! g. B$ V // By default, all `createActionForEach' modelActions have
9 d8 @/ N( K8 o# w+ a" ?& G8 G // a default order of `Sequential', which means that the3 S2 _! m) [* O4 P+ N
// order of iteration through the `heatbugList' will be& H& W9 K/ D. M( `
// identical (assuming the list order is not changed n9 i/ L: B4 i* e- z. ?2 i& ~2 g6 [: A
// indirectly by some other process).8 ~" g9 [) ?7 K7 [1 B( o
8 v% ^5 _" ~8 D' V+ o! z modelActions = new ActionGroupImpl (getZone ());
, I4 u. {5 h) }" _* ^' J5 h% l
, Q0 y3 ~1 v/ [# ~7 T try {
& O' c2 G/ v& J modelActions.createActionTo$message7 H/ G8 x8 L, t) ?0 D0 |2 G7 ]
(heat, new Selector (heat.getClass (), "stepRule", false));- e$ K( x$ L/ M' M, K
} catch (Exception e) {
& ?& N. O: s' I* [& w, p System.err.println ("Exception stepRule: " + e.getMessage ());6 o0 T6 [* @0 a5 ]& T
}
' P! t- u2 W& d- _. H: U5 f4 t/ F: H) m+ t1 Q6 c4 E$ f
try {0 d* A. K/ ]7 W; Y
Heatbug proto = (Heatbug) heatbugList.get (0);% }( A) ~* T0 f/ \+ g0 e
Selector sel = ) Z/ I8 K+ e0 [! S4 q, q; `# _2 q
new Selector (proto.getClass (), "heatbugStep", false);
" a6 f7 J, l# Y0 t* b: H actionForEach =
5 {5 x. z7 c. N% a$ M modelActions.createFActionForEachHomogeneous$call! I- x: W* l' U0 p. ~
(heatbugList,' v% h# |) H6 m# {9 ~0 H/ W
new FCallImpl (this, proto, sel,
0 U9 b1 T9 k. F3 j new FArgumentsImpl (this, sel))); P9 `3 M7 M5 G! c \
} catch (Exception e) {
' q9 h8 Z* X' R4 c" E e.printStackTrace (System.err);
, Y& K+ Q6 d$ S }
. y1 [. d& s3 k5 x, U
9 Z4 Q( e; Y6 e+ U5 ` syncUpdateOrder ();. r4 T: }9 n5 f9 F! n" k) f3 |
: ^ d/ W$ f. Z7 f
try {
' n; o+ Y9 e$ Z0 V1 e2 C5 w2 ^ modelActions.createActionTo$message
\4 f$ a: ^& { Y (heat, new Selector (heat.getClass (), "updateLattice", false));
( q: D" S; k! u: l6 i9 y } catch (Exception e) {' V. g, r1 r9 i2 d9 m: p
System.err.println("Exception updateLattice: " + e.getMessage ());! e4 G7 \( \+ \; G" P9 [4 [
}$ r5 x, r. _; T0 {. a% p9 d, ?
1 f2 l. \ d1 r1 x/ y
// Then we create a schedule that executes the
: E3 V" ~( D" | // modelActions. modelActions is an ActionGroup, by itself it7 z6 P# h: z) l! X8 t! ~; F4 _
// has no notion of time. In order to have it executed in% B# o7 y7 U3 p
// time, we create a Schedule that says to use the, {9 A7 e$ n) b# B! q4 j( m
// modelActions ActionGroup at particular times. This
* W( |; a' U& `' ^7 p5 m2 K // schedule has a repeat interval of 1, it will loop every3 |& W" c# J+ O
// time step. The action is executed at time 0 relative to7 Q9 e' r h2 w, R
// the beginning of the loop.
7 \! Q! q! n8 c: d. n* {8 @$ U% F. K3 r9 p8 q
// This is a simple schedule, with only one action that is$ t9 r9 p( a1 h
// just repeated every time. See jmousetrap for more5 B/ `' \8 ~. f+ ]5 h4 f
// complicated schedules.! S% s( B" v5 H; p: _& D7 i' q
, f/ n9 B( w3 t( g9 D modelSchedule = new ScheduleImpl (getZone (), 1);7 @) H$ E' Y& k0 Y6 ~- G) n
modelSchedule.at$createAction (0, modelActions);( c) T/ q: v `0 ~6 R* Q
" m/ v. B; Z$ f p$ E/ R# a" e/ n. l return this;8 G& j! e5 h. M/ r
} |