HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 _; I7 h4 e0 X' w0 c; N) h1 _
* t7 V3 a3 c3 g7 B public Object buildActions () {6 X. D% ?4 P9 e' C( v. e
super.buildActions();
& v; _$ ^8 w& I* [ _5 x6 c+ I; Z
6 @4 s- ~; @2 I/ x6 H9 s9 Y0 @ // Create the list of simulation actions. We put these in
' v5 k$ R3 z. N& M& L V( g // an action group, because we want these actions to be
4 h! `5 l* s, {$ i" B // executed in a specific order, but these steps should
* m6 z, V) C5 Y" V0 K: I; I // take no (simulated) time. The M(foo) means "The message% W9 k1 s ~9 t. y- j s
// called <foo>". You can send a message To a particular
9 q t, B, H/ }1 V! Z // object, or ForEach object in a collection.0 }) l: V1 i8 g( p8 v, m; X
6 m1 V: S9 \# ]3 N // Note we update the heatspace in two phases: first run5 v' E+ q' k `/ y/ S* M/ K
// diffusion, then run "updateWorld" to actually enact the
7 p3 V& y; \$ y$ x/ u // changes the heatbugs have made. The ordering here is/ k5 ~+ r# W, i; Q7 V! ]" b' K
// significant!
, [2 p J2 W# ]0 ~ ' K# a, i" O# a. d
// Note also, that with the additional
( b; Y) `. P3 A1 L! ~ // `randomizeHeatbugUpdateOrder' Boolean flag we can, l: A: K) u+ v6 @$ u- D
// randomize the order in which the bugs actually run
4 p8 }) s, E7 f& o/ T6 n // their step rule. This has the effect of removing any8 m. x+ ~; \ P# p- V! j+ S
// systematic bias in the iteration throught the heatbug) O7 |, Z$ X# ] V G+ b5 o
// list from timestep to timestep
' @6 n% @- M* R0 [, P1 U+ [! F 7 [6 d# z+ i1 J4 B* h
// By default, all `createActionForEach' modelActions have
" K M4 g4 H1 R/ s' n4 ~ // a default order of `Sequential', which means that the
' v$ f( a; n9 y7 B9 _: y; ^ // order of iteration through the `heatbugList' will be* l" A. i7 S' h4 o+ z
// identical (assuming the list order is not changed
1 f0 o2 W; X4 l5 v; Z# e // indirectly by some other process). ?, G. y5 N/ q" _; N5 e+ T& l
" x! j e1 E6 X+ @6 ]) a0 a7 {5 E modelActions = new ActionGroupImpl (getZone ());
3 y9 Q8 [9 j9 l8 m2 I5 o$ \) J: x3 U8 ^. L: y# v! K: P" E
try {
% B/ _ E f% z$ N! p$ p modelActions.createActionTo$message
" x4 s# b( x) v# x4 { (heat, new Selector (heat.getClass (), "stepRule", false));" }2 D8 H1 H! n- X2 _4 T
} catch (Exception e) {
' x: q" D- T: Z c4 c/ ^9 g System.err.println ("Exception stepRule: " + e.getMessage ());) S- f: m' e* E
}! U+ X4 a* O, ^* j. b- e0 V
$ \2 h( }) J8 A# o# ]
try {) D7 x8 }1 `" J/ N1 D2 Z9 w
Heatbug proto = (Heatbug) heatbugList.get (0);8 a2 ~' l8 M( U) p& U9 P2 C
Selector sel = ( G, d9 M' Z1 @2 ^" Y
new Selector (proto.getClass (), "heatbugStep", false);# Q. s4 U4 U: F' T0 o) O
actionForEach =
, s/ ^; v/ N4 w& Z: W3 R9 _ A0 x modelActions.createFActionForEachHomogeneous$call! O( S: o. _1 _# T9 L% o
(heatbugList,
& _' p- Z2 t! s. m3 U% _( Z, m9 c/ j new FCallImpl (this, proto, sel,
( X& \, x# Y, \ p d5 C+ P. x new FArgumentsImpl (this, sel)));
, x* k6 v1 l1 \8 |$ D7 u2 m } catch (Exception e) {
" U2 S, u3 I" V) A i- D( [7 v e.printStackTrace (System.err);
9 P" \% y6 ?6 h, U) r* o }
0 N# `, j' W: Z/ \ ! Q/ Y- X! o! M
syncUpdateOrder ();# X1 ~5 R6 E9 j' ]
+ t" B/ R9 I* ~8 C7 E" I try {3 s6 M# g" \, M
modelActions.createActionTo$message # ?% |. @' D/ d2 H. \6 ^" Y
(heat, new Selector (heat.getClass (), "updateLattice", false));, v X' n- G. l- G
} catch (Exception e) {
9 z! W C; R+ v( O System.err.println("Exception updateLattice: " + e.getMessage ());% Y& }( Q I6 o$ e# \& {: G7 `7 ]1 @2 Z
}
+ u; k- k; V4 v7 ]7 v" S0 a 5 n( W1 M3 |$ {8 q5 V
// Then we create a schedule that executes the
L6 P9 S+ R" G1 Y; u9 Y4 O // modelActions. modelActions is an ActionGroup, by itself it) E/ u9 L: k* \7 J0 W, l! K) K
// has no notion of time. In order to have it executed in
' F" M. V$ `8 s4 F3 i // time, we create a Schedule that says to use the
( s* G" U0 R- d8 }; k" U# b // modelActions ActionGroup at particular times. This* D A" G v r2 ]% b6 E2 `' F b% _
// schedule has a repeat interval of 1, it will loop every
! ~7 V0 U% j# b; j0 q3 g( g // time step. The action is executed at time 0 relative to
& I6 S: Q7 o4 i% R8 o( W+ n // the beginning of the loop.
- l! ?. D, p8 Z5 F/ J4 ~+ w
" B( e+ g6 }2 F4 Z" _, e& s // This is a simple schedule, with only one action that is5 ]# d( F4 g5 @: d' T/ s
// just repeated every time. See jmousetrap for more
; X) n$ V: |( a0 C7 q$ r // complicated schedules.
$ K% @. `" ]# F$ h! g 7 N! l! A. q0 w: N0 C h3 i& t
modelSchedule = new ScheduleImpl (getZone (), 1);, C8 L& ~5 U: d( k; H2 I+ g1 [5 ^
modelSchedule.at$createAction (0, modelActions);, k# Q% D* b. [! _0 |
6 F/ l9 k4 G/ G9 } return this;; I4 o: a+ D$ T; x; l" f
} |