HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 u" w+ A& p. c# l Y
- o# `" _* y7 Y$ t public Object buildActions () {) Z7 j# ]& I- a
super.buildActions();7 L3 ]) d; x7 V& Y' P8 v% H
) O- @( l+ S# x8 e4 P. T // Create the list of simulation actions. We put these in! L+ p* Z9 ]5 ]8 `# B
// an action group, because we want these actions to be& P, C: T# Z6 j/ K) Z
// executed in a specific order, but these steps should9 j3 d L: ]8 B2 |" `6 }! I- N* E; A
// take no (simulated) time. The M(foo) means "The message; Y3 ^" S6 r3 b# G4 W
// called <foo>". You can send a message To a particular) |4 ^+ ?( c5 }2 W0 g/ m
// object, or ForEach object in a collection.
# N: n% ^6 G4 R. B/ h . `' d) ~9 T* A5 C/ b
// Note we update the heatspace in two phases: first run
k/ X8 D- v" W- h( p# r' r // diffusion, then run "updateWorld" to actually enact the0 P5 r+ P4 ?6 T
// changes the heatbugs have made. The ordering here is
" `' |8 C' ~) ?1 f // significant!
+ A. w% o+ |# d! W8 ` d4 F' j3 I: C
* a J) V/ r3 m9 N& I: a // Note also, that with the additional
! K5 C# G. N! _# Z // `randomizeHeatbugUpdateOrder' Boolean flag we can/ `" o3 O6 g: A7 t" H) i
// randomize the order in which the bugs actually run s! G3 x; q& @3 [+ Y" a, h P
// their step rule. This has the effect of removing any6 j3 C! {5 F9 w5 `& x
// systematic bias in the iteration throught the heatbug" ]# {! e3 D: L7 N6 e8 k8 r O
// list from timestep to timestep+ s9 k. T; e, C
% A/ n- ]' F) }2 G& } // By default, all `createActionForEach' modelActions have2 N# Z3 {1 g9 S6 J0 w* A c
// a default order of `Sequential', which means that the; j$ R: d8 B) g- F; @: n4 z
// order of iteration through the `heatbugList' will be
+ ~/ s$ ?* V0 r ?+ M // identical (assuming the list order is not changed- y) T. c- w, Z
// indirectly by some other process).- _& X4 s! ~$ X+ k; ]- `7 M, B
3 m! g9 ^ W/ Z7 k$ S modelActions = new ActionGroupImpl (getZone ());. `" j0 w' N" I1 b6 c1 d
( h d4 g) p6 u. v( O try {
" r. w) ?: U$ X. C8 p! M$ r modelActions.createActionTo$message
! B1 N7 y9 ]1 `; v2 }- F (heat, new Selector (heat.getClass (), "stepRule", false));
: T& _5 e) P; P } catch (Exception e) {9 H0 _! y" `( ^6 H, @/ ^2 Z* i; `, F
System.err.println ("Exception stepRule: " + e.getMessage ());
0 d( V/ C6 N- U2 Z- _6 s }+ p1 j: \7 O0 Y( l3 C$ k, X
5 q% d& Z4 G! A6 d- W0 O9 A) I( n
try {3 B2 e$ [, r1 j- d* }( O- r/ c
Heatbug proto = (Heatbug) heatbugList.get (0);
3 X0 k2 N7 S: {+ |* I: W Selector sel = 7 [8 A" x& ?# S3 q
new Selector (proto.getClass (), "heatbugStep", false);
* j; W# }% D4 l3 B actionForEach =
' r8 V& y/ D3 \6 b) w$ o5 E, b modelActions.createFActionForEachHomogeneous$call$ X* w4 w3 P9 @9 r7 |* D- s
(heatbugList,
' b5 r9 o: @9 K0 e; n' W new FCallImpl (this, proto, sel,
. n8 [$ r E, L. w) P& R new FArgumentsImpl (this, sel)));
1 P# U9 ]) u5 |6 I, ~ } catch (Exception e) {
+ ~* T) o; s8 u e.printStackTrace (System.err);
& f/ O9 ^2 ]5 g* g }
4 x$ M; U/ T7 \, B8 b7 }% j4 q 4 K- n D* z5 Z7 s' e
syncUpdateOrder ();
8 [7 T2 W( R* j# r) V
* r2 ]- a# m5 C8 ? try {6 ]$ G0 B0 y& ?# i
modelActions.createActionTo$message
, S- x( n! U$ k- c' a9 ]# ^ (heat, new Selector (heat.getClass (), "updateLattice", false));" I( D3 A" e1 {& ^$ O0 T1 ~
} catch (Exception e) {
! x+ g3 D! J4 c System.err.println("Exception updateLattice: " + e.getMessage ());
8 p3 }3 P* [6 N. S1 o }
/ |- m. @4 P) |+ E9 ~ " W3 k, \6 K% a9 |+ y& z
// Then we create a schedule that executes the
- `: X) B5 S# y1 B9 f6 |4 v" e) y // modelActions. modelActions is an ActionGroup, by itself it
+ }5 F4 A; _# k // has no notion of time. In order to have it executed in6 b- \8 _3 R' B" B/ x
// time, we create a Schedule that says to use the
7 Z& D1 |7 ~2 C. k9 e) V // modelActions ActionGroup at particular times. This
- y' w% O! L w _7 w% F // schedule has a repeat interval of 1, it will loop every
3 }' q+ n* s5 a( B/ d- n) B2 P // time step. The action is executed at time 0 relative to
t6 @8 X9 n9 u: j2 o1 W: G // the beginning of the loop.
s/ a# N2 u3 }8 i+ ]6 f" K; v( k: d, G+ a8 T
// This is a simple schedule, with only one action that is" g" ?" _; M- k. P$ I
// just repeated every time. See jmousetrap for more
. o' n# _3 ?9 R // complicated schedules.3 n5 q6 e- j- E1 q3 T+ X
z% z# i3 u) K+ E
modelSchedule = new ScheduleImpl (getZone (), 1);6 [5 M+ B1 f o' ^" p
modelSchedule.at$createAction (0, modelActions);: z' L) P1 @' [* N m7 c0 Y
8 K+ J) b' o) p% h2 U( L return this;! g0 Y: k' e; L) r
} |