HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 I3 z' `$ D# d T% y5 i C
8 W, L' J Z$ W+ T9 n' Q! s! p% ? public Object buildActions () {
0 X' j% E+ h3 `' p' v9 r super.buildActions(); ~, V' b8 o/ M9 D5 ]$ k8 }
% n" F, {! y* {! l0 u
// Create the list of simulation actions. We put these in# F9 ]' E& d& X. f; ?& l
// an action group, because we want these actions to be
+ \! g! q; Z4 V/ R4 l& U3 Y0 W // executed in a specific order, but these steps should
! n) G. J& p' \8 @ // take no (simulated) time. The M(foo) means "The message1 `# n7 H8 e* [) M8 k
// called <foo>". You can send a message To a particular7 `+ r0 I- u) N9 i$ t. i
// object, or ForEach object in a collection.
2 Q1 A% u) S% n# Y $ z6 G# ]; |2 t# W4 p1 \* K$ a
// Note we update the heatspace in two phases: first run3 W2 g/ j' ~7 E1 F! Q1 |: N. n
// diffusion, then run "updateWorld" to actually enact the5 g. K* M$ E% m+ _, U. R7 d8 q3 e
// changes the heatbugs have made. The ordering here is
# {7 T/ c1 S: ? // significant!
& j! [; t/ p c2 I- T . f4 J. J' t, X6 [
// Note also, that with the additional. Q! F; u! C* t; {7 q8 [
// `randomizeHeatbugUpdateOrder' Boolean flag we can
$ \5 l5 R2 l! e, o* T1 @- H // randomize the order in which the bugs actually run
( _6 I. e1 e3 {' v T' o9 g1 r // their step rule. This has the effect of removing any6 i9 W2 B! t6 M+ o: j
// systematic bias in the iteration throught the heatbug# N& B. ^# R% S! q' h
// list from timestep to timestep
[4 Z$ g( m. V6 @0 R! r 9 H: i, q6 B: ]9 I9 ]4 D
// By default, all `createActionForEach' modelActions have
W" |. f0 _; z _ // a default order of `Sequential', which means that the
; Z4 J+ t$ s* q) M1 F5 U8 U" l // order of iteration through the `heatbugList' will be
5 q0 a& _( s5 C // identical (assuming the list order is not changed
' P: G2 y" a0 j! k' m6 O // indirectly by some other process)." | X- L3 H# _: |9 A; X
, I9 P& F/ U- ?
modelActions = new ActionGroupImpl (getZone ());
9 } L. u# ^* Y8 z% g: q. x7 B! ^4 }& H% z X" Y( D" \ A
try {) P8 J& U* k% l9 k# e! B L
modelActions.createActionTo$message( g1 g, m5 l+ ?8 q
(heat, new Selector (heat.getClass (), "stepRule", false));5 i" B- \+ a0 x/ r) L( r
} catch (Exception e) {
( m4 F4 l% U6 s/ U; s% C4 {6 q System.err.println ("Exception stepRule: " + e.getMessage ());4 C" t6 g5 T) A" D; l
}, ?" ?7 _4 h; C& ^
y: d8 Q; Y1 Y! Z: r$ C) @/ j7 C
try {
" o \7 e) @9 { Heatbug proto = (Heatbug) heatbugList.get (0);
6 C W7 K5 p1 L4 z' h) S& f Selector sel =
" L% y0 }5 s- O8 p8 p, k1 ~+ Y0 [8 ^; B new Selector (proto.getClass (), "heatbugStep", false);
- X: b" J8 N0 x" r1 Z1 ~4 p actionForEach =
[9 \8 `. n9 v modelActions.createFActionForEachHomogeneous$call! l8 n" ?. o3 x* W
(heatbugList,1 f1 E8 Q6 ]( n3 M" ]" P/ v0 M
new FCallImpl (this, proto, sel,- a' Q7 a! m# S" |* G
new FArgumentsImpl (this, sel)));1 W, n( K6 J: f; g2 `! f
} catch (Exception e) {
! P' v [+ M& L3 m; D; a e.printStackTrace (System.err);
; f$ e8 ]4 d! b! m, ` }+ }% |0 D1 e% c; T
) A& ]; Z- H7 p# E1 d: k syncUpdateOrder ();/ J K5 R; E) u5 v1 M
2 R1 C& {( \2 E$ r. S% Y9 g try {$ J) ^" N+ D* L' f
modelActions.createActionTo$message h% G3 n0 R& Z4 p
(heat, new Selector (heat.getClass (), "updateLattice", false));
0 ]7 `, ~9 Z/ z0 ]3 S } catch (Exception e) {
- i7 b9 Z) x( i7 w( I: _ System.err.println("Exception updateLattice: " + e.getMessage ());, Y; D) N3 b8 {3 v
} m; J( x: ^$ H' @4 M9 H) g0 H9 m
! o! x% d, q# q" C& Z* Y2 o( m // Then we create a schedule that executes the$ R& _# Z3 n5 g6 J0 _
// modelActions. modelActions is an ActionGroup, by itself it# [6 C! z r: {& N
// has no notion of time. In order to have it executed in# u1 b6 t6 s6 K# K, m
// time, we create a Schedule that says to use the
( n! g' q, r% T1 \ // modelActions ActionGroup at particular times. This; |- x# h# e5 m
// schedule has a repeat interval of 1, it will loop every
2 F) ]2 N- `% K9 l: I1 c- E8 X // time step. The action is executed at time 0 relative to8 o3 C2 Q2 M0 [
// the beginning of the loop.
' r- }" R& A4 e; p! x; [+ O: c6 O5 w/ A- {/ `2 Y! z- I+ I) c8 B( D
// This is a simple schedule, with only one action that is n1 Q: @* g7 F, |
// just repeated every time. See jmousetrap for more3 w: n1 j3 z* _& J% Y
// complicated schedules.
5 z; z7 T- m/ O% D
- u5 ~2 p' R5 d) e modelSchedule = new ScheduleImpl (getZone (), 1);
' [$ b* ]4 A4 a H3 G modelSchedule.at$createAction (0, modelActions);
, N7 B3 j1 G7 G, u2 j7 @8 A8 X
2 k: u7 u U$ ~. ^# a return this;
& o' F2 u- q2 b$ ~% k4 {& T } |