HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 ?8 o, N, Y3 U! `; S4 {& n
- R/ A* q' E' e1 q6 q, w! Y" o+ E/ E public Object buildActions () {
I/ p. } G& |3 h super.buildActions();
9 u6 i3 K, P. z0 d w! I % t& D- p# ?, X3 O5 n) b6 G5 |
// Create the list of simulation actions. We put these in
+ @" k# p- b! Z // an action group, because we want these actions to be
* }6 X8 F' m n' L& l; E: ] // executed in a specific order, but these steps should: A- X, A& F* Z" U0 ]: @# W+ b
// take no (simulated) time. The M(foo) means "The message
' F4 S, Z* R8 O( F // called <foo>". You can send a message To a particular
9 ]7 r9 A9 e, @" ` // object, or ForEach object in a collection.
0 h% K3 q) {2 j* X) ?8 g
2 c7 l. z( a; C6 |+ ? // Note we update the heatspace in two phases: first run
5 o) {' `7 o; U/ W8 ^ // diffusion, then run "updateWorld" to actually enact the5 B4 G y% J3 z8 w; c% \5 @0 e
// changes the heatbugs have made. The ordering here is; G9 c- r0 C, ^) k3 e1 ~
// significant!
% H* ?. J9 L* F: x2 [
: i' s. L# `8 E5 E& m // Note also, that with the additional
! I: V0 E! o/ r: |1 r( ?: n5 c // `randomizeHeatbugUpdateOrder' Boolean flag we can
4 g, @: V4 I @6 t // randomize the order in which the bugs actually run
6 w- [9 q! Z. X* C' ~ // their step rule. This has the effect of removing any7 e7 `, j$ c% r4 Z
// systematic bias in the iteration throught the heatbug: A3 T- i" U9 ]+ ?% b6 L d
// list from timestep to timestep% d' W8 J+ ~; G/ |, e& b7 ~
7 ^& L2 f B& @( E
// By default, all `createActionForEach' modelActions have" u% }0 l, b! H+ _
// a default order of `Sequential', which means that the' w( }5 s3 O" N: ~# |
// order of iteration through the `heatbugList' will be
" y- }) h$ Z# q0 F7 q; p // identical (assuming the list order is not changed
6 `% u( d, o; D. p% Q4 g // indirectly by some other process)./ o+ k0 D- l1 L0 t- _1 l
' f, X' \, y0 P8 g8 w, p modelActions = new ActionGroupImpl (getZone ());
& T. r) Z" z" K. }
5 ^9 i9 ^9 N4 g K$ _ try {
4 \7 K: P4 U- y0 ~: w s5 n- X modelActions.createActionTo$message* X$ U4 B r, V% Z' N0 m5 K. f+ \
(heat, new Selector (heat.getClass (), "stepRule", false));+ e2 z% ]$ H2 {; s# F8 I# t
} catch (Exception e) {' e' @& M( X6 S8 H, S
System.err.println ("Exception stepRule: " + e.getMessage ());
6 c" I1 v6 ~, J' C) U, | }9 R% R8 m j- h" \- V1 s2 F, q
. S9 a( v6 p- \
try {; C. J! e: j# ?2 O- C q
Heatbug proto = (Heatbug) heatbugList.get (0);! e( y; D5 c) O# E" d
Selector sel = + E; U5 f2 B, }- o+ O
new Selector (proto.getClass (), "heatbugStep", false);' F: T* H" Z$ _' A
actionForEach =
; D8 }1 w5 z( g n modelActions.createFActionForEachHomogeneous$call/ H$ v0 f, v) C( D, k
(heatbugList,4 U' G6 {/ P1 I$ @$ o, [$ l$ p0 I3 n. j
new FCallImpl (this, proto, sel,* Y/ [0 o( m5 X$ e6 y2 B
new FArgumentsImpl (this, sel)));
7 b. \& ]' b. b+ C/ Q } catch (Exception e) {
2 d" O1 [. [' M5 l e.printStackTrace (System.err);/ R5 M& H! m# ~3 M0 D
}0 w4 C; d% g: O3 o. F5 j8 u- s
' m( w1 C7 R8 k( d% \
syncUpdateOrder ();
) [' D6 k1 z2 A' z( E
: W7 a# K- W) w4 U try {6 m9 r' |: D4 p' D
modelActions.createActionTo$message
3 ]+ x+ {1 r# o5 B) l0 q. G (heat, new Selector (heat.getClass (), "updateLattice", false));
5 q, Y! J6 |& n2 R* Z: C( T } catch (Exception e) {
; O# R; P. P' l. X# C System.err.println("Exception updateLattice: " + e.getMessage ());
9 R* _2 T/ v. z% q- C1 f }
9 d1 \( X3 h) U! ]& u8 a- L # G5 J# R4 }7 ` N! F
// Then we create a schedule that executes the
) L6 W- O0 R4 m5 O( M // modelActions. modelActions is an ActionGroup, by itself it
1 {/ u* z: u( v6 l0 [" ~3 P // has no notion of time. In order to have it executed in, J! \2 G. y" g- }( V
// time, we create a Schedule that says to use the
- C8 a$ I# X+ f( c& ^ // modelActions ActionGroup at particular times. This
7 L) N* [) k4 l" \0 ~: K- R% k$ i // schedule has a repeat interval of 1, it will loop every! z9 h/ {& m/ N$ M" c- a7 n2 Y
// time step. The action is executed at time 0 relative to
6 ~7 u7 j8 g1 r, u0 T // the beginning of the loop.
1 {' d1 \: S) m% @$ }: y3 C6 Q* m- |2 K' `; x' k2 D9 L
// This is a simple schedule, with only one action that is. ~ `2 ~& }8 D" t! X
// just repeated every time. See jmousetrap for more
, w+ i" d3 I$ J+ V7 r/ X // complicated schedules.! J# P- v* }" A/ E5 k! Q
. X# v/ ~# a5 ~" e- ` G' r modelSchedule = new ScheduleImpl (getZone (), 1);
* P* G, z& p& M7 Q4 x, g8 ?! Z modelSchedule.at$createAction (0, modelActions);; P% f* e# D" Z' V! M: P
2 D( q. q0 j) H. e/ y" ] F$ ] return this;
! V- O, `. a1 }! i5 |0 e, W. r: J } |