HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
& k: F+ G3 z! ~3 e
; M7 X: P- e7 Q: x* F( Z public Object buildActions () {
" N& l& p" N" k; B! m8 t. G5 ], z: U super.buildActions();9 x( J% p# z9 ?0 X5 E6 S8 V2 t# P/ C
3 Y& M. l: @8 `8 T' {& } G, n- l/ J7 J // Create the list of simulation actions. We put these in
R9 H0 J4 z. \# z: Y, m3 E& n& Q // an action group, because we want these actions to be
9 u. Q% [, Y8 |4 ` Q$ D // executed in a specific order, but these steps should1 E9 _" x2 Z2 V _. k! @$ Z9 u
// take no (simulated) time. The M(foo) means "The message Q& l5 E- z; R& M" H
// called <foo>". You can send a message To a particular
3 L# f) T7 p# q // object, or ForEach object in a collection.
7 ]/ O* L ]2 n/ ~: S9 [) ^
4 s+ I3 t+ W2 M k J! L3 u; E2 l // Note we update the heatspace in two phases: first run( e' N5 {# {& z6 U5 T
// diffusion, then run "updateWorld" to actually enact the7 A* P8 F, P. Y5 y, k& T- X' e
// changes the heatbugs have made. The ordering here is3 a I: C* `; T8 i; M5 V O
// significant!
7 M1 }2 X) k: `; Y* K! f. ?$ t9 k8 T 7 L5 Q$ P3 m1 E B$ o& E, ~$ \
// Note also, that with the additional* V0 l' h! Q" L0 [
// `randomizeHeatbugUpdateOrder' Boolean flag we can7 k0 {/ ~ B4 p% y. M
// randomize the order in which the bugs actually run6 @, K2 B* w, b7 M w7 u# X$ X8 r ?
// their step rule. This has the effect of removing any+ V/ i( V7 x0 z3 c8 y d4 a0 @* {
// systematic bias in the iteration throught the heatbug$ l, p3 E0 H1 E2 K
// list from timestep to timestep* I2 D' P! U+ N `$ f
' N4 i: O9 a1 n
// By default, all `createActionForEach' modelActions have
! M! f# N2 q9 G5 s4 U0 V5 g) H // a default order of `Sequential', which means that the) t9 k3 P8 Z {0 u& p" u* [
// order of iteration through the `heatbugList' will be
4 E/ Q1 c# }0 H5 z" }0 y( ^5 n+ H4 M // identical (assuming the list order is not changed! O$ p; d e R5 k1 L: ?# c- a% L' D
// indirectly by some other process).1 ]0 {: _5 v3 v
8 v" A0 ~+ |/ ~: F& o
modelActions = new ActionGroupImpl (getZone ());
0 i; N" i7 @7 o' L, [
4 v5 a& _7 d% U$ x, \! u* b try {
2 L; a9 d3 ~$ s, A I9 Y- c" o/ N modelActions.createActionTo$message7 K1 n0 A$ ?4 D
(heat, new Selector (heat.getClass (), "stepRule", false));
3 Z l/ X8 p. u9 K } catch (Exception e) {
& q/ j0 l0 ]9 G) L( e+ j' z* N' r: B( C System.err.println ("Exception stepRule: " + e.getMessage ());; ~6 ^; T8 K" s) o* ]; _* u
}1 l" Q9 M9 @, u# p% K
- t& d K! W, w4 h; H7 f5 p try {
( n' D: `9 l4 F: m3 H Heatbug proto = (Heatbug) heatbugList.get (0);6 d; j1 h, ?1 c8 E6 p8 ~5 o
Selector sel =
6 E* M# s2 {, f new Selector (proto.getClass (), "heatbugStep", false);! U' M0 I- K; J" @4 d
actionForEach =
) E! g! p) h/ d K! b modelActions.createFActionForEachHomogeneous$call2 u' h0 T0 D# {! o
(heatbugList,5 j/ j" K" `" f* |4 _ h6 {
new FCallImpl (this, proto, sel,$ A1 J# ?0 l: Q; S
new FArgumentsImpl (this, sel)));
8 W5 n- w$ t" e, t } catch (Exception e) {
# |+ J$ n0 }, M8 o" s9 c/ N% O K" Y e.printStackTrace (System.err);. Z; F" k, v$ G1 I1 _
}
% I/ d; l1 F; V; m& E5 i 1 P5 b- @2 f# R4 w* h5 l0 Y# F
syncUpdateOrder ();2 q1 F0 k' d/ [& L% C6 X1 r
8 f; l2 t3 ]& S+ w8 B5 j$ G
try {
- _ P( `- d; E$ F7 y$ | modelActions.createActionTo$message
0 `4 s/ f; ]! p, k (heat, new Selector (heat.getClass (), "updateLattice", false));
8 m' [# S. i/ t1 J4 |0 ? } catch (Exception e) {
5 n- q$ j8 H; C2 }# Q System.err.println("Exception updateLattice: " + e.getMessage ());2 M8 P/ H, h" x# P2 p( i
}0 l: N4 Y. P5 @- \
6 l$ h9 [) W0 O
// Then we create a schedule that executes the; L6 L2 N* ~$ S- g& ]$ r
// modelActions. modelActions is an ActionGroup, by itself it
5 {) \! {9 S" n G // has no notion of time. In order to have it executed in
& |/ e0 T- W& I7 o // time, we create a Schedule that says to use the3 ?2 {6 Q, f f- B, N0 A" n
// modelActions ActionGroup at particular times. This
% H) X" f- |9 ?* X" |% \ // schedule has a repeat interval of 1, it will loop every
7 {8 n) {* n8 A+ S( ]% ^7 k // time step. The action is executed at time 0 relative to7 u: x% J9 b P
// the beginning of the loop.0 G. c9 G; M6 _! Z- `$ S# e1 O; Q: p* t" L
; S1 [9 n9 l* c$ b: m
// This is a simple schedule, with only one action that is- @/ `! o/ z* E3 _
// just repeated every time. See jmousetrap for more
- j" j5 O$ W. ]8 j; T' ?# h$ b5 v // complicated schedules.% _ m) E' M/ n* q9 N5 V
: Q% ^: }; I2 z* N modelSchedule = new ScheduleImpl (getZone (), 1);
9 m4 w# O! s' I, J% a# { Z modelSchedule.at$createAction (0, modelActions);- z7 D. ]3 r( F( [( @4 C1 O
) @9 ?- a3 O2 q return this;
+ l0 P. a5 G D7 W+ Q: o7 M } } |