HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 H4 n3 U. q1 q6 Q0 l
0 c% y$ ^0 L) x+ X/ [! ^2 l7 g public Object buildActions () {- M$ i" x) \" F" E
super.buildActions();4 @9 j/ g+ }5 @: P M
* Z" o* p3 Y: |
// Create the list of simulation actions. We put these in0 Q2 o& [2 V; i8 a. k* q
// an action group, because we want these actions to be
' d& U; R7 L L6 E' A% r // executed in a specific order, but these steps should
3 }9 \# m/ _. a5 a( { // take no (simulated) time. The M(foo) means "The message
6 o! y8 t: ~: k // called <foo>". You can send a message To a particular
0 Z/ Q; _! F" @ // object, or ForEach object in a collection.; s6 X1 G* ^7 N; _- x
! f+ v# v' V4 S. r3 [ // Note we update the heatspace in two phases: first run! U. t; S3 d5 b5 x
// diffusion, then run "updateWorld" to actually enact the
3 t1 ?# u) H# ~7 Q6 h/ c$ b // changes the heatbugs have made. The ordering here is
1 m5 n# {- m$ h4 { // significant!
/ R' C* q, N. I4 Y: A' j! d# z( @) G 1 Z/ G0 L' {& p8 _4 G
// Note also, that with the additional- w: O2 e$ S; p# Y5 [$ n7 O
// `randomizeHeatbugUpdateOrder' Boolean flag we can
7 B( I4 C& g9 m9 j2 Y6 v/ R // randomize the order in which the bugs actually run
$ {: ]) H9 b. a# V // their step rule. This has the effect of removing any
2 p/ {9 e% u; b1 O9 f. n! b // systematic bias in the iteration throught the heatbug- A& s) E* k- Y
// list from timestep to timestep, c- T( J. b5 z0 s
* |% U/ M9 s; u6 t' \4 x7 }# v
// By default, all `createActionForEach' modelActions have+ m& B0 ^1 s# n
// a default order of `Sequential', which means that the
5 _ s# u( S. f/ q6 { // order of iteration through the `heatbugList' will be t; D& u) V# k4 |2 u
// identical (assuming the list order is not changed! P# j' Y" O/ S
// indirectly by some other process).
! s) E8 i& G. k2 Y: l ' B6 F+ w, |+ `4 ^; p
modelActions = new ActionGroupImpl (getZone ());
! c) H ]$ O8 N' S& B0 V0 Z. G3 q7 ~2 B
try {
. B1 F8 e: z* R J modelActions.createActionTo$message! Y; a9 ?( l# v
(heat, new Selector (heat.getClass (), "stepRule", false));7 K$ x" V7 s8 S
} catch (Exception e) {8 w+ s# x2 ?5 ~5 h- [2 b& A% p
System.err.println ("Exception stepRule: " + e.getMessage ());4 ~5 L% h: W/ M' @6 D0 H
}
, W4 s' n0 R* n2 U4 `% t+ m% I7 G3 Y. a% A ?% |
try {; R0 i# `, O! }; m' q1 f9 c
Heatbug proto = (Heatbug) heatbugList.get (0);
2 F0 Q! t1 D0 l% z Selector sel =
, V7 r% z" O6 r6 G( ?& B' [' Y! O$ ? new Selector (proto.getClass (), "heatbugStep", false);
0 s( c+ U& n0 d* H& r actionForEach =9 ^. {' ]1 ]' q0 d: V2 [* f9 q
modelActions.createFActionForEachHomogeneous$call
# p, a& q7 W0 w' B& Q; @ (heatbugList,
& [" S1 L) e& v9 U; y3 Z new FCallImpl (this, proto, sel,; ~1 {8 X% q# `9 ^
new FArgumentsImpl (this, sel)));
6 ]( O% Q7 @" ~9 o } catch (Exception e) {
4 G/ m9 d! f' `: f7 u) J! ^ e.printStackTrace (System.err);/ x: D3 i* z5 K
}
* \% l6 `( I. l W: i( t . D/ f6 e( L1 Z( ~5 @
syncUpdateOrder ();& A% @' [2 b, e! v3 Q" f: E
' x4 M, T5 U$ I/ _( q- i; b( |
try {
& V+ B: ]0 B, {: O$ f0 p modelActions.createActionTo$message
& G( G/ b/ a& B" x (heat, new Selector (heat.getClass (), "updateLattice", false));
' ^! y! _0 k8 q# Z } catch (Exception e) {. c. {, W' W8 ~& h6 v; b, i
System.err.println("Exception updateLattice: " + e.getMessage ());
; |( `* Z1 l A }, J" h8 ?& ~/ z7 k4 N' }
7 }: [$ s2 Y4 |' W7 X/ M
// Then we create a schedule that executes the5 F% F6 E* x0 z+ \
// modelActions. modelActions is an ActionGroup, by itself it! x7 Y# u# B& ^! T$ S
// has no notion of time. In order to have it executed in
) d- U, x1 w* r3 o. A. J2 C$ s // time, we create a Schedule that says to use the) ]. Z3 n+ H+ W0 c: @
// modelActions ActionGroup at particular times. This+ _8 D: S2 x. J5 x2 f0 [; }4 H
// schedule has a repeat interval of 1, it will loop every
0 w* P7 H& Q" b& ? // time step. The action is executed at time 0 relative to
5 }7 h# {0 m- r% T9 D // the beginning of the loop./ y! t+ @: n$ A$ W7 d
' ~4 l" I# a2 }
// This is a simple schedule, with only one action that is7 b# E# L( w x0 |- ~( p2 m
// just repeated every time. See jmousetrap for more, N" a; Y$ D$ @, Z! z @& W2 V
// complicated schedules.
/ u8 A! `$ Q6 \: b. Y7 A. S A0 v% ]' X! v/ y( p& t+ G
modelSchedule = new ScheduleImpl (getZone (), 1);
) R* B. R5 H; q) [, c( L. B8 y% O modelSchedule.at$createAction (0, modelActions);5 ?9 w S8 y, i8 a% }8 t+ n
! G" l( o+ g5 i# @9 }
return this;
: O) B* g+ v6 r, `; K0 A } |