HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:3 B( x: V9 ~6 y O' ]) Q4 z
! S) J2 Z8 T' P# m0 W public Object buildActions () {) t7 a" g( F" M' C1 {4 q
super.buildActions();
* I( Y o, A1 b/ K* o. p & x3 ~7 e* T' C* G
// Create the list of simulation actions. We put these in
+ b7 i& F& _3 c1 J! s% ] // an action group, because we want these actions to be$ L( {1 S/ _: [$ r% L: C5 h
// executed in a specific order, but these steps should5 ]! W# ^5 Y+ `. a
// take no (simulated) time. The M(foo) means "The message7 Z" ~& |6 P# U* m2 K- ]
// called <foo>". You can send a message To a particular
& G( J9 m! @+ L: J1 {5 C5 v // object, or ForEach object in a collection.% M8 e0 N; Y: ?1 r+ \/ G' G/ a
4 `# ?+ p" _2 |% A( e" b7 o/ m
// Note we update the heatspace in two phases: first run& {' W" A" W7 p' I
// diffusion, then run "updateWorld" to actually enact the
1 {) J. J, \* ~6 }8 ? R: [ // changes the heatbugs have made. The ordering here is
6 m, n. E' d c8 q0 s @& g // significant!
# E& d+ k! o$ o- ?, _
5 C6 c* |9 p' G$ m2 E7 i1 b" R // Note also, that with the additional
/ f3 C8 P. ]5 O+ s7 q% O // `randomizeHeatbugUpdateOrder' Boolean flag we can
/ m Q: d; w) y; W2 ?* H // randomize the order in which the bugs actually run
' Y3 {) {* o& R) ]5 B& z, L // their step rule. This has the effect of removing any
- x2 }4 _; C9 A( G- g N7 u // systematic bias in the iteration throught the heatbug0 v% o3 s$ E% i; M
// list from timestep to timestep7 R. o6 D# V# w& b+ M$ R
* m# G7 e" Y6 o. a l4 v
// By default, all `createActionForEach' modelActions have: O" t5 Y1 R; d/ a8 [2 k- k
// a default order of `Sequential', which means that the" K" y9 \7 n M8 e m/ Y
// order of iteration through the `heatbugList' will be, {4 c. A* ]4 |: P$ c
// identical (assuming the list order is not changed
1 C4 { W! `) v9 C8 O // indirectly by some other process).
5 D+ i+ o2 y. x% d' X- z& l! ^ L& s
; z9 r% a$ z8 n6 v; X2 J modelActions = new ActionGroupImpl (getZone ());
- B0 H6 c5 I4 j" A' e8 X; i9 J
: |0 \# W! W- O# `, H/ C* F try {) O. k. `% a" B4 `# a, G
modelActions.createActionTo$message6 g0 D) h5 Z% w7 H- c
(heat, new Selector (heat.getClass (), "stepRule", false));/ g7 I( W" K% s) W' e: V
} catch (Exception e) {/ V/ O. f7 I0 W8 {
System.err.println ("Exception stepRule: " + e.getMessage ());+ N4 s& P( h6 U4 {1 @
}& [; G( @: _8 `3 M' M7 U5 X
) H- H- ], Z9 g" ^/ Y
try {( j0 `' X9 w: s5 }: H+ a, q$ d. k/ z
Heatbug proto = (Heatbug) heatbugList.get (0);
p- M. O$ I. G. K$ E: e Selector sel =
3 f8 L0 @4 O2 x1 {! F( } new Selector (proto.getClass (), "heatbugStep", false);2 a, }, ~* t& Z" e; R% U( E/ {
actionForEach =: {% o4 v! D1 P- u& U
modelActions.createFActionForEachHomogeneous$call4 E- _( o/ w, ~$ L* p
(heatbugList,% u( h a- H& l
new FCallImpl (this, proto, sel,
6 u- ~& u# K7 y new FArgumentsImpl (this, sel)));7 P9 k( C. j7 Z! u% L' o& E& C
} catch (Exception e) {# K- ?9 E5 {, S0 I
e.printStackTrace (System.err);
) q5 m E* H+ O- a$ W o$ A3 m% n }" }4 |5 Q4 v1 V2 o* r
R+ J* T" r. i ~, |: s1 H syncUpdateOrder ();
: I0 x+ {/ q- g+ ~
- C: c9 e. }- ~' R2 _ try {* {" \/ Y+ H m. y& I/ e
modelActions.createActionTo$message 6 Z9 r6 `5 t5 {9 r
(heat, new Selector (heat.getClass (), "updateLattice", false));( f& b K4 C1 D
} catch (Exception e) {
5 y. o2 P: O, S- ^) N$ | System.err.println("Exception updateLattice: " + e.getMessage ());( {" v& {( C: s2 c: f
}2 D6 Z: I1 E5 T [; k' ^- e
! {0 s" _5 _5 P7 l- q+ X' Y9 U' x$ t // Then we create a schedule that executes the8 s/ [+ {: ~- j! L1 `# K
// modelActions. modelActions is an ActionGroup, by itself it5 y+ @# p# O2 k$ P: ?& `% d1 E$ M
// has no notion of time. In order to have it executed in- b3 }' B3 J0 {( e% h+ C
// time, we create a Schedule that says to use the0 J8 V8 k2 X% Y4 n9 s' b) U. U. o* x* i6 |
// modelActions ActionGroup at particular times. This2 \$ \6 s$ X+ g' A
// schedule has a repeat interval of 1, it will loop every0 } B& c# U3 c) ?, h4 c- O
// time step. The action is executed at time 0 relative to0 }: |2 e/ z$ [* d* y6 \, t
// the beginning of the loop.6 P5 H4 M. [4 O( u( J' x4 G
) u3 f5 p! u Q9 P: N
// This is a simple schedule, with only one action that is
0 D L3 Y) b, y- i0 [+ P. L/ M0 o // just repeated every time. See jmousetrap for more+ Q M+ m* U n- k& B
// complicated schedules.
' F/ D* P! S w9 }( O
; e3 c; d) H X; a& S; d modelSchedule = new ScheduleImpl (getZone (), 1);
- j1 {8 p; N0 o% X modelSchedule.at$createAction (0, modelActions);
! T3 B5 c9 O7 m8 j! R" ~ 3 Y: _) ] i) [, n5 Q
return this;
4 b$ u7 \; @4 v8 O, M$ E0 n } |