HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
. D3 E0 }# E0 R; V# [" p, Y3 H: A& `2 a$ W; e
public Object buildActions () {" m; U+ r4 Z( i9 e* @( x$ E/ ?3 v
super.buildActions();. w: V: w2 x8 ~) D1 h
, u- k2 L( I( F. t H7 I6 K6 J
// Create the list of simulation actions. We put these in9 ?+ q* Z2 }& L: w
// an action group, because we want these actions to be6 j7 n0 S3 l" H8 M3 P
// executed in a specific order, but these steps should6 B- ]! A8 n' E% {
// take no (simulated) time. The M(foo) means "The message7 j% i. z, b' t2 }1 ]
// called <foo>". You can send a message To a particular
5 Q1 U+ {& J+ r l7 I% e // object, or ForEach object in a collection.6 i1 n; r$ c; V w
! e4 H9 \# C- N1 X2 A# m1 _) N // Note we update the heatspace in two phases: first run7 V4 N5 B+ G6 u+ \2 Q; k, W/ K
// diffusion, then run "updateWorld" to actually enact the
: X1 _( v$ h' I: b: o8 B // changes the heatbugs have made. The ordering here is' @- h+ }& I9 y+ Y! i
// significant!
& z- X8 b0 Q) J* {0 h: A 0 |* \/ ~% K2 U1 v1 T
// Note also, that with the additional7 h$ k# k% v1 g2 M3 B" D
// `randomizeHeatbugUpdateOrder' Boolean flag we can
[/ N6 J. S% b& t2 Y* W3 t // randomize the order in which the bugs actually run
# ~9 N L; i+ a# t" a8 E+ ]: K1 I // their step rule. This has the effect of removing any( D6 N& k8 l& N" d6 v
// systematic bias in the iteration throught the heatbug) s8 s8 c8 c+ Q6 w
// list from timestep to timestep
& s, i5 z E4 A0 j. }/ @: F- ^3 R + j2 }. F; ]" Q% ]# e5 _6 N
// By default, all `createActionForEach' modelActions have
" u, v2 _* ]8 [3 @% @$ z# n# e // a default order of `Sequential', which means that the5 ]: q, g$ o; c5 q( C
// order of iteration through the `heatbugList' will be3 n6 c( t( o* } F# ?
// identical (assuming the list order is not changed* ~4 Q( T# k0 a( I( P0 y
// indirectly by some other process).
' I. o. @% W1 L6 n" }" i
: R% {6 g& Y0 g u modelActions = new ActionGroupImpl (getZone ());
! X0 ~: d1 K+ z: e8 ~
, ^+ H, Q+ V2 b try {
& P1 }/ M2 ?$ g modelActions.createActionTo$message$ y0 I' f, R; X) Y
(heat, new Selector (heat.getClass (), "stepRule", false));9 W+ e5 B& m) X
} catch (Exception e) { A( z' v; W* }' z. R- }" I
System.err.println ("Exception stepRule: " + e.getMessage ());
" T3 V- r5 E/ \# b3 [9 g }
2 z! N4 T/ ~: n0 ~
" c% [7 Q( o+ @% M" H+ Z; C# P try {) c, Q1 @! Y8 q, X
Heatbug proto = (Heatbug) heatbugList.get (0);0 Y7 o+ U6 b( N# N( J
Selector sel = 9 c! y! O$ k' p8 f8 G& E. X
new Selector (proto.getClass (), "heatbugStep", false);7 R; L; K K' `) [
actionForEach =
' {7 x5 x- {3 T/ V- }% i4 T4 S/ ?7 ^/ b modelActions.createFActionForEachHomogeneous$call
) ?+ {+ l% B* P/ k+ U: D" W/ k, i |4 r (heatbugList,
" t8 n) q1 F: }5 |: ]8 T, ] Q# U* g. {" T new FCallImpl (this, proto, sel,& p; w$ f4 Y/ ?/ W
new FArgumentsImpl (this, sel)));+ I" U) d" {9 e3 Z
} catch (Exception e) {0 q" o# H* e! ?3 R( K, P$ {6 P
e.printStackTrace (System.err);' \: I1 c3 [2 s7 ^6 w7 ~
}7 k3 |! a H2 o9 x
$ Q) }/ {1 L( @5 w
syncUpdateOrder ();
# s) M2 u. }. V: ?* Y0 o& T9 e" W4 m6 }/ ^, G; x% ~1 t
try {' Y, F0 ?- q& L8 O0 K
modelActions.createActionTo$message
0 k" j. `/ I3 U7 l! ]7 M (heat, new Selector (heat.getClass (), "updateLattice", false));
4 q# i9 t/ O @, L7 H } catch (Exception e) {
{. N6 u' ]9 f& `9 n System.err.println("Exception updateLattice: " + e.getMessage ());
. I8 l0 P) a2 Q# o }* V& T. F- W# h. _) D" }
5 s+ }% Z; ~% q' ?1 F1 f( O. G
// Then we create a schedule that executes the1 |4 r1 [$ G) q8 [5 a1 [+ D6 {
// modelActions. modelActions is an ActionGroup, by itself it
$ U, X9 @. R' q- o6 }' t+ C // has no notion of time. In order to have it executed in* Z0 @4 q6 Q; |) q, j' J# ?
// time, we create a Schedule that says to use the1 H8 j4 B: l8 [ L
// modelActions ActionGroup at particular times. This
4 j$ K" E& c4 v$ o8 V. [+ \ // schedule has a repeat interval of 1, it will loop every
6 q0 J% ]2 t$ h5 ~ // time step. The action is executed at time 0 relative to
* B. Z4 h! Y& l% M$ ^ // the beginning of the loop./ X& |6 Y/ x0 q2 s
* L5 |7 I9 X) G6 Q* s. m // This is a simple schedule, with only one action that is, N' q" Q7 n% } M: Q# U$ h- k, y
// just repeated every time. See jmousetrap for more- z- o6 q* @! l; C7 K
// complicated schedules.) Z, p) s; J R" O- i) Z- g. x9 I
$ \& G# R/ }: p" O+ [7 I modelSchedule = new ScheduleImpl (getZone (), 1);* G) C% {# v/ ?
modelSchedule.at$createAction (0, modelActions);
' s2 A. v1 f3 w6 O9 T* P% y3 _ : J5 P+ y3 \3 b6 Y/ ^. ]0 x
return this;
# K# k- v6 R9 g% j } |