HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
' t- S9 w K0 T& U! ]
1 b q' M) W6 h( ]5 r, M public Object buildActions () {3 C7 V4 B; E( G# F( b
super.buildActions();# V/ v! Q4 x$ R& c9 _
2 a- v3 }" M8 C, y // Create the list of simulation actions. We put these in
, e: {! |& B' ~# H/ ^) e6 G // an action group, because we want these actions to be
9 [ h) t) t4 L& X // executed in a specific order, but these steps should
8 H$ R/ ~+ J( X) S, |/ L5 c/ q // take no (simulated) time. The M(foo) means "The message
; g; c2 a G: @5 C! f. M+ } // called <foo>". You can send a message To a particular
. o. `2 G/ {5 F$ G+ m // object, or ForEach object in a collection.3 b1 h/ a R3 C5 S# ]
$ t" `+ d- y1 g; x/ d
// Note we update the heatspace in two phases: first run) r8 c. z7 o' F+ O" r
// diffusion, then run "updateWorld" to actually enact the
6 z* z) b4 T" T0 k; v2 w; R // changes the heatbugs have made. The ordering here is
' |* v+ m$ q5 U/ W // significant!
: @+ v' y- o! K9 _: I4 \9 T. G 8 C% S* z$ p9 K# b3 X0 {7 ^
// Note also, that with the additional
6 z! F$ J) P6 {8 f' q: d+ {+ p // `randomizeHeatbugUpdateOrder' Boolean flag we can' b' t3 }/ A1 w8 \4 W& @$ D+ w
// randomize the order in which the bugs actually run
x: E0 r8 |1 E! G3 U // their step rule. This has the effect of removing any
$ c+ ~/ X/ l3 y8 E$ f" ` // systematic bias in the iteration throught the heatbug1 W$ _! g2 j' r' ~# M% l7 |
// list from timestep to timestep
+ h _+ a0 f9 X/ R 5 u4 s8 t: v1 S3 u/ G9 z0 \5 ]* T$ ~- ^
// By default, all `createActionForEach' modelActions have
3 {7 F) }! S. }. h8 @* ` // a default order of `Sequential', which means that the
8 S9 k6 L) S* A# s // order of iteration through the `heatbugList' will be
+ W0 Y4 d) f7 h* k // identical (assuming the list order is not changed8 P9 c, A: W+ B; a$ O P
// indirectly by some other process).3 x) z( h" O5 I# y( C: k, ^) V
9 r! V1 U2 G) G: J/ _ modelActions = new ActionGroupImpl (getZone ());
. K& @! ~$ p/ X1 b) e% y% p" {, r7 B' R' m1 \
try {
/ A" ?- U$ i ~ modelActions.createActionTo$message. P+ w) T5 _% [7 U/ L/ U' ]
(heat, new Selector (heat.getClass (), "stepRule", false));
0 q, g/ z" g: \+ @/ _$ L, O } catch (Exception e) {
) h2 C7 a) j( Y" ~/ H: E System.err.println ("Exception stepRule: " + e.getMessage ());# T& O9 _8 R Z3 O
}
, h0 Q# t# a# P G2 K4 x& n2 ~; K! p, P" ~: c) P
try {
2 r" w& P( T5 f* | p7 X# r& L6 d2 q Heatbug proto = (Heatbug) heatbugList.get (0); r( ?% K& H" y( F9 `
Selector sel =
1 W0 A' ?; W2 Y' k. T0 p$ U1 U" n new Selector (proto.getClass (), "heatbugStep", false);, G$ R1 O( ]( q0 d. R) A) d6 M& P
actionForEach =
( Q+ w9 b7 k/ W$ N1 A modelActions.createFActionForEachHomogeneous$call
- u: N: m% T' P0 y (heatbugList,
: Z; W$ A+ w [/ c new FCallImpl (this, proto, sel,6 X# a3 @! x" w
new FArgumentsImpl (this, sel)));
^' Q* x# e; f" b+ S } catch (Exception e) {0 |' C6 ~* y; e$ ^" z
e.printStackTrace (System.err);" E; k0 V6 j6 O7 _; {+ E; g0 _% t: v
}& x! @2 s8 ~% r
/ D; }; [1 D9 l' W; ] syncUpdateOrder ();
& {) a, M" y/ B. p) [* c3 I ]
1 V! T( ^: C) y6 K+ S try {
* A1 O# n7 p4 v% e% J modelActions.createActionTo$message
3 V v0 `6 z. q3 A d3 W+ Q4 y! x# s (heat, new Selector (heat.getClass (), "updateLattice", false));
H u3 `* u2 N" Q" z: I1 ~- ~ } catch (Exception e) {
% i- a. M3 x8 D& } Z System.err.println("Exception updateLattice: " + e.getMessage ());
- r0 {0 I0 b, }: z# f5 L }5 I* v- o7 R2 [
; W1 A; a Z% I* [1 P; G
// Then we create a schedule that executes the3 T3 I1 x+ Y9 O
// modelActions. modelActions is an ActionGroup, by itself it
7 \( B0 Y6 E( w. k x4 t/ j1 q // has no notion of time. In order to have it executed in/ c# t' n7 x: x( f
// time, we create a Schedule that says to use the
E- m Z; n: c- X" [ // modelActions ActionGroup at particular times. This
d. ^" j) A) M, T1 R$ Q5 f6 \ // schedule has a repeat interval of 1, it will loop every: T( ^: N* E' A X# a( J0 C
// time step. The action is executed at time 0 relative to
( k8 O: j2 \* c* ^ // the beginning of the loop.0 [# `& c/ U4 O+ n
; c$ T2 b; L* ~( g% J // This is a simple schedule, with only one action that is: g6 h: i1 f! B A5 B( ?
// just repeated every time. See jmousetrap for more# b; W. F" I4 z
// complicated schedules.
2 F" b- ]+ w& d- ?! L% t
. }, T. r9 f M& w6 h modelSchedule = new ScheduleImpl (getZone (), 1); r* r p1 [+ k3 K; e* `
modelSchedule.at$createAction (0, modelActions);4 t! C5 B0 Y/ C! |& A
' U& g+ d0 V2 t% } return this;/ q! N! ]! o z* g5 e; i; ~0 u
} |