HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: {+ j. u* S0 r4 C0 r; ^3 v9 e+ m% g( h; N
public Object buildActions () {
* d6 a$ p9 r/ m' s/ t6 R super.buildActions();2 c( }' R3 o- h2 n: E
% d3 K* \9 B5 o! n) s+ N# E* _ // Create the list of simulation actions. We put these in
9 @3 |' Y/ I# L0 ~3 E y // an action group, because we want these actions to be) j* C% \9 R1 U. T0 R
// executed in a specific order, but these steps should
4 g& R, H/ P% @: I7 N // take no (simulated) time. The M(foo) means "The message! w3 O9 q; l* y( Q$ g6 n
// called <foo>". You can send a message To a particular- f* N* T- |& d$ Z) A" d1 n
// object, or ForEach object in a collection.' ^& p4 }) N+ O" m0 a* R* A
9 ~: Q, h2 U$ h# V r" @# S
// Note we update the heatspace in two phases: first run( K" D2 G/ m# H& B
// diffusion, then run "updateWorld" to actually enact the
( f8 K6 I o1 C6 ^ // changes the heatbugs have made. The ordering here is3 R0 i8 a$ p' M# g
// significant!
; u3 d( C8 @+ F. h, {3 U$ p . j v) I) W4 [/ ]: T4 ~/ _
// Note also, that with the additional
; p; J1 U+ {7 Z# n0 r) Y, r // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 o$ S. N* Q) u( S // randomize the order in which the bugs actually run' ]9 K9 a& b$ y" v# p
// their step rule. This has the effect of removing any7 P" F7 ?. p/ t( ^# O+ `: q
// systematic bias in the iteration throught the heatbug
% \( x. g1 Z, a // list from timestep to timestep
1 |1 H# [6 V' q' e7 `$ c* t$ Q
: ?$ W/ x. D/ h3 w3 i6 F0 M2 ]9 A // By default, all `createActionForEach' modelActions have
/ V x) N4 P" E6 Y! A // a default order of `Sequential', which means that the. j3 F( E. G3 p8 [/ b! ?
// order of iteration through the `heatbugList' will be
( I( F; V" S' [- Z t- l L9 a' ~ // identical (assuming the list order is not changed/ k- y4 z& Z% i/ j+ M& W
// indirectly by some other process).
1 D/ C9 F$ U9 {1 J" h( M 8 w7 J0 t% Y, U! i! f8 M
modelActions = new ActionGroupImpl (getZone ());
4 I) ^* c8 }6 X3 r1 v0 V1 p! n. R5 r" V+ t" }4 a. F, l
try {
& Q" b4 A+ X% A3 S9 }1 t- G' i modelActions.createActionTo$message
; o2 a# ]+ k3 y6 N6 @' [ (heat, new Selector (heat.getClass (), "stepRule", false));
2 i& Y& v O( e" E } catch (Exception e) {
7 g9 m' {. B# E5 N$ Q System.err.println ("Exception stepRule: " + e.getMessage ());: v& G: Q) m" W1 I6 [
}
5 U" v/ E& ~6 K) o0 G
5 m; B% E% `- b' a/ ^9 b8 j6 y try {/ {1 ~" G8 ~, d8 v3 `/ U2 T* J
Heatbug proto = (Heatbug) heatbugList.get (0);, P: w/ X, L# K
Selector sel = 8 v+ `2 K9 a" X: J @. D4 m
new Selector (proto.getClass (), "heatbugStep", false);
3 v" `5 x4 e$ ^ actionForEach =3 c: T) [$ g! W% ]
modelActions.createFActionForEachHomogeneous$call% K2 s3 K: I' f
(heatbugList,
3 s! h! w9 P2 ]2 I1 l6 I3 N( z new FCallImpl (this, proto, sel,. F: g [* j+ k5 Z
new FArgumentsImpl (this, sel)));
3 {% e. S! E' w% [: r } catch (Exception e) {; e- ~9 c8 o* | D1 `
e.printStackTrace (System.err);
4 m$ `, F' e- J5 [8 V% E) ~5 l: ~ }$ U. e3 C9 ?* S$ |. k
1 l+ c, \! V6 V/ M syncUpdateOrder ();
. x2 b* ?) n3 s. r
1 J @4 n) d3 e; Y try {
. Z+ a+ y* }0 q8 _6 p modelActions.createActionTo$message 2 f1 w" k6 k# z! `% r, M
(heat, new Selector (heat.getClass (), "updateLattice", false));0 d' c8 q: t F* H% `+ C
} catch (Exception e) {; M* L3 Q7 y" m/ R( @
System.err.println("Exception updateLattice: " + e.getMessage ());% ~0 A+ @" k3 _4 }" k" D* f3 G
}7 M2 i$ Z+ K( _1 y' z
5 ^& b( i8 x3 e( }
// Then we create a schedule that executes the7 E5 C" O$ s5 j3 [) W. I& v
// modelActions. modelActions is an ActionGroup, by itself it
/ Y l9 n- T* G7 _ // has no notion of time. In order to have it executed in% Y+ E' f% s, T! v2 v
// time, we create a Schedule that says to use the! u+ H3 I% W7 F' z9 B/ U& {
// modelActions ActionGroup at particular times. This
2 ~6 q# A+ F$ W: w" d* |( w* P // schedule has a repeat interval of 1, it will loop every
( H. H; A) g8 x$ _8 t" V // time step. The action is executed at time 0 relative to' V5 d% S; }! l
// the beginning of the loop.
- L g6 H$ X& {" u; w- V0 N8 Z9 P8 k" @% N6 F
// This is a simple schedule, with only one action that is' |+ A% X- F+ L7 T3 a7 j
// just repeated every time. See jmousetrap for more
: F1 `3 z* g! M0 n( z // complicated schedules.
7 ]1 y/ ^7 h- C$ P8 k4 P9 I) ^# v+ V 1 z+ p7 D1 |/ I x/ I" `9 e' W8 X2 O
modelSchedule = new ScheduleImpl (getZone (), 1);
' O4 x N! u) u. t+ U9 k modelSchedule.at$createAction (0, modelActions);* z: j% T/ d: r6 O) ]1 M. U
8 O7 @/ s3 M% f' s% G$ u
return this;$ V9 |% z- u# ]- ]( N' _$ M& {
} |