HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 c" N+ w3 X( P# {+ w4 Z# m7 a2 B
" G* K) K; K( g# q$ J# o public Object buildActions () {1 U( x) ]& |5 P) }& n1 B) @
super.buildActions();! \- T1 z4 T; J' l1 W Y- z; s4 i3 N
0 E+ W& e' W) _+ ?9 Z" w: t- u' j // Create the list of simulation actions. We put these in
5 O& G l% C8 N8 H) I+ q+ R6 z" i // an action group, because we want these actions to be/ r) |- {& }% @ v( z
// executed in a specific order, but these steps should$ b( B4 p+ K! q( I
// take no (simulated) time. The M(foo) means "The message
/ v1 _6 f* k3 K! J4 d4 m6 B4 g // called <foo>". You can send a message To a particular7 Q* p. B9 C) Z x# B( x
// object, or ForEach object in a collection.
5 t% g' n8 [: g7 f+ R% J 6 h$ P. B/ c" u/ O
// Note we update the heatspace in two phases: first run( o8 \( R& b8 N- e. p
// diffusion, then run "updateWorld" to actually enact the
: V, X# ~ O4 U // changes the heatbugs have made. The ordering here is
( ?; J" q7 _$ C" H) e // significant!
* D/ a1 ~) P* Q
' R9 W% w2 ^+ \/ P3 Z* x8 U // Note also, that with the additional* c( }; t; |6 u( U9 P7 T
// `randomizeHeatbugUpdateOrder' Boolean flag we can
7 ]+ U" {/ X' V // randomize the order in which the bugs actually run
3 Q7 k H4 [# q3 w7 f( m! Y // their step rule. This has the effect of removing any
3 m+ B& g% l# a* `4 E3 H: L9 m // systematic bias in the iteration throught the heatbug3 `8 q, B& F8 l
// list from timestep to timestep
$ {% q: k) c5 F! V6 T6 d, ?) F
3 X% _4 h0 ~' D& m# o$ o+ U // By default, all `createActionForEach' modelActions have
$ W) p0 I& B, T // a default order of `Sequential', which means that the
0 b3 r0 J3 F6 B# ] // order of iteration through the `heatbugList' will be% }; J5 A- P. n
// identical (assuming the list order is not changed
" E- t7 ]% @& w; p2 V. |3 w // indirectly by some other process).+ @+ ~* k" R9 ?6 ?% O7 i; q k
9 O8 B0 T, {5 ] modelActions = new ActionGroupImpl (getZone ());! ]+ i4 s7 f$ F5 P4 r
% t/ u$ z3 ?: [
try {% \0 _: p: P2 y6 x# g
modelActions.createActionTo$message
7 Q" E) k# j9 @3 w7 U3 K: w (heat, new Selector (heat.getClass (), "stepRule", false));
& G- W9 m2 w' O } catch (Exception e) {8 j' |: Q# L7 _8 Q5 b0 O4 l
System.err.println ("Exception stepRule: " + e.getMessage ());
- k z6 }! P1 P9 L( t4 B) t }
/ p3 I& ?1 o) i) ]3 c5 r0 c/ f% G2 R$ A
try {
$ E( E; J$ M& N( i9 y1 A Heatbug proto = (Heatbug) heatbugList.get (0);
; U, J* M& E5 { _" E4 T Selector sel = - H4 P. i' t! V& M
new Selector (proto.getClass (), "heatbugStep", false);% D4 J( B+ ]$ V7 d* L
actionForEach =7 K$ J+ Y+ h+ I/ y# \! Q# L& _2 H
modelActions.createFActionForEachHomogeneous$call/ X1 n2 ~9 Q# ~/ I; D9 p
(heatbugList,
2 q: C; ^! @# v; _ i0 j4 H new FCallImpl (this, proto, sel,
0 `0 t% C8 t. }/ K new FArgumentsImpl (this, sel)));. _6 @8 T* l4 y/ {3 e
} catch (Exception e) {3 d. ?" Y% y3 V0 L: s% T6 R
e.printStackTrace (System.err);4 {! j8 G$ X" v
}
! u! }5 k c, R; w* T 6 g; }0 e0 y: t, h3 d' Y1 G
syncUpdateOrder ();$ C( ~/ x3 ]" }2 E3 ]& S
1 O5 B( {8 b% R4 g: g
try {
( j. }: `% g @8 W. G modelActions.createActionTo$message 9 ]% v4 y/ C5 x' |8 S8 Z
(heat, new Selector (heat.getClass (), "updateLattice", false));) P8 w q) {1 G% u
} catch (Exception e) {0 F( Z: ]1 L/ f* x0 S4 `7 P! D( [
System.err.println("Exception updateLattice: " + e.getMessage ());
8 M7 X3 e2 H" O W }
# u$ F7 Q/ D6 k, Q ' S& g! c7 P- Z, @" W( V
// Then we create a schedule that executes the& L8 K, _2 [$ u6 b
// modelActions. modelActions is an ActionGroup, by itself it
6 |7 X+ g/ D+ J5 f& p. w$ F) a // has no notion of time. In order to have it executed in
: y. v- T( Q/ n4 O' P( M // time, we create a Schedule that says to use the( \ A. H K# J, `- W4 w
// modelActions ActionGroup at particular times. This6 o/ h8 a7 D H( `. G4 k$ R1 J
// schedule has a repeat interval of 1, it will loop every
5 q% Y4 s4 |1 j+ C0 g // time step. The action is executed at time 0 relative to Q4 K! Q1 c* n1 g
// the beginning of the loop.8 I9 Q6 B1 R- M$ f1 }, }
1 G1 N4 `4 r7 j; u; o // This is a simple schedule, with only one action that is
) f' L, [" J( i/ |+ ?+ A, l j // just repeated every time. See jmousetrap for more ], s8 I3 o" }& f
// complicated schedules.
' ^/ [6 G4 ` x
: Y# w5 P" a" N n0 ^ modelSchedule = new ScheduleImpl (getZone (), 1);
: R) Q! F9 \2 |' @! \* C0 n3 r modelSchedule.at$createAction (0, modelActions);. ^- }7 |( F2 p/ V6 P0 F
6 v5 ?# y* c3 O+ p# Z return this;
$ l4 _6 F7 S, u U } |