HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
+ v& W% `$ ~6 C% r9 p& Z" L8 C
6 l; M9 a/ G. `% w public Object buildActions () {
9 g0 I8 H* J l6 v! M super.buildActions();
6 _/ Z/ z O8 b9 y/ H6 N
; z. \/ v( {) W7 I' A2 x // Create the list of simulation actions. We put these in
+ o( f, y# i% c [8 s // an action group, because we want these actions to be% s, h: _3 t1 k2 v. F8 U
// executed in a specific order, but these steps should! g7 r% N) P/ J% ^0 K) ^
// take no (simulated) time. The M(foo) means "The message" S' W8 W. Q4 ^1 y% }# K
// called <foo>". You can send a message To a particular
& ?9 b& i h ]5 }9 L$ G+ h // object, or ForEach object in a collection.' d& z" ^8 h4 ^& R# H3 S0 j
% U& B {, ^9 p1 I0 w G! o // Note we update the heatspace in two phases: first run
: |8 V# t1 N; ] E; y; t- D // diffusion, then run "updateWorld" to actually enact the# T$ J* T \3 p4 V; J
// changes the heatbugs have made. The ordering here is
' _7 k# L! m$ J" q! U9 Q // significant!
) s* @3 Y. I' _, i; b( S1 p" W3 ?3 {6 O
) b; U6 e8 I" x0 v" w( ~ // Note also, that with the additional
3 ^7 ]" g# q. f. n3 x4 v // `randomizeHeatbugUpdateOrder' Boolean flag we can; w9 q( v I" Q9 p4 v- ^6 @' F
// randomize the order in which the bugs actually run
! E$ w* r9 B& }& }% d: @) R; f // their step rule. This has the effect of removing any
( F; g: u* u5 l6 T // systematic bias in the iteration throught the heatbug
- P* V) m- |# X! m. W! c/ ?& A& a // list from timestep to timestep
8 d5 `6 ]4 A. _4 b4 @' w v + H& P% e5 U( X8 j" N: c. u
// By default, all `createActionForEach' modelActions have
+ O! M) Q1 A( b- f" n // a default order of `Sequential', which means that the" s' ?: s( d- t
// order of iteration through the `heatbugList' will be
G5 |4 C/ v) Z7 b // identical (assuming the list order is not changed
5 E3 `( l) T' g# e- I' F // indirectly by some other process).) I& M$ Z5 F" s# c. F: @& {
* |- l0 T! g5 k. I modelActions = new ActionGroupImpl (getZone ());
' c+ w ]9 [" k" E8 S- A
+ R8 J0 S) ]& G9 b try {( K7 l* m2 a3 e, H' l+ U
modelActions.createActionTo$message
# T8 M& H2 w# b2 _6 i8 |: | (heat, new Selector (heat.getClass (), "stepRule", false));. Q$ W- K/ ^- q
} catch (Exception e) {' o8 v9 Q) p: ?5 u n2 j) S
System.err.println ("Exception stepRule: " + e.getMessage ());( u$ [4 K' E' |9 J5 m, B' q6 b
}( x( j$ D6 t$ p! Y" O/ G. x& _
; C9 s" A1 e. L1 t* ]$ J- Y
try {+ l+ `8 L4 d6 p! `( v
Heatbug proto = (Heatbug) heatbugList.get (0);% v& n: Z( I% m0 } W
Selector sel = " [; l) x" n" [
new Selector (proto.getClass (), "heatbugStep", false);
- q' v2 @, O O/ ` actionForEach =% |' X0 `: N# T* M+ ?
modelActions.createFActionForEachHomogeneous$call
. y1 o ?3 M0 E0 o3 q3 L/ z6 | (heatbugList,
6 U( d9 l6 E2 A5 M/ y new FCallImpl (this, proto, sel,
! d, V2 b: F1 k3 Y new FArgumentsImpl (this, sel)));
F4 M0 g7 [2 k% k7 N } catch (Exception e) { O1 w& I J% N
e.printStackTrace (System.err);
: L- d1 U" L1 k8 P }* ]6 J; }6 D' R2 r0 Y9 E
- e5 [' W# S- O5 y# F9 a
syncUpdateOrder ();
) e! u$ e5 c! X2 k, r1 ^1 Y# o. U3 B" A- F
try {
7 |1 I3 E% y. ]8 w( U modelActions.createActionTo$message - e7 z9 Z+ C9 p& q: q
(heat, new Selector (heat.getClass (), "updateLattice", false));
& m; W) x4 l: O7 y- t5 d9 Q* |2 l7 z } catch (Exception e) {
" x3 O" L% w" e( n System.err.println("Exception updateLattice: " + e.getMessage ());2 g" w+ F4 c, g
}
) V2 N% \/ ]9 N* z* x% H
2 m# [" x l) t" Z5 o# g. ? // Then we create a schedule that executes the* ?) r4 z; @- U8 Y3 t2 k o/ j
// modelActions. modelActions is an ActionGroup, by itself it
5 G c2 v9 _7 |$ c // has no notion of time. In order to have it executed in# y/ y5 a8 E0 r! y3 b
// time, we create a Schedule that says to use the7 M" j0 Z% [# K: H; T
// modelActions ActionGroup at particular times. This7 U) [8 Z, u( C6 F$ i9 ~
// schedule has a repeat interval of 1, it will loop every
- @, L* Y0 g( c // time step. The action is executed at time 0 relative to& U4 v2 C, s; c: b' A: J. g
// the beginning of the loop.
" }. v, q' F# r+ W4 m& A9 @1 D
7 _( C8 u1 k5 E // This is a simple schedule, with only one action that is
, |% ^# O1 B8 A& R // just repeated every time. See jmousetrap for more: T ?5 ~- ?$ W: ~
// complicated schedules.
- f& `0 W5 T3 L, W - T- ^6 Z& x) w
modelSchedule = new ScheduleImpl (getZone (), 1);
5 v5 Y2 k/ N6 y, Q4 f7 b modelSchedule.at$createAction (0, modelActions);
x3 @* h1 s" U) Y2 i- ~" _' g3 c
3 N/ U3 R3 q6 {4 p" Q% J6 Q- r: @ return this;3 x2 F4 A& o) J, t8 @5 K+ ^8 F
} |