HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, m0 J$ l' V! M; R$ W. j
* u, v) P8 d3 e6 [1 n" M public Object buildActions () {; b( Q4 x+ ^ W7 R
super.buildActions();4 ? G2 F4 e9 J& g
' x0 e1 _: G% l |: s
// Create the list of simulation actions. We put these in3 v( S% Q) ]5 D
// an action group, because we want these actions to be% x9 r: t% Z% g- m5 D
// executed in a specific order, but these steps should
" o/ _( H3 M) O3 s! C. K // take no (simulated) time. The M(foo) means "The message# A* V8 S$ F/ |4 L* D' A
// called <foo>". You can send a message To a particular
8 l! Q1 ?: c8 s // object, or ForEach object in a collection.
" Y4 |1 i, N6 y( n5 r2 ~
* D8 g( o4 {1 ]: ^, C // Note we update the heatspace in two phases: first run9 C' i( S$ V0 @( {' w, ^9 q3 i
// diffusion, then run "updateWorld" to actually enact the6 t! x, z! V" d$ \+ h# k+ ^
// changes the heatbugs have made. The ordering here is
1 T# ]& t* j! K% K$ V3 J' Q0 u // significant!
6 E4 h( G/ O V
! ~! V% ~( _2 V; T6 B9 s7 ~ // Note also, that with the additional5 Y6 m3 t7 K6 u! Q
// `randomizeHeatbugUpdateOrder' Boolean flag we can
/ K+ t% L' e- V; E& S7 Z& i) Y9 y // randomize the order in which the bugs actually run
C) X* R m7 C. _" m& C6 T // their step rule. This has the effect of removing any( ~! A9 u: f6 I" f$ L4 F
// systematic bias in the iteration throught the heatbug
/ R4 [/ s6 C# T$ g3 ~ // list from timestep to timestep$ s# R1 @( X. ]9 ~; @
8 M+ C2 b! F- [2 b) {/ f // By default, all `createActionForEach' modelActions have
2 V/ u t2 P7 S& M0 t! k+ P* S4 W8 i1 q // a default order of `Sequential', which means that the
0 h" R* W9 B/ }+ h% q // order of iteration through the `heatbugList' will be
- X- r* z% H9 X% ]6 h // identical (assuming the list order is not changed2 ~% M0 j6 R- r( Z5 Z1 b9 b) V
// indirectly by some other process).9 K& F- h8 c- S
0 V- \+ b: ]/ ?& R& F/ { f( b9 z0 O/ b modelActions = new ActionGroupImpl (getZone ());$ w" s" o! }2 r8 y1 e6 k
/ J$ g4 k" B' A) t, R try { S# }, E5 x# z; @5 o
modelActions.createActionTo$message
9 U8 S+ ]& j. W' c9 X% q1 G (heat, new Selector (heat.getClass (), "stepRule", false));
/ Q. w" R+ E5 G, d* A+ J) ? } catch (Exception e) {
/ o3 H# B' j) Y" G2 _* I- V. T System.err.println ("Exception stepRule: " + e.getMessage ());
$ f% u! L _; x! x, r. c }$ Q8 w2 B. u' n; g/ Z
# Y/ N I2 G, M! { try {
: q" Q* c+ q8 w& h& ^$ B Heatbug proto = (Heatbug) heatbugList.get (0);1 L5 A7 W( Y7 E& n6 k7 x# F
Selector sel = % x0 M/ b* h" u" u. p) Q! e
new Selector (proto.getClass (), "heatbugStep", false);, a, c3 {* e {7 g H
actionForEach =
* C+ L. U' n9 h9 K4 y% ? modelActions.createFActionForEachHomogeneous$call
. v# Z, T+ I: @! x7 E' {' E. i* m (heatbugList,
8 G! s5 f8 O# G0 [) L& n new FCallImpl (this, proto, sel,
1 K( W( p* z H7 g8 ]4 ^ new FArgumentsImpl (this, sel)));
6 p! D9 K$ G2 n; Q# M6 m } catch (Exception e) {- P8 H& t4 g" w9 _+ z0 n* o7 O
e.printStackTrace (System.err);
t+ l/ v# S, z0 G }
4 u4 P ~1 Y- L3 c) C% t9 _
$ y" I+ a2 H% H/ p) N syncUpdateOrder ();/ p9 g/ }/ _& O* |
' @7 j7 y1 _* r8 f
try {
9 [; r5 Z' _* E9 ^ modelActions.createActionTo$message
# f; c/ v" @1 Z$ u! s (heat, new Selector (heat.getClass (), "updateLattice", false));
& N* h4 h) ~. H' T0 p! ? } catch (Exception e) {
! I% P' P) u9 o, u6 ] System.err.println("Exception updateLattice: " + e.getMessage ());
o, ^) C1 E5 @ I: t9 X }* o2 ^% o2 l1 f& X3 X
$ u- f9 l3 L6 g! r0 K p0 x
// Then we create a schedule that executes the
( {) |& G1 y) I$ B // modelActions. modelActions is an ActionGroup, by itself it
( H2 _" H! _' _2 V // has no notion of time. In order to have it executed in
* {, p( F/ x/ \2 A$ {8 L# B // time, we create a Schedule that says to use the
# z0 W5 z) ^+ D/ G/ x- k // modelActions ActionGroup at particular times. This
7 [8 F5 g+ p( h: S& ^' b // schedule has a repeat interval of 1, it will loop every
; I) k2 X- H( G // time step. The action is executed at time 0 relative to6 }; B- u! H) y' H
// the beginning of the loop.4 `6 A8 r2 j+ e5 D4 F3 f
3 m, u. j- I4 X$ e B8 I# z. [3 {- H // This is a simple schedule, with only one action that is
9 F0 P7 g, d7 C: b2 H% U* T // just repeated every time. See jmousetrap for more" f" T! j* T* x/ |1 s5 ]
// complicated schedules.
. ~: R# s1 t0 i% _9 L/ F0 n' M* s
( `) Z* f+ w3 T9 o, g modelSchedule = new ScheduleImpl (getZone (), 1);
9 H9 {0 r& Z$ U5 T$ W modelSchedule.at$createAction (0, modelActions);
8 c9 _; {( R( ` x9 p7 P
( V7 S+ M' K8 a3 E5 N return this;$ g- U5 @3 T) A S: G& Y
} |