HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 H" G, s& t# e7 s
/ d6 ?3 T1 s" a, \4 B9 s4 V
public Object buildActions () {& m; ^7 Q" M4 P) c
super.buildActions();' @5 }1 ^1 G) r5 h
/ Q3 W8 T. D. `) D2 ?, m3 P // Create the list of simulation actions. We put these in
/ b5 t. W Y) ]) W, X // an action group, because we want these actions to be" y2 p$ [. ]. k8 ^2 c
// executed in a specific order, but these steps should; R$ d; b; t. Z0 E4 f7 Q; ]
// take no (simulated) time. The M(foo) means "The message
- F* `7 g( y K$ x" l' f' n // called <foo>". You can send a message To a particular% u8 @. S* p- i+ a. E, P$ [) ]
// object, or ForEach object in a collection.9 E: l+ `5 f. e" _* ` [
" J: ?: f% b" d+ O6 x
// Note we update the heatspace in two phases: first run
! d( h2 P: k. `6 G* a8 N7 b- m* X" |1 a // diffusion, then run "updateWorld" to actually enact the$ L- D @/ d4 a& Q! |& p
// changes the heatbugs have made. The ordering here is" R% N+ O6 f; s( s9 I: B
// significant!
7 I' n" c* r7 E( p. z4 G
" z' z+ s* h' S9 C/ n // Note also, that with the additional
2 t+ e0 j& ?' N) R0 v! Q0 a5 y/ S // `randomizeHeatbugUpdateOrder' Boolean flag we can
# U1 c2 e% z# U. _+ O // randomize the order in which the bugs actually run
7 C+ x% I: T8 n/ [$ e) r // their step rule. This has the effect of removing any
W9 P. }" k+ R4 _9 T' T // systematic bias in the iteration throught the heatbug0 m( Q2 t% f- D& }9 x/ N z
// list from timestep to timestep
) D! E ~7 T9 Z+ t9 `2 ^ 4 G/ O4 r' ?# y0 f
// By default, all `createActionForEach' modelActions have$ T& q1 M" O4 r9 q
// a default order of `Sequential', which means that the; L; x8 Z4 {7 R4 C' R" X7 _: a" G
// order of iteration through the `heatbugList' will be
) @# o7 \" e5 t8 `: ], _. B // identical (assuming the list order is not changed, s A% i$ o/ j2 J
// indirectly by some other process).) _$ Z, ~8 r2 A* a. ^+ f2 j. h& K
0 X! P$ v2 J9 d4 y modelActions = new ActionGroupImpl (getZone ());
, Q9 d. J( Q9 D9 Z, q; u k1 f; |
7 C, y# i: b. }3 u! A K try {7 J9 S2 `8 ~! I/ ^0 }. m
modelActions.createActionTo$message
- J, m9 ?) {" P" w (heat, new Selector (heat.getClass (), "stepRule", false));
; [( r, O" G$ O, B" a } catch (Exception e) {6 N: I2 _9 h) t; S+ V: H/ r
System.err.println ("Exception stepRule: " + e.getMessage ());
F) q2 `/ R; d7 k# z }
6 ~ U/ K2 I, A
, {, `- ~4 E5 ^# X8 ]' ~4 X; B try {' B9 j6 g% }; r! e
Heatbug proto = (Heatbug) heatbugList.get (0);9 P) x) F# q$ M. y! W& Y
Selector sel = * s/ K& X! l8 N0 S+ L
new Selector (proto.getClass (), "heatbugStep", false);! L& }" `. h+ J( _0 [ }# U
actionForEach =
7 Z. Y7 r/ B# ~4 S$ |1 g modelActions.createFActionForEachHomogeneous$call# N/ O1 N! D/ }3 O! E( X
(heatbugList,4 m3 { n' ]5 Z% R9 \
new FCallImpl (this, proto, sel,8 @) q. W/ W3 \, @# o0 Z
new FArgumentsImpl (this, sel)));
1 P) W3 X" P \! Q2 `# T! V! a' k/ G# B } catch (Exception e) {" N. p2 }1 `2 u* k4 P7 ]* T: {! `
e.printStackTrace (System.err);$ g: R; V6 _5 A# y# H' x7 r4 l- c
}
- j5 X; z9 U1 g3 O% \2 W9 \1 S ( c* v7 u: T, q- O' \/ C2 \
syncUpdateOrder ();
" ~6 A! w* e. {4 J& f5 y( {. R/ E( g* O$ a$ o
try {$ A7 K! _: ^$ T8 i
modelActions.createActionTo$message
. J4 M5 {" ~: Q+ V' e+ V0 J (heat, new Selector (heat.getClass (), "updateLattice", false));
8 U7 Q* i$ N( ]5 O( u; G: U) `# c H2 N5 { } catch (Exception e) {
+ O9 j% }8 k& ~/ F" Q- D System.err.println("Exception updateLattice: " + e.getMessage ());
% X, X9 ~% c: k0 }9 Z }
5 F: b! Y( M- T! t, G1 k % Y% A+ o/ E5 @+ o1 \/ K
// Then we create a schedule that executes the5 Q& Y& p, ]' \8 ]! R- _
// modelActions. modelActions is an ActionGroup, by itself it
6 X9 K! x7 C0 ]1 Y# f // has no notion of time. In order to have it executed in
$ g" Y: z! F# A8 U" p) {7 l" B // time, we create a Schedule that says to use the
* ]+ }* Z6 N$ D0 a1 f9 M) q // modelActions ActionGroup at particular times. This
3 |' F8 n6 O! x4 y* J3 X // schedule has a repeat interval of 1, it will loop every( M; `4 k5 C, R! h" Y7 T
// time step. The action is executed at time 0 relative to
$ P, t+ S* N! B; k // the beginning of the loop.* }) s& K# w# {; s/ L& i
$ L: s) s t k4 N7 X8 \* ?
// This is a simple schedule, with only one action that is, P8 Q* j! g. o3 X. E; e9 L
// just repeated every time. See jmousetrap for more
! t% A0 ~) ^! `0 _6 S/ t // complicated schedules. u2 s+ @ u8 I# l" N
2 }4 Z# ~& L+ x" o
modelSchedule = new ScheduleImpl (getZone (), 1);
4 Q4 t* @- {# {/ O9 F" u' b modelSchedule.at$createAction (0, modelActions);: G- ~* ]: {6 G8 `
4 g6 y( \$ T+ s. @2 Z
return this;$ ~# S0 ^- N- ]5 f( Q
} |