HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
T; u' y1 y- G# ~+ Z6 P# [/ g9 {. J, [; l
public Object buildActions () {
, t7 \" W# l$ c ^ super.buildActions();1 ]1 U6 R8 \3 R5 x8 I/ g# p3 Y2 S
9 R, V# {; z6 S7 T% B5 Y$ {# b // Create the list of simulation actions. We put these in2 r6 J5 u' ^+ T* }) ^
// an action group, because we want these actions to be- E' @6 E% n2 ^& A) A- p
// executed in a specific order, but these steps should; z; @# y- G3 L& t
// take no (simulated) time. The M(foo) means "The message
E' i( t8 R. {- V) s // called <foo>". You can send a message To a particular; A/ A$ |2 z# j
// object, or ForEach object in a collection.
* o6 l3 n: O9 @' H% L7 X
* l$ Y3 W4 M$ c/ W0 ? m' K // Note we update the heatspace in two phases: first run3 T) T! [) ~# K, b0 ]* Y' U
// diffusion, then run "updateWorld" to actually enact the
1 B# `1 q- z1 _% R( t7 n/ ], x // changes the heatbugs have made. The ordering here is8 z+ V- k( b5 O! a
// significant!" ]# D2 L3 q: M: ]6 Y5 l
5 i6 o) ~, {( o/ _2 H // Note also, that with the additional
1 K) i" h4 n, g. w8 b7 B4 d" m3 } // `randomizeHeatbugUpdateOrder' Boolean flag we can* q# W6 S+ p4 a1 M4 E
// randomize the order in which the bugs actually run3 r) a4 w+ f& k
// their step rule. This has the effect of removing any
! J4 m8 g; E2 B, d // systematic bias in the iteration throught the heatbug
' k% X. r- a$ Q. A0 z // list from timestep to timestep5 I5 W9 U I/ {' U
9 X6 g5 X( g2 F. r7 ? // By default, all `createActionForEach' modelActions have
1 M1 u5 J& L* w: Q; r' O3 d/ A // a default order of `Sequential', which means that the
# l% x, v0 a, e& j5 z+ [! W // order of iteration through the `heatbugList' will be
5 E: e7 }" f+ K6 \ // identical (assuming the list order is not changed
! u9 n1 l1 s9 d* B: d' [8 y1 C2 U // indirectly by some other process).: o3 P: W/ ~, b0 C1 N6 L7 Z
# O7 f% J+ u: l. k2 Y: Z modelActions = new ActionGroupImpl (getZone ());
; ? k7 D. \# Y& q- M% L7 _ L) [4 o& k( q' z1 o% [
try {( E& w. n/ ?/ J+ [1 h4 P% P
modelActions.createActionTo$message' M% ?7 `1 E* u& i, C$ c
(heat, new Selector (heat.getClass (), "stepRule", false));
+ Y" a/ n( D5 M; @7 P/ O$ p% m } catch (Exception e) {
# Z8 V+ r R2 g% I0 j" N9 U System.err.println ("Exception stepRule: " + e.getMessage ());
3 ^' V$ A" Z& ~: D9 R u }
, s# E3 H5 R7 F9 l# r4 {8 I/ k, j* q% s4 n! x
try {) T* U1 N/ D! @: Z) |
Heatbug proto = (Heatbug) heatbugList.get (0);
: y- r+ v- \) I8 G( H3 t Selector sel = ' F4 ]1 q4 {. v( K1 d4 L& i9 e0 c
new Selector (proto.getClass (), "heatbugStep", false);
: C# D# ^$ o- x- M @8 [6 `# s actionForEach =8 w2 k$ O& A+ x' D. o
modelActions.createFActionForEachHomogeneous$call$ `! n+ I9 ?3 Y2 Q& A+ \% ^
(heatbugList,
7 Z4 g" ?% w1 f9 y8 e new FCallImpl (this, proto, sel,! I# L f! Q0 l: |
new FArgumentsImpl (this, sel)));3 s) U6 J6 a% A* J: h- j! [
} catch (Exception e) {
, o7 I2 R# A2 p% l e.printStackTrace (System.err);4 L4 G: k% u2 l6 g" I- X
}
9 K. h" v' Q' } ' ]+ x ?7 E% O9 |. j7 Y
syncUpdateOrder ();! I! Z3 L6 A) f' V9 A
% O* ~) D- f7 D/ z4 _& P try {: M4 x8 _4 \( b) c, U- W p
modelActions.createActionTo$message + K( w" {& ?1 s6 F
(heat, new Selector (heat.getClass (), "updateLattice", false));
" V I( i) R6 `! S3 A3 k- E$ p1 Y } catch (Exception e) {
: ?5 v) c* [8 G2 H2 o7 t8 D System.err.println("Exception updateLattice: " + e.getMessage ());& q( [; | I8 D" {0 b
}
7 [2 Q- n6 J# G' E- x9 y# j- S4 j
( d! ]6 p6 F" K; F& U+ z // Then we create a schedule that executes the
) h& S0 h: [7 }/ b3 f // modelActions. modelActions is an ActionGroup, by itself it4 c$ w7 t) o9 \& M- H7 [
// has no notion of time. In order to have it executed in7 R6 G" X* u' G/ z
// time, we create a Schedule that says to use the( q5 o6 V) x4 g+ D- l( O+ C8 h. e
// modelActions ActionGroup at particular times. This
: `/ x b& E Z: ~ // schedule has a repeat interval of 1, it will loop every' l" J1 l3 f/ q1 ^
// time step. The action is executed at time 0 relative to
4 C3 {" W# |+ Q' P // the beginning of the loop.
x. L! E' i: ]7 [, I' B9 w' Y8 U& N$ U" l$ b$ Y, ` b2 _
// This is a simple schedule, with only one action that is
) K! F# s6 R6 G // just repeated every time. See jmousetrap for more8 Z( X/ m! S7 d+ _# r/ ^5 @% N
// complicated schedules.
0 |! c2 G2 f B5 E# I4 @1 V5 H ?$ m
/ Z/ R/ s" a- K" ? modelSchedule = new ScheduleImpl (getZone (), 1);9 g9 Q$ h: \( L$ n4 e, v
modelSchedule.at$createAction (0, modelActions);
: O Z% {/ F1 J& o& f+ M
0 }9 |# M( R& P9 \7 f0 H! g return this;+ L1 D1 l% ^# R1 X+ G& p Q
} |