HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& M6 d3 K" D7 h* \! t$ ~+ a; F. x% x/ N% K' s3 [
public Object buildActions () {! N S4 N) x/ K& M! w5 I
super.buildActions();
G: k* [* W* t& W7 C/ b" ~
1 D/ H% R& |/ {/ X // Create the list of simulation actions. We put these in. Z* W7 {, ]% k
// an action group, because we want these actions to be
( w7 D4 y2 w3 q# R! b0 E( k // executed in a specific order, but these steps should' K; t3 u: c$ Z# q; s
// take no (simulated) time. The M(foo) means "The message- R/ t+ d' P+ W. i9 E4 Y& p$ t
// called <foo>". You can send a message To a particular
8 m9 O/ m) e2 U9 Y // object, or ForEach object in a collection.3 ^. J, L2 a( C7 v* W
6 k \, A* {4 [3 W) r // Note we update the heatspace in two phases: first run; E/ b5 \/ K; B( C/ h9 {
// diffusion, then run "updateWorld" to actually enact the2 I# M4 H" ]/ t" V! x8 }& O
// changes the heatbugs have made. The ordering here is) X6 K0 p% y8 F% Y
// significant!9 c% T1 C$ e- t) F" g6 G7 F" U7 g
5 l. Y3 Z' `$ J+ G5 y- R. k
// Note also, that with the additional
8 r( f; l' z8 J0 q3 D1 B // `randomizeHeatbugUpdateOrder' Boolean flag we can
" M8 B3 r, @$ n0 W4 w // randomize the order in which the bugs actually run3 H7 |) c" B6 S! z( {8 {, P
// their step rule. This has the effect of removing any
. B0 X' U, ]% i6 v/ j* T! A0 W // systematic bias in the iteration throught the heatbug) \7 U4 A0 l/ {7 x/ N
// list from timestep to timestep& q* W% t* x$ V! R2 S2 {
8 a0 D9 D4 k, @' C // By default, all `createActionForEach' modelActions have# T) X( H. [4 Z0 z
// a default order of `Sequential', which means that the
4 p* N- ?; F5 r0 ?5 |) a // order of iteration through the `heatbugList' will be
5 u) \& |, S9 r% Y- B2 {* ? // identical (assuming the list order is not changed
8 R. v$ }+ l9 `* ~0 r // indirectly by some other process).
2 c6 Q, l$ [, m: j. }* r
/ @4 E) v' p6 R modelActions = new ActionGroupImpl (getZone ());
$ a, r1 S! D3 f y4 l, I1 D- H1 x: l* r% }
try {
6 k; @+ A) l( B modelActions.createActionTo$message
' ~4 f8 d% M( H9 {" ~) | (heat, new Selector (heat.getClass (), "stepRule", false));
. v5 |: {. W' r3 E8 Z$ r } catch (Exception e) {4 V. b, E, e8 w
System.err.println ("Exception stepRule: " + e.getMessage ()); W3 w u7 d. d% a
}1 b5 [1 T8 a) i8 w/ V
J0 j: E9 k! m9 j try {
7 i/ b* F d: k" [5 ?! m Heatbug proto = (Heatbug) heatbugList.get (0); U' L/ Y! r9 [; e* I# o
Selector sel = 7 H" I* b: ]% L( ^. j4 t; A
new Selector (proto.getClass (), "heatbugStep", false);6 ~& s/ R5 b1 h5 k0 g
actionForEach =
8 b0 O @* M, f( s8 c# @5 ]! | modelActions.createFActionForEachHomogeneous$call
! a! G; v0 N `* l8 E( N (heatbugList,5 y" `) q$ Q" u
new FCallImpl (this, proto, sel,
2 ~( `6 d* ^4 K, c2 W! |( I9 E new FArgumentsImpl (this, sel)));8 u) Z# q* l, w
} catch (Exception e) {) c+ q9 m1 v! N
e.printStackTrace (System.err);
. A5 Y/ z/ U. T( r" P }; J! i* z" y7 k3 A6 C. j
6 ~8 b$ ^6 \, V( F7 e syncUpdateOrder ();
, ^3 N- a; V- c. b- M; p- } g2 y Y9 n+ C
try {5 I A- J4 k1 {" _ R& J" D
modelActions.createActionTo$message 2 y+ U' O" f- p# b1 l8 @
(heat, new Selector (heat.getClass (), "updateLattice", false));: f0 l! E5 S; s1 R
} catch (Exception e) {
: \0 P) i8 }3 w& y6 E System.err.println("Exception updateLattice: " + e.getMessage ());
$ M8 O ~- n. t% e }
) y, I: @: l$ [' ?! X ; v/ F7 U* w3 U/ V, v% h
// Then we create a schedule that executes the
" x; j% l. x4 o6 A& R // modelActions. modelActions is an ActionGroup, by itself it& |7 q! J s2 {7 |, E
// has no notion of time. In order to have it executed in& P# c3 [0 r g; o& W- o
// time, we create a Schedule that says to use the# g- V/ [" x: U0 g4 J6 d1 M
// modelActions ActionGroup at particular times. This
8 j/ E! q# C8 } N* p6 |# q // schedule has a repeat interval of 1, it will loop every7 d+ s8 y- s. a1 S
// time step. The action is executed at time 0 relative to- [+ O$ y( P3 v0 F! d; L* j
// the beginning of the loop.
! W0 C% N; k7 k8 m
& @# Y2 p0 E5 ?& [ // This is a simple schedule, with only one action that is% j3 | p3 {& ^
// just repeated every time. See jmousetrap for more
, [' e0 }8 u4 U9 G // complicated schedules.
L; f f5 y) E , y+ j* y# B8 _4 `
modelSchedule = new ScheduleImpl (getZone (), 1);% O: g: I: \7 D& V
modelSchedule.at$createAction (0, modelActions);
: l( D# s9 k; v: R {! l; ]9 y
) q" `/ f4 k! d0 r" O8 m9 C5 v6 q return this;. K% G% L$ N, N2 G8 @
} |