HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
$ S2 U2 q* p0 {- f
+ t3 |. ?. `' I public Object buildActions () {" ]9 i% Y k7 |) }0 [
super.buildActions();
1 E) d+ E" k8 N G
$ J/ I0 O1 b# W: K& E: E! z // Create the list of simulation actions. We put these in
& G5 @# |4 ~5 r7 l4 i // an action group, because we want these actions to be
4 Z; p; H G2 w; s // executed in a specific order, but these steps should% \# b$ Q5 a' Y* x& \; M5 L
// take no (simulated) time. The M(foo) means "The message
( C! E3 g8 }2 `5 e& v0 T // called <foo>". You can send a message To a particular! k; ?, R3 V6 {
// object, or ForEach object in a collection.
" Q* \# J2 j D1 L
8 N! U2 Y& F% U2 }! o, \ // Note we update the heatspace in two phases: first run# Q: i8 y5 J( w3 g
// diffusion, then run "updateWorld" to actually enact the
4 h4 c; @; o$ ^, `: m // changes the heatbugs have made. The ordering here is3 e- P0 T/ }6 s2 K F! x
// significant!
9 L3 {+ [7 q0 S, m% ]( z1 U ' E# S2 b# Y# w/ q: ?3 I& Y6 \9 V
// Note also, that with the additional
$ b1 k4 m' ~, p7 U8 \ // `randomizeHeatbugUpdateOrder' Boolean flag we can
+ [9 X0 H _) O) v, h- ^ // randomize the order in which the bugs actually run
1 S( x2 P( d- ~+ K // their step rule. This has the effect of removing any) _6 L3 u7 A* g- p* S
// systematic bias in the iteration throught the heatbug
, b& D& ?/ I) O* C$ P // list from timestep to timestep" l7 A, U+ o2 ^, B, F4 E' e
+ C9 a; z4 Q4 f. R; K7 B2 o6 P // By default, all `createActionForEach' modelActions have
: N& ]5 S7 f# B. n3 L8 n/ U // a default order of `Sequential', which means that the5 F% q" `# ?$ k! w& T2 t* t/ W1 G d! ?
// order of iteration through the `heatbugList' will be
, w) e9 D& ^" \1 H" G+ I2 Y( b // identical (assuming the list order is not changed; K: i( S& y/ _- E6 V3 o
// indirectly by some other process).% V* J7 ~0 u- M8 i( f& V9 N
5 Q! G( M% U m$ F3 o- C4 V" i. [. J
modelActions = new ActionGroupImpl (getZone ());
) Z' ]7 h V3 v# A1 |0 R6 `; O f7 D: k+ x) P. h% @# A
try {
o' P: h! g6 X8 T) S, t modelActions.createActionTo$message3 a2 x% g; ^: H4 T: G3 n/ j) N
(heat, new Selector (heat.getClass (), "stepRule", false));+ X1 Y" E9 L% g8 [5 K
} catch (Exception e) {
B A# P3 U2 @, E) \% b System.err.println ("Exception stepRule: " + e.getMessage ());
, ~7 C, a, X h. w }# k' O5 T/ q* s- r) N: [+ h- u
$ M) k& Q# T, ]9 } v' G try {
- j0 D3 S% `- `- i Heatbug proto = (Heatbug) heatbugList.get (0);% L+ V; @- W( t5 d$ q% z5 @ Z$ Z; o* B
Selector sel = * o1 `* n) h$ M% M
new Selector (proto.getClass (), "heatbugStep", false);
6 I6 T, X% T0 a1 g4 y actionForEach =
( I% b* L: I* k modelActions.createFActionForEachHomogeneous$call
& q! E. E% ~; G& I+ t; L (heatbugList,
4 _5 H9 B$ J- a! i( h new FCallImpl (this, proto, sel,9 P3 N6 V/ X, P: H" u- b' i
new FArgumentsImpl (this, sel)));2 I2 T% t6 v3 B
} catch (Exception e) {0 Z* d# _% r# F
e.printStackTrace (System.err);. l# \; ]; T7 x7 y( {$ H# S
}7 j& w' ~! u: L r
& T2 \2 D2 A- N) p& _, q
syncUpdateOrder ();
" w3 K5 o# i& s
+ y: r, i2 e; }# j5 L' i try {3 f, z% x" U3 r) W" ~
modelActions.createActionTo$message 3 B# k$ v0 @3 P3 g* Q. K+ y; V( `
(heat, new Selector (heat.getClass (), "updateLattice", false));- R) r5 j/ l0 z( |
} catch (Exception e) {
! _) D3 h+ j) d System.err.println("Exception updateLattice: " + e.getMessage ());
" g7 y$ Z( e% {" o) H! q }
7 W) W6 A* D) u& k ( ^ A# u8 e& @# `: W. Y
// Then we create a schedule that executes the8 E0 L C7 d5 O
// modelActions. modelActions is an ActionGroup, by itself it y5 O% C& r2 D n' t5 @; R
// has no notion of time. In order to have it executed in% N7 Z; H# W' e
// time, we create a Schedule that says to use the
( t1 Q) a) X4 t" D: I5 D // modelActions ActionGroup at particular times. This
# ?8 n% A) m* {, j // schedule has a repeat interval of 1, it will loop every
4 _2 F! l N' P/ Y& K/ w // time step. The action is executed at time 0 relative to
; G9 T3 G9 K9 @; d, N- T // the beginning of the loop.+ b- @9 d. K+ ?- i( y5 Y2 F
" S" [8 v6 } y8 s# t" |
// This is a simple schedule, with only one action that is0 S8 B2 m% y7 i
// just repeated every time. See jmousetrap for more
- j ?3 K3 j/ K+ V% H // complicated schedules.4 F7 I9 S2 i* @! U$ |
- u# L6 V0 o# w% U! z4 S8 I, Z/ c modelSchedule = new ScheduleImpl (getZone (), 1);
! |$ a: [- Z: e, h) j3 G L modelSchedule.at$createAction (0, modelActions);# T- j0 p/ _( r0 m) U% u6 B1 A
" g$ g% U5 h, a7 `- P& n* c. S return this;# J" K0 }* i$ x( x7 M* E
} |