HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
! w: f% v' C1 r4 o. i& p2 D& o, A6 y- z. }+ Q: {2 [; I
public Object buildActions () {
. t* n5 ~. d( w; o% X6 t super.buildActions();
: f+ |$ {1 Z0 k" r# W
$ G" t) q2 s3 }) V( i& f& h // Create the list of simulation actions. We put these in4 `3 {1 o2 @* }$ F, }
// an action group, because we want these actions to be" T6 ]2 y; t5 ~
// executed in a specific order, but these steps should
" A( x7 U3 Y& x( g4 c( f2 A // take no (simulated) time. The M(foo) means "The message
]+ A4 u7 {3 X& V // called <foo>". You can send a message To a particular
% t8 X, J. C+ C4 d K) j3 u2 d // object, or ForEach object in a collection.- O* K( P* }, w0 [2 J8 L1 p! v
' c, T: m0 O7 g+ O
// Note we update the heatspace in two phases: first run
9 @$ F$ u4 q I P8 R: S5 [ // diffusion, then run "updateWorld" to actually enact the7 q7 a/ i, V$ O1 w, J
// changes the heatbugs have made. The ordering here is( p: K9 T: {* ]# _
// significant!
0 X' p1 h, c- j7 Q: k' g3 x
5 T$ o! p# \5 M- l3 j% @ // Note also, that with the additional
! F5 }9 K# Z& Q+ N( y, f% K // `randomizeHeatbugUpdateOrder' Boolean flag we can) s& [0 l% U A$ j
// randomize the order in which the bugs actually run
9 l u4 O$ m" j. W- `! T' K( u // their step rule. This has the effect of removing any
, C* ]$ q) f% z! d0 N5 k // systematic bias in the iteration throught the heatbug
. w5 @& \8 Q9 @. g8 F; @3 U // list from timestep to timestep( L/ v: G; C1 b5 ~. m% J
M6 ]: i+ {6 W: J8 c" ^ H
// By default, all `createActionForEach' modelActions have
4 K8 d$ i' v% L // a default order of `Sequential', which means that the8 w8 K8 T# s, K$ l+ k' w! W
// order of iteration through the `heatbugList' will be
H: J8 o$ ?# Q: |% T1 G0 ` // identical (assuming the list order is not changed+ c- q4 P4 s+ Y& @
// indirectly by some other process).4 w! v' d: _) O$ N
: X$ j* ?2 r2 g7 J% h0 A
modelActions = new ActionGroupImpl (getZone ());
3 U" I4 f, a: o- @+ Z# A1 }: Z8 M
try {
4 [ f4 _1 c( c2 x8 j# [ modelActions.createActionTo$message. v( v5 k) n6 i4 M1 p4 y, \: N7 N
(heat, new Selector (heat.getClass (), "stepRule", false));* p3 W0 q. D# }- W- ]# x3 K
} catch (Exception e) {, Z. S% z0 W/ f8 _1 m) b# q
System.err.println ("Exception stepRule: " + e.getMessage ());
( e( t6 n9 B- n1 H( h0 O6 i( z0 b }4 V' }4 _: I5 @
/ r3 P) X$ y$ g$ E. F# Y9 A$ B1 l
try {
" h" z1 b& s0 n( U& R Heatbug proto = (Heatbug) heatbugList.get (0);9 M# q- f/ Y6 I& S* n) F
Selector sel =
( {! K4 v3 `: i) o5 X new Selector (proto.getClass (), "heatbugStep", false);8 `/ D7 j% S% @' H2 c9 z4 m: E
actionForEach =
6 `5 u+ h) A4 G; m( a9 D, s9 | modelActions.createFActionForEachHomogeneous$call
C3 \4 r) x+ R' U8 Y; f( I (heatbugList,/ C$ a: K' E( F& F5 ?2 ^ \3 O
new FCallImpl (this, proto, sel,
- X1 l! N7 h! ~5 C8 M new FArgumentsImpl (this, sel)));+ `- q+ W$ Z1 }2 q( e6 I0 n3 o$ L
} catch (Exception e) {
: O' u5 R' _3 l, L! w$ R4 r A; O e.printStackTrace (System.err);
8 `6 F5 F" l6 u/ Q0 d: m/ l }
% n9 j2 @: O9 j- | 7 P5 h% N) A4 g9 ~' v4 C9 B" t0 s
syncUpdateOrder (); o' y$ F+ G& ~
4 l+ V/ \- J/ }5 o, ?: [8 T
try {
7 e3 a5 N$ g! ]0 b( V+ J% M2 v( } modelActions.createActionTo$message 5 H+ V, k4 W+ }' c$ U
(heat, new Selector (heat.getClass (), "updateLattice", false));
+ v' F; X& ?) n; ]# } } catch (Exception e) {! J4 Q1 x/ h2 B& w+ h4 l
System.err.println("Exception updateLattice: " + e.getMessage ());
% [3 f1 T7 j" ]& ? }
8 c. [: z* U0 q 7 \! l# j- O3 w7 E" I) y
// Then we create a schedule that executes the
: m4 a" l+ @3 D# M7 f6 H8 c // modelActions. modelActions is an ActionGroup, by itself it
! \ ?0 J8 L5 {9 | // has no notion of time. In order to have it executed in
9 `/ |5 b& ?# z5 S6 L) W' I // time, we create a Schedule that says to use the2 N% J! g! x/ \2 X
// modelActions ActionGroup at particular times. This
. C: g; x3 l i, t // schedule has a repeat interval of 1, it will loop every( ^ Z$ V" x! a. C
// time step. The action is executed at time 0 relative to# e( X' R; h- j# L4 {: B
// the beginning of the loop.
, f# {) @" L) H5 F0 b3 L$ e1 e$ Z- o- s! n w) I7 V* A9 t
// This is a simple schedule, with only one action that is
# H0 a3 Z! }) }) ^+ ~9 T4 S+ d // just repeated every time. See jmousetrap for more4 r4 x2 s+ i6 S. Q
// complicated schedules.3 J& O* E1 s9 o9 E3 l! d+ U% |; t
5 ~' x4 Z5 }% H. g, o6 q
modelSchedule = new ScheduleImpl (getZone (), 1);, M) E3 x( e& I6 P9 D1 Q
modelSchedule.at$createAction (0, modelActions);
( k. W9 v. j* I* e3 M, ? ; d& }* z4 \# w- Q6 r; E
return this;" e% L" `$ k" n- V3 `* e
} |