HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& n7 k: @( ~. d5 d/ u# P2 A
8 f6 q1 x! m0 ? r, d public Object buildActions () {; n' Z Q% P0 a( ? O8 L& f
super.buildActions();
+ Y) X4 F* @- d7 z - b: b: L' j% ~& F
// Create the list of simulation actions. We put these in
7 e/ D7 s4 K# a, Y t& n // an action group, because we want these actions to be( _$ P- h7 E* U; U* `! K
// executed in a specific order, but these steps should! D4 Y+ M {3 y7 Z' O [
// take no (simulated) time. The M(foo) means "The message& ~" w* x: S8 C, d
// called <foo>". You can send a message To a particular) L$ o2 b& J! b7 _. c" l7 B5 U
// object, or ForEach object in a collection.1 K4 F N3 r! _1 h
: n" W, {, L' ~0 u0 Q- Q7 n
// Note we update the heatspace in two phases: first run
- }# z& q5 {: P% B% A4 N1 @ // diffusion, then run "updateWorld" to actually enact the- @- G6 ]' \% o9 h, \
// changes the heatbugs have made. The ordering here is& s8 a5 I- y6 z0 V8 b( j' x" Z4 U+ V( H
// significant!
5 U2 D2 ]( [2 }! G
: N! I$ ^6 d- e // Note also, that with the additional
7 e* t! c# _( u& P3 }" ~ // `randomizeHeatbugUpdateOrder' Boolean flag we can, f8 P) c5 \, @, i, \5 c# G0 R+ ~
// randomize the order in which the bugs actually run
* L; z* t3 P8 I; B1 J1 D6 L // their step rule. This has the effect of removing any! n0 ?8 J. w5 J. u
// systematic bias in the iteration throught the heatbug" L5 \3 x6 [2 `; I- j7 `
// list from timestep to timestep6 k! T% R$ Q% F; E# V: j% j& ?
4 Z P+ p1 Z- ?* }+ v- L // By default, all `createActionForEach' modelActions have$ V/ ^( q9 {/ O7 w
// a default order of `Sequential', which means that the
: e" m1 i, m; R! W3 R5 ]+ i // order of iteration through the `heatbugList' will be
F* t8 h" X+ i2 Q // identical (assuming the list order is not changed! B1 w( G4 \# A; G9 ~5 L7 |
// indirectly by some other process).
; i8 y# V2 q3 h; i5 R& [
0 F0 ~ D( c7 L0 l modelActions = new ActionGroupImpl (getZone ());
, V* _ y, W* v* T4 z1 v" q, f% T% N! y4 {1 `, c% d) Q
try {& k* Y/ h; U2 n$ z/ x: I* E
modelActions.createActionTo$message4 L0 F0 a: p' G2 T0 h
(heat, new Selector (heat.getClass (), "stepRule", false));% w5 Y5 L" A5 G0 {# c
} catch (Exception e) {
; a" T7 B3 n/ @/ e System.err.println ("Exception stepRule: " + e.getMessage ());
% m* }$ Y8 i7 T7 ` }
. H f% W' K0 o- V" j( j1 [! B% z! s4 P
try {/ l9 M" H% w) @; i v' h
Heatbug proto = (Heatbug) heatbugList.get (0);
4 x- ~$ _2 }( I/ d4 F Selector sel = . i& M1 b8 {; j# w% z3 P
new Selector (proto.getClass (), "heatbugStep", false);
0 K" A- E% Y/ k( N actionForEach =9 b6 \- s/ B5 Q. F# U" {
modelActions.createFActionForEachHomogeneous$call' m! r+ |5 F7 I8 U% X( J3 n
(heatbugList,3 j- G* n2 z) @/ N* u
new FCallImpl (this, proto, sel,- [5 ?# o# `# ^/ ?9 E2 G
new FArgumentsImpl (this, sel)));
7 y1 A" d! e- U% N5 J" L# j" J } catch (Exception e) {
\* a. z4 a/ a8 e: d, i( _ e.printStackTrace (System.err); e! k$ W& S. \% d0 d
}
* E; t$ [* ~" i5 q8 h; K* b
6 Y' {8 Y% s: F! N- Q4 d syncUpdateOrder ();3 ~& H6 ?6 m9 q
( D# u2 W9 S; x7 p4 e
try {: m6 o2 f0 c8 ]3 }+ @
modelActions.createActionTo$message
D) U0 T; z4 d (heat, new Selector (heat.getClass (), "updateLattice", false));4 p; O; ~ \' ~5 G5 l9 |5 q
} catch (Exception e) {
8 {4 _9 X+ a# |- w4 F) c System.err.println("Exception updateLattice: " + e.getMessage ());
- K# |$ l0 V7 q) q8 s- e }
% w6 K, M/ m& B ( j, a1 z6 j6 r3 N: I
// Then we create a schedule that executes the
! U x& |) r9 g4 n( _2 |% p$ Y' p // modelActions. modelActions is an ActionGroup, by itself it# ? {% l/ ?5 Q& U
// has no notion of time. In order to have it executed in) E8 S% g# L& s: t- ?% L3 z, E5 i
// time, we create a Schedule that says to use the v& }; I# h5 }7 [
// modelActions ActionGroup at particular times. This+ {1 r7 T7 ^# H D8 i. p( I( B% e& V9 e
// schedule has a repeat interval of 1, it will loop every
. K, j& g1 ]0 b4 y' ] // time step. The action is executed at time 0 relative to8 X& D/ C& W) h+ ~( \' @: Q p# J
// the beginning of the loop.
* L3 h# e; k! i ~. O
- h0 o* i; B9 z( L0 N // This is a simple schedule, with only one action that is
6 r' T; {, x: G3 |0 {. @" e) ^ // just repeated every time. See jmousetrap for more
$ b$ r( f* j( C. T ^+ M) u7 n2 @ // complicated schedules.
: t+ [6 i& e, I) T; ?5 k
; c) ^ d' Z" K* L5 H% A modelSchedule = new ScheduleImpl (getZone (), 1);7 g- j! A. L8 S5 r# Y/ O$ |6 f2 [
modelSchedule.at$createAction (0, modelActions);/ w) J8 i' V: O7 x3 n
, l# o- v9 G4 q return this;
4 G( f. C+ ]4 j; n9 h9 q% w- j } |