HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: `0 R; O3 Q3 X9 \' b! l3 O: J0 N. L: l2 P) X
public Object buildActions () {
9 i, \4 ?1 g, b5 `; Y9 N super.buildActions();
6 d& [* ]$ G/ o% B1 i1 J3 |
. s7 a. l4 _# m( B& ?6 b) M8 [ // Create the list of simulation actions. We put these in
5 `: c3 i0 p [& t- x7 @+ w3 { // an action group, because we want these actions to be
0 s" f2 ]' w7 n# I( B. t // executed in a specific order, but these steps should) C) B/ u. L8 m1 H8 ~$ e
// take no (simulated) time. The M(foo) means "The message; ? W, [% ~& D- V3 P/ [
// called <foo>". You can send a message To a particular7 d5 X- @/ |3 _8 t; h
// object, or ForEach object in a collection.
1 e" c& `8 U% B) z$ u$ X1 k 0 R) {% l, Q0 L' u
// Note we update the heatspace in two phases: first run$ p- `) H, h' i7 N" z
// diffusion, then run "updateWorld" to actually enact the
8 L1 o8 R7 u! G // changes the heatbugs have made. The ordering here is
4 _, Q+ h, Y: {* d" Q$ d // significant!1 q+ j4 Q# a" b! }% C
$ c6 H$ V: E' ^4 r$ W
// Note also, that with the additional
: c; M# X" l. X6 ] // `randomizeHeatbugUpdateOrder' Boolean flag we can
! q0 G9 f7 }# k; r& { // randomize the order in which the bugs actually run1 b3 n: Y0 p7 N ?# }
// their step rule. This has the effect of removing any
! `; z& W) a4 s( m // systematic bias in the iteration throught the heatbug
$ y& o. A$ N! j // list from timestep to timestep
8 @" W9 @2 ]0 b; s% U / N" l' C7 R% [8 u" [. g7 O0 A
// By default, all `createActionForEach' modelActions have
9 }6 B2 r- v7 ~* q5 |. e* a // a default order of `Sequential', which means that the2 m1 X) n9 N; B$ ~
// order of iteration through the `heatbugList' will be
- v" I! r: E) T' h1 H // identical (assuming the list order is not changed
; T8 `( ?# ~$ [3 [1 ~- Y3 M // indirectly by some other process).; X5 w% c) [# E. D" H' n. C
% D" Q6 @. p( B$ e1 t% X9 E modelActions = new ActionGroupImpl (getZone ());
1 B D$ Q3 Y2 K5 `+ y, {$ R% l3 H! \& n0 ?6 }; ~. M
try {4 f# B: S/ V# k, Q. |3 R5 \" @
modelActions.createActionTo$message9 t2 o5 V- g9 R9 M
(heat, new Selector (heat.getClass (), "stepRule", false));- X) Q% _/ A$ p* D7 s5 L
} catch (Exception e) {
6 z/ f7 ?+ w X. m System.err.println ("Exception stepRule: " + e.getMessage ());( H: {( f0 C% u- h0 Y3 R
}) h$ F7 t7 {: {6 ?# B6 @( H
2 I5 _9 L, x/ ?' ^; n. |4 ? try {
" h: B! u" M V7 T. c* H9 H Heatbug proto = (Heatbug) heatbugList.get (0);
3 |9 W3 K. a4 _2 S( [ Selector sel =
! H/ s3 Q9 k, G! E( l! ?/ m0 h new Selector (proto.getClass (), "heatbugStep", false);/ A% `: `8 C1 }5 D& d2 Q
actionForEach =
' Q4 ~) q, {) J) T- P& u modelActions.createFActionForEachHomogeneous$call/ m% @0 v- I4 _
(heatbugList,
, t: P, L/ C5 C# P" g1 e9 F new FCallImpl (this, proto, sel,5 [9 ~6 b& C; o
new FArgumentsImpl (this, sel)));: _2 P- h5 ]3 q6 q; G
} catch (Exception e) {
6 n+ ^0 X4 N A, x0 r e.printStackTrace (System.err);! }4 p& B' Y& w
}9 |; o! z' Z1 y4 L' u( ^9 V9 H2 L3 ?
0 L- w7 S: D- b syncUpdateOrder ();2 C6 N R4 A1 {6 o+ X2 T
( B1 Q7 [+ C$ S4 C0 I try {5 I4 v. c9 I, f
modelActions.createActionTo$message ! g/ \( w* u! z+ Y9 Q
(heat, new Selector (heat.getClass (), "updateLattice", false));4 {; b/ j7 q5 |
} catch (Exception e) {& a3 [$ c, r* \1 [" x
System.err.println("Exception updateLattice: " + e.getMessage ());
: f3 \0 c/ i9 S" E$ E$ h" K- } } d3 s2 |$ q, Y# E
9 O6 T7 I' C, y9 w+ V5 B& t- } // Then we create a schedule that executes the" {- c- V( C' R1 l
// modelActions. modelActions is an ActionGroup, by itself it
3 [7 m2 e/ c5 _5 Q9 L. c // has no notion of time. In order to have it executed in0 ~2 m2 k# B6 O
// time, we create a Schedule that says to use the3 b5 a3 u9 {3 X6 P0 _* V
// modelActions ActionGroup at particular times. This
: \8 f4 ]* G* z3 o$ O, v // schedule has a repeat interval of 1, it will loop every# \: D6 S W B2 i! l8 n4 H
// time step. The action is executed at time 0 relative to
) s4 W* f5 s" p // the beginning of the loop.# [. k, L, L( \5 [
- o9 ~$ j- p% ^! [
// This is a simple schedule, with only one action that is
, ^. [" y! M* A- o# P3 x) L, I // just repeated every time. See jmousetrap for more$ q# c; @/ f4 B' |
// complicated schedules.; E; `( l! H4 p( O
% r! h0 g9 ^2 `; I r! ^
modelSchedule = new ScheduleImpl (getZone (), 1);
/ F1 h L5 n4 f modelSchedule.at$createAction (0, modelActions);
" M/ j$ Q/ ~+ u0 u U* B# Q& X( s
return this;
1 D W$ p: u4 y } |