HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: ^: T! j, O1 O ] E5 ]8 J7 K, J( Z8 `- d8 R; U
public Object buildActions () {) D. w9 i. P5 i6 a* C x
super.buildActions();) T3 G ?6 B8 J" I& B" x3 s. A. S
5 C5 L) e6 j, o/ U" }
// Create the list of simulation actions. We put these in
, l! i+ {. k7 ^1 A; _6 o$ F7 ?$ f // an action group, because we want these actions to be
: b1 O* ] N0 c" d2 E1 h3 p // executed in a specific order, but these steps should5 C+ ]# g7 w% ~8 E9 S0 U }
// take no (simulated) time. The M(foo) means "The message. L1 t/ X4 e; J$ ~
// called <foo>". You can send a message To a particular
( |2 Q y( u* R# a1 x // object, or ForEach object in a collection.
4 g/ F( O0 q$ @ & ], P+ ~$ T" Y F
// Note we update the heatspace in two phases: first run$ j& P+ y3 I& M% {
// diffusion, then run "updateWorld" to actually enact the0 l8 m2 f. T& l# @
// changes the heatbugs have made. The ordering here is% b" Z' W3 a( v2 a) L$ w( q
// significant!- z# { a1 F9 r |9 O$ {; y G
: H1 A8 i9 L1 K7 S; f( b' z // Note also, that with the additional
- I J6 @) \( t% |, |+ z1 \ // `randomizeHeatbugUpdateOrder' Boolean flag we can
D! R; ~0 \9 a8 I& B // randomize the order in which the bugs actually run4 w8 M. m4 ?: V$ L& Y' u
// their step rule. This has the effect of removing any
# N) i; G* v: f+ T // systematic bias in the iteration throught the heatbug4 L! ~8 p6 X1 a9 V {$ f! M9 f7 ^
// list from timestep to timestep% N6 K1 b* j- k: S; B( x# ?
& U/ k: l1 ^8 A- Y/ }& U+ E // By default, all `createActionForEach' modelActions have
$ g5 U" V6 u" R* V3 M3 C // a default order of `Sequential', which means that the) f. I1 D) Q# a) H
// order of iteration through the `heatbugList' will be
5 c4 l' N- b) k* {; a& M( l7 Q // identical (assuming the list order is not changed$ j0 B3 b" z0 R4 v. Y, [6 k
// indirectly by some other process).$ [/ T- I3 {0 n' G( H4 h6 L
- W9 F5 I1 a2 Q* L' r5 c, g3 b1 j. z. `
modelActions = new ActionGroupImpl (getZone ());% o& T# b. T. ]% Q" e1 a+ r
. @* }0 f0 z+ w
try {
$ M; p! R1 |5 | modelActions.createActionTo$message
2 I. G5 m4 i9 [/ n+ v" p" _0 u3 m (heat, new Selector (heat.getClass (), "stepRule", false));( @( U5 k Y7 i# K2 P
} catch (Exception e) {2 F% Z0 v- x) G9 T4 @. v
System.err.println ("Exception stepRule: " + e.getMessage ());0 p: \! p8 B! x
}
7 A8 P, ^4 [2 D; d
& U; F& k# W5 ~2 u try {3 N; T# W# k4 U, ?% p, E9 ]
Heatbug proto = (Heatbug) heatbugList.get (0);; M+ A5 F. k+ a. L: M
Selector sel =
; Z" Y& ^7 x& \* J9 a! z new Selector (proto.getClass (), "heatbugStep", false);: ~( A$ B8 E7 \5 F# |" A
actionForEach =
2 l( D1 O8 w$ C( k) k" ~ modelActions.createFActionForEachHomogeneous$call: g6 f2 q, H6 Z" @9 B# ^
(heatbugList,
) s7 w: M, i% {& b# Q new FCallImpl (this, proto, sel,
+ z& `7 X5 c9 T( s new FArgumentsImpl (this, sel)));
2 t1 c: b. Q. c1 y" T* ^$ e/ c, ^8 C } catch (Exception e) {
7 a! z K- M, N) F$ P e.printStackTrace (System.err);
1 y" p+ B% f% _ }1 I, J3 `# C; s$ C; p
# n; O8 F1 m( F. A! p* ~" N syncUpdateOrder ();
+ S8 i% @! g. q! W
& Z8 c/ x1 b" Y2 O. X& Q try {5 w5 [- g- \" y/ S
modelActions.createActionTo$message / \' ?: Q4 J# B/ w; a
(heat, new Selector (heat.getClass (), "updateLattice", false));
: Y, O3 t, x% o4 \1 W } catch (Exception e) {
. v) o) L8 `2 J$ x7 u3 p# Q6 M System.err.println("Exception updateLattice: " + e.getMessage ());
: A8 b* N: a, ]; A$ \. |- P }
2 ]7 W0 Y, K# m( H; W' {! N
5 \( C E/ y* U, d* s X // Then we create a schedule that executes the0 K7 L- l( j2 _
// modelActions. modelActions is an ActionGroup, by itself it
; h3 U) ^" ^/ [( L9 N // has no notion of time. In order to have it executed in; W4 B- L K6 ^! v: M" I8 y7 B% f! z
// time, we create a Schedule that says to use the
( {2 M) e: V0 D* u; N // modelActions ActionGroup at particular times. This
1 Q2 i$ T3 X( h0 ?; D6 R% n // schedule has a repeat interval of 1, it will loop every* \8 T9 z; ?+ T: z
// time step. The action is executed at time 0 relative to1 u$ o2 f# p, r4 ?3 Q% s9 \
// the beginning of the loop.. t5 G5 Y- `7 g. y1 I
( _3 b/ {) V) _
// This is a simple schedule, with only one action that is6 p* X' U. \3 R% R
// just repeated every time. See jmousetrap for more0 f9 q" ]1 k; D7 e6 m& P
// complicated schedules.' n1 s, o, f. j- m
. }; g% u) [ @: j7 \ modelSchedule = new ScheduleImpl (getZone (), 1);( _6 A4 E F9 M
modelSchedule.at$createAction (0, modelActions);* v7 Q+ g' Q' O
# \$ z' n: z( S! s* r return this;
! E# O/ ~) h" i J" w( {8 r4 [ } |