HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:& f' l7 V& K# N2 b( @1 r
2 y! {: A/ o* M$ [6 o
public Object buildActions () {
& |5 h( ?. ^: O+ K3 M8 L super.buildActions();
; H, r8 W& K. y+ v: C5 @3 \# G) v: M $ I! \8 i0 U3 Q9 ?4 l& K- s b
// Create the list of simulation actions. We put these in6 h( _+ ~! `2 y
// an action group, because we want these actions to be
9 `- ]3 |! F- M% }# V7 M // executed in a specific order, but these steps should
3 x, P2 ~5 d/ |) [2 L // take no (simulated) time. The M(foo) means "The message; k* F% T/ F0 T3 \$ ~4 o' r
// called <foo>". You can send a message To a particular
8 F1 I9 h9 I9 w, m+ W! R; J1 G7 e // object, or ForEach object in a collection.+ X8 [. w' \6 j
3 R2 V' l' I" [# A9 o- K+ Q
// Note we update the heatspace in two phases: first run
# F/ V5 l! T$ l1 k$ h6 @ // diffusion, then run "updateWorld" to actually enact the
/ i9 F" x" W l& k' G9 V$ e // changes the heatbugs have made. The ordering here is i. Y; |3 i1 a$ q# l0 z6 @6 `- H
// significant!
- a0 y! f: V' K# U/ m( [ ! G5 P" L5 ? A4 G4 Q/ ^9 r
// Note also, that with the additional
) P* x l$ l2 S, E0 M1 |% y // `randomizeHeatbugUpdateOrder' Boolean flag we can
6 L' K7 h+ B: B, ~ // randomize the order in which the bugs actually run8 a% m$ _/ f% Z% j+ h
// their step rule. This has the effect of removing any
* m3 q6 t3 y3 u% l' `4 K& g# J* v // systematic bias in the iteration throught the heatbug
% ]0 o. N' K0 v6 T5 L& @1 i // list from timestep to timestep
. f$ L. a5 i# G6 ~/ V 0 {$ N' y/ z# E) K
// By default, all `createActionForEach' modelActions have1 Y" o) w' T5 R" a0 _
// a default order of `Sequential', which means that the
/ z4 p4 s! Q7 W2 N" n& Y // order of iteration through the `heatbugList' will be
$ Z7 A& z# m/ m3 @( ?4 b- I% \ // identical (assuming the list order is not changed/ T1 n+ M" Z R
// indirectly by some other process).
/ i8 q6 ]* I- Y1 G3 K, Z+ `
1 R, @& c6 G/ E4 v0 g% P Q: V modelActions = new ActionGroupImpl (getZone ());7 i( N/ s! q9 i9 q; R
5 ^2 Z$ X" g# X* m/ e1 f4 A
try {
$ ^' e5 b$ H0 f8 g5 ` modelActions.createActionTo$message
- j( O" v4 A9 ]7 B. ^0 p- h# G (heat, new Selector (heat.getClass (), "stepRule", false));" O; e5 \5 q; p8 D( q f
} catch (Exception e) {
9 W% m7 `% ?+ r- n System.err.println ("Exception stepRule: " + e.getMessage ());8 b! r4 @9 {6 E g4 ~6 @
}
$ v5 ]5 m0 O0 [3 p1 M) @# d2 o* \% T9 [
try {
& Z/ y( G8 N* m: W4 ~* k Heatbug proto = (Heatbug) heatbugList.get (0);% g; D2 W0 c# G1 X! k. z% f+ }2 ^
Selector sel =
" P% ~% ]* y4 i" X: _ new Selector (proto.getClass (), "heatbugStep", false);3 O; W* R, U/ O" B
actionForEach =
. g3 m5 I' P6 w modelActions.createFActionForEachHomogeneous$call0 I- K7 y; v2 H6 L% x$ ~. }8 c
(heatbugList,
* x' a4 u5 X1 i/ u new FCallImpl (this, proto, sel,1 u, m( {0 M `$ `2 j
new FArgumentsImpl (this, sel)));* q+ g. J8 W9 Y) y& u/ D4 K4 q* K
} catch (Exception e) {
. X+ c, w! x( ?. Z e.printStackTrace (System.err);
8 h% c& a, l6 w' Q: k2 A }+ s9 C3 U) v2 ~/ d% u7 [
! P5 K+ b/ p3 _' I syncUpdateOrder ();. h+ }3 ?- s; ?/ l7 c) a2 Z
1 v$ w1 _" z2 [1 F+ t. S try {
( S* j' |+ ?8 W7 } modelActions.createActionTo$message ) q. ^4 H. V( G; R4 w- `& K3 J
(heat, new Selector (heat.getClass (), "updateLattice", false));7 F2 @5 s% N, N, R! @. w2 X
} catch (Exception e) {
. ^& P% D1 K0 _4 W System.err.println("Exception updateLattice: " + e.getMessage ());8 D X U8 Q+ _) Z) i4 y& x
} l9 T# u& E" a' D3 o
( q& _6 g' |5 h" y: Z // Then we create a schedule that executes the3 f. _4 `- r6 I, B0 |; M5 w
// modelActions. modelActions is an ActionGroup, by itself it4 e! c% V' i8 D+ h: y* G3 Z
// has no notion of time. In order to have it executed in
8 x3 z2 K, }- ]0 t" n) K/ L // time, we create a Schedule that says to use the1 ~! a z7 z! {6 n
// modelActions ActionGroup at particular times. This
5 V7 ~6 v* s6 R" M2 k, d# B // schedule has a repeat interval of 1, it will loop every
( y3 }1 z* @' [5 V // time step. The action is executed at time 0 relative to0 V2 Y( U' ^# }
// the beginning of the loop.& [6 x1 m- u; ]
- f* X5 a1 B9 B7 ^ // This is a simple schedule, with only one action that is
$ d) i" q; f5 G" k // just repeated every time. See jmousetrap for more( o. x1 G" O: n
// complicated schedules.
8 k; \, Y d. G8 l+ w% i* P" ?. K( _
9 | f, X- d! V: N2 J modelSchedule = new ScheduleImpl (getZone (), 1);+ i* ], ^1 ?2 s" |! B1 K
modelSchedule.at$createAction (0, modelActions);+ o" n- o+ x& J" O( ^
; y9 U' A& w) R
return this;9 v" e* G% L N9 E* e7 c W
} |