HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
1 I0 I! I% F7 R+ O0 {# w. U2 f0 p; ]% D' o9 X! z, A
public Object buildActions () {6 {0 ], ?) C3 B
super.buildActions();- ?. \, Z/ i7 C) f3 m# E: [; H+ {+ |6 o
0 ^' s9 }! e( o! A$ N! q // Create the list of simulation actions. We put these in/ ^4 i( h$ E5 p! _6 z
// an action group, because we want these actions to be
, x1 J! O7 ?* J% s# ^2 C9 Y // executed in a specific order, but these steps should" y9 F0 v$ k7 I; I7 H6 d4 ]- ]
// take no (simulated) time. The M(foo) means "The message
1 K, W1 ]" Z$ L6 K& G // called <foo>". You can send a message To a particular
9 r$ i- l, i; f3 w/ v7 g // object, or ForEach object in a collection.
/ h, F0 k: P" E
0 F( P- c4 p& O( C& I- K // Note we update the heatspace in two phases: first run# j2 T2 j, A b# k
// diffusion, then run "updateWorld" to actually enact the7 @ \: w' e& J2 y
// changes the heatbugs have made. The ordering here is) H8 o( n8 p8 E! z
// significant!6 i; D! `: p& S
# x! z/ `2 B B% n: g6 o d5 `) v
// Note also, that with the additional
4 L* v# ` w' e: |( ~% q% N // `randomizeHeatbugUpdateOrder' Boolean flag we can
$ W! X3 M% {( v0 P Z0 L% F- i // randomize the order in which the bugs actually run& V# u( s8 g& l" E6 U! g, Q G
// their step rule. This has the effect of removing any8 e: m7 e, N. k/ w( T* ^
// systematic bias in the iteration throught the heatbug8 V9 c9 P* M* ^8 p9 M
// list from timestep to timestep
+ {& J" R/ q" S" C& e: @7 q# y1 B: d7 { & y" _. A3 t6 z6 C9 a; D
// By default, all `createActionForEach' modelActions have, P* p; F }* b2 M$ a7 N
// a default order of `Sequential', which means that the
; ]4 i' j0 z$ ~, M // order of iteration through the `heatbugList' will be) s7 n/ y3 E- K, i4 Z
// identical (assuming the list order is not changed
2 p. U# b( E$ z7 J. u, N // indirectly by some other process).7 ^/ \0 y; W0 l( s
# H" f) \8 R- z6 h
modelActions = new ActionGroupImpl (getZone ());
" E0 @7 J# k# ]9 m- l+ X! }5 O2 m
/ U o+ m1 e: w try {# {" C0 J6 ~9 @$ ]* B8 }5 x2 R. R
modelActions.createActionTo$message! Y% u! |8 Y! ]+ v: w% D7 Z
(heat, new Selector (heat.getClass (), "stepRule", false));
) l$ g2 K% J- D5 @' } } catch (Exception e) {0 N$ O7 X; ~+ _7 g
System.err.println ("Exception stepRule: " + e.getMessage ()); e: k" T# B6 k, U7 g, R
}( y6 A/ s, k7 r& h- K$ o% P( o
2 r* u2 \; e5 V* P: v
try {
" R4 W* F5 T2 J Heatbug proto = (Heatbug) heatbugList.get (0);
: {* ?: ?7 s% G |- S9 t Selector sel = 1 ?" W3 h$ _8 z8 L7 x0 V
new Selector (proto.getClass (), "heatbugStep", false);
! K( }# V* s& b9 L# K2 i$ J+ s7 S actionForEach =7 N7 T! s) u" g% C8 l! W3 t
modelActions.createFActionForEachHomogeneous$call& h& k7 o: l/ N6 k' {' U x% Z) X
(heatbugList,4 Q. r: D' X* R1 j
new FCallImpl (this, proto, sel,
" _: F; Q; R/ `& A new FArgumentsImpl (this, sel)));
; b& h1 f# h% w' K% Z9 l3 W8 I } catch (Exception e) {
& w& g8 A$ t% x( ?* H9 {2 M- ^ q9 A e.printStackTrace (System.err);
, f/ ?& F5 J I( y$ M7 F" j+ g1 r }
+ W. q t9 E/ Y- ~- S+ y, l8 u2 ` / H3 Z+ P ~: u" b3 U
syncUpdateOrder ();
5 J6 c$ g# G5 u! H9 c, _+ B
( @! {' }6 @4 o' a! {: Y: M9 C try {! L$ d9 V; ^! y. X0 x4 j$ k
modelActions.createActionTo$message $ X: y- X: i! w9 L$ N+ p' ?0 n1 A
(heat, new Selector (heat.getClass (), "updateLattice", false));
+ I2 B' Q2 k$ q& h- w* P# t) W } catch (Exception e) {
1 F6 D* ^+ L' {+ B& Z System.err.println("Exception updateLattice: " + e.getMessage ());
9 L3 H3 q+ E; b9 v) v6 O' R% z }
. [+ t# O/ A# J4 Q% P
; S/ q8 \ v- W' T# P+ c+ H# o( z // Then we create a schedule that executes the! S4 @8 k. \" m `* R
// modelActions. modelActions is an ActionGroup, by itself it7 T, l$ s6 r" K0 V* z
// has no notion of time. In order to have it executed in
. \2 |( C8 C8 {6 u% y/ ?# f: { // time, we create a Schedule that says to use the
2 Z3 w5 c2 L% m: I // modelActions ActionGroup at particular times. This
3 u2 e* ~6 ^; u# n // schedule has a repeat interval of 1, it will loop every" G- p, j9 f: x _
// time step. The action is executed at time 0 relative to3 K8 c; R! n7 k
// the beginning of the loop.8 z m- W5 H+ [! m7 `
2 ?5 ?; z6 x& t% {: D // This is a simple schedule, with only one action that is6 Q1 {) D, I* j( j, C
// just repeated every time. See jmousetrap for more
. i g/ k P z6 G0 m% d // complicated schedules.
, l2 i+ ]- Y7 H% L% _" F- @ 2 q8 }. b; W0 b7 N e
modelSchedule = new ScheduleImpl (getZone (), 1);
j% {+ k2 m! A modelSchedule.at$createAction (0, modelActions);! t' X5 H: d1 I: g: T
, E" ~( s" m3 \
return this;' A1 ?' o! m( F8 R
} |