HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
7 `4 P# k# L- j3 H7 t) O5 n) ^. x4 m0 Z6 X' E
public Object buildActions () {
9 ?+ i3 U9 X9 k7 D: i super.buildActions();( r# U- K7 ?, H. K* Y) @
) ]4 @1 E P/ V. }
// Create the list of simulation actions. We put these in
* _5 G$ j) N" P0 X // an action group, because we want these actions to be1 _5 N+ w0 w: F
// executed in a specific order, but these steps should
% Z7 @% W7 J( H3 z. q! O* b5 y // take no (simulated) time. The M(foo) means "The message- O6 |; F0 u' E# x& W
// called <foo>". You can send a message To a particular. j; w$ k! V1 S' P8 n
// object, or ForEach object in a collection.) s f# A! d a& ]+ w# `; [3 e; `
5 N% s2 i/ y# |# J // Note we update the heatspace in two phases: first run
4 a3 J. q, ^! i. W // diffusion, then run "updateWorld" to actually enact the0 M9 F) c& r- [
// changes the heatbugs have made. The ordering here is+ {/ y+ M1 m( C+ M
// significant!" s; V5 s/ ^5 A4 U
3 V7 ?3 c) x& d2 v' P // Note also, that with the additional
" [, @; |* e0 |0 [( j- o" r! k // `randomizeHeatbugUpdateOrder' Boolean flag we can
* e% O/ {. j1 }7 I; `/ f) O: l5 ~, [ // randomize the order in which the bugs actually run, [ H/ i1 p3 H7 ~: g
// their step rule. This has the effect of removing any
* b4 {- H: m* j3 {6 ?$ N6 w/ Y // systematic bias in the iteration throught the heatbug
b H6 Q7 D- ~( w% o% m$ M2 a // list from timestep to timestep
! r& c, H& t7 A. |, F# r * D# F& s9 d/ {+ f, A2 t& F
// By default, all `createActionForEach' modelActions have
0 z5 S6 t4 v- e( o // a default order of `Sequential', which means that the7 l8 P% j4 _! B5 @
// order of iteration through the `heatbugList' will be+ _) {4 Q! f' x2 Q
// identical (assuming the list order is not changed4 N$ r5 _" f5 C7 m
// indirectly by some other process).
0 m" p' t1 t0 d2 }. h) n# [" Y 0 }' u5 o. D0 W& \2 y
modelActions = new ActionGroupImpl (getZone ());$ ] q9 L/ [9 C4 E+ [- Z4 }7 H0 G
# r" d# V; Q, q6 [5 M; }
try {, e" ~' p( l4 b& r7 f* a+ e" E# z# N
modelActions.createActionTo$message
/ H: i/ L0 d2 l. B. Q (heat, new Selector (heat.getClass (), "stepRule", false));+ [# X5 w- s' S+ N% K
} catch (Exception e) {. N G. S6 c3 ]9 W* Z5 C9 U
System.err.println ("Exception stepRule: " + e.getMessage ());
6 @* ~7 s' S: x# z* | b }
- P3 y. v f H+ n% b6 Z3 z. w# k6 M- q8 T C- t4 O1 p! Z3 y
try {- h. h# B; |3 O" {! g! X& Q
Heatbug proto = (Heatbug) heatbugList.get (0);
0 `3 @! D6 O2 g7 j( | Selector sel = 0 g; g8 I+ H1 n( e8 n* }
new Selector (proto.getClass (), "heatbugStep", false);
: U0 }) |3 e( D) U: }1 o! y7 C8 B actionForEach =; b5 O! H$ M, r- u4 |
modelActions.createFActionForEachHomogeneous$call
- G2 T/ g& N2 ~& \9 X (heatbugList,
+ ~' G3 D( s" ?5 e2 e# P new FCallImpl (this, proto, sel,
% i) I) e/ J$ |# s& D& G new FArgumentsImpl (this, sel)));5 | U6 D' r" M( R! m
} catch (Exception e) {
, E+ b6 H$ q/ T* `7 V7 C e.printStackTrace (System.err);5 R1 T# S C4 A; @- Y& {
}
|7 t: M% b8 O& c/ N0 u/ e ~% V+ u- s q* g2 m5 o7 o
syncUpdateOrder ();
, s) L# T+ V* g$ C7 z. E* c% c4 [
1 ]& E: k7 Z1 r: t6 o) N0 R- y l try {
* P8 @; s/ X9 f& B# O modelActions.createActionTo$message
; m& P' }9 o' m n# \. Z (heat, new Selector (heat.getClass (), "updateLattice", false));
* C; }( Q2 r, v. l& i* `' Z* c } catch (Exception e) {
& k0 _2 V& i# |6 c) v System.err.println("Exception updateLattice: " + e.getMessage ());
5 X& W: V: Y' W3 x z' V }( |& d2 g; P S1 O5 x) z
5 d1 e0 q& E6 l U( `4 |9 i! H // Then we create a schedule that executes the; B0 K' r: e. ^0 O/ R
// modelActions. modelActions is an ActionGroup, by itself it" u& s) X: p4 T7 p# {1 d
// has no notion of time. In order to have it executed in
. Y5 _9 t0 {; l1 a0 [ // time, we create a Schedule that says to use the$ y. w+ D' @$ V. U( g' u- n: e
// modelActions ActionGroup at particular times. This$ n% [& b* i9 i$ N: u
// schedule has a repeat interval of 1, it will loop every
& o1 e* i4 u5 y A // time step. The action is executed at time 0 relative to( w" y+ ]) q9 ^! D P
// the beginning of the loop.$ ]2 G/ }7 Q, T) r3 d! V
7 Z2 E. J& R# q+ B% {+ ]" @; b$ S
// This is a simple schedule, with only one action that is
9 l% j! K4 z( H; A" \) J- j& c% d // just repeated every time. See jmousetrap for more
. s8 V1 h; d: e* W) U8 u // complicated schedules.
/ t/ R4 f9 F/ L" ^: |
- G( E, W6 k' r' o* G modelSchedule = new ScheduleImpl (getZone (), 1);4 m* W9 m' b9 }8 K4 A& c
modelSchedule.at$createAction (0, modelActions);
* A; p: Z) s& P R! z. |6 H) l
- ]/ y' v. c7 G return this;) `3 r: c) n& `, N* K9 r" f
} |