HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 K6 k8 O$ g; h7 [ T6 y/ G1 B
5 v- K& r3 U$ g1 d' z
public Object buildActions () {
+ Z: E1 c1 L' T. L8 J/ ~ super.buildActions();
- {/ r6 D0 f8 f, d, f# L6 T% f. R
. }2 T8 r$ |2 X, }$ \ // Create the list of simulation actions. We put these in
$ w w* w1 g+ [# l) [; s // an action group, because we want these actions to be# f' G. V/ _% ?% f5 P
// executed in a specific order, but these steps should
) Q8 b4 g% n7 q // take no (simulated) time. The M(foo) means "The message: r" w7 u& y6 y6 I/ q/ R E
// called <foo>". You can send a message To a particular
, q, |4 t) j% O) e9 x# {7 y // object, or ForEach object in a collection.
& g/ y8 h# ]" U# N# b
* U, {- x% d, _* N3 X7 x // Note we update the heatspace in two phases: first run k2 |; L- G" {8 Y7 W
// diffusion, then run "updateWorld" to actually enact the
0 l) z3 S" M8 K+ C6 @0 [ // changes the heatbugs have made. The ordering here is- V' H$ l8 a2 U$ F2 b! D
// significant!$ ^8 }8 P* R: y: p) d
# t' e8 K+ G6 V# ? // Note also, that with the additional
7 V, a; {# e# Z5 i2 v) y // `randomizeHeatbugUpdateOrder' Boolean flag we can
' \" n* C& z- k: ] // randomize the order in which the bugs actually run) g% s( N' @2 I U; P/ l
// their step rule. This has the effect of removing any
) I% T1 M% C) a/ r, a0 ^; R // systematic bias in the iteration throught the heatbug
. J3 O) y+ x/ W% r' r // list from timestep to timestep& m4 K( e+ l3 K# C0 p/ a& G1 f
; o) o# ^ D7 ~4 E
// By default, all `createActionForEach' modelActions have
' B+ L2 a; `" s# b. H3 f# C% ~# u7 S // a default order of `Sequential', which means that the
9 ]. w5 F* B: P0 G& x // order of iteration through the `heatbugList' will be* E# }$ x; E H; D8 m+ g5 X m9 N
// identical (assuming the list order is not changed
5 [3 a5 w0 i, a // indirectly by some other process).& F/ @2 n/ u C( K# D0 J
7 ?) ?( w% I s9 ]9 h modelActions = new ActionGroupImpl (getZone ());
7 P+ t7 {5 N. N! `! M: ?- v: r3 B. q4 u6 X6 W$ x
try {
4 z% d" v. ]/ l: s9 \ modelActions.createActionTo$message( H B$ Z# B5 X" a9 ^9 `
(heat, new Selector (heat.getClass (), "stepRule", false));) S' h3 M, M, b J' {- |" k' d
} catch (Exception e) {
7 U% H/ W2 |1 c+ b; X! h. G$ { System.err.println ("Exception stepRule: " + e.getMessage ());
8 \/ W$ ^0 g& l: F. Q }. R) r1 X& { E/ z
; A& R, V+ {3 {$ i, P( N6 o7 y
try {
( p9 B) a. E5 U9 D* } p: X Heatbug proto = (Heatbug) heatbugList.get (0);
2 `8 x" B; V# ^* F" R$ B5 k Selector sel =
" Y0 s) e7 @/ D+ M% P8 \6 ~ new Selector (proto.getClass (), "heatbugStep", false);
" k! S+ l) u+ r' w" w actionForEach =
) R! T7 K, `8 _+ \6 s' [- Q modelActions.createFActionForEachHomogeneous$call3 i$ {) [* Q5 H2 }
(heatbugList,( e3 b* X6 X3 J5 ?! I
new FCallImpl (this, proto, sel,
& w2 n1 [" t& ~, s% t: d new FArgumentsImpl (this, sel)));+ x$ \0 q/ k8 F* M, K: U
} catch (Exception e) {
& g3 _4 K, F9 V( W4 i5 K8 u e.printStackTrace (System.err);7 B8 C L: Y- ~. L' \ d' k/ w
}
0 U6 j- n5 _$ U/ z/ r _3 G% a0 [' ^8 W4 z
syncUpdateOrder ();9 `0 K5 z7 h& L4 t' {
0 |) J, @0 s$ A; v7 D5 a
try {
: a$ f, n8 l9 }( ?5 f modelActions.createActionTo$message
3 O( v# R, [& V) N* N: a9 e- ]" l (heat, new Selector (heat.getClass (), "updateLattice", false));. j! z2 v( Z. ] ]6 G2 ^2 V' v4 @
} catch (Exception e) {* S& ~1 B* o6 o! [# F
System.err.println("Exception updateLattice: " + e.getMessage ());" x" M" {7 s4 G) L
}
0 s) K+ p; b# ?! p : i) L) T" j5 E
// Then we create a schedule that executes the
7 `/ J2 y( f/ c // modelActions. modelActions is an ActionGroup, by itself it( ?6 y& M7 h) r. {# C5 k* h6 v
// has no notion of time. In order to have it executed in
8 v* n8 e9 G5 p( L$ @) o // time, we create a Schedule that says to use the; v$ h% ` F1 M; T/ ^; L3 _
// modelActions ActionGroup at particular times. This
6 C R. ?3 J9 J( m1 x# M% h // schedule has a repeat interval of 1, it will loop every3 E/ F; A. e; C" Y1 [: O9 F
// time step. The action is executed at time 0 relative to
5 f, f; h4 {3 K4 ^* ]) L/ X$ e) N // the beginning of the loop." y4 D0 _8 B& t |3 Z. y+ \8 q9 e
9 Q9 g' N. R) T
// This is a simple schedule, with only one action that is8 o" I: b$ g: C& c( s
// just repeated every time. See jmousetrap for more/ ]3 I/ B H" X! X1 \/ e! G
// complicated schedules.$ p$ s0 p+ V: _2 Q2 X
1 d* b" C1 k; H6 N) V: q) O: X
modelSchedule = new ScheduleImpl (getZone (), 1);, u6 c' i$ B# H( k
modelSchedule.at$createAction (0, modelActions);
8 I; e0 }* V& j 2 X$ N3 Q! Q0 K# U C
return this;4 }. {7 [, ^. s) O
} |