HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
0 \2 Z( F8 j# `8 ^' J$ J3 v. z N6 E" p# S& E$ e+ w( B' z
public Object buildActions () {
. V8 J$ z* ~/ _- Q! E super.buildActions();
. u! W( j% N/ z0 p/ ?$ F
( C1 }2 ^- r0 ^) U0 p // Create the list of simulation actions. We put these in
8 u0 b# w3 t7 P2 E2 F$ Y4 t) j: ? // an action group, because we want these actions to be
+ }" ~6 v/ A3 [9 i: O( N // executed in a specific order, but these steps should z8 r- [8 T) k8 o& c. ~9 x
// take no (simulated) time. The M(foo) means "The message6 c7 S( l2 X/ f7 q# D, l9 X
// called <foo>". You can send a message To a particular
2 S& `# L K' {$ ` // object, or ForEach object in a collection.8 G; P0 {- Z+ m# z7 ?
8 o+ P; I( t; b/ R8 G/ I# T. S' t // Note we update the heatspace in two phases: first run D! w% _2 W$ W& a7 x
// diffusion, then run "updateWorld" to actually enact the
+ D0 N- F# K* v1 ? // changes the heatbugs have made. The ordering here is! _) A5 ]0 l9 _6 Y6 H) H
// significant!
5 |0 H/ `& ?/ V8 B/ l0 e5 a& v + ^, N* C8 k: u0 n" I
// Note also, that with the additional
3 A: b z% J% P# G* } // `randomizeHeatbugUpdateOrder' Boolean flag we can
7 Z7 }( T5 l+ i // randomize the order in which the bugs actually run4 f/ ^& c @6 r- `+ H5 M
// their step rule. This has the effect of removing any/ [2 V5 b$ s; `1 B! ~* W' Z" }! @
// systematic bias in the iteration throught the heatbug0 y6 l8 H& D+ R9 t% Y$ J% r
// list from timestep to timestep
" e/ J8 n# t& j' e" m2 ?) w
7 A D, S" t! ?) x _9 d // By default, all `createActionForEach' modelActions have" x6 {* e- [5 s- h; ?
// a default order of `Sequential', which means that the# w+ K1 A0 V; J3 u& p. f
// order of iteration through the `heatbugList' will be# y: u9 ^5 W! H$ h
// identical (assuming the list order is not changed1 ~) o( T. f, ?
// indirectly by some other process).
+ P% d- v" a0 V 2 m" A+ f Z: V9 b
modelActions = new ActionGroupImpl (getZone ());
2 d* V2 d+ V9 D* c& t! W; D
& r, Y4 F4 A, |- _1 f/ j+ I9 ?) t7 t try {
2 g- G* |, E0 h% t$ I( P modelActions.createActionTo$message8 S8 ], y$ C- G
(heat, new Selector (heat.getClass (), "stepRule", false));8 ?4 z& D1 W* t. s! z
} catch (Exception e) {# F. J$ m: {5 h4 f X
System.err.println ("Exception stepRule: " + e.getMessage ());/ g T+ C' P$ A7 I
}
5 u7 g3 ]( q& ^* X" E" }
! ?2 ~6 N* a, e' E& U% p try {
- G1 X- g; T! c" A( m- R( w+ S Heatbug proto = (Heatbug) heatbugList.get (0);
6 n8 I, ?! Q. a Selector sel =
& c2 X& ^) s) H new Selector (proto.getClass (), "heatbugStep", false);
5 |3 B+ ~$ `, ?) Q) V actionForEach =7 \: D. \3 k( q, z
modelActions.createFActionForEachHomogeneous$call
+ |$ Q) h+ H$ r. t+ J$ O, | z1 w (heatbugList, N0 ~; U( v* z6 y
new FCallImpl (this, proto, sel,( H5 B, y% K P! Y& n! b+ j0 v# m
new FArgumentsImpl (this, sel)));
0 R: G) O; X, |) W } catch (Exception e) {
1 f0 r5 J6 y3 |& R e.printStackTrace (System.err);
4 k& o: a0 T3 m; |1 ~4 k }0 w' o2 d- E9 l
+ d/ o9 A2 J: R3 }/ i: C) D4 c
syncUpdateOrder ();- S! H/ g) }2 M6 P
2 D; U# x: U r5 l0 [
try {
9 a4 X& I- R7 l' j modelActions.createActionTo$message 5 a( P% ~" G6 ~9 M& o4 C) P. V0 r0 X M- |
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 J; m% e! t- G" z. c } catch (Exception e) {- j, ^1 R5 G, g8 J& r% E! \
System.err.println("Exception updateLattice: " + e.getMessage ());) {" O: u, o* ~/ D. w0 q
}# M4 Q3 d; N) h7 |" ]7 v
' m$ C$ D$ x! ~ // Then we create a schedule that executes the5 ?0 G' u) ^3 b* Q9 i3 O. ?4 r
// modelActions. modelActions is an ActionGroup, by itself it
- b9 O+ _; H K) d; V. z // has no notion of time. In order to have it executed in8 j7 k0 H% P | m9 e
// time, we create a Schedule that says to use the
% M0 \' j8 ]7 j* C7 s // modelActions ActionGroup at particular times. This5 H$ ?/ I# s1 g6 `
// schedule has a repeat interval of 1, it will loop every' \2 s' B- t* r5 r' ^% {
// time step. The action is executed at time 0 relative to
: U- f3 f# v) q$ H1 g2 | // the beginning of the loop.
) f+ V6 K) |4 C! J5 \ G' P! \* Q' A
// This is a simple schedule, with only one action that is0 J" m! Y; h k( _! Z7 s
// just repeated every time. See jmousetrap for more% ?+ z- x8 r) T4 H) k
// complicated schedules.
: i7 y5 _* q+ _' m+ A
! l: U* z+ D, i, V5 X. s; ^ modelSchedule = new ScheduleImpl (getZone (), 1);* G9 R- d k; G& @ a+ F7 l
modelSchedule.at$createAction (0, modelActions);
+ ~6 h/ {7 o0 B3 Y! H: E
: N" {9 W( e+ q" b C! T' A return this;
" i0 H. |8 l4 M7 v9 Q+ y. B } |