HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:5 c/ v8 n- d k! A9 s
# \& x9 C9 U9 n+ { p public Object buildActions () {# g; P( `3 Z# U: ~
super.buildActions();
/ x0 H! N, c1 D1 S4 H; ^
/ e8 @- K! [# u+ _! P4 J _/ ` // Create the list of simulation actions. We put these in
' l v0 R2 N" J! i- V# P' e // an action group, because we want these actions to be" x" g& Y3 ^5 q9 e' Q
// executed in a specific order, but these steps should
P0 q2 l8 A# ~5 [6 W // take no (simulated) time. The M(foo) means "The message
; T, \* b( I% \2 A0 Z9 w // called <foo>". You can send a message To a particular
" p3 _( s+ d; U% k2 E' S+ M // object, or ForEach object in a collection.9 D5 C! ~5 ~$ U8 q
( n, _ P5 \# q
// Note we update the heatspace in two phases: first run- B+ {1 h9 ]7 g
// diffusion, then run "updateWorld" to actually enact the
! v% ~! D8 i3 {5 E4 ^, F7 x0 s // changes the heatbugs have made. The ordering here is
1 i$ G0 V& ~5 V- v g' ~1 c // significant!0 c& p) E6 X0 E$ Z
/ P* P) C4 i c9 O7 j, X6 L // Note also, that with the additional
$ e2 _; u3 y- |& X7 w // `randomizeHeatbugUpdateOrder' Boolean flag we can( [. Z& D$ U3 N2 Q2 d
// randomize the order in which the bugs actually run
% P7 R9 H. M6 E8 a // their step rule. This has the effect of removing any9 }9 |" o8 @+ g7 ~
// systematic bias in the iteration throught the heatbug
( C/ s3 U7 P# C1 G2 O0 u // list from timestep to timestep
! t; y& E2 I3 o7 p1 y& n9 D. m , }3 N5 l+ U/ O2 E0 i7 g
// By default, all `createActionForEach' modelActions have8 @9 E7 Z# x+ e: s% C% E' f, y; O }8 ]
// a default order of `Sequential', which means that the
7 R/ y. C5 G' r // order of iteration through the `heatbugList' will be
5 W2 b* P/ l! F& z7 W' f- ?: [1 r- S // identical (assuming the list order is not changed5 g! T3 u- l, C/ k8 g
// indirectly by some other process)./ \8 } E$ ~. e; R b
- X1 d; C' d9 g: @ modelActions = new ActionGroupImpl (getZone ());
0 Y. Q. q u5 n# @3 U" Y
8 [' T# I$ [2 l+ w, w3 p/ H" s try {8 w' v3 K8 N# d" Y2 h. }, A$ N
modelActions.createActionTo$message
# D1 z8 D) Z9 ^* {/ G9 E. t (heat, new Selector (heat.getClass (), "stepRule", false));
4 V3 A- C! R8 U G- }2 | } catch (Exception e) {
; w0 |1 T0 j: Y2 z7 B4 C: `2 G6 c System.err.println ("Exception stepRule: " + e.getMessage ());$ t& D# q$ x* J% g
}
6 F# w" R; {6 J0 B( a$ a/ K# G( c( v6 W6 k D% ^$ B3 n
try {
* a- P( t. e; Y3 P" ~9 g0 J P Heatbug proto = (Heatbug) heatbugList.get (0);: L# U2 V; }& }/ t' X1 I
Selector sel =
' W5 ~. u0 E6 q8 n2 o new Selector (proto.getClass (), "heatbugStep", false); y/ p' t& @3 T% B
actionForEach =) W }4 }2 p+ s. X2 {) v( z! F& X
modelActions.createFActionForEachHomogeneous$call
# V* M* p% [6 R. ` (heatbugList,) F4 q- C2 [+ M
new FCallImpl (this, proto, sel,8 [- F+ G: M2 D7 q- K! g
new FArgumentsImpl (this, sel)));9 h( j7 p8 `' d& R
} catch (Exception e) {- c7 D! Y' b6 @; R
e.printStackTrace (System.err);# b/ h: C3 C- d* l
}
' b. M* j6 q' O' w, E : t% _4 q5 j/ c, G; _
syncUpdateOrder ();' H$ N) t/ `; z* B
( ? ?9 ^+ Y& ?1 w1 U6 ^9 P try {, J0 t" x3 Y" p# t+ Q4 _% X# S; Y
modelActions.createActionTo$message ' v5 H2 |6 v: \4 U
(heat, new Selector (heat.getClass (), "updateLattice", false));
: Z/ C: T; j+ y } catch (Exception e) {
) `; u2 e7 l6 e3 ]6 P& U/ k2 r System.err.println("Exception updateLattice: " + e.getMessage ());
8 z( Z3 B: O9 h$ R }8 ]5 _$ ?8 s! V; l$ D8 c
( h& N/ {( _* y // Then we create a schedule that executes the
# P* p% i7 Q* S2 l9 w1 t4 A // modelActions. modelActions is an ActionGroup, by itself it
2 h4 y6 }' I1 M // has no notion of time. In order to have it executed in3 J& }. J5 D+ `8 C& U/ w) J
// time, we create a Schedule that says to use the
2 l: [7 l2 p/ T5 A' l! C- a3 m* v // modelActions ActionGroup at particular times. This
, u! I( v6 `! O/ L8 E) ]0 a // schedule has a repeat interval of 1, it will loop every
; C+ g! O: Z# @: \8 c/ r // time step. The action is executed at time 0 relative to
% X& ]; K. Q# m9 T // the beginning of the loop.
; E* u' P6 k4 B" n6 p. E0 M8 _, q8 S: u$ L* n
// This is a simple schedule, with only one action that is, h+ j d4 b$ D* I+ A6 g
// just repeated every time. See jmousetrap for more
7 c% b# w* q$ }. r }- [ // complicated schedules.; \1 Q. x, s/ `" u3 ^
6 V3 j* _9 Y# x" B1 b4 |
modelSchedule = new ScheduleImpl (getZone (), 1);
: C" z' K: d T% ^/ T7 I) ~0 R7 ` modelSchedule.at$createAction (0, modelActions);% N3 ^, I2 c" i' \, ? V
; T0 ]: J1 c" G% U5 C% t! m, v! V/ y. ]1 T
return this;0 i3 G" X5 K7 p% |- M' w
} |