HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
* Y9 L4 G# r7 ?3 `+ n7 b& a; Z0 X1 g) A* {7 s9 V' m% b3 |
public Object buildActions () {
" Z2 a( r9 \; S" V0 J+ n( B- }3 ^ super.buildActions();
& d$ l. {' v* z5 G U% Q8 B
7 h5 f) R+ u& `" h1 G // Create the list of simulation actions. We put these in
2 q2 \3 A+ ]: x9 J8 T, q$ `$ Y- K // an action group, because we want these actions to be. S# ]/ I3 J+ u1 w5 y7 s5 c
// executed in a specific order, but these steps should
6 P' a/ n/ c+ U$ V9 ] // take no (simulated) time. The M(foo) means "The message
& E' @' F" s0 K, ?2 P // called <foo>". You can send a message To a particular
( p9 ~! Y O$ \. N& O // object, or ForEach object in a collection.
3 y9 @+ z+ [$ B% f/ _: f
* X; k2 C" {2 } // Note we update the heatspace in two phases: first run4 d; j& n* N; e0 o5 W" }8 W
// diffusion, then run "updateWorld" to actually enact the
' V& _, ?, H+ p // changes the heatbugs have made. The ordering here is6 [2 s+ q4 Z- c: X& s( a9 T# }! [
// significant!
" \* o, P" l% C
+ K4 V9 [. d9 F4 X3 i // Note also, that with the additional3 I' E3 U, S. G# J% b
// `randomizeHeatbugUpdateOrder' Boolean flag we can
) @" E. |3 F# p" M" {( `) }/ h // randomize the order in which the bugs actually run V4 Z7 y- V+ p Q8 c1 v
// their step rule. This has the effect of removing any
7 x' i/ J2 \7 I# t! W // systematic bias in the iteration throught the heatbug
6 M: O% S; n" w" I5 X" Q3 W" M' _ // list from timestep to timestep' m% A4 q" T% F% [# n( k' [. L
; J& i3 L2 ?- Z, \7 w3 L1 ] // By default, all `createActionForEach' modelActions have: [3 ?! x4 r7 p8 Y5 B
// a default order of `Sequential', which means that the# L2 {# z6 {* n
// order of iteration through the `heatbugList' will be5 O' W1 K' h( g& |# r0 O# y
// identical (assuming the list order is not changed
' k$ R- Q: _% c8 c // indirectly by some other process).+ H; f8 \6 U; W. s. F5 O
6 _: x0 \- _4 t: k7 K modelActions = new ActionGroupImpl (getZone ());' o, U7 G$ e5 S3 f4 K
' n8 A; j! z& D3 i) {9 A) J3 X
try {
7 q- U# W V8 v: O1 z# N. h modelActions.createActionTo$message
; R5 |; y9 O4 ^5 p& G6 P4 O (heat, new Selector (heat.getClass (), "stepRule", false));
0 r* O. k+ k: e# B+ J1 _6 w; d" S } catch (Exception e) {/ b) e. T! f4 `7 W
System.err.println ("Exception stepRule: " + e.getMessage ());) ~& \! v5 f# _6 @
}6 l9 g% t! E9 d
* x7 O V O4 H9 |2 M, P try {
5 k& i( A, l7 m; Z. Z Heatbug proto = (Heatbug) heatbugList.get (0);
. d* q& r% x6 M) [, N9 E+ ?% T Selector sel = / A6 d% P8 H+ U- t
new Selector (proto.getClass (), "heatbugStep", false);" }, K$ B+ S, F6 x7 a* E5 P( B
actionForEach =
' e5 K3 }& I, T modelActions.createFActionForEachHomogeneous$call
M! s, k0 n& ~2 X) I (heatbugList,5 K3 ?9 j$ f- [+ O( c
new FCallImpl (this, proto, sel,
- j, J. c! N7 X* K. W# }% _ new FArgumentsImpl (this, sel)));
& Q: }4 n: B7 F5 g/ H } catch (Exception e) {! a1 n7 {* r8 y, x( m" d
e.printStackTrace (System.err);* J: D4 K5 T: x& I, T
}# t" e$ p* `. E8 \% u( x* J
: ]# c4 |9 `2 @1 A8 T( h syncUpdateOrder ();8 ]2 c3 P8 I4 C
8 ?, b1 R& |/ ]1 U* T. n$ J7 c try {: X3 B) ]4 _$ L3 v L
modelActions.createActionTo$message
9 M, j/ K! r$ \ (heat, new Selector (heat.getClass (), "updateLattice", false));3 Z; i9 K6 k5 E0 W
} catch (Exception e) {
& v r& |$ u* d1 C System.err.println("Exception updateLattice: " + e.getMessage ());/ w4 y0 G! N$ t1 M
}$ E3 F/ B& H. e# x+ B% O
! G4 M( P% m6 W; t4 k3 C" i! j
// Then we create a schedule that executes the: @3 x5 `' M$ i' {+ u: f3 {1 O
// modelActions. modelActions is an ActionGroup, by itself it# T2 D* E+ I/ i
// has no notion of time. In order to have it executed in! @5 W1 ]6 z {
// time, we create a Schedule that says to use the
- |: `$ |7 }0 J. H% }# _. W! M // modelActions ActionGroup at particular times. This: j9 X" T( p4 M; m- T: A& j6 {9 c* _. j
// schedule has a repeat interval of 1, it will loop every
9 \! M; J: y. p; n // time step. The action is executed at time 0 relative to- P; Q6 t3 w1 `
// the beginning of the loop.
) C5 g/ O& n' B
0 X. c5 H8 E# V) U4 w% E // This is a simple schedule, with only one action that is
$ D! b& x+ O% }/ X- A: C* J; ^ // just repeated every time. See jmousetrap for more/ F8 ?# Z# d$ r" w9 o' G
// complicated schedules.
4 [. e7 U- E1 q5 F6 Y" x; T# Y4 X ) Y: l! t0 f1 w& B/ c4 U
modelSchedule = new ScheduleImpl (getZone (), 1);0 i! e/ |$ H% X6 c6 F" F
modelSchedule.at$createAction (0, modelActions);! q/ E" W# Q8 J9 V4 p
, i3 z& R2 z% \1 R8 K; `0 Y return this;$ ~( `* I; J6 H" e% w: @" ]
} |