HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:( ~% E! T4 P( M% {
! w0 G( J3 Z& ^) z+ Z; _5 S
public Object buildActions () {* I7 W$ e9 c+ @3 E+ J
super.buildActions();
$ _0 T+ Y8 d. k9 ^* q7 L! N * y* P, L7 R3 {0 o
// Create the list of simulation actions. We put these in" X. m- a3 v) Z& }: O8 f+ i
// an action group, because we want these actions to be
4 Y6 h1 `8 v0 w( a! Q // executed in a specific order, but these steps should, ~# M, }1 [. w9 U2 F
// take no (simulated) time. The M(foo) means "The message L$ A. b0 K: T, T; E+ {
// called <foo>". You can send a message To a particular, a! H% s; \9 b
// object, or ForEach object in a collection.
, m$ Y3 @* o% Y4 u 5 R8 x" }4 d3 d/ m) A" A2 }. I
// Note we update the heatspace in two phases: first run
+ Z/ j( a" h+ b, ~, N: I // diffusion, then run "updateWorld" to actually enact the
5 H, Z& F2 b6 _% L: {9 Z- x // changes the heatbugs have made. The ordering here is3 B) j& d. A, ~4 N- N
// significant!
5 V; E+ F! o/ n5 T$ z. t
+ K- [$ I% m) u! H, w // Note also, that with the additional2 U1 T0 u. ~0 ?$ r- I4 R1 B. U+ ~
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 [ U/ z( Y! D! a) g
// randomize the order in which the bugs actually run
' ? W* ~" o: v- j" p4 S // their step rule. This has the effect of removing any
3 j$ ]) b& l% A' z* q! _: C# Y // systematic bias in the iteration throught the heatbug6 _) g% L ~) b9 O4 m2 i
// list from timestep to timestep: D3 O& i8 d2 i# y" x* m
" F2 @. f- r/ n2 [
// By default, all `createActionForEach' modelActions have
0 j! J2 r& v _1 K$ `' j( g // a default order of `Sequential', which means that the. G5 ^+ d& A1 y0 X2 i9 T% d: g2 X
// order of iteration through the `heatbugList' will be
6 A5 I$ m# n4 q, O4 Y // identical (assuming the list order is not changed% V! L4 y9 [- e' Y6 F( M0 J W- ~
// indirectly by some other process).6 A8 R0 m* u+ v" c% H/ i+ B' N7 d
c- e3 }0 K3 Y) w2 l- g* [: E
modelActions = new ActionGroupImpl (getZone ());+ g+ b" M) a, y ~/ Y- a
+ o0 L3 {5 V2 ?, `- U x
try {
% g& p# @# x9 U modelActions.createActionTo$message
$ q Q$ t- b! m) ` (heat, new Selector (heat.getClass (), "stepRule", false));0 R5 q: `$ s: I. y! Z
} catch (Exception e) {0 D# y5 s$ I- b: h2 J% F& B
System.err.println ("Exception stepRule: " + e.getMessage ());& s( m7 h& o7 q$ X D2 j
}
: E1 m+ d: x0 m
9 B+ s" r C4 u2 s" ~ try {
( ^0 I. C3 H" f6 g Heatbug proto = (Heatbug) heatbugList.get (0);3 w8 @% y7 s! w/ e2 ]) `! y
Selector sel =
/ E8 }% q+ T+ l# g" x% l" p new Selector (proto.getClass (), "heatbugStep", false);
, D+ ^1 g2 U5 ]8 d! Q; t actionForEach =: g3 ]6 p& B* M) s
modelActions.createFActionForEachHomogeneous$call" u5 J4 X! u8 [
(heatbugList,
" |* n4 ~/ s. ^* ]' D; \, \8 }+ [ new FCallImpl (this, proto, sel,
9 F! k! u, O( L5 |! h new FArgumentsImpl (this, sel)));
- \3 U2 ?+ f$ N6 g6 V0 V( F } catch (Exception e) {
3 g! z; r$ }3 b& i3 f, X e.printStackTrace (System.err);
4 ?# X! I7 |: |. n6 W+ m }
& W7 b' M* z4 b. W. f. b- L
7 W; k" _' Y% g) ?/ \1 W- c; \ syncUpdateOrder ();8 k4 c- b( n, [; h4 ?9 m" I4 t
& y% O f( n2 P) J7 ~. N try {& O6 A6 g p' L$ J* m
modelActions.createActionTo$message / {' b/ h$ `6 |" p) d/ {0 w- q7 w
(heat, new Selector (heat.getClass (), "updateLattice", false));
0 w& \* G* F; g0 U! N8 _ } catch (Exception e) {8 y& o7 @6 X& {( T2 A; l
System.err.println("Exception updateLattice: " + e.getMessage ());
- _4 A7 {! C$ O0 M }% A6 X( x H- j5 Q( p) u
d2 {! j" ]# d
// Then we create a schedule that executes the
/ H; e6 ^* d" I, A // modelActions. modelActions is an ActionGroup, by itself it9 s# U( G; s" h2 Y8 M- l# r
// has no notion of time. In order to have it executed in' w* R5 y$ _. }/ b% Y0 }% U9 @2 ~
// time, we create a Schedule that says to use the. ?7 X7 R, m$ \% {
// modelActions ActionGroup at particular times. This1 n. _; ]) O3 w: K+ m
// schedule has a repeat interval of 1, it will loop every2 I7 l$ M" F7 Z( t' g, Z9 U4 ~9 Z& I$ h2 G
// time step. The action is executed at time 0 relative to
& i% ~3 f. i2 V1 y // the beginning of the loop.* o2 ~! i5 r' O7 }0 G# C
, r/ Z' A4 I* H/ j# b // This is a simple schedule, with only one action that is E/ f4 a/ d' d2 w) F1 b
// just repeated every time. See jmousetrap for more
! s1 u3 P/ I; M! M$ @1 T // complicated schedules.! S3 ^( p* Q+ J7 |1 E* V( [
# L, f( _7 o6 b* _: W* L1 [2 ^3 U modelSchedule = new ScheduleImpl (getZone (), 1);
& h1 U6 r% h6 ?9 l- `' w modelSchedule.at$createAction (0, modelActions);/ M0 T1 |5 r3 W
* K! [/ @6 N/ d7 u return this;
; G$ x9 v0 K) h/ } } |