HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 \! n/ Z+ }3 G! Y! C
7 A- x) w$ w5 i+ i7 f; G- i2 C6 N' H" ^! A public Object buildActions () {. R8 b0 m& ~( h$ e3 b+ J
super.buildActions();
' f' F0 b! u9 q ! G( F. c1 h$ Y# { n- E2 C2 F) M: X: {
// Create the list of simulation actions. We put these in
7 c# K/ R% x ~9 j4 ]7 ~ // an action group, because we want these actions to be: ]* a: u$ v1 j: K* a
// executed in a specific order, but these steps should
( f7 o4 X/ E: C! d* Y3 |# @ // take no (simulated) time. The M(foo) means "The message
3 i$ c5 d. S, j* a3 O' A // called <foo>". You can send a message To a particular
: g+ V3 w E, C/ o* S3 D' Y // object, or ForEach object in a collection.9 K- q \% V2 e! M b, _& n
! b' r1 a& h: A$ X y
// Note we update the heatspace in two phases: first run
( o! ^0 x7 V. W% Q5 l" Y2 |5 `- M // diffusion, then run "updateWorld" to actually enact the
@' C1 s# h/ a8 |. c // changes the heatbugs have made. The ordering here is- `+ `) h) u. D1 n5 O, W$ v$ k% B
// significant!" {% N# S# X3 M( [* [
9 f& s) Q( V) I& f
// Note also, that with the additional) `9 [2 s- A3 a& s
// `randomizeHeatbugUpdateOrder' Boolean flag we can
0 ]) T3 H w0 N+ O J- r // randomize the order in which the bugs actually run
' W. i7 f, Q- k // their step rule. This has the effect of removing any) H7 @* p4 O( ]# @: @: I
// systematic bias in the iteration throught the heatbug
' z% S6 a% ~ d7 W& M% G5 j // list from timestep to timestep( d+ h9 g# x6 x) R* w! E. ]
# ?& J0 t0 h1 { // By default, all `createActionForEach' modelActions have
$ i/ [: o& r( s1 k' n // a default order of `Sequential', which means that the
# K! @0 }; @2 H8 @! n // order of iteration through the `heatbugList' will be6 ^5 X. D# q7 p1 q
// identical (assuming the list order is not changed
7 t' U0 V0 u; E+ P& a% Q // indirectly by some other process).
1 Q6 ?# r0 t' e( k+ z3 a' ] ; l3 P c8 U7 J
modelActions = new ActionGroupImpl (getZone ());9 i2 J" q! |9 }4 S4 Z- t' |
" g: J. v- }+ u W; k# ` ]7 S* a try {/ j; x! Q# N: J! T/ e! p
modelActions.createActionTo$message
: O$ r* E. K/ C$ s' A; v (heat, new Selector (heat.getClass (), "stepRule", false));
* ]& s7 q3 U1 D9 B4 b! c. W5 J: S } catch (Exception e) {
9 J7 a4 o3 r- I. { System.err.println ("Exception stepRule: " + e.getMessage ());* t4 U0 c+ E; D1 P! z8 W8 B
}
5 }0 ]- C/ O4 |
& z, M6 R8 U% }% }& A try {
; A2 M R4 \- G4 h Heatbug proto = (Heatbug) heatbugList.get (0);
4 o8 R y2 ]8 C5 K- g+ e Selector sel = . U& z' i/ T8 | f& c; y
new Selector (proto.getClass (), "heatbugStep", false);
( I: `! f" [$ Q5 c2 m3 Y actionForEach =
/ Q( x9 Y* g A# p0 B! P modelActions.createFActionForEachHomogeneous$call
* t5 l j# Q' O3 e, T (heatbugList,* r; }( U+ `% I; P: ^' [
new FCallImpl (this, proto, sel,
% }$ y- W' S9 u6 u& U new FArgumentsImpl (this, sel)));. x0 T# f* a# G8 j5 J& h0 l
} catch (Exception e) {* \2 |6 P' D( Z/ h! e
e.printStackTrace (System.err);
) [+ K1 R" o5 p0 p8 X% x }# k7 i& p& C0 A* Z
! @. \/ G- I, y2 ]/ Q' E4 l5 G% |
syncUpdateOrder ();' v' g/ l2 T$ @
d: B3 H0 c0 t' n f
try {
# t' i# _ C0 O: ^6 F8 f. R modelActions.createActionTo$message ' m; C$ _3 Y( V& z, C
(heat, new Selector (heat.getClass (), "updateLattice", false));
9 ]8 @) Y. B K' @! B: l7 V( F- N } catch (Exception e) {
0 ^+ A' B# K/ Q8 R3 T/ G System.err.println("Exception updateLattice: " + e.getMessage ());! G/ m, q$ ?$ u4 }& x! D
}
. r' y( q2 G) k' i : G3 w0 c4 g" R& T
// Then we create a schedule that executes the
# O' i- {9 j A, a // modelActions. modelActions is an ActionGroup, by itself it. o& G/ K2 A/ v1 s+ s, R1 o. ~- x
// has no notion of time. In order to have it executed in
* y! z- ]7 C8 [; R; u // time, we create a Schedule that says to use the3 G0 _3 q3 c# K) J, {6 J {
// modelActions ActionGroup at particular times. This$ _' ^6 {7 \: Z5 d- @# o9 I
// schedule has a repeat interval of 1, it will loop every
7 T- c- U1 ^4 h/ _4 B. E // time step. The action is executed at time 0 relative to
: i! B# X6 Y2 w; T9 W% i- a // the beginning of the loop.
; P g7 K- K( y# i* {! y" H" I3 J9 z) H. G: Z( o9 A! q& a
// This is a simple schedule, with only one action that is
# b7 a! W/ k; m6 c5 f // just repeated every time. See jmousetrap for more/ e! {1 f5 u& n: g! m; \6 }
// complicated schedules.
( e3 k1 C1 K$ B
! s" O* s" s6 f( W! c0 ?" L modelSchedule = new ScheduleImpl (getZone (), 1);/ a; \% s; f: ?0 w( }
modelSchedule.at$createAction (0, modelActions);
1 y3 ]* U$ w9 B; b# r$ q
6 o2 z8 X9 k& m return this;
3 ~9 A' N8 L% h* ?& K* B } |