HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, k S0 M1 K' c$ Y, Y# b& _
9 _0 e+ A- s2 Y5 ^! X public Object buildActions () {4 W0 g& u, G; K; w, n9 ?9 }0 P( i1 t
super.buildActions();
2 D# D% A7 ^7 T" H; ]
4 Z4 V) ^; V! x7 M // Create the list of simulation actions. We put these in4 H& f4 b% o& \# F- \4 O& G- m( B, }
// an action group, because we want these actions to be" p7 s+ \/ o. `5 q/ E& F9 p I6 `
// executed in a specific order, but these steps should( ]: |& q! c5 G: P7 F' S# O( b
// take no (simulated) time. The M(foo) means "The message
; I9 \. a9 h P& J7 e& b# G // called <foo>". You can send a message To a particular
! e' w4 }" V( \ // object, or ForEach object in a collection.
5 R# g6 S) _/ I# _1 T( w5 z) ]8 z 2 o. ]" Q7 X! }" a7 I( ~
// Note we update the heatspace in two phases: first run
. e) ~3 ?* C/ L$ f p5 f3 ~ // diffusion, then run "updateWorld" to actually enact the
; G2 O" ~3 @. p, d" T) g5 v // changes the heatbugs have made. The ordering here is3 I; Q: ~( w# f8 j2 Y4 V' F8 G
// significant!
# n F& S% _( J1 q/ U! s ) [+ u, m% Q i+ y
// Note also, that with the additional
9 Y; ?8 |; E4 X/ X, }! I# D // `randomizeHeatbugUpdateOrder' Boolean flag we can/ `9 a, ]7 A1 q6 A; R$ g; i
// randomize the order in which the bugs actually run
1 k8 c5 C {6 g5 m3 J q( d0 l( \ // their step rule. This has the effect of removing any4 ~: q( _# j! t* K" ]+ @8 {
// systematic bias in the iteration throught the heatbug8 e, A$ H. ]' u$ n
// list from timestep to timestep
, J& e9 |. K5 s, h( a r) Q
" s# r, R; Q9 Q // By default, all `createActionForEach' modelActions have
( ?1 d3 c% }2 Y3 ^4 a // a default order of `Sequential', which means that the
$ ]% `% p8 y; _" {! E // order of iteration through the `heatbugList' will be
' u( q7 g: _5 k7 z // identical (assuming the list order is not changed. r r* M1 c; ?% B/ e. L
// indirectly by some other process).
, J$ c( Q. r, g3 T% ~: j' r9 r1 U0 W 3 ]& q/ [' o( C+ b5 `& ]+ S
modelActions = new ActionGroupImpl (getZone ());- x- g' F4 |) H# L0 N1 v3 k( w
4 j! W+ |" z! P% u; G" R4 |; L try {, y8 d! C0 D+ b2 |$ H
modelActions.createActionTo$message
5 v) U7 t: d" Y& t$ R (heat, new Selector (heat.getClass (), "stepRule", false)); ^) J' p1 {7 j! @ d7 s: `( V
} catch (Exception e) {6 ~+ Q' A$ w; n; V6 i; @8 {; r
System.err.println ("Exception stepRule: " + e.getMessage ());
' ^6 I8 \. k$ J9 m2 n }* K/ I$ z: ~3 }7 |4 A7 ^9 v2 v' n8 ~" ?) e
( \/ I. r5 P. o( d
try {- p; C5 I3 g1 u4 f, I4 |
Heatbug proto = (Heatbug) heatbugList.get (0);
8 I' D2 ?# B6 ]5 ]0 ? Selector sel = # N1 z* v( R9 n
new Selector (proto.getClass (), "heatbugStep", false); G& Q5 U+ v8 }
actionForEach = k- [2 V+ Z: X8 {0 P$ H: M# l% l
modelActions.createFActionForEachHomogeneous$call
1 @# J% U4 j7 d1 ? (heatbugList,% P/ w5 N( H% E: @" ^
new FCallImpl (this, proto, sel,
3 j. I8 D& a( t3 ?* Y) c5 T* d new FArgumentsImpl (this, sel)));
' i8 C' o. }$ L& B2 y/ J } catch (Exception e) {
) G) E/ U8 s' u! @0 |+ h e.printStackTrace (System.err);
( X& M# s; M4 D% O& g; }2 ]( s }
6 X2 F5 L8 e% D$ s4 ^, f1 P: D 1 V1 D* A! |4 J5 o8 W2 X$ i% c
syncUpdateOrder ();8 C, G1 w5 F6 I1 t
. W I' l: |2 r* z4 [7 ~/ R try {7 \4 H/ d3 }( v. O: j! Q
modelActions.createActionTo$message
$ v: @4 o, n% c0 E* E (heat, new Selector (heat.getClass (), "updateLattice", false));
$ {5 o" ~9 U( U* d } catch (Exception e) {2 X4 _+ M4 m. ~" I& a: I8 L
System.err.println("Exception updateLattice: " + e.getMessage ());0 ?0 l( K% Z. H% I
}
; i" H0 p% ]6 m% N . Y$ T& \" E4 a' M$ m, U, p( U
// Then we create a schedule that executes the, q4 G/ }1 N, a8 T8 k; P! t o: c& K
// modelActions. modelActions is an ActionGroup, by itself it
O5 K2 A3 a; W // has no notion of time. In order to have it executed in/ i: C- g9 j L( Z
// time, we create a Schedule that says to use the
2 ^4 I# Q6 v. {! B. S' l // modelActions ActionGroup at particular times. This8 i- `) s' s% }9 N
// schedule has a repeat interval of 1, it will loop every
. E5 L: d9 }2 T9 i6 y* q5 I // time step. The action is executed at time 0 relative to) i! J' J+ _3 `- I7 {0 l
// the beginning of the loop.' A9 i6 U8 N7 w" I: F
( v' ]6 ^- N) q0 p- \
// This is a simple schedule, with only one action that is
0 t1 Z) S. j2 }. q: Y2 ] // just repeated every time. See jmousetrap for more
9 O h. v3 X) Q& l$ p5 n // complicated schedules.
+ f. a# K$ f# P6 k3 f / U& R6 Q) I8 I" Y- T
modelSchedule = new ScheduleImpl (getZone (), 1);9 e; O' Y# i! O2 J
modelSchedule.at$createAction (0, modelActions);% G- T$ g1 n9 c( J
2 A4 d7 ?! V, l& _4 E" K
return this;, C! O* \8 a; K3 q3 U
} |