HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
8 ]5 T4 S* l% K3 J3 P0 K$ C4 x
5 B* Y! b% O1 `. r public Object buildActions () {
2 Z# a" n5 I0 Z) x& ^/ o3 Q! P# ~ super.buildActions(); j3 g. n" h" H
, u6 V4 L3 N& @6 W // Create the list of simulation actions. We put these in
! n8 v5 S' q7 { v4 G! [ // an action group, because we want these actions to be
) [& C$ \+ ]. ~, @3 t' M // executed in a specific order, but these steps should
6 R7 _5 M0 c5 l1 `# v5 |2 o // take no (simulated) time. The M(foo) means "The message
9 z% h; e" F v. j8 p( J8 [ // called <foo>". You can send a message To a particular% f8 z8 n- W8 Q4 f0 r, y. N
// object, or ForEach object in a collection.
4 b1 K: j1 S: \- L: Z
7 P. B9 L+ a* W7 ]9 D; g9 P! A" \ // Note we update the heatspace in two phases: first run; b4 |" H& y0 E8 j, T3 v
// diffusion, then run "updateWorld" to actually enact the
0 s9 V# b" X% F9 J+ G7 R // changes the heatbugs have made. The ordering here is
. X$ k8 s1 s, c$ T // significant!
: T+ f- O: R% ~+ h( w5 B
1 G7 A2 E: N6 L* s8 @3 X9 h // Note also, that with the additional
* H! Y4 M* K, f% d- d7 O // `randomizeHeatbugUpdateOrder' Boolean flag we can4 `6 r: s! ~) b( Q2 X
// randomize the order in which the bugs actually run- s) q1 g$ }: G% V. ]
// their step rule. This has the effect of removing any
" O) c8 T z2 F6 d, x2 ~ // systematic bias in the iteration throught the heatbug
; z4 e" ]) Y' D8 ` K' T6 { // list from timestep to timestep
. P6 s; d1 e# n( z5 {8 S 2 o$ u6 G, [, {. M) ?+ |# b
// By default, all `createActionForEach' modelActions have
1 Z6 Z8 C J: m- ~ // a default order of `Sequential', which means that the, [* Q4 |; N) B! b7 ]# V: [
// order of iteration through the `heatbugList' will be5 f- y6 Z7 W+ D; ?# U/ Y7 z4 A
// identical (assuming the list order is not changed
, c) p$ m1 P: O) e8 h // indirectly by some other process).
$ \' x6 w% ]5 a
! W, U1 w) c+ y" e modelActions = new ActionGroupImpl (getZone ());
- B9 x7 i8 Y' N8 B7 I* d3 J* ]1 r' h/ p4 s7 m
try {5 u# q W2 m2 i! a% ?
modelActions.createActionTo$message1 E5 ?8 y4 U8 c7 z1 Q, c) q. R' M
(heat, new Selector (heat.getClass (), "stepRule", false));; _# d0 v- t. m0 r
} catch (Exception e) {
$ Z7 L/ l5 b4 U) N System.err.println ("Exception stepRule: " + e.getMessage ());% G+ V9 f! }& v H1 g8 D# U( p) Z
}
# P% [/ `( E4 _8 C% S6 ~% |" x7 u7 u7 y" V* h
try {
9 |- Q* I9 s g- h3 L Heatbug proto = (Heatbug) heatbugList.get (0);2 J$ c" R& x; i0 Z& U
Selector sel =
9 r5 V' f7 T$ _! U" @1 b- u0 S2 N: d new Selector (proto.getClass (), "heatbugStep", false);' K/ `& t: u" H8 S
actionForEach =
5 M+ Y" p' `. @! ~+ [ modelActions.createFActionForEachHomogeneous$call
) G6 ?7 M- h2 v9 ?- k# X (heatbugList,6 O$ c4 N/ \" k7 V4 z4 E; T" w. ^
new FCallImpl (this, proto, sel,. f/ h, I( h1 I5 `$ i, X, R
new FArgumentsImpl (this, sel)));7 W5 [2 R; |5 E' d9 g, ]4 A5 j1 i
} catch (Exception e) {) V9 [- ~) T e- @' v- a
e.printStackTrace (System.err);
( L3 [& X) t; S. O8 u }1 `& [2 P" G, ?
$ [% g& \- F) c( s7 J7 A1 |
syncUpdateOrder ();( ?- x' `0 m# K3 e" e3 _) A
7 @) Y" ~) i: |; k; o& d# G try {
8 W$ C1 {4 O$ _4 x; |7 N; A* y modelActions.createActionTo$message
7 |$ B6 p9 I% X- ] (heat, new Selector (heat.getClass (), "updateLattice", false));
) u$ c6 \% I, @3 C3 u7 w" Z } catch (Exception e) {4 N+ B, s1 _) T4 H: ]5 V
System.err.println("Exception updateLattice: " + e.getMessage ());1 U0 ? _0 L/ C9 O/ N
}% r4 W' Q* o) l/ R4 C
4 i' V: {9 W9 {! ?0 f- _* P1 X // Then we create a schedule that executes the
7 [3 w6 [1 Z, A1 \4 g' A2 L // modelActions. modelActions is an ActionGroup, by itself it$ a" P: Q/ l1 ?2 j' h0 D5 l
// has no notion of time. In order to have it executed in
7 m1 d8 Q3 O6 x9 @ // time, we create a Schedule that says to use the
. K2 {, R7 [. b" s // modelActions ActionGroup at particular times. This
0 h, N8 k) v9 _, F! c' F1 w // schedule has a repeat interval of 1, it will loop every
/ y& M' b$ y! d/ ?8 ]) i // time step. The action is executed at time 0 relative to) H L; c! W$ {, v+ m, M P
// the beginning of the loop.
' C! ~$ L5 U8 @, |; c/ ~. ?4 x5 P) n$ p; w
// This is a simple schedule, with only one action that is
3 l9 c8 y! v' Z& T, P) i& S; l // just repeated every time. See jmousetrap for more8 J5 N/ a, T$ C. b4 L4 ]
// complicated schedules.) G: w- [4 Q8 a+ F
8 Y" N! |$ K6 l0 n, S( A& }* G- ~% I modelSchedule = new ScheduleImpl (getZone (), 1);8 D) i5 l- S8 i7 }( j/ q' i' w' j V* a
modelSchedule.at$createAction (0, modelActions);. f- ?6 J0 Z, c+ Y C
1 V( B# Z9 y# \& O$ {- m4 b return this;" I3 |# @4 [; d
} |