HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 ?' L, J5 o1 g6 A4 V; l9 Q( i7 T7 L8 e3 N4 \3 H7 k$ l- d
public Object buildActions () {2 j/ d4 `) N# G Q- C& j
super.buildActions();
/ ]: A/ ?' O/ ] ; R& ?5 h5 r8 a
// Create the list of simulation actions. We put these in
: p' Z; n6 j# R9 Q: F // an action group, because we want these actions to be1 E# M% K& @6 l7 n1 B/ J
// executed in a specific order, but these steps should; C/ U% |$ w7 w/ F& Q ~) P
// take no (simulated) time. The M(foo) means "The message# o; C* b' a% Q' H3 Z0 |. g
// called <foo>". You can send a message To a particular
$ {/ n, g/ y$ O* @1 e0 l$ ]5 Y% I // object, or ForEach object in a collection.) L8 u! x \/ X; i1 X) Y+ z* r
- g& u7 U7 ?- `1 d: s0 o // Note we update the heatspace in two phases: first run
1 U3 r- |! C6 O. m7 F // diffusion, then run "updateWorld" to actually enact the
. ^8 t E% Y0 e // changes the heatbugs have made. The ordering here is
+ [ s$ ^3 ]% [5 ~* A$ t& @' r // significant!9 f+ }1 v; T! Q |: Z1 L, G
, Q7 _: v5 ]: i" h# s! U3 p5 f# x# u
// Note also, that with the additional+ n$ M: T% Y) s" ^ N
// `randomizeHeatbugUpdateOrder' Boolean flag we can
0 b6 b- z9 I& ]+ B8 L" j // randomize the order in which the bugs actually run6 s" ?/ M) i- ?# I8 W
// their step rule. This has the effect of removing any4 V; `# U- o1 ?
// systematic bias in the iteration throught the heatbug, d9 k- k' r* Y" f, }
// list from timestep to timestep
8 o# q( l* a4 S) g2 R6 ^- Y
6 m I! X6 U% @3 Z# u // By default, all `createActionForEach' modelActions have
$ m* R/ Y5 W# Z1 a/ X* [ // a default order of `Sequential', which means that the9 M1 y) m5 [- q0 ]1 D* P4 W( \: h- Y
// order of iteration through the `heatbugList' will be
: ^% i) G2 ?4 t2 A$ ~+ P( N // identical (assuming the list order is not changed
1 o3 M* [) X: k // indirectly by some other process).
9 h5 [/ C) \1 o $ I% H9 I: J% M8 }! C& h w! I6 D& A$ `
modelActions = new ActionGroupImpl (getZone ());
7 G9 ?/ N; x* _* l) `2 K8 _5 ]& }
try {
5 \ e3 e2 Q* | modelActions.createActionTo$message
. P7 a& n) g# ]2 x2 n8 V6 @ (heat, new Selector (heat.getClass (), "stepRule", false));* e" y& z, L4 p" n. p h) K
} catch (Exception e) {( A) \& i v% O# P- ]
System.err.println ("Exception stepRule: " + e.getMessage ());4 N. j3 A D$ ]
}
! k2 Q, V* _( k* S) K5 ^' F1 E. j/ q6 W$ P% n [8 ?
try {
. l+ f+ n% p% X' c1 @1 X Heatbug proto = (Heatbug) heatbugList.get (0);
6 B3 J+ O! U% N) E" l& T) |) U. P* B Selector sel =
2 H! E' `/ z8 n/ r2 f+ n new Selector (proto.getClass (), "heatbugStep", false);
' o. u1 ?8 O! M+ N" O- {( t1 F- @# o actionForEach =9 h+ n6 G/ _$ T8 G; R, a& Q) H
modelActions.createFActionForEachHomogeneous$call
& k& k. T. w! |( o+ b) B (heatbugList,1 b9 n1 @" b" K1 c& K9 I4 O( m* ^- F
new FCallImpl (this, proto, sel," z9 a1 Z+ a5 v. H" `0 h
new FArgumentsImpl (this, sel)));
8 T" o$ d, {% w. A1 Y } catch (Exception e) {
; T: _# l& Z0 J8 m' I e.printStackTrace (System.err);" {6 X* A( H: }$ \
}
' L0 l% f* h% z1 L/ m1 _ ; j8 T* E/ m; E7 @! N: ]5 P1 ?
syncUpdateOrder ();
9 B) e- g5 G+ L2 B& F: i# g# `* B- |: `
4 ^4 T" T l0 r3 Z% m* S/ x9 j4 j try {
3 m- N& E/ |; \' U3 x- [ modelActions.createActionTo$message
* K4 T& E3 e- M (heat, new Selector (heat.getClass (), "updateLattice", false));+ j8 C9 C2 e) S2 L) H+ H
} catch (Exception e) {
8 Q" d! Q* {2 @/ a i System.err.println("Exception updateLattice: " + e.getMessage ());
) R2 M$ C. D& u; G( y+ e4 I }
, T4 Z4 G- u6 U. X % q9 G' J. Z7 j5 ^% A/ C4 |
// Then we create a schedule that executes the7 N! K) C9 t) C4 U6 p h
// modelActions. modelActions is an ActionGroup, by itself it
% g! F5 w& V# U# D% J3 g // has no notion of time. In order to have it executed in
5 D9 B) T) h# g/ a" \" B // time, we create a Schedule that says to use the# v( V! M5 j/ q5 y
// modelActions ActionGroup at particular times. This, s; ?& A& D0 z! z- M4 n
// schedule has a repeat interval of 1, it will loop every3 E& J) l; y9 d0 B
// time step. The action is executed at time 0 relative to
2 h( d7 l5 \( h1 B // the beginning of the loop.4 @! w3 R: n8 q, Q7 E' U
3 m% J' U& p# W, p
// This is a simple schedule, with only one action that is
; a8 A( P5 `: P! b, |1 D% j' a' M // just repeated every time. See jmousetrap for more' u. s' Y7 y4 \. `& z: f
// complicated schedules.# |7 F5 }% x9 O3 v& _
* D( ~; V' V n7 ^, O modelSchedule = new ScheduleImpl (getZone (), 1);
6 R6 o# o" d' l( i( n modelSchedule.at$createAction (0, modelActions);. m, z' G3 H- n& K
3 W* {, d4 y3 [+ r0 G/ i
return this;
! P8 x, v% u) ]' | } |