HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:' D9 L- l4 y B# @& U% O& g
, f* X% |+ t9 i1 {. Y6 k& d
public Object buildActions () {
1 o9 I8 K+ n( @1 ~0 M super.buildActions();
: K9 [3 F- h1 A % M9 [1 ~; |# T2 E# P
// Create the list of simulation actions. We put these in1 ?, G) R0 |$ o5 \5 Y1 D
// an action group, because we want these actions to be7 M9 r& D1 K$ H5 [+ Y8 E
// executed in a specific order, but these steps should( p# Z7 H; H5 J
// take no (simulated) time. The M(foo) means "The message. v/ Q9 R1 C' N8 J v' `# g7 [
// called <foo>". You can send a message To a particular
. G) D! \3 E' n5 x; f9 o2 b) q // object, or ForEach object in a collection.- }8 m2 P N G
/ l7 A9 p8 F) d // Note we update the heatspace in two phases: first run
, ?( s( s! q- c3 @ // diffusion, then run "updateWorld" to actually enact the* ]% h Y# w; \4 N2 z3 X! @" _. n, H9 a3 W
// changes the heatbugs have made. The ordering here is5 {3 [- n, N; @$ h
// significant!, J: s8 k9 E" w) U2 ?
; Y) F q* m! `* P6 ?# o
// Note also, that with the additional
4 m! T2 Y& k4 q& J4 U: {- j! c // `randomizeHeatbugUpdateOrder' Boolean flag we can
! T/ O h/ m9 f0 `) S) J8 L // randomize the order in which the bugs actually run
! O l$ x0 w& ?4 j' I // their step rule. This has the effect of removing any
" L6 E. F. J# Y( N& K5 e // systematic bias in the iteration throught the heatbug
9 u2 P! n; p( L( t6 R! M7 D i // list from timestep to timestep
1 _; K, S2 s/ | w4 |1 J) U" { 3 [3 A2 w- D1 u% u: [* x) C
// By default, all `createActionForEach' modelActions have
9 H0 A- t3 b; n+ m8 z7 m4 V // a default order of `Sequential', which means that the
. j$ i4 R" w# S% s( { // order of iteration through the `heatbugList' will be
8 W {/ `4 z R' \ // identical (assuming the list order is not changed
- p) i5 m+ \8 d) S$ F, {% z+ o // indirectly by some other process).
/ ~. J% P. N5 |7 Y K
3 g5 Z: |2 K* L i modelActions = new ActionGroupImpl (getZone ());2 @5 I& b o! O" ]' ?& k% i
4 [- _/ z2 F: L* C' V, I
try { ~/ t2 M# _: a, t N( G6 _- V
modelActions.createActionTo$message7 Y" }/ f+ l; V
(heat, new Selector (heat.getClass (), "stepRule", false));$ U8 ?( m; A3 U7 K
} catch (Exception e) {
4 p8 n0 k8 M& R/ Q! p m, r System.err.println ("Exception stepRule: " + e.getMessage ());8 V f- P8 O# ]+ g) k) U. v5 ]( _
}# L( M7 ?6 O) s b
8 B: `/ p2 w- ]5 v2 [- r" _ try {
% t' y5 H3 K/ y8 ]6 K Heatbug proto = (Heatbug) heatbugList.get (0);# f1 q, t4 Z# \( p% G
Selector sel =
$ c: u9 n' C* E9 |2 s+ | new Selector (proto.getClass (), "heatbugStep", false);+ x( o6 R7 f0 ?7 ~: e# R( `
actionForEach =
1 V" d+ \, {, j& w2 Z+ W$ g/ _ modelActions.createFActionForEachHomogeneous$call6 x% K) T c9 B" f$ I# o
(heatbugList,
7 p' |% W7 w' C( A" o new FCallImpl (this, proto, sel,: Z* _4 V- _$ t
new FArgumentsImpl (this, sel)));0 g1 \, _ c! u1 X2 l0 O6 Z7 j
} catch (Exception e) {
/ I2 E7 c4 Q J+ z, } {4 Y H e.printStackTrace (System.err);$ w0 J1 L$ j% N; {
}/ Q1 S1 Q. L, v' k! F
, w: Z. [2 C2 y6 B syncUpdateOrder ();
; G9 P) x, ~2 [2 F( F e3 l* Z# t4 k3 u: E
try {, z, p$ f+ }% Z' Q. g9 h
modelActions.createActionTo$message
1 S( m7 j* _! y8 r! N% F (heat, new Selector (heat.getClass (), "updateLattice", false));; P! s0 v" W+ i" o1 @
} catch (Exception e) {& j0 r1 q( o$ V% D+ d
System.err.println("Exception updateLattice: " + e.getMessage ());/ T3 @# Z( ~! U) W0 F( a
}; _9 d8 W* ^' `
; V$ X; c3 X% [3 {5 h. Q4 W1 i/ \ p1 @7 b
// Then we create a schedule that executes the8 J8 F2 I2 x* _& N+ H7 s
// modelActions. modelActions is an ActionGroup, by itself it
0 f7 b+ Q# F2 I0 T& M // has no notion of time. In order to have it executed in. W; A7 `3 T$ i- L+ R
// time, we create a Schedule that says to use the
2 m$ T. `4 M9 c3 H( _ // modelActions ActionGroup at particular times. This u4 O2 P1 x; B( \* y$ r) P4 ?" G
// schedule has a repeat interval of 1, it will loop every
. d# r/ C/ K: Q' y# p // time step. The action is executed at time 0 relative to
/ ]) k: B o; w) `' g* Z2 u // the beginning of the loop.3 T# N0 H. s f B& }' N9 W. E1 H
+ a" B2 k# ? \: v3 \: U // This is a simple schedule, with only one action that is
/ L& f3 @5 Q( U1 _ // just repeated every time. See jmousetrap for more1 p$ ]9 j% H5 n9 ~" L2 s
// complicated schedules.
/ {2 s- M; d) ]3 x2 h n
5 }( @3 N3 }8 x3 ]* ^; P modelSchedule = new ScheduleImpl (getZone (), 1);
# y7 ?7 `; E" ?: X3 X modelSchedule.at$createAction (0, modelActions);& M; \2 s9 i5 g* p. ^$ D: g
" e8 C! G+ O* O* Y5 L
return this; l$ b/ z3 H5 [# D E
} |