HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! H% y6 y3 q7 N' z- S, M1 n7 j! P, C, o, @
public Object buildActions () {
1 R$ v! p1 {' n& t5 N: D super.buildActions();7 e' Q7 y9 Y( U {) X1 m
1 V) M% O+ P& U* }4 |! } // Create the list of simulation actions. We put these in$ g8 i) `# `) J+ \, z, q, i2 ^
// an action group, because we want these actions to be% X( f9 X: Q; x$ h y- B
// executed in a specific order, but these steps should
, y$ v, u# j& B1 L+ o // take no (simulated) time. The M(foo) means "The message
8 ]4 R! H# f8 ` @6 ]+ W/ C& H // called <foo>". You can send a message To a particular4 O/ R# y# Z( ~$ ]9 h) }7 T
// object, or ForEach object in a collection.% ?' {! { W2 G# Z- E" x1 \
2 l0 x! G% M7 {
// Note we update the heatspace in two phases: first run, s) @5 t! `; G. y
// diffusion, then run "updateWorld" to actually enact the8 {8 @' d4 x1 ]; @
// changes the heatbugs have made. The ordering here is' L, t1 m7 K6 r+ r8 T8 C
// significant!) [( O7 t& _. D' C- p9 c
# G: o1 y; [5 j, M, @5 M
// Note also, that with the additional
D5 W) ]7 H; Q2 e // `randomizeHeatbugUpdateOrder' Boolean flag we can
h ]' u* n( o" Q. Z6 }5 \( s2 c. \$ e // randomize the order in which the bugs actually run2 h* u q. M6 L2 S
// their step rule. This has the effect of removing any
; k7 N4 u' Q; F H( E& O // systematic bias in the iteration throught the heatbug
# w) t: C& Z1 y8 c/ Z // list from timestep to timestep
; O. [0 f8 A) \0 m; ?. s ; }. Z: A, I; i+ Q+ F6 d, |
// By default, all `createActionForEach' modelActions have1 F7 n, S ^! O t5 { W2 d9 r
// a default order of `Sequential', which means that the2 N5 J/ Q6 t3 v0 [# S6 H9 x( F
// order of iteration through the `heatbugList' will be
1 n) l' Q. h Y$ @% p // identical (assuming the list order is not changed. k' b' {3 E* i
// indirectly by some other process).0 k) v2 b0 E! h; y Z
$ B& u) O0 J* Y% S' c+ ]
modelActions = new ActionGroupImpl (getZone ());3 _9 n' b9 f8 m5 g6 w! P
7 p( Y V; _, n- W3 C try {# M" {0 p" v0 \% J2 B6 M# F. C
modelActions.createActionTo$message
+ I/ G7 n: Z& Y$ {8 u1 A8 G Z (heat, new Selector (heat.getClass (), "stepRule", false));
, R7 s( w2 N e } catch (Exception e) {0 P7 r% a H7 L- ?. \
System.err.println ("Exception stepRule: " + e.getMessage ());' n7 Z8 W# r0 I/ O
}
E. U8 o ^0 N6 `4 B/ ~
4 J) R; L7 u9 A+ N, w try {8 |/ o8 |; @& s4 y, n9 D
Heatbug proto = (Heatbug) heatbugList.get (0);
% L$ d+ o$ S! _; u; s Selector sel =
: l2 d8 D& V; {$ e. H# B& t new Selector (proto.getClass (), "heatbugStep", false);
1 H9 t+ {9 r' l; Z$ U( t/ a actionForEach =
+ r; @+ `# m7 s: G& T5 X6 P0 i modelActions.createFActionForEachHomogeneous$call8 Z0 a4 ^5 Z& I2 d9 i' l& ?( C
(heatbugList,$ x5 q3 J) |4 @. [" s3 B
new FCallImpl (this, proto, sel,+ s8 g" n" k8 T8 j
new FArgumentsImpl (this, sel)));! f8 y" p% R9 h5 {' A8 S9 \1 a
} catch (Exception e) {
+ o! o5 @( \& w9 \7 X2 F$ | e.printStackTrace (System.err);8 i$ W5 O6 m/ Y. y9 n9 Z- c4 g8 U
}# \7 X4 C% ?' F
, {/ \% I/ V. I8 S; Q! s
syncUpdateOrder ();6 {" b$ J0 t. X1 P& k; S6 V4 ^/ [
! {2 m# j8 y5 i& T: M0 m- @" R
try {
) d7 V1 |% h4 B modelActions.createActionTo$message % d' l; [9 a# w( c! u4 V; }$ B2 ~' u
(heat, new Selector (heat.getClass (), "updateLattice", false));4 G# z3 |. l5 j( c3 ]6 Z% B
} catch (Exception e) {
" P: ?3 E. |# x: ?% O System.err.println("Exception updateLattice: " + e.getMessage ());% r+ L( U% H% y/ o' b
}6 A0 _# @* A x+ v- C% I ]
8 J# s8 F" R1 z- m/ y+ X // Then we create a schedule that executes the, E/ K S0 l* {
// modelActions. modelActions is an ActionGroup, by itself it5 M- m3 P4 y. w+ ^% S3 e
// has no notion of time. In order to have it executed in
" W t- n0 H0 K1 `3 q // time, we create a Schedule that says to use the6 y6 F' r+ Z) `, ]6 \8 P5 S6 m
// modelActions ActionGroup at particular times. This+ Z( C( {4 Q* M B! O* C
// schedule has a repeat interval of 1, it will loop every2 e% n- v' b& I; I4 n: k. a# ]
// time step. The action is executed at time 0 relative to
' k' `- P7 x7 W) C2 r8 e5 I* m# L // the beginning of the loop.2 D5 k* ?: M# X
7 j% t$ M/ i! v* W8 K // This is a simple schedule, with only one action that is
! p2 ~% A6 m7 {5 P3 e" ?! T- z // just repeated every time. See jmousetrap for more
5 _& w0 X2 o) C) Y // complicated schedules.6 e! U( K2 }" ^0 h9 u- ]+ ~
2 i6 N7 P8 H, D/ K
modelSchedule = new ScheduleImpl (getZone (), 1);
U+ G* T' b' A }; N2 y modelSchedule.at$createAction (0, modelActions);
8 {& f7 E/ C4 O/ ~4 Y4 S
, R8 t! L8 `. W6 n return this;
6 D( z7 [' c/ o/ T3 G+ T9 c } |