HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:% n: v5 V" g& a$ \: b
# [: u t$ K4 t! T6 E/ y
public Object buildActions () {
/ G+ t M( Z. |- Y super.buildActions();
5 _! V& k: o. ?& I
( S5 p5 N0 A" {7 i7 ? // Create the list of simulation actions. We put these in
( c' j+ T& z2 k) |* n2 v& S, ^ // an action group, because we want these actions to be1 f3 n b$ R3 e" \$ Q5 G0 u
// executed in a specific order, but these steps should
, `: @3 s2 H1 e" i8 P // take no (simulated) time. The M(foo) means "The message
* e1 T) p: O& l% I; L // called <foo>". You can send a message To a particular
/ O" h$ I3 {1 ~4 }2 W3 f // object, or ForEach object in a collection.9 K3 F0 o7 k' z( i0 o& u3 }
9 w" m# t0 Q3 j) d5 j% v // Note we update the heatspace in two phases: first run
" t" {# M% ^& c3 U( N1 o |) p // diffusion, then run "updateWorld" to actually enact the
8 O0 E, `- s* |, p3 R: N. [' y5 E& f/ @ // changes the heatbugs have made. The ordering here is( a; K1 `& ^- G1 h1 P, c
// significant!3 i' k( h6 N" n9 S9 e8 t$ ?* n/ a' }
+ E0 B! c( T' l
// Note also, that with the additional% I2 z! F6 n5 C* G) J
// `randomizeHeatbugUpdateOrder' Boolean flag we can6 ?* p* I, ?0 K+ ] o h; F0 u
// randomize the order in which the bugs actually run" v1 m; ]5 y9 O) K/ @
// their step rule. This has the effect of removing any
( E6 U' O. G0 H, I0 O // systematic bias in the iteration throught the heatbug8 M9 H" y# W2 t& n# D
// list from timestep to timestep4 a$ Z% S7 ?9 j, R
% E1 |; I6 O7 W" D* j- o h# c6 ~ // By default, all `createActionForEach' modelActions have K" e, q: ]7 z1 C$ Q* e
// a default order of `Sequential', which means that the
- D2 G1 x1 Q: w- F // order of iteration through the `heatbugList' will be
8 H3 X" U C& C* a/ L& z% U# ` // identical (assuming the list order is not changed) [ h* u' }! V9 t" `" b
// indirectly by some other process)., ^* c0 T F6 O/ x0 y: {
1 N# F% s+ f, r0 `7 H3 G+ T5 q6 y- u: X$ T modelActions = new ActionGroupImpl (getZone ());
$ G& z. p+ u4 j6 W3 D0 B* _- Q
. n% N; ^3 e/ P2 T8 ~% T3 b try {! f9 N3 H0 p& A. g: e! y: e2 T
modelActions.createActionTo$message; ~0 R; q% i0 P$ \
(heat, new Selector (heat.getClass (), "stepRule", false));
5 i! a- x$ ?: y } catch (Exception e) {
$ G( Q7 n& [1 c6 S" \; \8 C8 f System.err.println ("Exception stepRule: " + e.getMessage ()); I& S7 w+ ] h( ]
}- l" D( ?" M8 S. T& Y
; k7 {& t- q! {- R1 e try {
8 G, g- d; r k. `2 \ Heatbug proto = (Heatbug) heatbugList.get (0);
d7 I2 N: M+ c3 X: i Selector sel = 2 S+ g' @7 v, B. u4 D" q
new Selector (proto.getClass (), "heatbugStep", false);
5 G/ d* c6 x/ U9 f3 Q5 g I; p7 B9 C$ Y actionForEach =
6 I: Z! _; f$ B5 ^$ H' s modelActions.createFActionForEachHomogeneous$call
' y: S# [4 G& c6 K1 s! J5 \ (heatbugList,
2 p2 H9 P" j$ k8 k+ s b new FCallImpl (this, proto, sel,
$ G6 z0 i& f7 u# U) _ new FArgumentsImpl (this, sel)));
& f5 y6 R w( b) v } catch (Exception e) {+ u) I$ L3 \( l. M) n% z
e.printStackTrace (System.err);
2 h0 {7 U5 ]8 k5 ?% {1 ^+ V& ^! r }
7 k! Z* z' \: o7 k7 j8 O
4 \6 x1 l# n, P% t: @0 k Q syncUpdateOrder ();
3 j5 ~7 |7 }% E/ U9 R, `. U% G6 z4 J6 o9 J$ H% \8 ~7 U
try {
9 N( t* D: _; ~ v$ t1 M modelActions.createActionTo$message * E$ `) j3 h* n, p# b3 T. { ?
(heat, new Selector (heat.getClass (), "updateLattice", false));
- W2 s: F5 d# \6 i8 |/ @5 C } catch (Exception e) {6 p) k6 F/ Y' @% v" m3 u
System.err.println("Exception updateLattice: " + e.getMessage ());( I( }* _+ g4 Q6 J, R/ ^
}
+ [2 M3 X `: q+ R9 g+ ~2 O5 a + p- o' r& C5 `1 l4 k! d' _
// Then we create a schedule that executes the9 W3 a! H, z2 r# H% ~7 f" ?( a
// modelActions. modelActions is an ActionGroup, by itself it
: F* B0 Y' Q) O& f // has no notion of time. In order to have it executed in7 w- T6 p/ u: Y/ _" ^! `7 ^# b
// time, we create a Schedule that says to use the
6 v" v' K0 }* Z7 w; d5 O // modelActions ActionGroup at particular times. This( Q. W, W* N. M3 V! O
// schedule has a repeat interval of 1, it will loop every
' ?; A4 h* U/ [ // time step. The action is executed at time 0 relative to
) L1 i) [2 {$ [3 r4 y // the beginning of the loop.
: F4 M5 v7 Y8 j+ A1 H8 O, \. [0 Z3 J- H4 S" ^% r. Z
// This is a simple schedule, with only one action that is l4 _3 X) I" I' @( }/ r
// just repeated every time. See jmousetrap for more* E1 A: N; ~% {5 J' a& p
// complicated schedules.
1 t$ Y: W, q* M$ A6 x: A * Q1 O$ C/ f) r5 A- N, I
modelSchedule = new ScheduleImpl (getZone (), 1);
: u; ~1 i! @* X) i- V) J5 K modelSchedule.at$createAction (0, modelActions);
/ i- q# u2 x) ?& c 0 z$ d2 L* ^) e' a8 D+ ? n
return this;
8 u9 x( ^( a# V" [" c, T } |