HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 m! N# k$ l8 O0 A6 J, e5 P
3 F7 U$ Y3 h! d% i1 e0 {) \0 u; f public Object buildActions () {
~ G! k7 ^ l8 a' [* u super.buildActions();
: \1 B( b, x3 c) r7 u8 N" [ & e7 ]% E( I% ~# D* b) ?
// Create the list of simulation actions. We put these in3 M% f% B3 n* h6 R- e- d+ A
// an action group, because we want these actions to be* z( a/ T2 H! Q' F( n
// executed in a specific order, but these steps should
- S- v+ C5 U9 j0 n/ F // take no (simulated) time. The M(foo) means "The message
: e- j. s9 w2 b1 O, d // called <foo>". You can send a message To a particular6 r4 l: t+ ~1 F' |
// object, or ForEach object in a collection.2 B) ~; n0 Y6 v$ s8 y
8 A3 {9 O; ~. t" m // Note we update the heatspace in two phases: first run' M4 B7 B: e" r
// diffusion, then run "updateWorld" to actually enact the1 h: o" _/ e& O& |
// changes the heatbugs have made. The ordering here is7 W0 F7 X1 P5 M% P8 J4 q' W
// significant!
3 z" @( `" S* Z; j, e
( w- D. f5 d* D) z // Note also, that with the additional6 o( G- h4 }$ i6 J
// `randomizeHeatbugUpdateOrder' Boolean flag we can, | s8 K! ]( Q# M) P1 i
// randomize the order in which the bugs actually run4 n! _/ U, w% b5 G: b$ f
// their step rule. This has the effect of removing any
. m) `2 V" P+ p* `0 @ // systematic bias in the iteration throught the heatbug
, B& D/ K# E+ Z% n // list from timestep to timestep i T' c) f& d! L7 {. I* ~( O- f+ |
3 j2 n3 d5 E c9 ^& L( v; U4 ]% _- O
// By default, all `createActionForEach' modelActions have' X' O3 ]8 ?& P$ V- `' d4 g3 I/ s
// a default order of `Sequential', which means that the
3 S7 w7 L2 o* b: }8 T // order of iteration through the `heatbugList' will be* W$ b" G" A' [4 [) J- \7 @3 L
// identical (assuming the list order is not changed
0 H+ p8 }' j( D8 y8 N" l) x // indirectly by some other process).
: L& i: G0 Q* w/ j- Q) Q. f 2 `4 e& z+ V4 R' l3 {
modelActions = new ActionGroupImpl (getZone ());
) p( c$ B9 I3 I: ?2 D; f( J* B7 @# e$ T# W2 f& Q
try {, I, M/ E+ W7 s, w; L
modelActions.createActionTo$message
8 b+ Y) X2 h+ K) [& J/ _, {0 K (heat, new Selector (heat.getClass (), "stepRule", false));& _) e* s% Y& N# U$ [* }7 U
} catch (Exception e) {
& ]9 N8 g3 [- p7 X2 F2 W System.err.println ("Exception stepRule: " + e.getMessage ());) n! d: \4 v% d5 p
}
4 r! X1 D8 N9 T: n/ k" F- ^- r
7 E3 e4 w1 X5 ]5 a E( [4 A$ ^ try {
1 ]& u8 V; H* } ] Heatbug proto = (Heatbug) heatbugList.get (0);3 z( A: m2 u" o; X. r# n
Selector sel =
$ v4 y5 F; c: t2 X9 f new Selector (proto.getClass (), "heatbugStep", false);
% _' V; w0 E- s# |& G actionForEach =; x+ Y/ B0 f+ a$ ?
modelActions.createFActionForEachHomogeneous$call" c' F) \& ] D2 D
(heatbugList,
# u+ A& c' k2 o& m5 G) ] new FCallImpl (this, proto, sel,9 N$ k# r! i. F8 Z5 j4 a4 h6 c
new FArgumentsImpl (this, sel)));* y' v, k& ]5 {8 c% b$ _, s' P9 |
} catch (Exception e) {7 T2 i4 T, }: M3 N1 m2 x, u
e.printStackTrace (System.err);; W/ o; V! e) D/ R# J8 Y: Y
}
% l8 y7 j* O2 K+ t6 T$ [
- X( _$ L4 W( C0 s" y5 P1 { syncUpdateOrder ();
) s v, m: X q: C& h- R: d1 n. c1 o; b1 E- u0 j) A
try {
) f7 A: W4 _( s9 W, I modelActions.createActionTo$message
: a/ R5 ~7 O7 Q# l4 x, H" A (heat, new Selector (heat.getClass (), "updateLattice", false));
: f' c$ {6 B1 x% h- p& j" @ } catch (Exception e) {. n5 ` n; L+ K6 t3 C8 Z
System.err.println("Exception updateLattice: " + e.getMessage ());) D# Z" `6 p6 _2 I+ h8 R1 p
}
2 q% a, b8 T% C& ~/ ] . a) k" b( l D) i
// Then we create a schedule that executes the# U) R( T1 j+ t( _' ^+ c, q0 n3 Q
// modelActions. modelActions is an ActionGroup, by itself it
( f) U) | n9 U* g // has no notion of time. In order to have it executed in Y2 e; k# N7 d9 Q
// time, we create a Schedule that says to use the
& d' i; S1 ?" ?, l5 y: T: b // modelActions ActionGroup at particular times. This
1 s- x# `9 ~& {* r3 R. R // schedule has a repeat interval of 1, it will loop every, @* `1 J& N7 S5 J6 e- R# g
// time step. The action is executed at time 0 relative to
2 |. @2 }8 L+ s // the beginning of the loop.5 y7 M# M: y4 o% o' L V
* |! m( w, n( j4 D: y- O8 |6 N
// This is a simple schedule, with only one action that is
+ S( c% s4 w+ e- I$ L* ~, F // just repeated every time. See jmousetrap for more
" X- ? {7 n1 ]8 Y+ j; o* I9 y // complicated schedules.
+ o, h3 F/ t: u% h7 \ 7 D* w( m( A9 {5 q* T, W* _8 G' G
modelSchedule = new ScheduleImpl (getZone (), 1);
; G- V3 R4 @8 F, V0 V0 D modelSchedule.at$createAction (0, modelActions);
H1 \% O( l" n# {& Y0 a- Y 2 }+ [+ p. E" Z4 A
return this;6 ?: |3 x9 O0 R; l( E
} |