HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ I# y" M, k6 W( t z J+ @" n
6 `8 p: T; K; x3 v N8 g U public Object buildActions () {4 V9 u* B6 [# j2 d5 X; |
super.buildActions();
& K. q% I& m0 d" Q 0 J5 R& R# E* C5 [6 U5 `& j) C8 O
// Create the list of simulation actions. We put these in
$ W* ~$ U1 J6 m0 g* [& q // an action group, because we want these actions to be
- b& \& l/ S" J" [ // executed in a specific order, but these steps should0 Z; ~6 M# W. z' w+ C
// take no (simulated) time. The M(foo) means "The message7 _! x. I2 d! `+ F' Y3 n
// called <foo>". You can send a message To a particular& C* H4 e0 |: R T# a( G; h
// object, or ForEach object in a collection." [6 c0 w# N m
1 Z+ N6 |3 }( f/ }* R1 a9 x: Z
// Note we update the heatspace in two phases: first run
2 q/ ]6 {4 B$ j3 |3 x2 a, v, E // diffusion, then run "updateWorld" to actually enact the
, o! |$ _1 q3 F" R+ l // changes the heatbugs have made. The ordering here is; G' ^; }0 R9 X7 L9 K( O
// significant!
4 O T. s# E! @( q L0 z" x
& D8 {; O# r) Q1 S* E3 P# N // Note also, that with the additional
6 o- Z! _% ~% }$ h" J // `randomizeHeatbugUpdateOrder' Boolean flag we can' \: O" W9 n( |8 W
// randomize the order in which the bugs actually run7 m% Z. C8 f3 R$ B7 e9 Z: P: n9 }0 W
// their step rule. This has the effect of removing any/ Y8 D4 [8 P1 n! {$ C8 L; B& s
// systematic bias in the iteration throught the heatbug* o; C$ E) f- P
// list from timestep to timestep) \- V" `( ?+ s( H$ {
3 A9 _4 K/ O0 S
// By default, all `createActionForEach' modelActions have
# ]" D/ L( O3 ?; O0 q // a default order of `Sequential', which means that the
# w) d' a% L+ e2 x // order of iteration through the `heatbugList' will be
2 B P! k0 z- O; {, Z // identical (assuming the list order is not changed/ s* E4 G) R* U( s
// indirectly by some other process).
9 X$ N$ y2 Y" l2 K- k4 \ 4 @3 }% m" J* B& L: S+ q, A5 Q
modelActions = new ActionGroupImpl (getZone ());
# j, V- }" f# H7 U
% X8 x! L5 {5 G" o8 |( F) A( T1 E try {7 v" v( p: s! A" P3 L3 h
modelActions.createActionTo$message$ n" t- `/ E6 b Y- u0 E1 L e7 A
(heat, new Selector (heat.getClass (), "stepRule", false));
# u& F9 q/ j0 o! K8 N9 I) c } catch (Exception e) {4 b; I& T# ~7 x& o1 A0 O* _5 x
System.err.println ("Exception stepRule: " + e.getMessage ());& q* i, s5 n* Y; w3 P( G* }( W
}
3 Q6 p# |/ }6 ]$ \; _$ k8 e
# O6 ?2 {3 V) Y! k8 @0 k try {5 \+ U; K4 m, O* ]/ w' J- q8 N
Heatbug proto = (Heatbug) heatbugList.get (0);* j+ J- |1 u4 D- W V" _" P
Selector sel =
! q( J/ S* a0 P4 q& ~ new Selector (proto.getClass (), "heatbugStep", false);
* [! `! X4 I& d" E actionForEach =+ ?+ s5 g! ~) \7 o) }5 f
modelActions.createFActionForEachHomogeneous$call
& z) | X2 B+ Q (heatbugList,2 i' B6 w9 _9 \: G
new FCallImpl (this, proto, sel,3 R( `' o. E3 ~( U( E
new FArgumentsImpl (this, sel)));
7 h( q/ _& C$ N2 d/ A4 O7 c+ O } catch (Exception e) {
: Y8 f1 R( i4 K# J* J' j' f e.printStackTrace (System.err);
4 Z- v5 p' m5 M q% Z9 v% K; ^: F0 X+ x }- d/ O. u* a5 S/ s8 a+ I
& g) {, i0 X2 X) n7 [ syncUpdateOrder ();! o. i. x4 C+ m; q6 d6 X
$ _. Z! [2 t& ~ Y! v
try {
0 u/ C# q [, x modelActions.createActionTo$message 5 v1 ~$ a: G* ^
(heat, new Selector (heat.getClass (), "updateLattice", false));9 [, I7 `1 N ?8 l
} catch (Exception e) {, q' L& w0 F" y# a) Z2 z L7 k. w
System.err.println("Exception updateLattice: " + e.getMessage ());
e( d3 N: ~; B8 \4 P# |, Q# ?: @ }; b' N# L' o0 u1 F" r. @
4 |$ C3 G2 P7 ]' U% M1 z8 k- m+ V
// Then we create a schedule that executes the$ w- Y( l+ ]6 u; C) m
// modelActions. modelActions is an ActionGroup, by itself it8 P" ?" I/ O( s
// has no notion of time. In order to have it executed in
* |6 J, l! B) Z: W9 { // time, we create a Schedule that says to use the: x1 f) V3 K3 U7 \: K
// modelActions ActionGroup at particular times. This
1 Z, I' M* v$ H6 n3 c2 ^( s& D8 | // schedule has a repeat interval of 1, it will loop every8 k: q, o* P. ]' o
// time step. The action is executed at time 0 relative to
4 K. ~! k; ]: _+ a3 } // the beginning of the loop." _8 ~ S9 u% N0 {
# r& o7 ?3 ]$ s- b r$ Y // This is a simple schedule, with only one action that is; b. y" I; t' s- J4 e Z: }8 j# c
// just repeated every time. See jmousetrap for more
; i: Z" l0 ~5 J) T6 |+ v) d // complicated schedules.& F) \, P, @7 y* ~
& @- K* q) S% ?# }8 L modelSchedule = new ScheduleImpl (getZone (), 1);/ Y* H* M& K2 s1 @$ w
modelSchedule.at$createAction (0, modelActions);; U3 W6 {3 Y* W2 ]% R9 \# i
' Y7 S6 y m, P return this;
' P" z9 U3 A7 U' \' w } |