HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
/ d: o |, Y( T7 D( N: [) R3 J( h+ ^3 \: J% B
public Object buildActions () {- O* a: ?( T4 P8 @3 E
super.buildActions();7 v0 _" J8 l3 V d- r
' ]' ?4 N0 ~5 i. M
// Create the list of simulation actions. We put these in
' L T4 A2 K, p // an action group, because we want these actions to be& I3 ]+ L( } l( B9 i! D9 J
// executed in a specific order, but these steps should& J( L2 l: j- u) E6 m+ N+ k
// take no (simulated) time. The M(foo) means "The message1 [4 J/ g: `: G4 v x
// called <foo>". You can send a message To a particular
# F' `' f" E1 x/ q3 D // object, or ForEach object in a collection.
' p$ H3 O2 }- }! k' I& e
1 }, H6 n" S$ A" h! y: [$ u // Note we update the heatspace in two phases: first run5 x0 |, l) Q) ]$ w- q
// diffusion, then run "updateWorld" to actually enact the
$ B4 d& e+ e; Q1 _+ V' L // changes the heatbugs have made. The ordering here is
1 _1 K4 e# d1 A; z+ V& a3 m // significant!; T! t# }0 \( W3 q/ g" K& L
: I4 q# \3 z! {# k Q
// Note also, that with the additional
/ ]0 V+ w( l' b$ b // `randomizeHeatbugUpdateOrder' Boolean flag we can
* ]' C8 |% k5 s( J2 V) i // randomize the order in which the bugs actually run: w, l/ s2 o/ l; ?) h
// their step rule. This has the effect of removing any- o1 P' y2 I. l) ~/ E$ O
// systematic bias in the iteration throught the heatbug
, x' E! g1 h8 {- F( G0 b // list from timestep to timestep/ o# Y+ J& x, Z4 _, R; K
: U7 q8 `) e7 V. c) {
// By default, all `createActionForEach' modelActions have% A2 m% E4 B3 U/ }7 ~2 M! U. g) {
// a default order of `Sequential', which means that the g: o d5 O; {! b: h7 I
// order of iteration through the `heatbugList' will be
3 s/ E i" b9 x4 m# e4 O // identical (assuming the list order is not changed
" ^! O( V! V$ F // indirectly by some other process).
7 U0 e2 y1 [5 T. a' m 4 ]+ w3 {, k+ f) T$ f
modelActions = new ActionGroupImpl (getZone ());
$ `; y3 J7 g8 s& N( ?7 |
0 ^- C- h+ f; a7 r# |- ~2 g) [+ V# ~3 [ try {
7 m8 N; {* w% ?6 R+ ]! _ modelActions.createActionTo$message9 T5 h% C3 e+ M [) Z) f, h9 l
(heat, new Selector (heat.getClass (), "stepRule", false));$ ^" k" Q# a* s. t
} catch (Exception e) {
7 C, w: q0 \9 O. x* H9 T System.err.println ("Exception stepRule: " + e.getMessage ());6 p/ n5 T' k/ r$ c( G" l; T/ ~
}1 G2 d7 x2 p5 S0 M4 X1 o
3 H1 L, v- ~' q& X3 V& J9 Z& w1 u try {
, v9 N8 P2 G: ^- h4 }. x0 X Heatbug proto = (Heatbug) heatbugList.get (0);9 J4 c; V. a2 t+ i5 ]9 z6 @) c
Selector sel = 0 X4 z1 ~3 o8 r* P/ I5 h; h
new Selector (proto.getClass (), "heatbugStep", false);, Z* r& S, o+ f( S
actionForEach =
. d4 N6 D2 o' }, ?- U4 l9 r modelActions.createFActionForEachHomogeneous$call
2 o' j E0 \" i- w+ C (heatbugList,7 o* n# L8 `* i+ J
new FCallImpl (this, proto, sel,& N0 T; O* g: b9 X4 e7 g
new FArgumentsImpl (this, sel)));
9 M4 |% b$ _( C% | } catch (Exception e) {7 F+ n' V( j4 G4 _0 i% ]( V
e.printStackTrace (System.err);. @3 @1 |0 D4 K5 }: x0 }( o: m
}
$ E2 c1 d1 @; ?0 o
) _7 M% _3 d9 ]7 P& r8 o* m k syncUpdateOrder ();
; ?3 U! l& o% j; B9 ~) F1 ~# l" R& b% i/ z% P0 h" z* r. G
try {3 V$ \* d) F, ^: }+ J+ k
modelActions.createActionTo$message 6 X- K% O5 p4 C) i
(heat, new Selector (heat.getClass (), "updateLattice", false));
2 t- l# c* e, M" a' @( i( \9 `# J } catch (Exception e) {
1 v5 y1 o3 m% B' z7 x" f System.err.println("Exception updateLattice: " + e.getMessage ());
) z1 Y z0 ?2 m! o/ Z }1 u% T, M2 t6 I9 K! w* F% A
$ X" S* b" c+ A // Then we create a schedule that executes the
# I( W$ N4 a5 ^! p( F( R0 a$ x2 J // modelActions. modelActions is an ActionGroup, by itself it' N1 h' d$ N5 v" f* z: L
// has no notion of time. In order to have it executed in
' ^' r5 v6 z) X) |7 x // time, we create a Schedule that says to use the
* S5 J0 N1 ]% k7 } // modelActions ActionGroup at particular times. This- ]* u+ P$ V( Q- u" @0 Z u9 J
// schedule has a repeat interval of 1, it will loop every
0 _% P% w% `& ^* I // time step. The action is executed at time 0 relative to
% @9 ]6 r( y* D( k N // the beginning of the loop.
* J0 Z, O# R' D4 X+ l' C6 e
) b/ c; X, a& z+ P( P- }& x // This is a simple schedule, with only one action that is. A& i- j* e! @
// just repeated every time. See jmousetrap for more
7 T7 B- y% p% [* X4 Y // complicated schedules.
7 [; C) R$ Z! b3 B' a. U- d ; t5 ^2 _" P- l2 H, `
modelSchedule = new ScheduleImpl (getZone (), 1);6 H y- ?/ W+ c7 Y& Q
modelSchedule.at$createAction (0, modelActions);
4 s. k$ N) H- ~2 n7 r+ p 4 o+ P6 {2 a6 u/ }. K7 G
return this;
* q/ ?1 c" }+ W } |