HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& U3 n3 D% P/ y
( q' o" T9 j7 {: N public Object buildActions () {8 r% i/ @, Y# b' L+ `% i
super.buildActions();
3 S8 i$ g7 t1 [1 \1 c o, j! g2 F. K% Y
// Create the list of simulation actions. We put these in2 z+ W2 U6 B, r2 n
// an action group, because we want these actions to be) S7 l7 J) C& w1 L4 S
// executed in a specific order, but these steps should Z% P: _# e7 D
// take no (simulated) time. The M(foo) means "The message
' Z4 K4 c: ~4 i // called <foo>". You can send a message To a particular
+ D( j8 w3 g0 H9 V2 ]4 ^ // object, or ForEach object in a collection.
: P! s9 N" m* A* A/ X8 D
/ ?% v4 v% c/ o/ ?- z% G- V // Note we update the heatspace in two phases: first run' ^& |3 I7 |( f8 N& D3 \- x
// diffusion, then run "updateWorld" to actually enact the# v% h# t. M; t% M ^
// changes the heatbugs have made. The ordering here is6 W* m. p1 B$ ?2 F! Y( H1 R, T4 m
// significant!) W! O: S" t: ^1 ^* \2 T
% D, U6 h" i k- B" a
// Note also, that with the additional: L# p; L3 g7 N
// `randomizeHeatbugUpdateOrder' Boolean flag we can C+ B% I i' {* }* a) a2 M
// randomize the order in which the bugs actually run
( [6 R# a4 ^# l9 W9 K9 I' @0 M- O // their step rule. This has the effect of removing any
9 }. C1 F9 p K. M4 [/ j$ j! T" F // systematic bias in the iteration throught the heatbug
; Q3 X' d3 C& Q) L // list from timestep to timestep2 Z" N3 K9 W2 I! Q/ D( X
3 s/ A h& m# F; e& R( ~3 T // By default, all `createActionForEach' modelActions have9 K# R% B& N0 v. |, W: k
// a default order of `Sequential', which means that the2 t/ I. `) Z; q' T/ D0 d% v3 k
// order of iteration through the `heatbugList' will be+ ]- k& M" P2 G$ z
// identical (assuming the list order is not changed. ^ u' p# y) v" Y8 s5 I7 N6 Q
// indirectly by some other process).
$ w1 V3 Z6 Y5 ^1 u! y # R* J. u' I4 W) Y1 t2 w$ b. P8 K
modelActions = new ActionGroupImpl (getZone ());) M) E4 H/ k8 l0 r2 l
/ F( R/ s) Z% T; ^# z5 n try {
' K/ |/ s! q$ S& H E modelActions.createActionTo$message/ W6 z/ ~, k1 B( O- k
(heat, new Selector (heat.getClass (), "stepRule", false));
, r1 @. ?( s0 _: b$ p; {/ ^ } catch (Exception e) {* U, `9 @1 D r. A- ]
System.err.println ("Exception stepRule: " + e.getMessage ());, F" T7 T' J$ Y, j
}
3 v: N+ t# n+ d5 A1 e9 M$ v) t, C8 ~; s) p: @2 k
try {; { K( Y( Q8 @) `8 p) C! @9 t
Heatbug proto = (Heatbug) heatbugList.get (0);
" H; x& c- q# f" |; R) D+ a% y Selector sel =
! y# m% l Z5 z8 T1 i new Selector (proto.getClass (), "heatbugStep", false);
! _4 L: G- H0 b9 B# L( n3 t/ g) e8 u actionForEach =! r+ @0 \8 W. V, ^; a
modelActions.createFActionForEachHomogeneous$call
, M7 \# d' O k" A4 G! x (heatbugList,8 ^( ^5 i& g$ R; V
new FCallImpl (this, proto, sel," U& X4 X, y8 d8 m. p% {
new FArgumentsImpl (this, sel)));4 r4 L% j/ h& w! `# E
} catch (Exception e) {+ C2 K7 x" T/ @6 B; e; x
e.printStackTrace (System.err);
: N4 l! @) ]# [) _, q/ M$ ^, G7 _ }7 k& {% H/ V; A- p
+ u4 d4 c5 W1 k$ g
syncUpdateOrder ();
0 c y! \4 p3 P# }# Z
2 b& d1 }# j1 q: x# F; H, Q5 N6 {: E try {5 n, u5 d+ B, A3 }7 C
modelActions.createActionTo$message
5 c- L5 K' E! n4 g7 O" r (heat, new Selector (heat.getClass (), "updateLattice", false));1 G4 @! a/ w, a4 T, x7 N R$ b6 o
} catch (Exception e) {
/ \5 ^, l Z- f& s7 D3 o% c System.err.println("Exception updateLattice: " + e.getMessage ());9 X' x7 I' h" u# ~) m- l
}, n0 @ k% ?- ]
7 K; u: I7 g9 D2 T' {! k5 g
// Then we create a schedule that executes the
* D0 J5 o$ T$ }+ c2 ^# F# }+ [ // modelActions. modelActions is an ActionGroup, by itself it
8 m# C! {7 _4 Q! j ?! ^ // has no notion of time. In order to have it executed in: d( G+ k a* ~0 V! x g; w
// time, we create a Schedule that says to use the4 z. I* }$ N8 P- `* w+ g
// modelActions ActionGroup at particular times. This( |3 Y) u: |9 o% V! e+ d' j& n
// schedule has a repeat interval of 1, it will loop every1 O7 i& `' Z, r" k( c" E; S
// time step. The action is executed at time 0 relative to& U, U- D+ H5 O/ ?" c
// the beginning of the loop.
) J% g7 E2 |3 F. y7 n- q3 E% P
9 j2 G( n2 V3 m. F$ k a // This is a simple schedule, with only one action that is0 k8 U% ]4 u: H. d
// just repeated every time. See jmousetrap for more
: Z, i; F W5 T5 }# ^! L. k' | // complicated schedules.
/ H3 j) Q1 n, V$ y
- z4 G9 W6 c& X5 [$ S modelSchedule = new ScheduleImpl (getZone (), 1);
5 Z! u$ O* Z8 T8 {2 b) _! W modelSchedule.at$createAction (0, modelActions);9 s( ~) G# M: R$ J/ b
1 L" ^: T+ Z* P" Z3 z+ u6 P) n
return this;
$ H; G& v* O" ^9 N5 D } |