HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:7 }7 H% [5 D+ m& Q; N& }
& W3 O0 L6 X. ?5 `
public Object buildActions () {- s( P; o6 R3 }/ Y) A2 t
super.buildActions();& z- N4 D* M. c, B
t0 [. G2 |# h5 U/ ?0 E // Create the list of simulation actions. We put these in
: K6 P& E: v! Z: j // an action group, because we want these actions to be
& k/ ?/ d, |% L$ a // executed in a specific order, but these steps should
f, q6 r2 h" N5 T# {% | // take no (simulated) time. The M(foo) means "The message9 z- ~! X0 S) `$ p) I7 D; H" r
// called <foo>". You can send a message To a particular* w# n; p3 B0 [0 ?$ G
// object, or ForEach object in a collection.
* d1 f) E: k& Q. w# t: P * D2 P7 r; u1 w @+ V
// Note we update the heatspace in two phases: first run$ X5 g; R; k# w1 z7 y( v
// diffusion, then run "updateWorld" to actually enact the
5 Y3 ~* ~" o' e$ J5 [* \7 W // changes the heatbugs have made. The ordering here is
$ _5 s k! A! r% i1 ~) k // significant!
( X9 o) q) n+ @% O
1 q# L9 i8 D/ S // Note also, that with the additional
5 |% T% K9 @+ Z. z2 k/ {1 P$ {7 Q // `randomizeHeatbugUpdateOrder' Boolean flag we can
% s$ u! P2 R. J$ a2 U) z7 j/ I4 N, A // randomize the order in which the bugs actually run
& L# l- L$ \# U1 Y7 U // their step rule. This has the effect of removing any
4 ?/ z! _0 g m2 q // systematic bias in the iteration throught the heatbug
* D: I# d) z9 U3 K4 V; V // list from timestep to timestep
7 q+ F# f; q' X, ~! I # Z1 h3 V( k3 M8 R* C
// By default, all `createActionForEach' modelActions have, t$ H. P. a. z' s& @+ D# ^
// a default order of `Sequential', which means that the
8 E" ?1 f* R5 r3 c# |+ l5 J& m // order of iteration through the `heatbugList' will be) R3 T% I& g* b
// identical (assuming the list order is not changed
: Y, @$ K N3 R$ r // indirectly by some other process).% G. h5 z4 O0 ]" R1 _
/ p2 C1 o: x6 d+ {& w6 Y modelActions = new ActionGroupImpl (getZone ());6 E, @0 n, p8 N, l
( d' g6 h5 p8 u5 D( X
try {) X M6 m" W/ a3 g8 R. y8 S
modelActions.createActionTo$message
/ T P$ U* ?1 R1 A! j2 B. o! O& Y# c (heat, new Selector (heat.getClass (), "stepRule", false));4 X4 y9 ]6 Y' \+ \- K; p# t2 W
} catch (Exception e) {' _9 n& H1 T, d+ B
System.err.println ("Exception stepRule: " + e.getMessage ());
5 B3 A) u- v- g/ ^7 s }; H4 Z2 R" T% z6 j7 H
, L+ y0 e/ G1 M9 J0 J: N
try {4 z: f3 N* h6 U7 X1 L) ~' z
Heatbug proto = (Heatbug) heatbugList.get (0);
2 T6 C& l) |+ ^0 `& l* E9 B Selector sel =
3 y+ S% s$ d# @3 M$ Y" l9 A! S' v new Selector (proto.getClass (), "heatbugStep", false);& i7 B s3 M% L$ g; A
actionForEach =- u# k1 j" e: O& ]2 S" I
modelActions.createFActionForEachHomogeneous$call
+ H4 Q, s! N2 E8 f Y1 {- g6 F% b+ v* v (heatbugList,9 W2 r n2 I4 O9 ^1 b! r5 A; V
new FCallImpl (this, proto, sel,) K7 F7 s% o6 L. }, |
new FArgumentsImpl (this, sel)));* l6 X* C0 F5 H' J0 L
} catch (Exception e) {0 H* ^) {/ c# b( v# _+ J9 {) i
e.printStackTrace (System.err);
7 K0 @ T; w+ |$ F: `* U4 G# |, b3 ` } m0 d5 E0 O5 R$ v( P, h! o e5 T
: P4 ~9 e- T! o. G+ [
syncUpdateOrder ();
) l/ u$ O' C+ X9 n1 Z5 }% A/ }
2 n" M/ m7 V% a' r1 R1 \ try {8 |# f+ [% T# R
modelActions.createActionTo$message - S2 L3 W" C$ ]5 @. b
(heat, new Selector (heat.getClass (), "updateLattice", false));6 |9 H* H$ w% Z' [7 C& P$ S
} catch (Exception e) {
% H) `% }1 I$ j, K System.err.println("Exception updateLattice: " + e.getMessage ());8 i0 s. w: u1 K5 m0 D
}
* P1 @4 A+ _# G: I; v) Q
: b' @9 G0 N7 q9 e* r9 c4 J8 { // Then we create a schedule that executes the' o/ }/ Q8 o b# ?" \" ] z# T
// modelActions. modelActions is an ActionGroup, by itself it R$ q0 K9 c" I: V1 P$ l) M/ C( I/ Y
// has no notion of time. In order to have it executed in$ f* _! M" H! t ~ O
// time, we create a Schedule that says to use the/ {8 [& }6 R9 M: B) }5 F0 o
// modelActions ActionGroup at particular times. This
9 o0 G7 W# q: x% d' N // schedule has a repeat interval of 1, it will loop every
% {" `& L/ i5 {0 w+ \ // time step. The action is executed at time 0 relative to
5 X; M7 |. @1 v+ a/ c // the beginning of the loop.% V1 d4 O# D% w" D+ p) b
1 w( l! a+ u4 ]3 m/ k) k# \2 e
// This is a simple schedule, with only one action that is
9 N' E! Y- Y g! R // just repeated every time. See jmousetrap for more
- Q g' g9 ^7 E' g // complicated schedules.$ S Y% m/ J3 d! e7 }
! C V% Y8 V- i
modelSchedule = new ScheduleImpl (getZone (), 1);, z$ g. q L/ C1 n! }7 R" T# k
modelSchedule.at$createAction (0, modelActions);
2 F% V. u' d% n
0 i) ^2 J; B! J) N# h3 | return this;# j/ v8 T G0 n$ \; c
} |