HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
" u2 K; i, o7 P) [" J
* ~, D# W+ g& h" h) N public Object buildActions () {9 l3 j7 \# |( H; C
super.buildActions();/ G- K" C3 i0 \4 U W9 i$ U+ d
8 x4 T( ]6 I1 w8 M // Create the list of simulation actions. We put these in; F* R9 M# M+ v; a2 A( `3 ^
// an action group, because we want these actions to be
; f+ C! b0 Q* t1 @6 @ // executed in a specific order, but these steps should
1 ?$ _3 h }0 N* ]% J7 q // take no (simulated) time. The M(foo) means "The message. g0 X$ g: H, @9 j+ j3 b
// called <foo>". You can send a message To a particular
0 O7 O! G! `+ [. R* Q // object, or ForEach object in a collection.
! P7 i& j, Y. V* y
8 Y1 d! Y9 T+ S [2 W // Note we update the heatspace in two phases: first run
4 X; T# \/ y: `' C' ~+ m* Y // diffusion, then run "updateWorld" to actually enact the& S% B4 C5 j7 R
// changes the heatbugs have made. The ordering here is- n; @0 q9 C. S9 A2 m+ K8 \
// significant! M) n. }' o1 k$ K( x- }$ r' J+ v- e
( k; \& U1 J: V
// Note also, that with the additional r9 e& k" j1 v3 G: X
// `randomizeHeatbugUpdateOrder' Boolean flag we can; K6 _4 ?: X V& f# ^% j" F
// randomize the order in which the bugs actually run
4 [. R; l6 z& w0 C5 U/ u! z+ E // their step rule. This has the effect of removing any6 @( P/ r+ C% z5 h% z* ^) b/ u- H
// systematic bias in the iteration throught the heatbug
* B. y! E# s7 \8 j // list from timestep to timestep
3 J* C# L$ M& y0 q& V# @
: K- \+ x8 ?6 ^- R# ]0 o% g // By default, all `createActionForEach' modelActions have
: y2 P; M6 _# }+ t5 c7 b // a default order of `Sequential', which means that the7 X+ C6 } s8 p$ p5 \
// order of iteration through the `heatbugList' will be
! a: ^) W) `" w* W // identical (assuming the list order is not changed
9 T# r" t( h8 y6 `6 Q! z" n) d // indirectly by some other process).9 T8 ^$ N; B7 s7 ]. ~1 @
, r3 d8 b! C( r5 m1 n modelActions = new ActionGroupImpl (getZone ());
" a; _* D2 a) Y
9 ^5 U; {6 [* \# }2 Z try {
' R. F1 Z; q7 |5 g [5 G modelActions.createActionTo$message+ a: U+ M* b& _( i1 M/ {
(heat, new Selector (heat.getClass (), "stepRule", false));
* `+ l: Q1 b( L } catch (Exception e) {$ d2 L. ~$ A$ \$ d% k1 p& R
System.err.println ("Exception stepRule: " + e.getMessage ());
) S; ~% T: ?# s; h: O }
7 e7 f/ e7 O3 C) S$ z9 H b3 G$ p2 f- W0 b
try {7 s8 s0 k) \ ~5 n# J; T
Heatbug proto = (Heatbug) heatbugList.get (0);
+ [( [1 g" A! X9 U Selector sel =
+ }$ g1 j. {+ u; F- e' C new Selector (proto.getClass (), "heatbugStep", false);
5 b5 ^' T5 m8 G) P5 {7 T' b8 G actionForEach =( E3 f! c7 D( g1 l& ^1 ^0 ^
modelActions.createFActionForEachHomogeneous$call
3 o0 o- E- E9 R/ D* z# X (heatbugList,
2 R0 p! J! c$ N3 K# H new FCallImpl (this, proto, sel,
0 b' j& D) _% B# J9 \& @ C. _5 d new FArgumentsImpl (this, sel)));
6 }% e' i8 U) s8 }/ b# `' s1 b/ v/ c } catch (Exception e) {
) r5 U: q1 p5 C7 j y$ Q( P3 z e.printStackTrace (System.err);
" a) T; ~+ k+ m; d% l/ g/ x }# s$ a5 o: H4 y- d5 Q
' A7 p: x" e! S' A+ X, x
syncUpdateOrder ();# @! I0 l9 c, Y# T n+ c
6 K; ^& U, v# x. } try {
, e7 m& [8 S( q! ]& ^7 c" j, w modelActions.createActionTo$message
: m, O+ H( F: j8 B (heat, new Selector (heat.getClass (), "updateLattice", false));, c6 m. A8 z( y0 S2 d8 p U, Q5 C
} catch (Exception e) {
5 s7 l: \% {- n3 K6 t System.err.println("Exception updateLattice: " + e.getMessage ());
/ y u# N0 V0 ^# }( b: A1 l/ e2 c }9 y" h/ Y* |6 k3 O* m( c
' ^$ f" C0 O9 L+ j1 A F
// Then we create a schedule that executes the
' h. Y; y" ~) ` // modelActions. modelActions is an ActionGroup, by itself it0 z* s6 r% Q3 |& C% j9 g
// has no notion of time. In order to have it executed in
" b6 j6 m1 R5 C" O$ M+ q4 h1 C2 e! a // time, we create a Schedule that says to use the5 k% X1 o" g% k6 W
// modelActions ActionGroup at particular times. This% Z' L: h7 O% o+ |4 V
// schedule has a repeat interval of 1, it will loop every
, k( R, ^( ]; I+ Y // time step. The action is executed at time 0 relative to
) p$ Q$ {8 }! p! J5 B) I8 [ // the beginning of the loop.2 o6 n4 @: |: { u W" P& N
g0 y- _) e/ L, e. L, g; a' { // This is a simple schedule, with only one action that is M4 d9 c: k9 Q
// just repeated every time. See jmousetrap for more' [5 P. r" \ V- q3 u' e5 k- a
// complicated schedules.# G9 H# d6 F C' s
2 G3 F: j8 y5 \1 ?. v modelSchedule = new ScheduleImpl (getZone (), 1);
7 S# s, k3 Q+ z o5 p modelSchedule.at$createAction (0, modelActions);" Q. x1 |! c$ Q: t) d4 |
+ f6 j3 u. D2 }2 l; g
return this;
1 ]( T) I9 O" }; f7 }' ^2 E2 N } |