HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:2 r3 E! d' p5 @( U7 T' w
) X/ ?; U& |8 c9 I7 M public Object buildActions () {0 v9 c/ `+ w) |1 N a
super.buildActions();
+ ^- O* X4 U; o7 T% `. w 3 z4 s. J1 @& S [! t
// Create the list of simulation actions. We put these in
$ b: z1 V8 U/ Q7 h P# l2 f // an action group, because we want these actions to be
' T0 O: V1 |8 o9 f; B0 q& m5 t5 H4 | // executed in a specific order, but these steps should- v" }5 `, }, c$ `
// take no (simulated) time. The M(foo) means "The message
4 h9 h V! e* i% x# R! [4 y // called <foo>". You can send a message To a particular3 |' g" }; T& ]" u, Y
// object, or ForEach object in a collection.- M8 Y; H: w! N8 T. Y& Q* h& V
- ^' b! S4 K% M
// Note we update the heatspace in two phases: first run
- O8 j$ B9 d% n4 [" U5 p \8 h- A // diffusion, then run "updateWorld" to actually enact the
5 D+ b1 a- n" G/ ]: i& g x2 D1 K // changes the heatbugs have made. The ordering here is
7 R0 G4 u' J( e+ O; x _* t // significant!
* s) O. u9 S. k) c+ L% l( w6 \) `
% |- P ~& {& F- O1 H7 s // Note also, that with the additional
0 f# e2 x k1 z! G0 W4 D z // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 u' ]7 |! ^7 [& Z% B // randomize the order in which the bugs actually run$ U2 W; T1 |. _! W$ ^, ^
// their step rule. This has the effect of removing any
% |3 M# Y- w: w0 }* K) e // systematic bias in the iteration throught the heatbug& n+ |2 d' q6 A& P2 S+ [; p
// list from timestep to timestep! M4 a% E7 I* C& x& h1 x
- T' ?; w9 |) |: P% B, h* D, e
// By default, all `createActionForEach' modelActions have
/ r9 k+ M' l0 w( c // a default order of `Sequential', which means that the
6 L& C8 |8 m0 r3 t' }+ V2 }. C4 T // order of iteration through the `heatbugList' will be
/ T0 F. o2 J5 F8 W5 T // identical (assuming the list order is not changed1 G- N4 ~, m+ Q" U$ o
// indirectly by some other process). N" D% {( a6 ~
0 v! p. R4 |, J, k- l modelActions = new ActionGroupImpl (getZone ());
1 g/ Y) X( p2 F: [$ m7 y; b# y& y
. A4 y% N: N3 ~1 p, w try {% f* r) }) H2 a) {. x! `6 `
modelActions.createActionTo$message
" Y& Y& U S e& x( _% ^; | (heat, new Selector (heat.getClass (), "stepRule", false));1 f" X5 Q/ o4 X. t& T# C; N |& C
} catch (Exception e) {$ E/ e3 C, H- C$ D3 P1 C: W
System.err.println ("Exception stepRule: " + e.getMessage ());( b' C2 B ^% s7 J
}
' ?& S* @0 d% \: J9 A: C, r, J7 K- C
try {
7 ^# k( E! S7 G! k! } Heatbug proto = (Heatbug) heatbugList.get (0);
/ W& |% W0 e: Q; V Selector sel = t' [+ n+ j. Y" D* J: U) A9 E
new Selector (proto.getClass (), "heatbugStep", false);
/ j$ r, R8 h5 \' K8 g; ^ actionForEach =
6 O5 U: k9 O, {& z" B& a modelActions.createFActionForEachHomogeneous$call
: p& g0 y" ]7 t/ W. T, ? (heatbugList,7 V6 u# A, E( q4 \" ^
new FCallImpl (this, proto, sel,
0 X; r; n3 _% I8 s# A8 H new FArgumentsImpl (this, sel)));! z( \8 k/ }8 V0 i/ \0 {. \
} catch (Exception e) { N) W% l6 `* o# J
e.printStackTrace (System.err);: w% X F9 O2 \
}6 u# D7 I* x. Q/ j0 e
# v7 S. P; L/ O9 }) M5 i
syncUpdateOrder ();- E9 J, J- H' x* U6 a1 v$ w4 @9 b
/ [ A0 v9 v& T3 P try {
: w3 i4 z" P2 ~$ r! O! g modelActions.createActionTo$message
3 M& H! _% {- a) D, r (heat, new Selector (heat.getClass (), "updateLattice", false));
6 [7 D! n1 y4 E5 Z) _1 d } catch (Exception e) {$ r0 k" A' m6 U+ O4 V( _
System.err.println("Exception updateLattice: " + e.getMessage ());% }% |( u2 c5 s% J( e+ r7 {' |( Z9 K# ]
}
[! _1 E; x! p! T2 e {
6 U/ R3 J5 f- P& o+ D; s, D // Then we create a schedule that executes the) l: [6 U9 e3 Y" T" F
// modelActions. modelActions is an ActionGroup, by itself it
$ i! a7 H: s! z( Q) m8 s) z" ` // has no notion of time. In order to have it executed in6 F" G! H0 |2 d7 ^
// time, we create a Schedule that says to use the
' \! N- e. i! x5 @. Q7 @ // modelActions ActionGroup at particular times. This
+ i/ U; v6 R. `! [2 j0 D // schedule has a repeat interval of 1, it will loop every
+ B' [- @( W: p ]2 B' t2 Q2 W // time step. The action is executed at time 0 relative to
, d7 h( A2 `7 s+ D3 H( i7 n+ Q* X z$ ] // the beginning of the loop.
5 _! q s: l3 e. S& u" |: c8 [; N. L) H7 Z( R$ Y& K
// This is a simple schedule, with only one action that is
2 Z! Y5 M6 _& j# M/ J! v$ o' Q0 t* I // just repeated every time. See jmousetrap for more
`6 A( t% j3 ^ // complicated schedules.
3 _/ Z {1 L" w- |4 y* U
7 x0 u: j/ w% Q modelSchedule = new ScheduleImpl (getZone (), 1);
- e: I, A- _+ h1 n/ c* H1 g7 }+ { modelSchedule.at$createAction (0, modelActions);
/ _7 l0 I6 Y) f/ h; g9 K" N9 W 4 _8 z- i& e3 T2 @: k, ~
return this;
6 Q# R# l; n* z } |