HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! o( `& ?# f: j8 K
3 E0 c# }. r% ~& h" Y& q public Object buildActions () {
* e6 I# g% T* B+ ?: V2 i super.buildActions();0 C- Y! u) J3 L
1 t3 j4 L1 c5 k5 _' E( m) f. }! }2 ] // Create the list of simulation actions. We put these in
% B, t$ \+ v) D) P# l5 G // an action group, because we want these actions to be
+ ~4 R* ~* N( z5 x6 w // executed in a specific order, but these steps should
% u4 c6 f4 ]8 F- v3 } // take no (simulated) time. The M(foo) means "The message
% Q2 P1 x! j# v" ? // called <foo>". You can send a message To a particular. y- q- v% [4 d0 @; _
// object, or ForEach object in a collection.
' u: a, T6 F; I. t+ t7 M
/ r+ p( q* W/ P, X7 L* k9 S // Note we update the heatspace in two phases: first run7 q7 z, ~( _, O1 G) V9 e: C
// diffusion, then run "updateWorld" to actually enact the u' `+ Y# k8 b- D6 H! i# k
// changes the heatbugs have made. The ordering here is
" d6 q6 N$ a+ i& K // significant!0 B* f% M6 s: Z+ B; o; w
% e% e! v, b _1 z6 j // Note also, that with the additional) \1 |/ ?! u* a. e4 p+ {
// `randomizeHeatbugUpdateOrder' Boolean flag we can. Y# t6 D! U) P. L* {! j! [
// randomize the order in which the bugs actually run
8 k3 c, H! E1 Y& ]/ a/ L; u // their step rule. This has the effect of removing any, [7 U6 J3 G+ P8 b/ G
// systematic bias in the iteration throught the heatbug
/ Y' A% y* ?9 {- _6 g1 Z5 R+ t // list from timestep to timestep
) A7 V; M* K4 G. Y( [# p % Y" I4 {+ `9 o* h; i
// By default, all `createActionForEach' modelActions have* g# c5 s4 ]( z. ^4 M
// a default order of `Sequential', which means that the
7 \1 I% a3 I9 H // order of iteration through the `heatbugList' will be4 |4 i# ^9 k# L+ {4 i( {& z
// identical (assuming the list order is not changed6 U) X) U6 r+ g: J: D' x
// indirectly by some other process).# S. F- X% I8 I* L& v2 q3 B
+ q& O5 E' A: R" \/ P# ]2 p modelActions = new ActionGroupImpl (getZone ());
3 G+ P) |3 N: [1 P1 e
1 S( u! a+ l0 A) O; y2 r: m& @1 t- X try {- t) _; Z- P0 A# v
modelActions.createActionTo$message
1 y( _, ?! _: ^) T (heat, new Selector (heat.getClass (), "stepRule", false));
: G( X* ~: \5 L% e2 |$ K } catch (Exception e) {* q9 X# p, N# R- E
System.err.println ("Exception stepRule: " + e.getMessage ());
) e8 F6 t. P' C$ s }
' C( s: U: o' u) V$ v, I. I1 V T, P: ^, B# ^! @5 ]
try {
+ B/ _8 V7 ^& M- o! U3 X4 ~* X7 U. O Heatbug proto = (Heatbug) heatbugList.get (0);
- o5 B1 G9 A* v- @3 J0 I H Selector sel =
% p) }+ q5 [) e, ]# ~, p new Selector (proto.getClass (), "heatbugStep", false);
' x3 g1 w e5 z5 e% _ actionForEach =
3 `0 M. B/ ?" ~" F" [- g: y% Y modelActions.createFActionForEachHomogeneous$call4 v" y, Y! [% x
(heatbugList,; R4 k; P% B; S9 w! Y, y
new FCallImpl (this, proto, sel,
# v/ e* H1 O' J7 ^3 h) h/ \) n new FArgumentsImpl (this, sel)));
0 O K6 a( ~, m* o$ Y) y1 u } catch (Exception e) {3 @5 ]5 |8 A8 B/ M
e.printStackTrace (System.err);
. v# m, f$ V8 F8 E }( P" z# l- w4 M+ E
' {0 l& j7 `1 E# a' E0 e syncUpdateOrder ();( j7 E9 b0 y/ t5 I& U$ Y
: B$ j& E$ I, g/ P8 @
try {
$ j* z' t5 i6 I3 P+ P. \* W modelActions.createActionTo$message - M) K" H/ I. N7 T- E- \: C
(heat, new Selector (heat.getClass (), "updateLattice", false));
~5 B- Z1 w8 p5 s/ x% ^6 |$ g. O } catch (Exception e) {5 o, m) U$ u; |: l7 Q
System.err.println("Exception updateLattice: " + e.getMessage ()); Y! @$ X/ {3 z# O7 s, h
}
" j- X1 ^7 ~+ H8 ~% t8 L# O
5 a7 Q/ F/ A2 f3 I2 |0 P9 H // Then we create a schedule that executes the
" z6 H+ c+ j. I; X- @' R1 B // modelActions. modelActions is an ActionGroup, by itself it& P6 a6 V$ S' E* ~! C. V5 K8 k
// has no notion of time. In order to have it executed in" q7 F2 f6 m' N. T7 \
// time, we create a Schedule that says to use the P6 e/ X/ w% A) C3 f
// modelActions ActionGroup at particular times. This- H8 h( X7 I, U; Z9 w* D% D5 r1 ?' N
// schedule has a repeat interval of 1, it will loop every$ P/ f8 `3 }$ G8 a1 b5 t
// time step. The action is executed at time 0 relative to$ G, E7 [8 a' q% l. h& K
// the beginning of the loop.
& o+ f7 z' B/ I- K; g" Y2 o! l8 y4 D- p( V, {5 H' J
// This is a simple schedule, with only one action that is/ M; t2 C8 b5 n2 F7 ]8 ? p
// just repeated every time. See jmousetrap for more" P. p& u6 D7 Q5 y- U* A; R
// complicated schedules.. C/ E+ h3 D5 q8 ?0 d
. Y& `# Y# _, k, P- V
modelSchedule = new ScheduleImpl (getZone (), 1);
- E: n+ u7 x8 D9 N- F# @ modelSchedule.at$createAction (0, modelActions);/ u# k {7 c! u0 o3 B6 r
& e5 I' a7 t# @) X return this;
& f6 Z% ?4 N# f& B } |