HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:+ Q7 B `- Y1 y& O5 i+ Z$ C
9 E2 B9 @: q* t: T- c% j/ G I F
public Object buildActions () {
y1 m4 ~. E2 C& {" n super.buildActions();" s) w# t Z) l8 N- Y
6 ^+ V( R1 ~8 ?8 ?% w
// Create the list of simulation actions. We put these in
! ~5 @* ]# M# j$ P4 O) I$ w, d // an action group, because we want these actions to be
( V- F% @6 m' | f+ j- R% P // executed in a specific order, but these steps should
% b R' Z6 \9 \( q // take no (simulated) time. The M(foo) means "The message8 |5 u1 Z; j$ \* t
// called <foo>". You can send a message To a particular5 e' |! y0 u9 X& O( r* O/ L" u
// object, or ForEach object in a collection.' n" n) N3 ~0 M! q! l+ M* Z: |( Q; v
9 d6 P% m9 h. f$ x/ u
// Note we update the heatspace in two phases: first run& f! J7 M7 i* I9 u' ~0 |; H0 u
// diffusion, then run "updateWorld" to actually enact the% D/ e. A$ y( U' y; r; a
// changes the heatbugs have made. The ordering here is
: W# q& R$ f: F$ L c9 _8 c // significant!
2 l# \; z( b+ ]) i9 F8 L
0 {! x- x$ B( ^, K% y3 Z; ]# }, r6 d // Note also, that with the additional- Q) P' h* Y. b; u* a; q
// `randomizeHeatbugUpdateOrder' Boolean flag we can
1 L6 N, C3 ^# Q6 a. |$ g- o // randomize the order in which the bugs actually run7 L6 {1 o2 ~- k# h
// their step rule. This has the effect of removing any
9 Z; \" t! A1 n1 S5 q) B // systematic bias in the iteration throught the heatbug) u1 d* ^' O0 u: F
// list from timestep to timestep
+ U2 V% \5 i) ? ; l- f% x- z3 p
// By default, all `createActionForEach' modelActions have" Z; V) S" ~" n+ W. s4 g
// a default order of `Sequential', which means that the
1 n! g, [+ B" |- V3 B // order of iteration through the `heatbugList' will be
. Z) m# w8 t& q; o // identical (assuming the list order is not changed5 ], U1 S3 c; u7 i A
// indirectly by some other process).2 `, V6 L3 Z/ P' A1 P4 S/ s6 c( ]
0 |8 I2 P. b- H; Q4 `+ c modelActions = new ActionGroupImpl (getZone ());
0 E6 s8 |9 Q7 `" A! `! {; P, }! e
! ~6 |% f) B5 Q8 m) X try {
1 f, S9 e& F8 ?0 x modelActions.createActionTo$message! Y1 i* m/ F3 P
(heat, new Selector (heat.getClass (), "stepRule", false));# y) `% B/ ^4 |6 I8 e* t8 J
} catch (Exception e) {
2 g6 H0 |/ P+ q% b y System.err.println ("Exception stepRule: " + e.getMessage ());
. v( N- J3 u# I: j h }
2 g- f( F' w9 B8 Z3 z7 o! ~% l8 T
try {
( r- F$ S; |0 Y, T# q0 s& n Heatbug proto = (Heatbug) heatbugList.get (0);6 G2 z( X2 ]) N+ e0 U( g% e
Selector sel =
; @: t4 f4 A# [* N' Y9 V new Selector (proto.getClass (), "heatbugStep", false);% X7 i; m+ m" {$ U7 O6 Q8 f
actionForEach =; m" K6 }* I2 l
modelActions.createFActionForEachHomogeneous$call$ b, X, o. ]* C5 }+ ?5 x
(heatbugList,
( \1 v; K: O4 E1 s new FCallImpl (this, proto, sel,( x6 I2 F5 e* U; D$ X2 b0 d
new FArgumentsImpl (this, sel)));2 y2 i) [6 a" ]9 j3 r6 t% i- x
} catch (Exception e) {% v9 Z+ J$ `3 a. D7 D
e.printStackTrace (System.err);
5 w. ?; P( F* t$ Q n }
6 ? d; n! s4 t " ?% Y/ X2 B: L0 i/ D% n5 K1 q4 e
syncUpdateOrder ();: K- Y% V$ n4 m9 s- y
: [% I9 T" ]/ T: T# N G try {' o5 {1 z0 F2 E/ t& i/ z
modelActions.createActionTo$message * m0 E9 H# ~! j2 j! U2 W( W
(heat, new Selector (heat.getClass (), "updateLattice", false));
* w$ z$ n: ^; v! n8 F, ] } catch (Exception e) {& t9 Y" F2 u1 W5 e
System.err.println("Exception updateLattice: " + e.getMessage ());! i3 g- s! G' z5 ~7 U1 Y1 e
}- E; u4 j% @# J1 y
* a4 O5 K9 u. z# N
// Then we create a schedule that executes the) ?' v; V+ p3 J- H
// modelActions. modelActions is an ActionGroup, by itself it( E0 a8 v+ `. g$ s+ ~) e
// has no notion of time. In order to have it executed in
7 ?, d5 @3 N* g // time, we create a Schedule that says to use the" A- Q" u* j; Y+ Y
// modelActions ActionGroup at particular times. This4 v& H, d( }! j' c( L5 y! V, j
// schedule has a repeat interval of 1, it will loop every# w- d1 `( ?6 Y+ n. O2 ]
// time step. The action is executed at time 0 relative to
( f R7 h# Q3 u2 a // the beginning of the loop.5 ?* V$ ]! H9 w+ r0 [- ^
g7 Y$ \( y- L
// This is a simple schedule, with only one action that is7 I/ O* S o* b0 ], n$ w
// just repeated every time. See jmousetrap for more6 g& b) Q- L1 s
// complicated schedules.
& Z( m* F7 A0 z' e
/ v$ L- T ?! Z) x8 ~0 _2 _8 O' Z modelSchedule = new ScheduleImpl (getZone (), 1);, w: s& r* z' T# J8 L3 W: i
modelSchedule.at$createAction (0, modelActions);( u& s: Y7 U# |, _5 n: }
" L9 G$ J3 v7 k! J
return this;! w. g4 x) A2 a3 Q
} |