HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, ^: I( b, n! @8 W
8 p# B, p; P" y1 _% d public Object buildActions () {/ j( G$ n7 @ C2 B
super.buildActions();' q+ u, B) |1 n- z# a; A, y
+ g4 y: W% ^: d0 V; K. }* A; H // Create the list of simulation actions. We put these in- _0 T5 J. Y' }6 C/ r) }" y
// an action group, because we want these actions to be
& R e d! t9 Z6 G+ Q0 H: h // executed in a specific order, but these steps should
; K. d# i/ _9 h5 V6 B- } // take no (simulated) time. The M(foo) means "The message
% U& O" N3 ]3 P: r( u: Z( r // called <foo>". You can send a message To a particular; X/ ]9 P& ]; i1 n7 c/ x+ g
// object, or ForEach object in a collection.8 Z2 g: T* q' f2 O& Q U% ~/ w
# ` M! G/ P4 T, o! ]
// Note we update the heatspace in two phases: first run
" b8 V0 Y- k+ M2 T // diffusion, then run "updateWorld" to actually enact the Y3 K4 F" S" O6 {! _
// changes the heatbugs have made. The ordering here is0 n$ n$ q% Y' r/ I# E
// significant!
, j5 A, \. O( x1 v) f
2 T6 j- m$ y4 b- m( ` // Note also, that with the additional! l4 \6 p3 I# i$ Q( `
// `randomizeHeatbugUpdateOrder' Boolean flag we can
) B5 x: l9 t2 u0 J% z8 U) h // randomize the order in which the bugs actually run
3 b7 n) n1 ]& T) l$ Y3 t9 M // their step rule. This has the effect of removing any2 Z9 l R5 |9 `, X
// systematic bias in the iteration throught the heatbug
" i9 e2 a/ Z0 [3 R# P7 ~ // list from timestep to timestep
: m2 Q: {' E7 F( z c3 h I* z! Q0 Z% p" s1 m6 M; w
// By default, all `createActionForEach' modelActions have+ |% _$ e& W2 u4 R7 _
// a default order of `Sequential', which means that the
/ B7 Z- E$ U% X8 |2 C9 B // order of iteration through the `heatbugList' will be
" n% j/ i7 a6 Y9 w // identical (assuming the list order is not changed4 z) R5 J. |2 P+ x! A5 ^" b" Z$ l
// indirectly by some other process).' o3 T, g. J5 V% ^: f, Z$ f* `
* B! a b& l5 `$ @# L
modelActions = new ActionGroupImpl (getZone ());
: I; t( y6 v9 C- W/ v' }4 i% o* n" n% o- P3 _6 _9 f; C1 [
try {
1 G: y: L- q i. O: D modelActions.createActionTo$message5 r# _4 N T& A
(heat, new Selector (heat.getClass (), "stepRule", false));
( F! Y+ i4 t Q& b } catch (Exception e) {
- V$ y2 n" q K3 Z System.err.println ("Exception stepRule: " + e.getMessage ());% E+ |4 V" B% t' x$ a
}
w3 L) \' x4 W5 u% W% \' q
2 z3 A* u3 j/ [0 E1 ` try {
2 _5 u( Q( P) _8 A, s' u' i Heatbug proto = (Heatbug) heatbugList.get (0);0 _2 I5 Q- F' ?
Selector sel = 7 V9 J: Z- i9 x- c9 w( z
new Selector (proto.getClass (), "heatbugStep", false);
5 Y( X2 d# ]3 E$ @ H4 U- A0 m+ p actionForEach =
9 u1 B% }0 G; m) L9 Y& ]$ Y. f3 v modelActions.createFActionForEachHomogeneous$call; ` ^- o8 `6 s
(heatbugList,
4 T0 d+ b$ x& a1 b new FCallImpl (this, proto, sel,. z1 q3 _' a- V5 }8 u# d
new FArgumentsImpl (this, sel)));- Y! R# S& t6 R1 q
} catch (Exception e) {
% e3 Y4 ^. o% S2 T, c$ W4 ~ e.printStackTrace (System.err);
3 k" s6 p: l, t$ ? }: ^5 D; X. D8 }; R
- k9 {, F8 c4 v; g1 E
syncUpdateOrder ();8 r6 i+ H6 v9 u7 s% `( t; c
3 c |1 y; |# \
try {
4 C) {2 G1 Q. `9 Q9 _ modelActions.createActionTo$message 0 }0 a. B" n0 ~
(heat, new Selector (heat.getClass (), "updateLattice", false));
% |/ p9 b+ y6 o' M# b } catch (Exception e) {
8 {1 J: |7 Q: c$ v! E7 \' x6 \ System.err.println("Exception updateLattice: " + e.getMessage ());* X0 i" P( O6 N) e. d1 L# a+ l
}! t2 {+ S& r/ L3 ?
2 b" e0 v( Y! m. \; v* ]
// Then we create a schedule that executes the& e8 V$ G7 L! z- r& c1 v
// modelActions. modelActions is an ActionGroup, by itself it, J( B) A9 m8 j
// has no notion of time. In order to have it executed in
$ U) J, d: A f8 }( A6 k // time, we create a Schedule that says to use the
3 D, H0 i8 o; l* F // modelActions ActionGroup at particular times. This
7 J: G; {; P6 l7 f- ~9 J // schedule has a repeat interval of 1, it will loop every
$ C" N) Q7 u. d* e, a // time step. The action is executed at time 0 relative to
/ G0 Q& a+ w* N& L // the beginning of the loop./ I6 l! f8 Z1 c3 }0 |$ b
' D4 D" V, W5 Z // This is a simple schedule, with only one action that is
6 x9 M/ A3 w" z // just repeated every time. See jmousetrap for more
* A* w2 r# p$ d // complicated schedules.
* D' \2 L7 y* T 6 J- ^7 I6 _5 w' @; N) I7 i
modelSchedule = new ScheduleImpl (getZone (), 1);- R+ `5 K+ F* |9 o4 y3 w4 d
modelSchedule.at$createAction (0, modelActions);
. ^' t+ c+ `0 ?4 {# H& j $ s" @' b% M& T8 B6 n k# D, \, C
return this;
2 J& `, Y* Y5 d8 v5 v. ^6 T } |