HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; X* s" h: M; q# D" d" _
: p7 }7 x5 `! A* Z3 y5 Q/ G6 c, q public Object buildActions () {4 q1 ]/ V1 B- S1 q, K8 x
super.buildActions();, m' i' z* A+ K8 } A* z: T
7 e B1 u6 t9 ]* I // Create the list of simulation actions. We put these in) r) W# m* |+ b3 f- Q
// an action group, because we want these actions to be
0 j$ E" L+ N4 W+ A // executed in a specific order, but these steps should
2 p7 h, ?7 w* k# U W* C // take no (simulated) time. The M(foo) means "The message6 v( b& J5 b6 S% y% j$ @0 H. q3 S7 o
// called <foo>". You can send a message To a particular- a$ k- K. ~! q; s# m
// object, or ForEach object in a collection.
3 I% }' k1 K8 q& F# p
" e4 C" T% E' P! E6 e // Note we update the heatspace in two phases: first run( {; F% Q8 r% x; k( X$ O
// diffusion, then run "updateWorld" to actually enact the0 x' ^. x6 t' ?% Y9 t7 Q! u# s
// changes the heatbugs have made. The ordering here is
3 @' K8 k5 j+ y* x6 K; O$ C // significant!( Q: Z# R8 z: D6 w$ l
( `+ \9 v) @1 o) h* u" J F6 d7 y: ^ // Note also, that with the additional
8 @9 t. q% K2 F5 o, |! m& ? // `randomizeHeatbugUpdateOrder' Boolean flag we can
! P6 M7 w7 z# @% h' T4 U // randomize the order in which the bugs actually run! Y& `0 D0 l& L0 y9 i8 d- d
// their step rule. This has the effect of removing any
2 w1 |* V; ~% U0 p. B // systematic bias in the iteration throught the heatbug. q6 J0 j# Y0 o
// list from timestep to timestep' l. H. y T7 o( _- `# S% N1 \
* g4 e) W E! o7 H, @+ s, z // By default, all `createActionForEach' modelActions have
9 V" a0 _7 J6 K0 ], E5 y // a default order of `Sequential', which means that the& Z U1 C4 r3 K0 j; G: [/ L0 `& _5 z
// order of iteration through the `heatbugList' will be, Y8 ?0 ~6 q5 O
// identical (assuming the list order is not changed4 ]7 ~. ?& {" w) u; H
// indirectly by some other process).
; k5 g$ Z& j# A / Y$ H* y! N# j {0 k& ^2 I
modelActions = new ActionGroupImpl (getZone ());
: z! I6 D9 c$ E `3 x$ w( k% z. S
try {
6 G P8 t0 n+ t- r4 z) r* T modelActions.createActionTo$message
- }9 J% v& |+ Z% f) W- [: c (heat, new Selector (heat.getClass (), "stepRule", false));$ t; [, f: N K- ~% L, r$ a! {4 K
} catch (Exception e) {
5 b. t- l! F+ ^7 n x1 d4 w System.err.println ("Exception stepRule: " + e.getMessage ());
, s9 ~8 `; J6 f. }; J' k }
# U; l! l& p* z7 i' C- y! M# K. _0 T5 k) E
try {* W8 {) d& R# D' h
Heatbug proto = (Heatbug) heatbugList.get (0);4 p s$ W/ P% C. e0 Z t/ o; C
Selector sel = + v/ I* o- G& N# Q& h2 B9 u
new Selector (proto.getClass (), "heatbugStep", false);
% j# V' x# Y5 Z" E actionForEach =
/ G$ z0 c+ Q' a; q. j4 O modelActions.createFActionForEachHomogeneous$call
6 x. [- d* ?" y* h (heatbugList,4 J: v2 F' N8 I5 M9 E1 {) V% `
new FCallImpl (this, proto, sel,
& p2 ?# y& q0 w5 P2 y" U3 r new FArgumentsImpl (this, sel)));3 k! F! T2 E# [( k1 g
} catch (Exception e) {2 z. Z0 A' }) r; [
e.printStackTrace (System.err);+ u% s$ {' B3 H1 l8 d8 M& O
}) o- n% Z$ d- X6 v. j
' u b- @# }$ w
syncUpdateOrder ();
% l% n, h$ ~* `! i) J
7 d( k9 {; S1 q5 l: a+ t/ j0 V2 ] try {3 l0 {! Q+ u5 s5 [/ l
modelActions.createActionTo$message
! |/ {& V- _4 p, v7 ~0 `- E5 `: | (heat, new Selector (heat.getClass (), "updateLattice", false));
1 Z* Q5 N0 z- v3 w: b } catch (Exception e) {
) L$ f8 T# k+ ^3 r System.err.println("Exception updateLattice: " + e.getMessage ());
: j, u4 N, o7 l2 A6 [! K* a }2 J4 [/ `. A, r! c1 N
' k& B$ u( R- @. N6 c5 W
// Then we create a schedule that executes the
5 u' ^! a9 w% e$ | T8 T' ?# E' T // modelActions. modelActions is an ActionGroup, by itself it, y1 g7 C% h! P) \; s- m
// has no notion of time. In order to have it executed in9 }* g* J3 r0 u
// time, we create a Schedule that says to use the
2 J% X9 |+ C! b% ? // modelActions ActionGroup at particular times. This; x6 F$ V; U7 n9 _" _. r, J
// schedule has a repeat interval of 1, it will loop every
, P e' A, y6 l6 L0 l7 H // time step. The action is executed at time 0 relative to
. e- W- A) W$ q# R% C // the beginning of the loop.
6 {: @$ N4 o) u( \5 D# ^( L
$ l: b8 y1 S& r% X. s7 D // This is a simple schedule, with only one action that is
0 U$ H+ [6 A! N7 o5 z4 X- w3 }) { // just repeated every time. See jmousetrap for more
9 m0 z; `: p/ ~5 r, s // complicated schedules.! f M/ E4 ~/ ?/ `; d# }
+ z% d" ?2 m+ s" G1 _- j modelSchedule = new ScheduleImpl (getZone (), 1);. q$ b2 w( |6 L+ i2 Q( [, k
modelSchedule.at$createAction (0, modelActions);
( x# H6 I; @. n5 j/ E, ^/ \ % x$ J. ] ?3 H/ K9 L
return this;3 w z N' i1 H, @2 y$ ~ ^3 e
} |