HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:# K$ [" T3 Y0 }, u6 U2 Q* R. c) s
, w/ c o' y3 A public Object buildActions () {
) S4 f3 ?: B% c+ U/ J3 q# B% D super.buildActions();
& T7 n! A% W, Q z n # O3 t3 l/ ^, y
// Create the list of simulation actions. We put these in
; J. C" E/ x( ?4 j // an action group, because we want these actions to be$ b, x) T7 f! ^3 |, p: W
// executed in a specific order, but these steps should
/ ^0 }$ N0 O9 G2 F // take no (simulated) time. The M(foo) means "The message
& L/ U. C' ^" n# K. N: V // called <foo>". You can send a message To a particular
2 |9 T' g& w- J: [( v // object, or ForEach object in a collection.
# ^. o3 l% Y0 P. y: m" h- V 3 M. O, o9 [% g+ C$ w
// Note we update the heatspace in two phases: first run- g, J2 n, y' _
// diffusion, then run "updateWorld" to actually enact the
8 ?: n# N0 A$ I! ^$ c# M // changes the heatbugs have made. The ordering here is
- M; W! W7 Q% V! Z! n // significant!) x i! ]+ m3 K# h( W0 q
" L9 U# L% C, Y9 t) U
// Note also, that with the additional
: u7 D$ F- E5 R# o: u: X% H // `randomizeHeatbugUpdateOrder' Boolean flag we can0 R0 e1 Q# \* N% I6 Z
// randomize the order in which the bugs actually run1 ~( j% ~( |( ~" C3 d3 A% O5 _
// their step rule. This has the effect of removing any
/ s3 _0 v3 v: n // systematic bias in the iteration throught the heatbug5 m6 l/ {' K+ I( h, Y- I
// list from timestep to timestep
G/ ^* Y E/ h$ [" z Q7 s7 r9 z
/ Q+ B7 l, L7 M2 ? | // By default, all `createActionForEach' modelActions have
5 C3 J- ^- V5 ^- r+ u& d: e5 v // a default order of `Sequential', which means that the0 u. G3 Y; o' T3 ?. {- L
// order of iteration through the `heatbugList' will be
6 L- c7 U0 ?8 [9 e @& f* w // identical (assuming the list order is not changed T8 f4 m, o, f9 S. g
// indirectly by some other process).
' V: x \% H: O& K4 f+ Y, Y1 I9 m$ Q ) d0 I0 }2 o v( ?, {2 d- s8 Y
modelActions = new ActionGroupImpl (getZone ());
$ I# u& A* ^- i
% y) T4 o7 x' m- {0 Y+ q- A try {7 ]9 F% s' B* Y. y) b
modelActions.createActionTo$message
$ t; q5 P- a9 q* L (heat, new Selector (heat.getClass (), "stepRule", false));9 H9 ~3 g8 `3 }# P
} catch (Exception e) {
6 W5 P+ S, |9 f5 V' u System.err.println ("Exception stepRule: " + e.getMessage ());
4 b, z& \, x: |, r1 v& S5 L }+ ~2 L6 @' a" ^7 o
# S4 ]1 s4 W1 P# v
try {
% p" ?% u0 z* ]" F% b' }2 m W# a7 F Heatbug proto = (Heatbug) heatbugList.get (0);
$ }9 d; O" F0 }, ~. k: H5 D$ h Selector sel =
+ j6 G A3 a5 B: B1 k8 v new Selector (proto.getClass (), "heatbugStep", false);
' b' m( E+ c5 W actionForEach =
) p/ Y$ F: U4 g0 w2 H! T1 o' O modelActions.createFActionForEachHomogeneous$call) ~; r1 Z$ I' ]* p& [
(heatbugList,
" |& N" N. j2 I5 l3 [, g. Y% n4 r new FCallImpl (this, proto, sel,
2 E4 |2 G9 c0 _2 g' O) A6 _- g new FArgumentsImpl (this, sel)));6 f! K+ d8 l4 C5 f4 t. h, m
} catch (Exception e) {; l: M/ ^' f4 I# I
e.printStackTrace (System.err);- @, L1 O2 |8 f r3 k% U _' c" C# [
}
" z; ^( n6 O$ R$ j% T- Z# [" {2 [ 2 y$ g3 _* ~1 [. r1 z' k- P1 ^. X
syncUpdateOrder ();
1 `. s" ]% g+ B q' ]
- g; ]9 x; S9 G! K( Z2 } try {
" o9 ` A |- j) E1 P2 Z( i, x modelActions.createActionTo$message - |5 s ^4 U6 w
(heat, new Selector (heat.getClass (), "updateLattice", false));3 q# m% ~) ~# j8 s1 v
} catch (Exception e) {- P# u) n7 u5 [' `0 r3 F0 m# v3 p
System.err.println("Exception updateLattice: " + e.getMessage ());3 W- I; n: d5 n7 t. R
}: _2 P# V$ \1 `8 y, f( }
) L' ?" r' J3 ^5 u! K
// Then we create a schedule that executes the
/ }9 l/ q: U g+ d7 G // modelActions. modelActions is an ActionGroup, by itself it2 p i! V( _6 p8 r8 @* |, Y: E
// has no notion of time. In order to have it executed in3 G2 X! n0 b' f5 [7 N
// time, we create a Schedule that says to use the
: @ }, ^- i% j. v, ? // modelActions ActionGroup at particular times. This
2 M& G1 C( {& ~* \1 L // schedule has a repeat interval of 1, it will loop every
, r# U0 k7 \! e& z5 g0 J! P1 M8 s& Z // time step. The action is executed at time 0 relative to1 S1 |! `/ _' }
// the beginning of the loop.: y6 O0 P3 \; } O6 U
6 `+ j+ S6 t9 A. |& M // This is a simple schedule, with only one action that is) C- i# K Z$ I
// just repeated every time. See jmousetrap for more& d( p% e* C4 j& ^7 a5 E
// complicated schedules.
; B S( `+ [+ S6 s0 t: R ; l3 D* ^! S; P9 f o D/ z
modelSchedule = new ScheduleImpl (getZone (), 1);" Q: G: v4 e: D( z1 X
modelSchedule.at$createAction (0, modelActions);
- w# a' h) O/ Z1 l* a5 _ m
3 O' L# } i9 ?8 K! ] return this;$ d6 J' v! c4 I9 \4 u# N) E
} |