HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" Q1 v, z% k# T8 a X
6 z0 l$ b k8 y+ o
public Object buildActions () {
0 h% ^: p/ d. h }3 c- A' \2 k) J3 h super.buildActions();
! C: @+ E5 q6 Q$ P5 S' k+ v. j : b* _7 A/ _% \5 j& e
// Create the list of simulation actions. We put these in1 ^3 T: N( z* `
// an action group, because we want these actions to be
# c$ b6 U) | x% ]. ~0 ^( B // executed in a specific order, but these steps should
0 g }( }) { J3 N9 ?( W _; X3 \8 j" s // take no (simulated) time. The M(foo) means "The message/ u$ o m! r! y; r
// called <foo>". You can send a message To a particular2 F/ X7 h6 ^$ {! N" S5 a8 o
// object, or ForEach object in a collection.
; t1 v2 H! N/ A$ {' J7 U 2 R( k2 U! k4 s& t) R/ Q' Y
// Note we update the heatspace in two phases: first run
; k: }2 ]: T9 ?& O3 N3 i // diffusion, then run "updateWorld" to actually enact the
; c7 A# X! _3 A+ p5 s* p+ O* q // changes the heatbugs have made. The ordering here is
( i% U! n: h. }# R# O. N" G+ `% _! v // significant!
- u+ O$ ]9 p' |- K 0 r- {" S4 w( o+ O
// Note also, that with the additional& \9 h. s7 ^# u+ T7 T
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. o8 h' j7 O9 w+ w% c/ o // randomize the order in which the bugs actually run
3 b- x, n% p/ V( t! g // their step rule. This has the effect of removing any1 j9 ~- x0 ?( d! V( W* h% J
// systematic bias in the iteration throught the heatbug
: u7 |( U- M& W // list from timestep to timestep
0 B& P9 i6 p; s, b9 u $ N% S3 g. ~3 N/ G/ \1 V6 `
// By default, all `createActionForEach' modelActions have0 y ~- q3 A o" ]( M
// a default order of `Sequential', which means that the: s9 |5 E9 _- x- ? j @
// order of iteration through the `heatbugList' will be
: K; y: l) O; N% G0 r4 V) F // identical (assuming the list order is not changed
1 J* O, v+ x- i, t8 [; S, B! h, ? // indirectly by some other process).- Q N, p1 _2 q4 |- \
0 K: h% b2 p9 B- Y6 \' t
modelActions = new ActionGroupImpl (getZone ());+ G. r3 M6 \( q# A1 b0 }* V: `9 }2 x- i
+ [" y s7 ~! \( d try {$ m: h, T, P, M: e/ h' t7 T1 D K1 Q+ n
modelActions.createActionTo$message& l! g c9 Y/ A8 L. h4 Y2 T& y
(heat, new Selector (heat.getClass (), "stepRule", false));
3 i! a* `5 y9 K/ u; a } catch (Exception e) {
/ s% U4 @* L, F- ]# U0 Q5 P System.err.println ("Exception stepRule: " + e.getMessage ());$ ^* C' D% B: q/ r
}! z0 B+ d& ~3 I s; E" b+ v0 O
1 G% ^5 x& A4 W
try {$ C; R- n, F/ r7 G, I
Heatbug proto = (Heatbug) heatbugList.get (0);2 c3 y* G* o5 ?! B9 y. Z
Selector sel = ; d y o' d# O5 Z _
new Selector (proto.getClass (), "heatbugStep", false);4 S- h/ n! r8 _4 } d( h- N- j
actionForEach =
9 F' _( Z6 ]/ q$ k# [9 q' _' C modelActions.createFActionForEachHomogeneous$call( j }4 `7 Q! D6 r" y: H$ R
(heatbugList,
( U' z/ s( J( S, O new FCallImpl (this, proto, sel,
; B8 X: L2 m0 {. h6 v% w$ z* S new FArgumentsImpl (this, sel)));& @. ^, _! N& B# C2 e( `, {- `' b" _. e
} catch (Exception e) {, A" `3 k) o% G" m) M9 B6 p$ b
e.printStackTrace (System.err);6 ?8 s- g: ^4 E6 }, b0 Y
}
" s. J/ u" z' |3 W" f' p: d/ M3 ~ / r) l$ w1 V3 t8 @7 q' [, \ l4 ?6 }
syncUpdateOrder ();
' s' a, K7 ~) W d& _ u9 c$ W5 r0 _$ ~4 n$ w& v0 [& y
try {0 |* _" b1 s4 T/ |# i
modelActions.createActionTo$message
$ b0 V) ]. K+ r& D0 T (heat, new Selector (heat.getClass (), "updateLattice", false));+ I9 r0 \$ ^2 V5 i
} catch (Exception e) {6 n) D3 O- ?0 Z) k! x9 x
System.err.println("Exception updateLattice: " + e.getMessage ());
2 P; D! v+ a+ J+ ?" v6 X }# H% P6 n9 Z, i+ X
/ C. O) K! b1 M- n
// Then we create a schedule that executes the; C) ~, I2 E# {! v8 R/ l
// modelActions. modelActions is an ActionGroup, by itself it
8 ?" j9 u$ G" p4 ~ // has no notion of time. In order to have it executed in
, M e6 B; U; C // time, we create a Schedule that says to use the4 V# O8 g' ?* l/ H7 W$ @
// modelActions ActionGroup at particular times. This
5 L3 w# c* o' i& O" K // schedule has a repeat interval of 1, it will loop every9 k- z. G( p7 X
// time step. The action is executed at time 0 relative to
" }2 C" V/ ^5 n% D+ r3 A# b: A // the beginning of the loop." y/ |) f* e9 i
* z' o) ?6 m$ G1 B3 x! Z+ ^4 a
// This is a simple schedule, with only one action that is
% D) [" R# S p0 x$ b // just repeated every time. See jmousetrap for more$ W7 n0 y: e0 K- L$ ^9 R+ u
// complicated schedules.
7 E. @/ M' i! u! L " P3 w" q% E. P+ t+ n3 ~4 [& D
modelSchedule = new ScheduleImpl (getZone (), 1);
! k% F# q6 |5 U* Q! \1 W; B modelSchedule.at$createAction (0, modelActions);2 ? [$ P0 t" X' I
8 Q, [( V% i7 c+ C7 y/ |
return this;
" p+ Z* G" a- E! o1 X2 _ } |