HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 C1 g8 g* B1 G" g3 q
" ~, g" Q% o: m: C4 c public Object buildActions () {: ^2 @. i5 w5 N
super.buildActions();8 ]( z$ K! ?$ o" ?. \ Q
0 r9 Z) V# e. x3 l) J6 t/ X // Create the list of simulation actions. We put these in) w# C( k. X8 n
// an action group, because we want these actions to be
/ ^& { m' z4 l# ?" Z5 | ~ // executed in a specific order, but these steps should
7 p& h! `" W1 `, b" n; k // take no (simulated) time. The M(foo) means "The message
4 I. r# L D9 O% ~1 v // called <foo>". You can send a message To a particular/ w9 {. g* K* ^4 L' ~2 q/ y1 }/ \
// object, or ForEach object in a collection.' y- e& O' h7 H9 a' I
. p5 E8 n, a) \% J
// Note we update the heatspace in two phases: first run
& c" W# S. O7 ], S // diffusion, then run "updateWorld" to actually enact the
# g; L0 }8 u) K3 i7 [/ w) ? // changes the heatbugs have made. The ordering here is, e1 I/ d; }* H& T
// significant!" H( T4 U, Y3 M9 X
, H! |+ I" ^: E1 l* e/ z/ N
// Note also, that with the additional
/ f9 Y' l; c! X# @( ?2 r // `randomizeHeatbugUpdateOrder' Boolean flag we can
' g, G) c: \# z6 f8 A/ P // randomize the order in which the bugs actually run3 U' X6 i) j; }, S. x2 }
// their step rule. This has the effect of removing any3 W4 m1 r& g5 |( w7 k' a7 h
// systematic bias in the iteration throught the heatbug; s: g4 y* S/ Q( {, `5 c" j
// list from timestep to timestep: m& n+ v9 e; S: w4 }6 a
5 M( ?; R3 Q3 L* n0 M) C
// By default, all `createActionForEach' modelActions have
8 Q& w% N, D% D: r" I9 W& O // a default order of `Sequential', which means that the" S' q$ w: i2 }( t, b( i5 D
// order of iteration through the `heatbugList' will be
! c" y* w# \5 `$ I# \6 v // identical (assuming the list order is not changed1 [: P: n" d0 O% F$ D8 c
// indirectly by some other process).0 `. d* }: T. t ^2 R
% x6 u+ M8 Z# r" ]$ ?, d! O modelActions = new ActionGroupImpl (getZone ());
' Y6 z1 P9 w$ \4 c+ y' D# Z
% Z( _) K2 u& F" r; g! U0 Q1 v- C try {
' n3 G& Z2 y: t3 _4 B modelActions.createActionTo$message( o" }' U, k/ M6 Q
(heat, new Selector (heat.getClass (), "stepRule", false));4 B. r/ k8 W+ W; }
} catch (Exception e) {8 `9 z) d6 H2 X2 ~5 R9 G7 Z
System.err.println ("Exception stepRule: " + e.getMessage ());5 `- ^" z6 f5 y* V" N5 e1 ]8 n
}1 v% [2 {, |! T& l, @
* t m+ H: T, q% G' B try {
. ?! V9 M9 \' N/ ^" N Heatbug proto = (Heatbug) heatbugList.get (0);
. l; o& _0 t9 r. }9 I& e Selector sel = * r! }6 h( Z$ P- F1 q& t
new Selector (proto.getClass (), "heatbugStep", false);
; U$ M1 |& |/ h8 O6 s0 H7 j& k actionForEach =
4 z/ S, ?0 X4 ~7 E; d4 t modelActions.createFActionForEachHomogeneous$call
2 h% p+ f2 C' W3 v2 W' [% B( B (heatbugList,
! E6 `. b+ u* i+ m- Q/ s new FCallImpl (this, proto, sel,
1 c G; Z/ W9 c, b" Q1 V2 {: y- | new FArgumentsImpl (this, sel)));
0 L) O/ Y0 g) W! _% X. P! b3 P2 t } catch (Exception e) {
6 E, e7 G% ]3 u8 T e.printStackTrace (System.err);
7 H3 _, @' P) v( d* V* G1 V- G }
1 G b9 N2 b' E$ a" H$ v
" r8 n2 `" v, {" q1 `) @& n syncUpdateOrder ();" S( g/ g# V# Y: M- `" F
5 x- o) q7 R7 o& Y# n, [
try {
C' {0 h3 ]! p modelActions.createActionTo$message
c l, c. n, s' P (heat, new Selector (heat.getClass (), "updateLattice", false));
% } z" h. r6 p0 R8 R } catch (Exception e) {
# C. e4 I1 O1 Y3 _, U System.err.println("Exception updateLattice: " + e.getMessage ());7 @ a9 h& i& k+ v# I# [# l
}
. q+ ?9 _9 k8 y
9 |( B/ Z* L" e* p& J }8 P1 O: f // Then we create a schedule that executes the
$ Q( ^4 B8 ?( V' q8 S! ~ // modelActions. modelActions is an ActionGroup, by itself it9 h/ R" ^2 D0 W$ D
// has no notion of time. In order to have it executed in' J6 | ~! a6 p1 w; }5 Z+ C
// time, we create a Schedule that says to use the$ S( b n) o0 r" a
// modelActions ActionGroup at particular times. This
* p e$ ?7 B: J) M! b# H // schedule has a repeat interval of 1, it will loop every
3 p# I; u. X# \3 R& ] // time step. The action is executed at time 0 relative to
" ^0 V1 @9 Z/ n) x Y9 e // the beginning of the loop.
9 G( \$ I* D( M1 e- k
, q' z/ h" |2 H U1 } // This is a simple schedule, with only one action that is
) t" P0 ?6 X! L- y: _& o7 z // just repeated every time. See jmousetrap for more
- ~7 L7 d) U9 R/ k // complicated schedules./ `* e2 [6 \" Z$ L$ p$ T, ^: h# q
! w; v% j( A3 S
modelSchedule = new ScheduleImpl (getZone (), 1);
# W- @5 ^5 n& }% `- Z# O modelSchedule.at$createAction (0, modelActions);4 t- z# ~, z6 Y: J6 w
! |# o* E2 Z2 V6 Z return this;( h4 \+ q% Y+ ?* a: z
} |