HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& |2 f$ A; s4 Y3 D8 o+ U7 } ?# Z
v+ M* h- \4 K, K, @
public Object buildActions () {$ @' R/ _ L8 R; Q$ K
super.buildActions();
7 X2 P9 [6 N7 {0 W! h% b # d" R5 c9 x2 x5 \& M
// Create the list of simulation actions. We put these in
4 z0 X' X+ u( o6 C- e% H // an action group, because we want these actions to be
/ L8 p- {6 F2 `( x, J# t4 I, c // executed in a specific order, but these steps should
. I6 K+ p; R# b1 B5 x% k // take no (simulated) time. The M(foo) means "The message- a5 K( Q5 G6 h2 a, T
// called <foo>". You can send a message To a particular! n; v4 n* C. T" H! L3 X7 Q
// object, or ForEach object in a collection.
& _8 ~4 ^$ n4 p: _ , E8 g% Z! w j% p
// Note we update the heatspace in two phases: first run
1 m3 j( ?3 D3 k1 a: l0 v& p0 L. e // diffusion, then run "updateWorld" to actually enact the
/ b# m0 n! K$ H( k7 i // changes the heatbugs have made. The ordering here is! _4 c1 F; B9 h, ?
// significant!3 }# N( @7 Z( {) ~
9 h5 n0 ?4 ?8 L5 Q8 R6 a
// Note also, that with the additional2 y0 ~& a! G( c! x' H7 q# `( f
// `randomizeHeatbugUpdateOrder' Boolean flag we can% j; z+ y9 ]6 h- F
// randomize the order in which the bugs actually run
& I J6 j9 T* p5 x // their step rule. This has the effect of removing any
+ ]# ?& v5 b; r2 j7 I% o // systematic bias in the iteration throught the heatbug( C! P% C X9 D9 z. l# L
// list from timestep to timestep8 w, M x; P- p! F) Y+ o) B
V* I; r& f/ u, V' @/ L5 U( K: I: H
// By default, all `createActionForEach' modelActions have
( O7 g/ @1 S( a' @2 { // a default order of `Sequential', which means that the x2 W' J4 m9 ^, h
// order of iteration through the `heatbugList' will be6 J! L" }$ q+ T0 R
// identical (assuming the list order is not changed
& a i, R g5 t2 s // indirectly by some other process).9 T7 W, C f8 F, F- ?
! g j7 I( g" p modelActions = new ActionGroupImpl (getZone ());* @& B- a! ^( ~% n, u! ]- G
% l+ k0 N9 v3 \: W5 A. `1 _* I5 R
try {2 B- A6 I J" |1 o* U4 T, k
modelActions.createActionTo$message
9 U3 A3 o1 |8 Z& X+ m/ W' q+ j (heat, new Selector (heat.getClass (), "stepRule", false));6 h" E2 o. H% Z h8 L- Y5 K
} catch (Exception e) {
( N2 k$ ^2 J j) y! t. p' g. H System.err.println ("Exception stepRule: " + e.getMessage ());( z( `" ?0 X- I d2 E4 e; ~7 ]
}% c9 t1 X/ W& A2 Z
5 j/ B3 l0 b1 }8 |/ }3 R5 j try {
; |7 d: R3 u/ U5 s! f5 a Heatbug proto = (Heatbug) heatbugList.get (0);
8 u' @/ n0 v& T0 v+ ^7 r! N! e+ P Selector sel = 7 F; d" s5 s* K( ` Q# j; T% l' r
new Selector (proto.getClass (), "heatbugStep", false);
. G j! `+ T& Q# T, l f W actionForEach =
& ] M, u0 y7 T/ J, k8 j' { modelActions.createFActionForEachHomogeneous$call9 s+ @* O2 d( E8 _
(heatbugList,% i7 E. X+ z. Y& z3 e
new FCallImpl (this, proto, sel,
7 a7 J* J) e$ W0 w1 T, x new FArgumentsImpl (this, sel)));8 i! H+ g( J. f' O$ p2 o& F
} catch (Exception e) {
/ O; T" `6 e" i e.printStackTrace (System.err);
3 `3 o7 h9 n. w$ s; _$ r+ F }2 Z8 ^4 b5 Z9 m' N" B' p
' O6 ~" U, K4 G% i7 t8 R' f
syncUpdateOrder ();1 R" q" Y3 c! f# w+ p/ Y8 w
) P6 n. i6 P9 s/ A" e8 T
try {# k" L( j9 `) W6 T$ i- w+ U
modelActions.createActionTo$message . Q6 i& b5 ]6 I
(heat, new Selector (heat.getClass (), "updateLattice", false));
" P% E+ B$ I: j, O6 o N } catch (Exception e) {
0 C7 l# h+ t3 W# O- a8 J System.err.println("Exception updateLattice: " + e.getMessage ());
. }& Y8 }7 M3 T c }# e0 v% f& _7 w* U# R) R0 U
3 r" A* c1 j' ?9 J // Then we create a schedule that executes the
! a I; r: g* A$ n0 \9 T // modelActions. modelActions is an ActionGroup, by itself it+ \ C) n4 q! O1 J; N/ b
// has no notion of time. In order to have it executed in
( K/ r1 M& b" q0 }8 {9 Z! ~5 N // time, we create a Schedule that says to use the
7 k5 R" P& {1 q% F% L // modelActions ActionGroup at particular times. This
/ J+ }$ d* P. q- W7 H6 l // schedule has a repeat interval of 1, it will loop every& W0 @& x3 ^& h; P; V+ d3 A
// time step. The action is executed at time 0 relative to! |1 _: P) y, P( Z* F0 q0 N; d$ Z
// the beginning of the loop.3 k( }8 k5 i( n. m' }# R9 n
6 E; S, p3 R# r2 p# c% r9 C
// This is a simple schedule, with only one action that is; x$ J* q4 F! ]! G
// just repeated every time. See jmousetrap for more+ k$ q8 Q- ~3 L+ `
// complicated schedules.
F' J7 I' Z% N& X. T/ I) R " o2 Q5 _- J% s) i L
modelSchedule = new ScheduleImpl (getZone (), 1);
0 ]# s7 T$ \+ O* u% P modelSchedule.at$createAction (0, modelActions);2 O( x; S- \( G9 q9 I; x
/ @/ z6 s* o8 e* }
return this;$ a# G e+ I3 D5 F8 a9 P
} |