HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:, x- b* ]& p2 _3 p( f% f. j
; [; k; l% f4 N: a! h3 u6 G! J
public Object buildActions () {
7 q9 C# W( B6 Z" r! {1 j4 t super.buildActions();" g( O0 }1 Q& H4 `
1 T% G& Z! S- O) } // Create the list of simulation actions. We put these in
5 W) G( Y( ~/ }: v // an action group, because we want these actions to be
* E6 j8 _6 @' x2 l+ b // executed in a specific order, but these steps should* @1 @ u+ E! ]5 L5 e0 w
// take no (simulated) time. The M(foo) means "The message
- M0 m9 W2 Z! W/ F4 P: Y // called <foo>". You can send a message To a particular! z# q9 v! V" t$ ]' {$ p3 V X3 h' J
// object, or ForEach object in a collection.
, j" [$ E$ w, b% c5 T + Z; ]. O0 a" [* I$ Y! M3 ~
// Note we update the heatspace in two phases: first run- ?" U$ L4 g( v1 G9 K0 m7 w) O
// diffusion, then run "updateWorld" to actually enact the
9 u( [5 e7 E3 O( S // changes the heatbugs have made. The ordering here is
' q" T# [" J7 Q+ V! U9 r // significant!
( j9 t: O1 b) v" b- j& [ x# d' H! x( H
// Note also, that with the additional9 y `5 I' ?& O* P6 z% \) w0 }5 e3 z
// `randomizeHeatbugUpdateOrder' Boolean flag we can
4 T1 e- Y \, l: Q- I: ~1 a // randomize the order in which the bugs actually run
, C$ E) d7 r5 o& |) V4 P2 b5 J+ h. T // their step rule. This has the effect of removing any3 t; ~9 K/ b/ \8 p
// systematic bias in the iteration throught the heatbug
9 m: j) q- Z2 g+ t5 C; `5 m/ z2 x // list from timestep to timestep
. |* S% E( ^. T( |* G! Z4 v 9 g% C2 ?( K/ F$ ~( c' Z; O
// By default, all `createActionForEach' modelActions have
1 U/ _2 R7 ?& ?" s, d // a default order of `Sequential', which means that the; ~& S+ m: o; [& h: }; H/ _
// order of iteration through the `heatbugList' will be& M3 w* |3 \9 C+ ~$ {
// identical (assuming the list order is not changed
/ c7 m% Z- ?3 n // indirectly by some other process).8 R0 j X* u/ C3 m, I+ L- l/ Q
9 ?' c; m! z' B' V modelActions = new ActionGroupImpl (getZone ());( ? c# P% m2 Y- Y2 X M
3 T* ]9 @* s* X$ s5 _ try {" Y. t4 j$ Q8 N K- n4 R
modelActions.createActionTo$message
0 R& S! F0 @+ p" f (heat, new Selector (heat.getClass (), "stepRule", false));
. e, e1 e: s( ^7 J. P) M( v" k } catch (Exception e) {
9 T; M* W3 r; B6 l2 g! l System.err.println ("Exception stepRule: " + e.getMessage ()); B$ J' ^/ }& C/ g
}& l! K' r2 _) B1 @' y9 j
* H, P- Y/ N* i T$ | c, _* ?
try {
/ E/ H& S" e* A' P# n$ |; E3 } Heatbug proto = (Heatbug) heatbugList.get (0);4 d$ s2 C, ~& h3 l
Selector sel = 0 g* \; X8 I2 |' d. f1 v3 O" q
new Selector (proto.getClass (), "heatbugStep", false);
# C# d& W# I4 H9 ?8 c4 b actionForEach =
2 j. k3 ~7 ]" u- i* r `( d modelActions.createFActionForEachHomogeneous$call
6 B5 L& C: A! U6 I (heatbugList,
& a6 D! {$ e0 x. Q& [9 F/ D/ B new FCallImpl (this, proto, sel,2 d- l9 p. `( M+ x; r
new FArgumentsImpl (this, sel)));6 A# H5 h3 A. I& [( w. \
} catch (Exception e) {# c4 x. C5 Z2 j# p4 [5 Q8 ?/ d
e.printStackTrace (System.err);
( T! N# l8 O9 t x5 E }
: d* R" V% Y+ | ) u* q$ t' V- w: N1 e% ]
syncUpdateOrder ();
+ B! r- L" M' I) P" Q
" t8 L9 Y$ H8 c8 R; n3 }1 A1 n try {
" v& g3 I- O$ T modelActions.createActionTo$message * {: L4 t- V3 f; B, X! l
(heat, new Selector (heat.getClass (), "updateLattice", false));: g- w7 ]7 _9 D2 N) r# ?1 _
} catch (Exception e) {8 M h C1 I) r5 J1 _- L
System.err.println("Exception updateLattice: " + e.getMessage ());
K9 W; [! `# B& r, l }
8 ~; F, i- X. o r6 {! r$ E3 v8 B " Y% i$ ?' e4 Y
// Then we create a schedule that executes the
/ n/ Q5 N" Y. a) U- e- b. I // modelActions. modelActions is an ActionGroup, by itself it
9 h! C8 Y# O+ a# E; y0 Y // has no notion of time. In order to have it executed in
8 }$ ^7 H0 Q, X; H // time, we create a Schedule that says to use the# ~, x8 l# \4 Z) p9 G* j* ~0 u( i
// modelActions ActionGroup at particular times. This
7 P4 z+ ~/ N L) s8 M // schedule has a repeat interval of 1, it will loop every8 P0 X( `- { V( e* C
// time step. The action is executed at time 0 relative to
& E ~' F* g9 z8 s4 N // the beginning of the loop.
4 G+ q4 t- S: D2 n/ t
, O" a- X/ _. `- p1 k // This is a simple schedule, with only one action that is
2 \( l- v& z/ d% B+ _ // just repeated every time. See jmousetrap for more4 e2 q" B* z4 e( I
// complicated schedules." S2 u* j6 a7 W
' v$ T! L2 m) p modelSchedule = new ScheduleImpl (getZone (), 1);
+ n1 l8 b R- n+ S) ^+ U modelSchedule.at$createAction (0, modelActions);
- ?- e9 Z% ^1 N9 p/ o0 V4 f , \( I i# u1 |4 S
return this;; V+ n1 C0 G' c) j6 y; F
} |