HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 D) B+ R( o7 \1 F$ g* P3 F8 ^& S7 s0 s% L( }, B+ O- C$ i+ q1 m- l
public Object buildActions () {: L$ ~, i, L* b3 ?0 @7 A/ {) c% H
super.buildActions();8 e0 B3 P; c7 c7 P
9 D4 U' o- `0 v L // Create the list of simulation actions. We put these in" J5 |# [% c6 G, N! }0 D; q! w1 P
// an action group, because we want these actions to be
9 X0 v" X( Y. i5 a2 ^6 N! U // executed in a specific order, but these steps should& W- M' v$ M+ [) s/ C
// take no (simulated) time. The M(foo) means "The message
) ]* n. @6 l6 s+ q0 g // called <foo>". You can send a message To a particular; k# q) @% c! W J$ {4 }( ~
// object, or ForEach object in a collection.
+ f, c6 s$ q7 B" F
: \& f; v/ S( r& j // Note we update the heatspace in two phases: first run4 t0 v( ^& L }& C8 _
// diffusion, then run "updateWorld" to actually enact the6 F$ Y& a6 g( p* o8 ^! D6 H
// changes the heatbugs have made. The ordering here is
Y2 h9 R& v; | // significant!
+ m8 f/ S* C+ G- l ( G# i% e2 y% d8 l: k# w5 @
// Note also, that with the additional! s. z! Q/ Y* z; N) p4 M
// `randomizeHeatbugUpdateOrder' Boolean flag we can& u0 o# }% e$ t3 h. g
// randomize the order in which the bugs actually run" t8 {; S* q! @: {( V+ y$ I
// their step rule. This has the effect of removing any
! @9 A% j* y+ X7 u // systematic bias in the iteration throught the heatbug' b; f9 V, g9 c9 b4 ] \
// list from timestep to timestep
4 [/ P. s' v0 V z0 L s0 l; \' S d: A
// By default, all `createActionForEach' modelActions have
4 k( L/ C; R! }' C6 }3 N# }6 T // a default order of `Sequential', which means that the* J( T5 Q$ E" ]1 t9 M% J
// order of iteration through the `heatbugList' will be/ p2 \6 R) _0 `" S2 \% d" c
// identical (assuming the list order is not changed
1 _" U4 p2 {' k // indirectly by some other process)." h4 Q, ]8 O) F3 Z$ s) Y- s
6 g# _: h) z9 S }5 I/ I
modelActions = new ActionGroupImpl (getZone ());
' O# K' c2 g- B8 N! P% i2 n! B4 z. \/ [, T9 m
try {
0 e/ K2 W/ S7 e5 k. ] modelActions.createActionTo$message q$ {# M+ P$ y3 D2 P
(heat, new Selector (heat.getClass (), "stepRule", false));
, ~6 y/ f7 r1 ?+ N( m0 o } catch (Exception e) {* X) C+ _0 i3 S: e
System.err.println ("Exception stepRule: " + e.getMessage ());! _# q5 F# v3 }" T6 t5 ^
}: |( I# o8 V8 O& v* N- I Z' N+ d
. H) r3 r: ]: O( l8 _ try {6 ?/ ~6 S% f% T5 m
Heatbug proto = (Heatbug) heatbugList.get (0);: O9 h* i9 J2 U
Selector sel = + D8 [# x* L! ]% c3 I* |8 [' m
new Selector (proto.getClass (), "heatbugStep", false);
+ v# e" d$ V- r7 k7 H actionForEach =
) z7 N- a! v2 j2 O& _ modelActions.createFActionForEachHomogeneous$call
: c; Z% b% y0 v+ t (heatbugList,
& {4 c8 t6 Z/ x new FCallImpl (this, proto, sel,3 ]+ z. Y5 e$ r* ?* \; I3 Z! \( ?
new FArgumentsImpl (this, sel)));
' F3 N' [3 I$ [+ ]3 L2 f) O } catch (Exception e) {$ ^: d# g; F+ v, C+ u1 O
e.printStackTrace (System.err);, \: \) B9 m+ K) O; c* b, m& L
}8 o1 b$ b# ]% D- m- v- E
0 p/ b- n+ `% D0 v0 V; M: D, I
syncUpdateOrder ();
; E4 E! \" l/ }' C8 `3 Z" ?
. W$ d k/ z1 G: S# Z try {
" m. b; l( L2 p% E- _ modelActions.createActionTo$message
& ]" h/ P& d3 W3 h( G& k- } (heat, new Selector (heat.getClass (), "updateLattice", false));; r' E0 l, @: D% n7 b
} catch (Exception e) {: r' t8 ]6 E' g9 L$ ]) N+ N7 k
System.err.println("Exception updateLattice: " + e.getMessage ());7 A `+ ^( e, y
}
) x( j& k7 [ ^6 _: ?
$ P# O( @. w, x" h& |8 ]0 N // Then we create a schedule that executes the+ g! \) U; q, n# k$ \# X. f7 s- ?
// modelActions. modelActions is an ActionGroup, by itself it; C# M. _" o* U. O
// has no notion of time. In order to have it executed in% D4 J+ e/ ^$ G. [
// time, we create a Schedule that says to use the
5 J' ]: P3 G! F# g6 d // modelActions ActionGroup at particular times. This {! N' i7 f6 R/ c* q; R
// schedule has a repeat interval of 1, it will loop every
' S: z5 V1 J$ j, v; U" w9 I // time step. The action is executed at time 0 relative to) o' k. [, \9 G8 h* M. F
// the beginning of the loop." i+ L2 u6 z. a1 Z/ d5 Z5 w
+ f& h+ {* w/ `& _7 e- r
// This is a simple schedule, with only one action that is/ m* J2 D, Y7 D& ^! `$ J$ I# U7 x; g
// just repeated every time. See jmousetrap for more
+ i2 |) ]$ Q. g; D' ^( l# C // complicated schedules.
! t I4 o' s8 A7 n. m7 s ' s3 I |' F# |0 f5 R" M" M. S
modelSchedule = new ScheduleImpl (getZone (), 1);
# o* @" R! C) f; G9 c8 b modelSchedule.at$createAction (0, modelActions);* i" C0 i8 w5 m4 _$ ~2 g% i
6 \, S+ X' `% f return this;5 L% d. z0 }! i
} |