HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ ~0 E8 }- p- R d3 E* P# x& @/ L' p$ K) x: q. x3 ~
public Object buildActions () {
" d% M' \! N; R7 }1 h0 m super.buildActions();0 _: ?- V& M; @: P
; ]: k$ z- I {+ J$ o# A1 i
// Create the list of simulation actions. We put these in' Q/ p" U0 j3 b5 a. ^
// an action group, because we want these actions to be! F/ w+ z% \( I4 p3 H' ^( u
// executed in a specific order, but these steps should
# b6 h' Y: s$ |5 F; B // take no (simulated) time. The M(foo) means "The message6 l+ ]5 n8 y3 v$ K
// called <foo>". You can send a message To a particular, K8 S: X$ W9 J$ F
// object, or ForEach object in a collection.6 D2 D6 g7 o3 l9 _; T
5 ?3 T2 O1 z( P- K A! y7 e* ?+ z4 s
// Note we update the heatspace in two phases: first run
2 o) f4 t0 s) L4 T // diffusion, then run "updateWorld" to actually enact the( R/ F$ n* O4 V' V2 T
// changes the heatbugs have made. The ordering here is6 B4 X" y! P# O, ]
// significant!& B. N V3 o/ U" S
2 D7 n" u0 ~/ L! }3 p9 P
// Note also, that with the additional
O) p' q* V7 |7 { // `randomizeHeatbugUpdateOrder' Boolean flag we can
) z0 A: Z9 I8 _2 U: o // randomize the order in which the bugs actually run
5 m0 ^# u* y2 l; {2 J1 K // their step rule. This has the effect of removing any
1 Z) R# f# O# [2 h% s0 ]# C5 z // systematic bias in the iteration throught the heatbug1 c' P# i# j2 C/ F/ P$ J' D# ^
// list from timestep to timestep; ^- L( r1 @+ a* }* J. u* k" ]* y
) w- b$ o( B( K* \8 P) C& s+ V2 \
// By default, all `createActionForEach' modelActions have
6 t8 `9 J1 b/ [" {2 W7 K; r // a default order of `Sequential', which means that the
$ N# v0 m3 E4 F9 M w7 P# G // order of iteration through the `heatbugList' will be
! V w n1 S3 h" J // identical (assuming the list order is not changed0 M1 l& E! t' o
// indirectly by some other process).
/ b1 x$ G, s/ T* r6 E c9 B5 J% N& H$ W" v5 G* O
modelActions = new ActionGroupImpl (getZone ());
3 ~) L( ?* Q% ^: t0 }& ^9 [& V# C6 e5 ~& p0 V* Y
try {
F% V3 U# g3 v% p& c! ?/ Z5 s( I modelActions.createActionTo$message
2 w. p6 C' }$ }$ X+ B9 j1 b% |. m (heat, new Selector (heat.getClass (), "stepRule", false));0 f# M8 S, Y7 T& L
} catch (Exception e) {
& h, W5 B- Q( m P5 Z$ A System.err.println ("Exception stepRule: " + e.getMessage ());
2 A( }# c' r" F }
+ ?/ q# P1 ?. ~9 A- ^& ~" l' m* A# B0 P* v
try {2 W( C9 t) L% {7 L
Heatbug proto = (Heatbug) heatbugList.get (0);
" `4 |- I9 l$ q3 P/ M( g6 d o Selector sel = & X- j! [$ h% W& F
new Selector (proto.getClass (), "heatbugStep", false);
& d: R5 m# o9 C& z! R$ Z: G actionForEach =
6 |& J+ W# q8 U& P, { modelActions.createFActionForEachHomogeneous$call& M& y- j f. h8 B
(heatbugList,7 { m# B- e+ |' `* l2 C
new FCallImpl (this, proto, sel,* _/ o% |# l7 i/ `9 [. y
new FArgumentsImpl (this, sel)));
% i6 Y! N4 o: \3 q1 Z- L# I+ @ } catch (Exception e) {
1 a% O5 p/ E' n; V$ M* d e.printStackTrace (System.err);
/ _( k) k- T$ x( q" X6 r) `3 b }. ?/ Z5 M, j9 I \
5 Q' f! I9 [/ [5 b syncUpdateOrder ();
[# x2 d: J2 }0 T$ w0 |4 T3 f, z$ h
try {% z2 n8 f9 N4 b* A
modelActions.createActionTo$message 1 z9 m. K! W$ b- I* f
(heat, new Selector (heat.getClass (), "updateLattice", false));5 a! b6 x: ~3 B i0 n
} catch (Exception e) {
% y% v. U# W/ o c) f& o System.err.println("Exception updateLattice: " + e.getMessage ());
: n0 O8 ]5 x* a }2 M- _" |6 G# k
, c; U2 \. R% q. e/ Y# i5 [* P
// Then we create a schedule that executes the5 h! U* C1 t. k k, N) Y* K" L, ~6 W
// modelActions. modelActions is an ActionGroup, by itself it
% b( }; _6 \' P0 h2 z0 X2 A% Y! b // has no notion of time. In order to have it executed in
8 @- h, r2 d' Q" C+ X7 S$ { F // time, we create a Schedule that says to use the
/ @" Z) h1 `! C) B7 l5 g9 X9 f // modelActions ActionGroup at particular times. This
A+ {2 ?/ v: j5 N) H- s) w // schedule has a repeat interval of 1, it will loop every: k$ y. L: j5 W! X l
// time step. The action is executed at time 0 relative to8 `' [/ L1 a! t& i4 |( N2 c
// the beginning of the loop.' n. d" M3 [% R% h8 R5 A1 Y
$ n, k) B! q- o2 w! T. G2 Q* z0 |8 ^ // This is a simple schedule, with only one action that is
, e( X L# T* m' w+ Y- s // just repeated every time. See jmousetrap for more' N7 `7 A, s: K8 \
// complicated schedules.% R3 C3 X/ D7 X3 {3 |/ }3 i. |2 @
7 Z# J4 ?' e, x( e6 G' { modelSchedule = new ScheduleImpl (getZone (), 1);
. W9 R! o2 y* t6 p7 p- [ modelSchedule.at$createAction (0, modelActions);: Z. j: H/ x5 M
6 H7 u2 Q( ~) w0 W1 q/ s; _
return this;
; r2 W; D" Q+ k! o } |