HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# J% n0 G- h6 Z! w+ @ r u
/ [* P) c4 U7 l0 o1 F" Z* o public Object buildActions () {
+ z7 {3 y, d0 v, `* T" o5 H* g super.buildActions();
9 {) n- h [$ }2 p3 _* z: W2 C3 W
$ F- S6 O9 Z4 [' P6 ~$ w // Create the list of simulation actions. We put these in
' t8 m- d/ F( `, Y // an action group, because we want these actions to be+ c! \$ Y+ o8 i5 k7 y
// executed in a specific order, but these steps should
) r! F4 ~3 a% E4 t# k9 h* u/ @0 N // take no (simulated) time. The M(foo) means "The message7 ?* a" z7 L% r9 \. L$ q6 L! \# i
// called <foo>". You can send a message To a particular2 L$ w2 a8 `1 c' B" p
// object, or ForEach object in a collection.
1 r) k% e0 h$ G5 B3 J ' k- ~2 y) M7 ^+ o& P5 g! e
// Note we update the heatspace in two phases: first run
4 e5 |/ K6 O1 d" n" o; x9 l // diffusion, then run "updateWorld" to actually enact the( D' u; v( f0 j, Q/ n* O( |' T
// changes the heatbugs have made. The ordering here is
2 D) H3 `) l7 b4 {* y- ] // significant!' b$ R' o% q7 y+ u. X! J" k
$ e G4 p2 N( G/ k7 ^
// Note also, that with the additional! |, I8 X; W5 I6 Y: h! j
// `randomizeHeatbugUpdateOrder' Boolean flag we can
) r% n! O" @# B% ^: U // randomize the order in which the bugs actually run
' z5 }! ?9 A6 u) ~; v // their step rule. This has the effect of removing any" A! p$ l6 g3 F' I* @- g
// systematic bias in the iteration throught the heatbug1 R- [6 D% _5 f: k$ \
// list from timestep to timestep: f" K- x1 L9 {9 I' Z" |9 T
. B( n' q# P, J: ^
// By default, all `createActionForEach' modelActions have4 k& I5 L) E* H# O0 z/ J7 W
// a default order of `Sequential', which means that the" s3 X: r8 i6 ]- \# x: o( y
// order of iteration through the `heatbugList' will be* a2 S, \+ x ~7 l- R0 i
// identical (assuming the list order is not changed+ W/ w# N! E3 ]4 V" R1 C+ S
// indirectly by some other process).4 y$ W( e1 ]$ [8 W$ K) w; s
( Y# R9 o. S" u5 U modelActions = new ActionGroupImpl (getZone ());
3 \/ f* @ O9 S8 |( Z: q6 j, P! v# a# K: y' _6 c0 J3 |0 Y
try {
+ E _7 F6 p8 g5 `7 C+ i) g modelActions.createActionTo$message9 z4 B& g/ Q5 Q4 [4 w4 i$ o
(heat, new Selector (heat.getClass (), "stepRule", false));
$ X7 Z" ^6 D" I1 G. Y% Y } catch (Exception e) {
6 d* c7 T$ {/ m2 s$ K( l System.err.println ("Exception stepRule: " + e.getMessage ());6 O" x" j1 t% T. e$ B, W _6 m" R$ G
}% o, v/ b' `5 f9 I+ \
6 C" d9 x! M; `3 d) L5 w
try {4 t; _8 V6 I) w* p
Heatbug proto = (Heatbug) heatbugList.get (0);" j- P6 N0 r; S2 A8 c$ O
Selector sel =
: W' u, T( ^3 a5 N7 `( T new Selector (proto.getClass (), "heatbugStep", false);- j( f$ S9 \- e% C9 ^
actionForEach =
9 l2 e2 e9 L4 F modelActions.createFActionForEachHomogeneous$call! r8 U F4 S- Y- Q( T, n
(heatbugList,
Y; B9 k2 L* P7 Z& C4 b new FCallImpl (this, proto, sel,( i8 }! y6 Y" u, G" a* `
new FArgumentsImpl (this, sel)));- `8 z' i/ c) Y$ q. d
} catch (Exception e) {% j: V. ? K( Y4 P6 w3 `% V7 e
e.printStackTrace (System.err); h7 D9 _4 g! I/ y0 C) E0 i7 t c
}' [$ n* `9 Z' k3 w
5 T i* T# v/ {# G$ d( F3 k
syncUpdateOrder ();% l. u- R: `. i& r1 s( N
- t8 b" P; m1 y5 [1 V+ {
try {9 |* {! \4 D# ?$ b
modelActions.createActionTo$message 6 [. P: ?2 `) R# Z
(heat, new Selector (heat.getClass (), "updateLattice", false));
0 b E4 o0 q. c, X9 ~5 a' o- G } catch (Exception e) {
7 i, _" k2 P8 ]) T& X0 a. J& A System.err.println("Exception updateLattice: " + e.getMessage ());
3 f" V( _9 }! G1 j; s9 Z }
3 |0 a8 V8 Z" P4 ]2 _
* K6 ]8 v0 A0 F2 |7 y9 h2 i // Then we create a schedule that executes the
/ R8 n" e* L% L; N9 Q+ n/ _ // modelActions. modelActions is an ActionGroup, by itself it; d/ @6 |) P$ {* S* g
// has no notion of time. In order to have it executed in# W2 U! ^" L1 j4 U( n3 N
// time, we create a Schedule that says to use the
( p, p/ U @0 y/ |6 y+ ?& v // modelActions ActionGroup at particular times. This
0 J* W0 J8 K- ^+ l& a1 Y7 M% W // schedule has a repeat interval of 1, it will loop every
8 x" A! r, g1 I) a ] // time step. The action is executed at time 0 relative to. R% x' B* I" q8 H
// the beginning of the loop.
( G4 Q, [$ k; t- U6 _2 J9 s0 P% z7 F8 N1 U
// This is a simple schedule, with only one action that is# O" s2 W) g+ E: A+ o% W
// just repeated every time. See jmousetrap for more/ t4 x8 E4 f/ v; @' o
// complicated schedules.
5 \0 W2 a2 W! p$ t3 v9 g o/ Y; p
6 q' f3 S, h' ]0 h3 n modelSchedule = new ScheduleImpl (getZone (), 1);( o4 n0 M* Y6 x P
modelSchedule.at$createAction (0, modelActions);$ F* R4 T# U& d( d. n# `7 H8 z; w: [
: X! K/ L* [( s; X$ c! `& d+ M1 k
return this;5 D6 @3 L5 ^+ H% j; p& t( N
} |