HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 E; P3 _9 ?6 ^$ X: ]5 [7 o$ N. j. |
public Object buildActions () {
3 n, X0 N8 L* i7 d super.buildActions();
, E) M; T/ ~" k! O5 I8 \ 3 ~' n2 ]+ a' y# l+ c& S. s/ ?( A. T
// Create the list of simulation actions. We put these in
# {2 O7 g) k; t$ z- _! P) @ // an action group, because we want these actions to be
7 u" R4 _" M5 M0 Y* g // executed in a specific order, but these steps should( R4 L2 n3 p' q* p+ S8 k6 M. U
// take no (simulated) time. The M(foo) means "The message
" C* M8 g8 _+ H; o4 U B# k0 h3 X // called <foo>". You can send a message To a particular# Z0 X& Q8 t& O, }" e# D; [
// object, or ForEach object in a collection.
0 p9 i# e% r8 @ ; S- ?( h4 B0 D' G9 o G2 B
// Note we update the heatspace in two phases: first run
; E8 @& w: j. `) |: k% a // diffusion, then run "updateWorld" to actually enact the3 K! n, L# D( x8 [" b9 F! u$ m
// changes the heatbugs have made. The ordering here is/ u# O' P6 B2 G# e+ l5 I
// significant!( {5 }$ ?1 \& o: d
" Q0 l3 J/ G6 N1 |1 N
// Note also, that with the additional
: d" N3 N6 k& n: J! \* Q // `randomizeHeatbugUpdateOrder' Boolean flag we can: M) b" P0 I! {: [) i6 S* N& e
// randomize the order in which the bugs actually run
" {2 {! {9 b; ]- D% f // their step rule. This has the effect of removing any& B- o: a1 A/ s- S& q2 Y
// systematic bias in the iteration throught the heatbug5 E& a9 k! j- R6 [4 I
// list from timestep to timestep v6 M1 _9 t1 N0 y6 C
+ R; h: ]% Y! ?! R! y( M) r% V // By default, all `createActionForEach' modelActions have
- j; D: N, B. [. J! X- d* A. E5 h // a default order of `Sequential', which means that the
1 M4 q/ U" x1 {, [& v, \. ]* ~ // order of iteration through the `heatbugList' will be. `2 ?' \2 z, Y8 n/ q- K0 N
// identical (assuming the list order is not changed4 Y* p0 [ ~; ^& b5 [
// indirectly by some other process).8 W& H* C: {/ p6 T# b1 M
5 n- n; Z: q6 q+ {" Y modelActions = new ActionGroupImpl (getZone ());# c( E5 _ w' W9 E
; u' f& I) }6 ]! N9 u" k try {
2 a$ \( _7 {) G* B4 d modelActions.createActionTo$message
3 t7 O* A. T8 O* }) ~ (heat, new Selector (heat.getClass (), "stepRule", false));9 d. S3 k$ W7 @+ p
} catch (Exception e) {& `: j+ V% H& g# Z7 w/ D, {! k
System.err.println ("Exception stepRule: " + e.getMessage ());" ]& o! f- c, B; I
}8 L" L6 A3 }2 l4 v" S" w; h
) q6 q/ M: c& M" J& e1 F
try {3 V" u6 W2 C$ M ^
Heatbug proto = (Heatbug) heatbugList.get (0);: s; E: }& R/ n; U, V
Selector sel = 0 T9 c: K" j& z! j2 X
new Selector (proto.getClass (), "heatbugStep", false);) Z: {# l8 q$ H9 r% N9 R
actionForEach =
% X& z4 t/ W* P6 U0 D I modelActions.createFActionForEachHomogeneous$call* q9 |# L0 z: m
(heatbugList,9 F7 G2 H B" k5 W, C
new FCallImpl (this, proto, sel,/ ~! }; N& `+ ]" y+ k' d( \3 t% n
new FArgumentsImpl (this, sel)));0 Y, o! t& D; D* u* _
} catch (Exception e) {
. v. ~0 U/ \+ |2 u; t: ] e.printStackTrace (System.err);& W+ Q2 G+ w1 e+ X; x! V4 R
}
! s: w! p6 u: |% G; z( Y
5 w' W/ ]* O8 Z3 f0 g4 }0 C3 |8 ? syncUpdateOrder ();0 ?9 x) g; H0 v9 q
$ p$ _+ M5 Y( g/ [. u try {# w: s* K0 N, |) m7 G2 _
modelActions.createActionTo$message
7 @. `3 v9 T' \ D (heat, new Selector (heat.getClass (), "updateLattice", false)); R/ m% L# c! ?8 P6 o) S
} catch (Exception e) {/ I2 T- ]% @0 V' V% L. J
System.err.println("Exception updateLattice: " + e.getMessage ());7 m8 W# q( y+ O9 _, j. V6 h8 z
}7 j; k. \$ Y+ w" F" b) k
b/ x" G+ `. |+ c, K3 C // Then we create a schedule that executes the+ v" x$ s) Q, ]9 @, C
// modelActions. modelActions is an ActionGroup, by itself it& {9 j, U& e7 P
// has no notion of time. In order to have it executed in- S9 [" z/ H( u) v# V0 }
// time, we create a Schedule that says to use the2 X' f# o' Z/ d
// modelActions ActionGroup at particular times. This1 c3 F6 e9 t" }. @' R$ s2 @) v
// schedule has a repeat interval of 1, it will loop every+ ?4 i6 A" v2 s6 o
// time step. The action is executed at time 0 relative to
+ b9 t0 w/ V: v/ d$ M // the beginning of the loop.
. H. n3 \! J0 K
1 [9 O' H+ C' f // This is a simple schedule, with only one action that is
9 M4 D+ K+ ]8 @ // just repeated every time. See jmousetrap for more# @6 `. b: e7 S
// complicated schedules.
: f: R- {! x" P% P
# n T& Z$ @- U- t! @" f# o: ^4 Y% Q modelSchedule = new ScheduleImpl (getZone (), 1);7 K% _% [5 E2 z! e: s9 W i) Q
modelSchedule.at$createAction (0, modelActions);
) m! {3 q9 ^' m
3 ?8 V$ q$ t1 ]8 i7 \! r0 }/ y2 ^% x return this;
9 c! Y2 Z: T, G( j } |