HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
A7 x: `8 X2 V+ j3 Z1 y J# n$ P
public Object buildActions () {) i7 u- }, z: T5 H! }2 G6 A; _' a: @
super.buildActions();6 t# l: L/ W2 n6 U
$ M1 G5 W3 V9 o& q* b# O // Create the list of simulation actions. We put these in
- \3 B2 R Y0 u# |9 I9 o+ K // an action group, because we want these actions to be
5 ~8 N6 I/ g* @ // executed in a specific order, but these steps should- U6 S2 w, I2 f/ `/ g! T
// take no (simulated) time. The M(foo) means "The message
5 K4 i5 k |, o, [" b( w // called <foo>". You can send a message To a particular& S7 {" X% Y* m8 Z: v u+ o
// object, or ForEach object in a collection.; d# x: ^2 x. v% V8 e( ]# m
7 i! E# D4 B4 i& p( \' S // Note we update the heatspace in two phases: first run
6 ]$ U4 p% R; H) ^ // diffusion, then run "updateWorld" to actually enact the
! S) q: @3 `5 r/ i1 `# D/ V$ b* P) W // changes the heatbugs have made. The ordering here is
' X( X/ |" z: ^8 p f4 \# c // significant!
5 l0 ~( ?" t, R5 u: _% b 0 F4 g9 I c2 }; |+ @+ e( Z
// Note also, that with the additional& K1 m% U- P! S0 G0 C8 ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can
n7 T, x k, [! d/ z // randomize the order in which the bugs actually run
) c9 |4 F$ g- ?( U // their step rule. This has the effect of removing any2 j7 M( I8 _/ _
// systematic bias in the iteration throught the heatbug
; O# P# S* n# e: N% x0 _3 i // list from timestep to timestep7 ~1 x3 x: B" d1 }
, Y3 A! G7 q9 I0 }: m8 h' H // By default, all `createActionForEach' modelActions have3 C2 w) h6 b$ S w' p. i
// a default order of `Sequential', which means that the
2 n# d, V- f- H // order of iteration through the `heatbugList' will be
" @7 ^/ ]& N$ X8 M // identical (assuming the list order is not changed
/ O$ } F i1 c" l0 P // indirectly by some other process).
* V/ s) W! ]& ?8 g4 d3 R
% y2 B& t- F3 G e. t! | modelActions = new ActionGroupImpl (getZone ());
: ^- v7 S6 C- p# ?) Y$ }
6 h& u4 e+ [5 S2 ^ try {. a0 C2 X4 Q7 f& }7 F8 L* h& \
modelActions.createActionTo$message
- ]0 c" O! Q7 i; O (heat, new Selector (heat.getClass (), "stepRule", false));5 U9 v7 X. f% A6 s* x% @# z
} catch (Exception e) {
( u: o2 O7 s+ Q( J System.err.println ("Exception stepRule: " + e.getMessage ());
: ~6 m |5 }- H% [ }
4 i9 b% h) N1 | J, j0 [0 s
4 P4 V; ?$ D; R' E: _! W1 W try {9 S" E0 J8 J6 f' T5 C u( M# ]6 s
Heatbug proto = (Heatbug) heatbugList.get (0);2 m+ N( |% i! x3 X4 M
Selector sel =
2 |2 L5 c( Q, Q2 }# b new Selector (proto.getClass (), "heatbugStep", false);& Q1 c+ @7 v5 \, V, d. o
actionForEach =! I' A( M7 j: `2 |( C5 J
modelActions.createFActionForEachHomogeneous$call
+ }4 W" r* \; l& c8 G' |8 g5 D (heatbugList,
- o0 X* I2 I5 w6 w) k8 u- ?% K new FCallImpl (this, proto, sel,
/ {. _4 [5 ?* f/ p% f) O0 z, e& Z new FArgumentsImpl (this, sel)));
1 a: t& z4 n6 k# U. I } catch (Exception e) {
7 O* `. q% x# S! x! ] e.printStackTrace (System.err);
* k" c+ y. y0 t% @ }1 S9 O: o$ w4 ~ b
; L( J3 a* e/ w
syncUpdateOrder ();
4 [$ g2 V- G2 n% g$ C) W' |, C @# }7 l0 S/ `
try {
. H; d2 v# S8 m. b. Q modelActions.createActionTo$message
1 F( {3 J- e V) e- ]( T7 `# e0 k" }' B (heat, new Selector (heat.getClass (), "updateLattice", false));
9 M7 s+ W8 {3 P } catch (Exception e) {
, f* x2 }! Y) R. t u$ O$ H System.err.println("Exception updateLattice: " + e.getMessage ());
! G6 [( `( \, v x G }9 p* e9 } ?9 s
6 z6 @. g0 n8 G" e5 o+ J // Then we create a schedule that executes the
% L3 X: l: h* ?3 U // modelActions. modelActions is an ActionGroup, by itself it- h- x: T. J# U) G1 D
// has no notion of time. In order to have it executed in, i* Z9 h$ u. D9 c; ], ~' Z
// time, we create a Schedule that says to use the2 t4 h; B/ h' B3 Z
// modelActions ActionGroup at particular times. This3 b: d! G0 ]- T# o
// schedule has a repeat interval of 1, it will loop every
- w, A7 N$ S8 f8 k4 C // time step. The action is executed at time 0 relative to5 {9 x% A: W& @2 a" ]" |
// the beginning of the loop.7 u! `9 E" [" Z. n
$ Z& |: u1 H7 U" _ // This is a simple schedule, with only one action that is3 g. R5 x' T* L; L! E
// just repeated every time. See jmousetrap for more
' U+ E/ |' G7 } // complicated schedules.; N* C* e% g8 L% O2 G9 C/ c
# D! g" U5 t ~/ P$ W
modelSchedule = new ScheduleImpl (getZone (), 1);
P0 M' _5 G7 a modelSchedule.at$createAction (0, modelActions);1 [% A" R' e7 s: @/ F
, @% J1 \; z7 `7 m- q. ?3 x6 u- e1 w return this;5 s) p! U* v" I) L$ o$ Y- I
} |