HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, }( g! d, R( N+ a) D
7 s3 ~8 V- P7 Z- ~* w+ V { public Object buildActions () {' B. O3 L8 {% r5 Q0 I7 v
super.buildActions();+ U0 _8 A& P" A; ~
% t( e' R- ?9 Z" a' u
// Create the list of simulation actions. We put these in* m& l/ j$ g5 E( D: g( T
// an action group, because we want these actions to be
% R5 c1 I: X# x1 @ // executed in a specific order, but these steps should* \% I% O/ {8 M' L0 l! m# U( }$ x
// take no (simulated) time. The M(foo) means "The message: {, e' G' X8 s+ N- h
// called <foo>". You can send a message To a particular
1 J- @) Z3 J7 C# b- M4 l- X // object, or ForEach object in a collection.
0 E% n2 ^' r" i& d$ f
5 {! F. `- E+ r, f# j- j* J8 x // Note we update the heatspace in two phases: first run
, [; g( W# _/ v: A; \3 S, T; Y% K // diffusion, then run "updateWorld" to actually enact the
/ f2 Y# P" W. S // changes the heatbugs have made. The ordering here is
' y7 A Y* ^; Z- B // significant!
, X' q8 l5 W6 n6 S& u6 N* v
; Y3 o8 y6 C, }, v // Note also, that with the additional
- q" M( t8 U ~$ N7 h/ t0 ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can+ Z; C. O( Z5 v5 u
// randomize the order in which the bugs actually run6 l% o3 Y/ H) `0 N
// their step rule. This has the effect of removing any1 r0 t, q1 i% r4 q
// systematic bias in the iteration throught the heatbug
6 ` |! S! N! s% {& R( F5 f. Z // list from timestep to timestep% E+ {3 w3 y: [0 V& ~1 K( {1 }
, |3 S# ^ }8 U# ^: X! C // By default, all `createActionForEach' modelActions have
9 _! }- s3 j5 T g! L; ~- b& w" R E // a default order of `Sequential', which means that the
5 } j' h( a0 o) c E5 x2 } K // order of iteration through the `heatbugList' will be
; Q/ z% S0 {! h' m+ Z$ W // identical (assuming the list order is not changed
: L+ i2 l/ g3 Q // indirectly by some other process).2 e+ |. t2 b8 ^/ t
* t; R. M& M1 W" ?* x k6 L( `$ L
modelActions = new ActionGroupImpl (getZone ());6 J: ~7 x8 C7 f' i, G- o( T
3 A, z7 k- K! H+ h5 _- C2 @ try {2 u) c7 o+ h: g
modelActions.createActionTo$message
4 t& J% b2 t) s( w y& e (heat, new Selector (heat.getClass (), "stepRule", false));0 F9 {9 }8 d/ c7 b
} catch (Exception e) {$ E2 I, D# D# Q8 J1 w, v) R
System.err.println ("Exception stepRule: " + e.getMessage ());& b! e6 U3 `) j9 c5 x% F2 }
}6 z7 ?0 }, M o( g- c4 {& _
6 n2 t c1 k. d try {6 Z$ u" X! @. s, e
Heatbug proto = (Heatbug) heatbugList.get (0);
, Q. c$ i9 v# y/ Y* i7 B Selector sel =
- b9 B( |+ p# ]8 _0 O, x @ new Selector (proto.getClass (), "heatbugStep", false);
; \' v/ j6 t. M3 [. T- n2 K actionForEach =
" f# u# ^5 _+ z" i. t5 h, D modelActions.createFActionForEachHomogeneous$call
6 L- g) j4 _" S e3 { (heatbugList, U' h- h) Z# f# `6 r0 K; s6 ~5 e
new FCallImpl (this, proto, sel,
: I% {2 Q `1 f/ r" P' D6 B9 K. d+ A new FArgumentsImpl (this, sel)));; M5 l" u/ y" x3 j7 t* T; _9 O
} catch (Exception e) {) T5 |: }4 `, p# y
e.printStackTrace (System.err);
( a! h" E1 z( z- L- L5 O- S" a }
/ Z8 e; K9 u6 S3 @9 y8 ^+ T. T, ` " d0 e4 d) {) K9 Q. b8 u$ I
syncUpdateOrder ();, x6 i/ G, d: |
3 B. G: R- A0 n* V) A9 Z" ] try {
4 n/ L8 r: B5 p$ M1 f/ o; M modelActions.createActionTo$message
( ], `) H7 l% j- } (heat, new Selector (heat.getClass (), "updateLattice", false));
" S. p+ X/ U' z: R& B+ O M } catch (Exception e) {
! P$ o) t9 W, o2 m& g System.err.println("Exception updateLattice: " + e.getMessage ());
& @3 g8 F! u. K/ L5 e }) d# O6 Z" Q% P/ R) n. M
% b7 ?) L( Q2 d: W
// Then we create a schedule that executes the
4 E- V2 H' X3 ~" h" S5 x) h4 k // modelActions. modelActions is an ActionGroup, by itself it
+ c; S$ l+ E* [: \8 _ // has no notion of time. In order to have it executed in
c' w; Z. V2 X m // time, we create a Schedule that says to use the) x4 \- m+ u# M( |! `/ \0 S4 o2 U4 q
// modelActions ActionGroup at particular times. This
" l! u+ E3 V. @# `0 ]7 I; a) k1 s0 [0 G2 e4 e // schedule has a repeat interval of 1, it will loop every: g, X* S" T$ w( b! y Q# f! `9 G
// time step. The action is executed at time 0 relative to$ `- `3 X9 ~- ~7 U1 V- A, B0 r
// the beginning of the loop.3 ^9 n5 h! c, O4 b
/ Y- {( ~" T. f/ ~ // This is a simple schedule, with only one action that is
" |* C1 i7 g1 [' k. ] // just repeated every time. See jmousetrap for more& b- f2 F$ u3 \6 y8 c
// complicated schedules.) Y, @: n/ p; f2 t& s! r8 g
. i$ Z7 \, O. N+ O w m: c modelSchedule = new ScheduleImpl (getZone (), 1);; t& l0 m$ D" g, B. n( V
modelSchedule.at$createAction (0, modelActions);
4 l# F4 r, a2 L
! l+ y% y7 D4 i: Q N return this;+ e; T5 M% L% p9 n, ~5 q, {4 f8 I
} |