HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
, W, S4 f- q. r D, Q7 i; Q$ A% E/ }0 Z8 Z
public Object buildActions () {
+ o" M" R- c# k w: O* L h7 i super.buildActions();
3 h- `1 k+ b* J) c! K5 r
/ O7 `1 A b: t2 Q: r& n // Create the list of simulation actions. We put these in& [. _2 W8 v9 `/ d% o8 c* N
// an action group, because we want these actions to be. h: y1 I$ |* w3 i
// executed in a specific order, but these steps should9 D( O: j, z/ t5 Y4 O9 V! J
// take no (simulated) time. The M(foo) means "The message8 E# ^( D6 J8 O3 C
// called <foo>". You can send a message To a particular# o! f7 f6 f& F! H4 {$ w$ F* c
// object, or ForEach object in a collection.5 E: F0 v5 z4 ?6 W8 _
( f# D, d. A) v& w! m! [ // Note we update the heatspace in two phases: first run
6 l3 t) s4 a2 g3 L0 K, \$ Y8 l( x // diffusion, then run "updateWorld" to actually enact the( V0 X' _+ N" e; z& ~
// changes the heatbugs have made. The ordering here is
; ^% x8 S/ C6 e* z6 x7 ?+ q9 ? // significant!6 s) @3 O5 G8 e- I/ c& a& m
+ X0 |+ q1 S& | // Note also, that with the additional
+ \1 {/ [! ~3 ? L // `randomizeHeatbugUpdateOrder' Boolean flag we can# H$ }, F x1 D
// randomize the order in which the bugs actually run( \% u) i7 O( n* ^
// their step rule. This has the effect of removing any
]- u6 x$ s7 z- ~1 h$ ?; ~/ j // systematic bias in the iteration throught the heatbug
5 A' _7 z# H( b& \ // list from timestep to timestep4 W# q6 v& \' ^ W
2 ?% [1 ?5 K+ U4 V I; p
// By default, all `createActionForEach' modelActions have, ^2 E5 N0 ]7 N/ ?# ?+ f
// a default order of `Sequential', which means that the8 ]. ~/ Q3 g9 [8 M8 R6 G
// order of iteration through the `heatbugList' will be* n6 K8 i' i. f( c \6 r" I
// identical (assuming the list order is not changed
7 n8 P3 N a- `/ |* \1 _" H: i // indirectly by some other process).9 v: K: Z, `5 k7 T
/ C0 \' ~9 ]$ _& R1 _ modelActions = new ActionGroupImpl (getZone ());; r! [6 q( K/ L& n6 h
- P( P- D! L* D7 C. I0 Z
try {
. Y, E0 o: _% i. e" r; I8 J3 k: W modelActions.createActionTo$message
- W/ l- g6 c7 U1 \1 p (heat, new Selector (heat.getClass (), "stepRule", false));7 K) k' X- k! f8 A0 J* m d4 p
} catch (Exception e) {
2 y M& l# g) O System.err.println ("Exception stepRule: " + e.getMessage ());1 L) r% W. i/ s3 J" Q$ m5 v
}
8 [' ~4 Y3 Z3 O! k& Z* J
& F, T8 F- c2 ]$ a1 k& p0 W: U try {
2 K# R [3 s: i9 B J: ? Heatbug proto = (Heatbug) heatbugList.get (0);4 r/ J8 [3 ?) r- d' p( E( y5 R
Selector sel =
x/ K9 |7 {1 v% n& I. K new Selector (proto.getClass (), "heatbugStep", false);
, ?9 R3 B( b/ v# r7 i; w actionForEach =8 {' j* A' J. f6 [# M
modelActions.createFActionForEachHomogeneous$call
4 M4 a8 @" u0 g. q5 j" h (heatbugList,
( d- O, W7 m& ^; [ new FCallImpl (this, proto, sel,* y. n0 Z( f+ r* n* e
new FArgumentsImpl (this, sel)));* H: _% Y" q9 ~4 ~9 ~9 Z
} catch (Exception e) {
9 k6 d; ?' q! M9 A% `, ^1 S" i e.printStackTrace (System.err);- G# l! ^! V0 i' A1 o& E8 L
}
, h4 A& b) f. [1 V3 V1 |
; ~, Z8 N0 o4 M3 F n- z syncUpdateOrder ();0 y4 e7 T g) @& |8 ~' A
6 T" E% o4 D: ]& l( F2 e8 g% Q try {
* L' t6 F( L6 P# j; t6 Y6 c2 E modelActions.createActionTo$message . U) p! e7 L: s' }# Z G
(heat, new Selector (heat.getClass (), "updateLattice", false));
6 ]" C0 N8 y+ i3 p } catch (Exception e) {
; l% |. G9 U0 V! }' ]; W System.err.println("Exception updateLattice: " + e.getMessage ());
# Y5 B2 _9 o f+ l }- g& K: x8 i0 h7 }
8 _; |" y6 O7 d2 w$ z/ N // Then we create a schedule that executes the
' p( s& L7 M; S# b2 O // modelActions. modelActions is an ActionGroup, by itself it
: ?6 S5 C2 X2 _' c# E* P // has no notion of time. In order to have it executed in4 O8 `7 P/ @& W* m3 _- r
// time, we create a Schedule that says to use the
, |) Q0 [7 I7 p" z- j) i7 W // modelActions ActionGroup at particular times. This6 X" U, ] }1 s {/ K
// schedule has a repeat interval of 1, it will loop every& [1 n9 t* H4 { _: Z+ D2 p/ ]
// time step. The action is executed at time 0 relative to% v& b# E- T ]2 W: N- ]
// the beginning of the loop.
8 K. j3 E1 ^6 {3 V! K- _5 A, H3 Q4 F ^! p
// This is a simple schedule, with only one action that is8 i$ o8 }9 s4 `" j/ n9 S
// just repeated every time. See jmousetrap for more% `/ P6 X% F) Q, \1 G
// complicated schedules./ t, u/ [8 Y+ X9 ^$ M
) j( w: A3 G3 r' y! T modelSchedule = new ScheduleImpl (getZone (), 1);3 `2 `& e& X% {+ P( R, |- g9 {5 a
modelSchedule.at$createAction (0, modelActions);
$ w. u- `& P* d0 t( t8 e1 a
& D6 m1 @" n6 G4 D$ `* T return this;5 d. j; o2 H. y# W7 G
} |