HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 S: ]* R* o" e" P
' Z* E- C3 T' l2 @# ]0 U public Object buildActions () {* ?! b+ P. }- P$ I# k. F1 h
super.buildActions();* m9 V2 i$ e3 y! K F$ K
8 {: I$ n t; V$ j. X$ F4 g // Create the list of simulation actions. We put these in
. S% l# A( @+ q; k // an action group, because we want these actions to be
& v9 K5 W9 _; ` // executed in a specific order, but these steps should
4 k$ ?, k5 H* \# C! C- X3 s1 g7 f // take no (simulated) time. The M(foo) means "The message
' t) \( R" Z; i" K7 G" a // called <foo>". You can send a message To a particular
' N/ `5 G0 c! F // object, or ForEach object in a collection.$ `9 M8 o* n' e A9 R" x9 k
% e2 e% p: ^- U9 C* i. ? // Note we update the heatspace in two phases: first run
" P6 F- E% x" ~; z; Z6 M // diffusion, then run "updateWorld" to actually enact the" @4 J# g$ x: B) n, j/ }* X% h
// changes the heatbugs have made. The ordering here is
- ?1 [# \; R7 a; u+ m* P& v; o6 u // significant!
$ [( G& D9 M6 { F& d) K
1 t" N/ U; S$ b& _ // Note also, that with the additional, S% M F: Q' K
// `randomizeHeatbugUpdateOrder' Boolean flag we can" k9 Z7 P6 E: h- |7 H# ~( H C) w) [
// randomize the order in which the bugs actually run2 _& `+ X3 L& D- V
// their step rule. This has the effect of removing any
; a7 t" N- }% @# z // systematic bias in the iteration throught the heatbug t' i/ R: r2 ]/ ~8 X
// list from timestep to timestep
v6 l; A& l# p$ t2 F' T8 k0 Z0 P
1 C$ a& O" a/ R8 Y // By default, all `createActionForEach' modelActions have
( F! R+ G7 S) o( |1 ?/ Y0 d // a default order of `Sequential', which means that the5 I. g5 M7 S0 c# K0 X3 K' M% I, Y
// order of iteration through the `heatbugList' will be
% \3 g5 I( |$ r // identical (assuming the list order is not changed" ?3 _$ Z2 Q$ G! _# R% |' j8 |2 O; \; _; u
// indirectly by some other process).6 b" [) L. [/ n {8 d
9 @2 A/ V/ L0 }/ g2 N9 }
modelActions = new ActionGroupImpl (getZone ());
; Y/ y5 P3 _5 f* d5 o
: S" V+ p9 ^6 m/ G; G try {% M2 K) t/ k- m# n6 R! Z! s' D
modelActions.createActionTo$message
" Z# G b8 N/ J( D2 X7 V (heat, new Selector (heat.getClass (), "stepRule", false));) N, i6 r: ~7 N7 q* n' C& N
} catch (Exception e) {5 H) x6 k: x& O3 E
System.err.println ("Exception stepRule: " + e.getMessage ());
r& r4 A% Q- x2 N4 _9 n) K3 \; _ } h' [, t. ~4 X$ W0 v6 |
+ c5 e# N. W/ {7 Z4 |
try {
9 U1 }6 b9 w! d+ v B2 h. s0 W Heatbug proto = (Heatbug) heatbugList.get (0);: b& }3 c' Q% P8 ^
Selector sel = 4 v3 J" o+ W5 |3 Y- S" u8 ]- B- D
new Selector (proto.getClass (), "heatbugStep", false);5 N$ {6 K8 `8 |; R! `: _
actionForEach =& V- D( q: b5 |
modelActions.createFActionForEachHomogeneous$call
: c; d- r, y: X# u$ V5 q; o (heatbugList,6 ~/ J) a5 z3 Z1 O9 [" p, S$ o
new FCallImpl (this, proto, sel,
% U) ?# @) y: P" A6 V! S new FArgumentsImpl (this, sel)));; x% G b) i: Q# j3 d
} catch (Exception e) {
$ h' L& D- F; n0 Q/ a e.printStackTrace (System.err);* K0 M0 X6 ]! T
}4 Y! L: n& o) y! S9 b
/ V$ g4 a5 Q( W$ ]5 ?& @" ~$ F! K syncUpdateOrder ();
1 X& M+ r7 i9 n' w" e
/ I& h, a. ?2 a. w6 G# r3 m9 x, c try {
# J, |) e. u! T6 D modelActions.createActionTo$message 0 t+ U0 ]( H v# l
(heat, new Selector (heat.getClass (), "updateLattice", false));
' ?; X* G6 L/ R; a* G6 S. T# v } catch (Exception e) {6 z$ \! c$ Y$ O3 I9 b
System.err.println("Exception updateLattice: " + e.getMessage ());
h) ]7 l3 D$ t# B" Q4 } }
: _' g8 N6 u: y; D3 o: k. j
. b- H) F$ y' v: \$ F+ Y$ }* Z // Then we create a schedule that executes the8 D! B1 P) }" P5 i# N6 A* _, _
// modelActions. modelActions is an ActionGroup, by itself it: F- l# V8 ]4 D( W4 x% }
// has no notion of time. In order to have it executed in
2 H/ L* S `( W8 Y // time, we create a Schedule that says to use the
# T4 L$ X1 J; ^# ~ // modelActions ActionGroup at particular times. This
; T6 c5 v1 ]% T5 C3 ~ // schedule has a repeat interval of 1, it will loop every
0 [1 H! K4 X2 S // time step. The action is executed at time 0 relative to. c6 y. W" e5 i; R3 I
// the beginning of the loop. K8 Q9 @8 w# W
9 b: X5 O6 i9 j' |& K8 e // This is a simple schedule, with only one action that is. ^9 M- q* ^ i) c9 g# f4 P' M
// just repeated every time. See jmousetrap for more" z3 p/ L, ~( T0 e+ p- y
// complicated schedules.
3 L. U; `( s! ? - R/ i$ c2 u+ U, F5 @/ O7 t
modelSchedule = new ScheduleImpl (getZone (), 1);* ~1 a& m; P/ D& J" X% \
modelSchedule.at$createAction (0, modelActions);
* {( }& n; j( H. _; f( E6 s
5 ^; ?7 X# j" G8 [6 { return this;
: V @9 t2 a" Q- R } |