HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 a$ \3 D7 F( s; l- ?
1 A/ r1 t0 t( s+ [
public Object buildActions () {# L3 e0 _& b0 I4 r7 e7 a0 v
super.buildActions();
0 p8 {3 {3 d& K, m% l * Z' _3 U$ F0 k3 u3 N) K$ s
// Create the list of simulation actions. We put these in1 Q' l3 A5 x1 H+ d
// an action group, because we want these actions to be
% x2 k$ K9 N3 u+ z8 k7 b6 J // executed in a specific order, but these steps should7 y! C6 T0 E$ O' U5 u6 H
// take no (simulated) time. The M(foo) means "The message- ~9 ]. \3 L) v( x6 \' b0 K/ p- D! {
// called <foo>". You can send a message To a particular/ }( R& r3 p! p2 l1 y
// object, or ForEach object in a collection.
7 I; Z3 q' Y* N$ S0 L, p! C - K2 w F6 T. D9 P' d$ ~
// Note we update the heatspace in two phases: first run
' m( Z; e" U: @2 a8 U" m" y) R$ n // diffusion, then run "updateWorld" to actually enact the
2 o+ ^% U' N( D: p2 O) \ // changes the heatbugs have made. The ordering here is
& Y# k6 B; L! a6 J% B2 k // significant!
8 B0 Z$ H$ f4 d$ j: J1 q; }
' r, V/ ]/ N6 v$ t0 E // Note also, that with the additional; i9 h9 K9 \3 ~# W$ l6 P
// `randomizeHeatbugUpdateOrder' Boolean flag we can, o3 J( K' Q7 {( ~' M' E' _
// randomize the order in which the bugs actually run
' A/ d/ J5 [7 X) C8 }2 `4 I/ Z // their step rule. This has the effect of removing any
. w, t% t2 |* K2 f k // systematic bias in the iteration throught the heatbug* G0 ]" e( o" h; J
// list from timestep to timestep, t- x) P' }4 d% U$ y
' c, n$ {! N) {; w- O // By default, all `createActionForEach' modelActions have
6 g$ I7 W/ O e' @: w! q3 {& T" G7 | // a default order of `Sequential', which means that the: z k$ f5 r* X |& u
// order of iteration through the `heatbugList' will be
$ |" @( _) ]0 V5 L% i // identical (assuming the list order is not changed
0 V, `* ]3 ]% V3 G* S // indirectly by some other process).* o d4 _0 }3 e: M! U* Z
; g7 N% L! k8 L$ A8 Y- Z, o modelActions = new ActionGroupImpl (getZone ());" y% w6 `, U3 g
! K! g9 A: @% \" ~ try {
' |$ E* _( k8 E9 r5 } modelActions.createActionTo$message
/ G$ Q p3 K2 r% T6 p; Y7 g5 w5 W (heat, new Selector (heat.getClass (), "stepRule", false));5 B. {* s" H3 D; G
} catch (Exception e) {+ g, `6 F0 ]8 a% K/ Z
System.err.println ("Exception stepRule: " + e.getMessage ());
7 ^( l3 A1 }' v) Q1 _+ ]6 a+ F6 L }
* q$ Y) G% [( K( ~1 o0 w
( ?- J! r( T7 a( B9 ]/ Z try {
4 u8 D: U( _( R* B Heatbug proto = (Heatbug) heatbugList.get (0);$ T3 H2 p4 c! {0 d( J
Selector sel =
( h% U( a9 z/ T" \9 v new Selector (proto.getClass (), "heatbugStep", false);' R0 S. A5 r. |/ @: }: x8 C; w l
actionForEach =
: P5 O4 H. H# U) K1 z( X$ m modelActions.createFActionForEachHomogeneous$call
- D. _! Z( N% K- V5 G/ _ (heatbugList,. n6 z3 m' {# i# J6 H
new FCallImpl (this, proto, sel,
. F/ a& d3 B* f. u$ T3 z new FArgumentsImpl (this, sel)));0 v' G' \1 b- u, R) J8 `
} catch (Exception e) {8 d* K+ A$ y! a
e.printStackTrace (System.err);8 J& @8 Z: @ W; w4 I4 J! U
}6 {* ~" k! u/ J- I& H- }
0 F. d) J0 s9 D: z! w
syncUpdateOrder ();% S0 P; E0 b# V, j8 X4 U
* k# R4 H) |- T7 U; ^; W try {6 k% ~5 k1 h) }; b8 j( Y
modelActions.createActionTo$message ( H' H: b' a7 E) v2 k
(heat, new Selector (heat.getClass (), "updateLattice", false));, J) Z# T! J5 I3 V# r9 }/ x/ f/ j% K
} catch (Exception e) {
% h( \# _7 n* ]; j4 _ f1 i System.err.println("Exception updateLattice: " + e.getMessage ());( d3 n( n0 U1 M- C
}0 F9 d. E9 A: Q8 y
8 b; U h" L2 P5 r3 s# N& j
// Then we create a schedule that executes the
) K% k. O3 |. L5 @ // modelActions. modelActions is an ActionGroup, by itself it
" s, }( [- ?4 B& H' ~: T // has no notion of time. In order to have it executed in
% F2 O) n: I3 U" B `' g // time, we create a Schedule that says to use the. F# a8 Z: x7 F
// modelActions ActionGroup at particular times. This
2 M$ _9 t3 v/ r! y3 C // schedule has a repeat interval of 1, it will loop every" |7 o, r" g$ a' G
// time step. The action is executed at time 0 relative to
4 s+ W% ^: y. V; t+ t // the beginning of the loop.
! O: B8 O- Y/ `" M Y) e! q! N
0 w" X5 f% q9 z0 Z7 T // This is a simple schedule, with only one action that is$ A: c1 J9 @6 \% I7 Q( ~- g- t
// just repeated every time. See jmousetrap for more( t" Z9 s. b% J7 Y2 O ^& P
// complicated schedules.
( e' F" e" o" R5 r ' b+ [! u% z8 Y& I$ z- _4 Y$ G
modelSchedule = new ScheduleImpl (getZone (), 1);
B/ i& _& ~* ?+ @4 A2 X( U modelSchedule.at$createAction (0, modelActions);- j) R9 R: k4 {9 M9 U6 l
' B7 N: s$ Y, S$ G+ @9 f; _, r
return this;! @( T* N+ E+ I9 ~
} |