HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! [% b. y( m* p1 R, b( ~" N8 L
! ^) M8 T3 f) j) [; S2 [9 t public Object buildActions () {8 D* V0 e& o) s$ E2 X. ]3 g8 z ?
super.buildActions();
( X' }: v: y1 x% T0 r L7 P
$ m& S! c; L" G2 W8 W1 \& R3 ~9 W // Create the list of simulation actions. We put these in
# c5 F* `; g2 |1 r // an action group, because we want these actions to be
1 b4 s, d1 ?* b3 Z. M4 `3 h/ T // executed in a specific order, but these steps should
1 o: b. n2 c( n0 w // take no (simulated) time. The M(foo) means "The message
9 O: w6 k0 E& j' J // called <foo>". You can send a message To a particular5 Z. J9 o: E$ a1 D0 Z O
// object, or ForEach object in a collection.
% `4 K# F' T2 o# f1 Q, [ : b+ g4 e6 |! P) L% G
// Note we update the heatspace in two phases: first run; |& ?' p5 Y8 L. K3 v
// diffusion, then run "updateWorld" to actually enact the
; a; f' g. D+ K( z // changes the heatbugs have made. The ordering here is
" O! R) }' H9 f' h // significant!
6 i! Z4 [3 Z: E6 n
* S8 }9 b5 M# B: g6 _ // Note also, that with the additional7 _, K0 Y1 y; L p1 G
// `randomizeHeatbugUpdateOrder' Boolean flag we can
2 e" w& f& t4 \2 {3 j- F // randomize the order in which the bugs actually run
: \# I! l: I& ^% T3 p8 ]: K+ S // their step rule. This has the effect of removing any
* k3 I9 W0 H( |" A // systematic bias in the iteration throught the heatbug
2 Y/ @; r; \6 e- t+ s // list from timestep to timestep
0 J) L1 _+ B! [$ ^7 {, G/ s
; V! V- \# M; x- u7 U // By default, all `createActionForEach' modelActions have
! D4 [( |# `: {0 G, p1 \ // a default order of `Sequential', which means that the
9 L1 Y2 z, |/ |& B3 L // order of iteration through the `heatbugList' will be
7 }: {0 u& m* L# r6 j% K // identical (assuming the list order is not changed6 T. g1 z, |* h2 Q* F6 n4 E
// indirectly by some other process).0 R, n/ B4 K. n* z& R
6 D/ Y- A9 q7 e$ ]" g* [3 ] modelActions = new ActionGroupImpl (getZone ());1 q; P( W2 C9 @; U7 o2 T6 [
# S3 R* b: y; Z% z5 [
try {7 u4 x" f* C9 @. I* a
modelActions.createActionTo$message
" k& [1 c1 Y$ `$ p (heat, new Selector (heat.getClass (), "stepRule", false));
5 _# j, Y0 L/ \& Z" F! l } catch (Exception e) {
1 e0 I, `' v9 \/ O6 c; M8 V System.err.println ("Exception stepRule: " + e.getMessage ());5 x8 e% G8 \3 i0 b' a
}
; F' W5 o1 Z9 c# |, _- h: f7 N8 t, V* [! \) w: ]& f' s
try {9 }3 a2 Q, O; v; X% ?
Heatbug proto = (Heatbug) heatbugList.get (0);7 F. u! J/ D' @2 _
Selector sel = - A/ X* y7 V0 T
new Selector (proto.getClass (), "heatbugStep", false);
; Z M& c) _5 m) U; n5 z actionForEach =
: k) [1 U) P, l/ ]- _. l modelActions.createFActionForEachHomogeneous$call+ Z7 H5 X7 Q# }/ x, x2 [
(heatbugList,
$ j6 S' x T4 g3 @9 s! k) o new FCallImpl (this, proto, sel,: N3 U8 o; G& W
new FArgumentsImpl (this, sel)));
, Y3 X. @5 ~$ Q5 p- P* L } catch (Exception e) {: J8 [7 j/ f7 |) Y+ y7 F6 F1 h: Y6 J
e.printStackTrace (System.err);
3 S; Z; a/ e8 [/ ]2 } }
! X$ I" P& ^- ? 0 D0 N5 l( U5 F' ~# Z# a+ k- X
syncUpdateOrder ();
, n& j$ g* N3 N, P
/ q& t! N3 B r* l; Y7 ?1 j+ q try {+ ~" y: B; d7 L* q
modelActions.createActionTo$message " c/ S6 |- \: c$ {- [# u, G) O
(heat, new Selector (heat.getClass (), "updateLattice", false));
5 U- e, ~- G4 J" | } catch (Exception e) {- w. }: I6 }) X
System.err.println("Exception updateLattice: " + e.getMessage ());/ I; \( C/ @$ G( x1 g, x4 E) `
}
0 j, g7 V( H" r- i 6 f5 E8 j- {8 D g( X9 D4 g: W! f
// Then we create a schedule that executes the P; E, V& J# k9 ~
// modelActions. modelActions is an ActionGroup, by itself it" @- h0 ~$ M& G" D t0 t& V
// has no notion of time. In order to have it executed in
; e- w8 ?, e: g; d! {" U$ v Z3 F // time, we create a Schedule that says to use the
+ y3 f1 k; t/ `* O% j) ]. C // modelActions ActionGroup at particular times. This4 b% l0 T4 {5 @/ c( D7 ?' F
// schedule has a repeat interval of 1, it will loop every
, }8 j4 K/ J2 P9 ]2 a7 {. u+ o6 h- p/ t // time step. The action is executed at time 0 relative to/ s# T: W& s, ~& S1 A2 Z
// the beginning of the loop.
* p: |6 A+ {" B9 w# g; K$ @, Y2 o3 ^8 }/ J" \4 ]
// This is a simple schedule, with only one action that is* q# [+ i2 u# a) m
// just repeated every time. See jmousetrap for more2 j8 f: v" F( z; U% U% |8 ]
// complicated schedules.4 M; @% C+ b( C
/ l7 m# {9 ~; N$ o) W* U modelSchedule = new ScheduleImpl (getZone (), 1);" G; n2 O1 [3 o3 j0 \- y
modelSchedule.at$createAction (0, modelActions);: D5 ` @! c! x$ ?9 j, o# j
) X8 b; ?7 v% n& W) _
return this;
0 m( S. L3 Q* z6 U6 D9 y } |