HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, E* _7 E4 m% \% S* P& t# R+ a
" v. D3 ? e2 ?( w3 U8 E) S public Object buildActions () {
7 h8 E0 U6 m& a! ^! q0 ? super.buildActions();9 Q/ W" L$ t0 h
; ~# B6 C! e2 U' ^; f' w // Create the list of simulation actions. We put these in
) \+ R& Z5 h' ]6 L // an action group, because we want these actions to be! I t0 M4 n0 ?6 S: b3 d1 H! f
// executed in a specific order, but these steps should7 d) j. O7 y3 t, E8 {
// take no (simulated) time. The M(foo) means "The message1 U5 \6 U- _( {; K# P5 Y
// called <foo>". You can send a message To a particular
6 f( W7 J& x" h' X% m6 ^ // object, or ForEach object in a collection.
/ c4 o$ L! Z1 x3 f. @9 G, E
- }4 ]. z# @6 F( u ~ // Note we update the heatspace in two phases: first run" ]: |9 I; D I* v3 {3 q+ d
// diffusion, then run "updateWorld" to actually enact the' w2 n* `- |; s1 m8 L& D k% {
// changes the heatbugs have made. The ordering here is
0 @: q$ Q$ }8 h // significant!# R% r" I& d; B& I
. B: a+ h: v& M+ L X _
// Note also, that with the additional
2 H1 G) X- x$ V2 n* y2 U0 v // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ g' H+ o3 L6 k, X // randomize the order in which the bugs actually run" }3 ^( N* ?+ Y; c" s, Y" o3 K, ?
// their step rule. This has the effect of removing any
: ?, B' B8 z$ f3 v2 u0 p) [ // systematic bias in the iteration throught the heatbug* E* ]5 q* ?. N4 Y
// list from timestep to timestep
, T1 U; \+ V" \$ Q - A( W+ I4 z/ o4 e# J n
// By default, all `createActionForEach' modelActions have( x1 x3 ~9 }% ~2 w% z; U& @3 o
// a default order of `Sequential', which means that the5 ^3 N; S$ R" k$ U$ P! _7 I
// order of iteration through the `heatbugList' will be8 S# a v1 A1 T/ V& h5 |
// identical (assuming the list order is not changed' N! d' }5 q: `4 A5 G2 H7 v5 F
// indirectly by some other process).: C2 H: E( c" U1 K- C9 n
4 f# X2 ]( I% b5 ]2 q4 D& i
modelActions = new ActionGroupImpl (getZone ());
' e' B: e! f" f+ w, ~, M/ F. u' B9 g: g. j! e7 l2 t
try {( s; c' ]0 m! X9 {/ Q
modelActions.createActionTo$message X% B) |3 g8 H# I- z
(heat, new Selector (heat.getClass (), "stepRule", false));+ k3 j. o. P2 t. `, e
} catch (Exception e) {8 a' H1 A+ @1 N8 ?; u& Y
System.err.println ("Exception stepRule: " + e.getMessage ());0 m" h3 g. J6 }' x" Y
}
0 S- J! r9 Q( ?' J6 v# z
/ n" ]' L# [2 y' O try {, J$ A7 }1 D) h/ f
Heatbug proto = (Heatbug) heatbugList.get (0);, U S* v& d# x+ y2 d( h1 S: R
Selector sel =
* Z' t2 F, E+ p new Selector (proto.getClass (), "heatbugStep", false);2 {' k$ v" J- o
actionForEach =& X! @' P* N- @' C( g! ?; N$ f
modelActions.createFActionForEachHomogeneous$call! }* b7 w. |+ v( W+ x+ h4 f% \
(heatbugList,
2 P0 L- P+ E1 a$ D2 H new FCallImpl (this, proto, sel,' p7 }3 \$ ~; {/ ^) D+ J& W
new FArgumentsImpl (this, sel)));9 |9 [, t7 ~2 H/ U+ q
} catch (Exception e) {
9 @8 z+ D7 D! P6 Z4 @' ~ e.printStackTrace (System.err);& r1 Z4 w8 \: ]) y
}8 ]9 e& c5 s/ E% T0 s
, I/ v! ?( g3 s! H syncUpdateOrder ();- A7 ?: F4 Q* P: `( [
8 J. s, g+ g2 P) T2 |
try {9 ?! g! |0 a( Y* I2 m
modelActions.createActionTo$message
/ G' f, d1 ~) L; y (heat, new Selector (heat.getClass (), "updateLattice", false));$ r2 Z( ~- G" Z: j1 R, q
} catch (Exception e) {& }- }" F& v0 {3 t: \5 L+ P0 g
System.err.println("Exception updateLattice: " + e.getMessage ());
2 i; {' \( P; ^ }0 F( K4 m1 R% [1 _
" G4 R% K0 N" q# _7 b
// Then we create a schedule that executes the, L& N$ j: ~. K" ]" u U- |
// modelActions. modelActions is an ActionGroup, by itself it
; }2 j, }9 c3 P$ c2 `' z6 S // has no notion of time. In order to have it executed in/ R* I' m& v$ v( i2 z' t# P
// time, we create a Schedule that says to use the
+ b* P7 o+ a$ ^+ R) j, w- f4 x$ I // modelActions ActionGroup at particular times. This8 W% Q1 N3 D$ L# b- @
// schedule has a repeat interval of 1, it will loop every
6 F5 K+ R3 ]" o6 J/ X/ N // time step. The action is executed at time 0 relative to
2 Z }% S5 `/ T b H0 Y // the beginning of the loop." t P+ G6 i, P9 K }9 O
) M% \$ \" x; q5 F" L) A5 I3 k
// This is a simple schedule, with only one action that is
2 [, a, O& [9 d4 M. `' T; ~- @ // just repeated every time. See jmousetrap for more; v% }4 r( H/ b0 ~. K3 q: j
// complicated schedules.
) I+ l& E7 i* x% | R$ L 4 \" G5 M, R+ e. a3 M
modelSchedule = new ScheduleImpl (getZone (), 1);
4 l1 ^' i b8 r; O5 Z$ N3 S0 s modelSchedule.at$createAction (0, modelActions);9 q# G5 D! y" I3 Z3 M) q
' K) X5 B2 l# Q" l
return this;
6 d3 D; P. Y Y4 P. y4 i' d; o } |