HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 w- n: x! A( y% h: p
; G( L4 {" Q) U public Object buildActions () {5 @; H- @8 ?- m2 y5 J% G
super.buildActions();
' Q7 T$ M3 `8 F6 W( Z3 | % Q& N% C& E0 p X3 w
// Create the list of simulation actions. We put these in
0 p9 }6 p5 p9 S7 m; ^3 g- d // an action group, because we want these actions to be# g& o$ C9 x# x+ k1 S1 {
// executed in a specific order, but these steps should
]1 Z1 u- ]8 x) W7 L9 y# C. [8 N // take no (simulated) time. The M(foo) means "The message4 e1 A3 e& d7 h/ K4 Z" R3 r- N
// called <foo>". You can send a message To a particular; m1 C7 E( k. ^
// object, or ForEach object in a collection.
: i) N! ]4 H/ d , y5 I- P; U- p5 z8 f9 _3 h
// Note we update the heatspace in two phases: first run; `2 c6 R+ F# I7 d! C
// diffusion, then run "updateWorld" to actually enact the
* U8 t5 @8 X6 O9 X' r7 T% X( Y // changes the heatbugs have made. The ordering here is
7 J" r1 ^9 p8 F9 g, b0 ] // significant!
6 J* b/ M: }$ ]3 Y- m2 V
. C: C; {% U5 X) N // Note also, that with the additional
5 P: z# a7 u5 P) x& [# V! ^ // `randomizeHeatbugUpdateOrder' Boolean flag we can! w3 H. d6 v; G
// randomize the order in which the bugs actually run8 n5 j6 t8 j; H) N/ Z) K
// their step rule. This has the effect of removing any4 G0 M" `$ l0 x# S
// systematic bias in the iteration throught the heatbug B: v O' r: X9 W$ r
// list from timestep to timestep
7 w4 z/ U' O+ D; B. H$ t
! C2 k1 j6 f0 A! V# z // By default, all `createActionForEach' modelActions have
. C4 v5 A4 _# K/ A5 |5 n // a default order of `Sequential', which means that the
- @# Y# j) g6 G( _3 F I- B! A# ] // order of iteration through the `heatbugList' will be
' W' Y& W, T+ N: h0 @ // identical (assuming the list order is not changed
+ x _* E# ^5 z2 h2 X; f // indirectly by some other process).
6 i( s3 n% K" F1 o/ G
! v8 F6 A4 B5 b" P# \6 j% t modelActions = new ActionGroupImpl (getZone ()); q7 M# ?- \1 ^7 J
: I9 N2 r7 W" W/ [% ^
try {
7 l- n+ q4 w" ?! ?( i6 ~% @ f modelActions.createActionTo$message, L. L9 r) S6 F) m% l, P
(heat, new Selector (heat.getClass (), "stepRule", false));/ G* Z5 }* Q6 M% I
} catch (Exception e) {
: v0 ~% A6 A3 Q7 b& { h6 l System.err.println ("Exception stepRule: " + e.getMessage ());
2 V8 o' F, A5 q4 P% y }5 }; P+ \8 O8 z, N
7 k' s6 x5 u" Y% n* D) P+ n try {+ Y! x( H$ k9 [" @4 `0 f$ G
Heatbug proto = (Heatbug) heatbugList.get (0);
% Z7 f$ X) N2 Y" G1 S4 ^ Selector sel = + \5 L2 Y! F0 z! {8 V' }1 a
new Selector (proto.getClass (), "heatbugStep", false);/ w u, p3 {* I
actionForEach =
5 r3 o _6 g+ { O$ G modelActions.createFActionForEachHomogeneous$call
' n- b' l" B; ~$ l# E9 l* F (heatbugList,
- ^2 {8 @& K$ x% e, S7 T new FCallImpl (this, proto, sel,6 N4 g% G, V% Q2 z
new FArgumentsImpl (this, sel)));
% c' u) m" ?; s2 ~5 Y } catch (Exception e) {2 Q$ P1 Q1 F; O; u4 c
e.printStackTrace (System.err);6 `! Q7 v: D7 R6 F3 n: X' o
}
8 d8 n8 }$ ~4 `: C) P + a& D7 b; B# l8 d
syncUpdateOrder ();6 Q/ F+ {2 J0 s/ g$ ^2 N+ ]
9 c! S4 R# C4 C8 H( x5 @ try {
6 }' V9 ?; S5 J! ^! W( d7 r. G- z0 ^ modelActions.createActionTo$message
) e# Z) o6 M6 _# Q$ U# c) ^ (heat, new Selector (heat.getClass (), "updateLattice", false));
( {5 F( v b* m: A) s0 m* F3 h# { } catch (Exception e) {
% A* P% ~2 C' E" Q System.err.println("Exception updateLattice: " + e.getMessage ());' T) K2 L6 f% e
}8 [1 V* ~& P5 p9 y- H2 I' S2 x5 P
: x) H: }4 \( g: S9 z
// Then we create a schedule that executes the' s$ _; @+ |- L8 [2 s$ p9 X5 O, U6 R4 b
// modelActions. modelActions is an ActionGroup, by itself it6 r- S9 n j2 k6 }) [" H/ {0 q, r
// has no notion of time. In order to have it executed in" E! ^$ {! S# n0 o: M& t& A( l
// time, we create a Schedule that says to use the2 a7 A9 `8 i6 v0 w$ H' d: G, g0 p
// modelActions ActionGroup at particular times. This5 g& A4 r3 ?# A0 y! x- ]
// schedule has a repeat interval of 1, it will loop every, b; p/ H0 a4 o; L: a4 w$ s" J
// time step. The action is executed at time 0 relative to
$ {& J* D. b' @9 q // the beginning of the loop.
; `1 y3 \0 W* d! ?# d4 j4 H
8 h' H; K5 R/ S8 R2 N // This is a simple schedule, with only one action that is
, J. Y- t4 A, ]' {8 h // just repeated every time. See jmousetrap for more
( i6 i6 q+ l6 ~1 m. j // complicated schedules.
+ q' S; `0 h; n' ?7 F; N ]7 ^3 Y
3 _8 p( i8 }8 i( V3 X modelSchedule = new ScheduleImpl (getZone (), 1);
& z% n4 ^' g/ v( V: b modelSchedule.at$createAction (0, modelActions);
" t, |+ @( i. R" g# s+ V 5 q6 O' s- L+ C% a8 S+ h# q
return this;; N" k$ M3 _7 u; l
} |