HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 i6 M7 f: q) T
6 o. n$ ^6 T* x5 Z1 E public Object buildActions () {: ]+ d# x0 u$ R
super.buildActions();$ p9 }8 P0 g5 ]: s) \, b
5 r- f1 e2 M; I4 l( A8 g0 `
// Create the list of simulation actions. We put these in* l0 }: o1 i" N6 r; C& m
// an action group, because we want these actions to be
& N4 x8 y/ N/ [' J: H; g // executed in a specific order, but these steps should
9 I8 }- K5 Q0 \4 e# ?, x4 `0 K // take no (simulated) time. The M(foo) means "The message
! z- z2 ]. N# E4 S) D$ I5 i! ^ // called <foo>". You can send a message To a particular: F5 D) @3 j8 C* V$ v
// object, or ForEach object in a collection.4 q1 E5 C4 |1 ?" F5 Y
' c$ i% U9 w! m, a- {% p q+ D. ?
// Note we update the heatspace in two phases: first run
& {' e5 K( e% G! W& [# X0 @ // diffusion, then run "updateWorld" to actually enact the
) `: O; @! l" H2 C1 D5 o* B // changes the heatbugs have made. The ordering here is
3 i2 ^4 A6 ~) j# X5 v // significant!
+ w- P# D" ^ ]- m
) c$ \$ Q$ t. t# ^ M" Y // Note also, that with the additional, c; m1 U9 O ]% m
// `randomizeHeatbugUpdateOrder' Boolean flag we can1 R1 _$ H0 P; o$ r/ o% l
// randomize the order in which the bugs actually run5 v( B! T1 g6 e* _) H1 S$ {& s
// their step rule. This has the effect of removing any
* S) q& T* c. D // systematic bias in the iteration throught the heatbug8 X$ m, @) v6 H0 A, @. a
// list from timestep to timestep- v! B! Z/ m3 W! {
9 g7 q3 B: E( ?/ S! L8 F' ` // By default, all `createActionForEach' modelActions have g1 Z9 k. o- N' E4 J C+ O
// a default order of `Sequential', which means that the- B6 ~ s! p4 o7 l6 F8 g$ R' v
// order of iteration through the `heatbugList' will be1 ^4 y- ]( u/ _
// identical (assuming the list order is not changed/ T1 S1 g3 c: I% D
// indirectly by some other process).
}: t) S1 ?# w' p" |
0 h4 n+ F( U+ ?9 l' T modelActions = new ActionGroupImpl (getZone ());
: u- b/ v* y( ~- j. x3 ~! k& g3 D; Y0 h! m7 P8 M1 _
try {5 W+ w6 z& d7 `6 d/ D7 H3 }& p
modelActions.createActionTo$message6 Z' I3 X% ]/ r, t5 {
(heat, new Selector (heat.getClass (), "stepRule", false));
. I' Q) O( d9 P. l9 R+ u' M2 o } catch (Exception e) {
& @! \3 e+ ?& O1 t: L+ f System.err.println ("Exception stepRule: " + e.getMessage ());' a+ e* h. h% k. f; y" _( B
}
3 l3 J& r& W% t0 P1 c& I0 v2 _/ M9 o) Q2 j
try {
2 S' }8 I% `0 D8 p$ y+ a Heatbug proto = (Heatbug) heatbugList.get (0);" q- w5 |% q5 Z0 _- b& R4 ]! f
Selector sel = 7 {" i5 @$ P" g" r
new Selector (proto.getClass (), "heatbugStep", false);" N" L* o7 F0 U+ b* D; M
actionForEach =
, N' k9 [, o5 ~7 K1 \ modelActions.createFActionForEachHomogeneous$call3 V+ Z& _; j# h7 V
(heatbugList,
+ Q$ A0 T1 C/ ~! Q! Z& h; k new FCallImpl (this, proto, sel,
% ]0 n0 X/ @ @* D% u+ B& \1 S new FArgumentsImpl (this, sel)));3 k0 N* \% N& S0 s- Y
} catch (Exception e) {1 E# n% l2 u# k+ ^% @
e.printStackTrace (System.err);) r, c0 D9 D; b4 R. m1 |
}# V+ r. X( o3 q5 a6 v7 K& E
) V1 y* H) N. ?6 z: t Q syncUpdateOrder ();- [% x9 O( C6 q$ C% U; ^
: j. q+ V* X) k6 B" P: r
try {
: J# I1 w' m) o; ]) c) U1 r modelActions.createActionTo$message
% n$ J o* P+ U. a5 h1 { (heat, new Selector (heat.getClass (), "updateLattice", false));
: {" ?4 e2 V0 }) v3 Y" ]: \ } catch (Exception e) {# B! j) g) ?( G6 a9 ]
System.err.println("Exception updateLattice: " + e.getMessage ());4 ~ t* I" }" X9 o, i- f% R
} v: }* U- I, `* I0 ~/ x
% j5 x& ^: S7 t& _# r. U // Then we create a schedule that executes the& d5 M) B3 a& `
// modelActions. modelActions is an ActionGroup, by itself it
) |! W3 {3 a4 N9 q" C& A // has no notion of time. In order to have it executed in( C/ `4 B O# r& W! n
// time, we create a Schedule that says to use the
. @3 Z" S7 A" e) y" O; x // modelActions ActionGroup at particular times. This
* F# L5 L& f& _2 E0 @8 P3 D) c // schedule has a repeat interval of 1, it will loop every
' @; K2 M* z% u3 @2 i3 _ // time step. The action is executed at time 0 relative to
- U! }4 K4 Q, M9 } // the beginning of the loop.
# |( B h( A9 B( y. A8 h: O# c
0 U" V3 S1 Y: f: r- U% X // This is a simple schedule, with only one action that is
0 q" H A& o" v& `8 F3 a // just repeated every time. See jmousetrap for more
, m, y! C0 H( E8 i9 y // complicated schedules.) S' K6 e: \" l2 D1 h
. `7 T# |% ~" q. \, i1 W$ Z modelSchedule = new ScheduleImpl (getZone (), 1);4 L, d# G/ z4 J
modelSchedule.at$createAction (0, modelActions);! K r( a. D3 ]' `$ }* z; o3 ^& ~7 s
8 d& Y6 k% G, r
return this;4 ]4 s' D) H7 S
} |