HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
3 T9 N( \* A6 H: t5 F4 b9 G, J% l* {- Z% r# S+ I* @2 m" f
public Object buildActions () {6 q. K; ~6 l7 h
super.buildActions();
7 `7 `' r3 Z/ Z
" [/ ~5 U7 } P# L // Create the list of simulation actions. We put these in
6 r- t- S$ ]$ `9 E/ J0 ~: V // an action group, because we want these actions to be
) d, g$ | U; u$ C3 H& q // executed in a specific order, but these steps should6 o6 d( h7 i8 W9 M3 U1 f
// take no (simulated) time. The M(foo) means "The message
' Z& W6 R G" X; b9 q4 P2 N* z // called <foo>". You can send a message To a particular; ]. K" d- ]3 V* z2 P
// object, or ForEach object in a collection.
, x# E& d! v1 p& i% q" O( l, h * p+ T0 N# A" `! k5 o) r+ I
// Note we update the heatspace in two phases: first run$ ~$ D# X! Z6 b }8 a5 b
// diffusion, then run "updateWorld" to actually enact the
3 c# F, }+ a' h9 n. Z // changes the heatbugs have made. The ordering here is/ F6 ?/ [7 Z* }2 V- r
// significant!
, W$ j; v5 T7 o! t 7 t( B. }( i: M& @
// Note also, that with the additional; z) r' E+ i5 C6 }" ~1 o$ I
// `randomizeHeatbugUpdateOrder' Boolean flag we can9 J, z3 a% k! X# Z3 q2 V
// randomize the order in which the bugs actually run
+ n: [# l C, P M$ q& M1 \ // their step rule. This has the effect of removing any3 E1 n+ x( t4 T# M8 M: p6 ?6 g7 A" a
// systematic bias in the iteration throught the heatbug; q1 ~& P0 H: Y
// list from timestep to timestep! U: b' t& v \- `0 Y4 `
+ ]3 L# f7 b+ C9 [' p0 d
// By default, all `createActionForEach' modelActions have% ^$ _- }. J y2 F# ], _
// a default order of `Sequential', which means that the
6 r$ M2 F7 j0 Y( o% N // order of iteration through the `heatbugList' will be6 J+ |5 e# p9 K: X$ [
// identical (assuming the list order is not changed3 o8 E0 M; I9 L. B6 T
// indirectly by some other process).1 J/ b+ A; Q" q; r& k' V4 ^
, Y. C" d( z$ N% {% i+ i modelActions = new ActionGroupImpl (getZone ());
/ {$ ~" I+ v1 I1 y* o/ N- K- [5 M* N. ~
try {# C" N9 e9 u& m# w& N
modelActions.createActionTo$message
5 h9 Y5 E4 g' d( [* A (heat, new Selector (heat.getClass (), "stepRule", false));
* a$ n7 n Y- j: l } catch (Exception e) {
( p& b- L, V% N: ?; l' | System.err.println ("Exception stepRule: " + e.getMessage ());. {! I ^ x U
}
; g0 u; R4 \/ S8 }! V9 x0 [5 [% w- r/ m8 Q0 e! t& J% I+ Y
try { y2 {) [, D0 V% O- @+ E
Heatbug proto = (Heatbug) heatbugList.get (0);
1 n/ |7 Y* r7 U Selector sel =
% C. P. T6 M: R new Selector (proto.getClass (), "heatbugStep", false);# T Y1 C+ t) Z/ l6 e/ o
actionForEach =
2 v8 K7 H' I! _! M- B6 H modelActions.createFActionForEachHomogeneous$call6 z/ l+ J. c, x
(heatbugList,# d8 U! \ `4 v4 `4 u2 z V
new FCallImpl (this, proto, sel,
' k0 S/ w }3 W. Q2 N4 U) o ^! V new FArgumentsImpl (this, sel)));
/ O# q2 j7 J, U9 ]% i' c4 N } catch (Exception e) {2 ?. {) c- x# \1 n) Y1 w3 h
e.printStackTrace (System.err);/ x& ?6 N0 Z I# }2 V8 g. L
}. b* m0 C5 m3 C) U1 F5 H
" }* X- C' O' i# G& d+ A& i1 t
syncUpdateOrder ();- O* s# P3 z: e0 \ m7 ~
|5 k) d# C! s/ t, @9 {
try {% v$ Q Y( p9 N P) `+ a. ? V
modelActions.createActionTo$message c0 J" K, s% e1 F
(heat, new Selector (heat.getClass (), "updateLattice", false));' \1 ^$ w* B+ p, U$ ?
} catch (Exception e) {: _4 t6 ^$ B( Q0 K
System.err.println("Exception updateLattice: " + e.getMessage ());: _% v2 X+ g# k; `/ I
}" s2 u; M8 L" m' Z- x
; e1 @7 u W4 ], `' N) N3 ~
// Then we create a schedule that executes the
4 E+ L9 X# x- r8 s, L" l // modelActions. modelActions is an ActionGroup, by itself it+ Q$ N0 E9 k4 J/ m! D" E" @
// has no notion of time. In order to have it executed in
$ l- D6 w. w% l% b3 T // time, we create a Schedule that says to use the
5 G5 x! _( T1 t: [; R9 x' z // modelActions ActionGroup at particular times. This
* y% I$ G/ l% J9 ~$ |4 k/ ^ // schedule has a repeat interval of 1, it will loop every2 k* x; ?9 d5 I* n! s
// time step. The action is executed at time 0 relative to
6 c" s! I; P# d4 x% [. h // the beginning of the loop.
& b" j9 n2 ]* E2 I6 R8 `% k3 e& e; ~; t
// This is a simple schedule, with only one action that is
N0 U. S) k D& P- {( s. I // just repeated every time. See jmousetrap for more
& _5 Z m" W0 ]8 h- h O D+ g // complicated schedules.
8 [4 S6 |( ^3 y# |8 k" e4 G' U 0 A& w+ G1 q/ Y$ L. a0 @! F
modelSchedule = new ScheduleImpl (getZone (), 1);
: G; V! s6 H6 b' {9 j/ s0 H* @ modelSchedule.at$createAction (0, modelActions);3 Z4 P! s Y: N. Z+ x y6 u0 A4 T
6 A7 D9 w6 Y& \: ]* G2 o( C1 X6 }4 R return this;& U }3 {9 Z- ^9 h) [6 S6 b" k0 _% z
} |