HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 }3 k$ S+ B( Q; I5 B2 I, a7 g- \
* H9 l8 z5 V# Y& h! ^, ~+ A" Z public Object buildActions () {3 Q: B7 R- D$ e: {- B
super.buildActions();6 `/ Y& i' G7 l) X
$ K2 c' k( k2 q* X
// Create the list of simulation actions. We put these in
. K7 J" b3 h" V4 z0 A // an action group, because we want these actions to be7 N1 z2 \; _" I: m& B# `' e# C+ w
// executed in a specific order, but these steps should
& \* u/ F# D- s$ j4 t // take no (simulated) time. The M(foo) means "The message+ {; P$ f% n, H- f- V4 Y9 R
// called <foo>". You can send a message To a particular3 X8 l& {& { D) i9 k
// object, or ForEach object in a collection.
# j4 g7 k/ C$ O1 S$ o
( j5 U- Z9 _8 w+ ` // Note we update the heatspace in two phases: first run$ ?1 V( \$ H8 r" P' A: b! L
// diffusion, then run "updateWorld" to actually enact the
4 E2 p/ j0 U4 w0 H; m1 B // changes the heatbugs have made. The ordering here is
. q/ k% ?! R+ ]0 F7 V6 G7 c // significant!
2 Y2 v' @. [1 a. y6 L Z6 l% E
- r2 C; f) y0 K$ Q // Note also, that with the additional" M3 L* h" H9 E" c* n( V
// `randomizeHeatbugUpdateOrder' Boolean flag we can X; F" b D2 b n0 u: b1 Q
// randomize the order in which the bugs actually run
7 O0 b5 h: E: C W ? // their step rule. This has the effect of removing any
8 o) s0 T/ Z1 s+ j; T" P // systematic bias in the iteration throught the heatbug3 ~5 t( y% Y" Y7 E7 y. {5 c. {
// list from timestep to timestep
4 B" M! G( S+ {' g+ q
. s5 I' @- c: U$ F // By default, all `createActionForEach' modelActions have
; q7 l2 a, z$ p* E // a default order of `Sequential', which means that the) h, i9 `) u; q5 ^; \5 u& Z0 S6 {
// order of iteration through the `heatbugList' will be
: @6 A8 a: D$ }& Q( W% y$ L* B // identical (assuming the list order is not changed
8 g% a8 q! T- I8 ?% X) s // indirectly by some other process).3 t$ F4 {) M9 ?# D" t6 J
% O& l _( w, w* ~7 V modelActions = new ActionGroupImpl (getZone ());% T* u( r1 u7 ~2 ], D- a0 o
6 [1 I6 j7 m' _2 s2 R try {
: p7 z( V6 g- Q$ R0 H1 m9 z' l5 s modelActions.createActionTo$message
/ b+ `. ]; ~! E) c. t6 @ (heat, new Selector (heat.getClass (), "stepRule", false));( i( B# _1 X* ^+ o( |9 A
} catch (Exception e) {
, G) E" H/ V4 `+ h System.err.println ("Exception stepRule: " + e.getMessage ());! |; u( e% q! s2 o/ o- N+ }0 ?
}
$ F) P5 j, A7 M4 Y+ F8 i2 f- i
0 {& h. z" m6 C+ `$ R try {
: @8 j) q C% F: K) K& L Heatbug proto = (Heatbug) heatbugList.get (0);
7 k8 l# w' v9 ~/ ^ Selector sel = 2 T" V5 U$ L9 h) m$ p5 S, f; D
new Selector (proto.getClass (), "heatbugStep", false);
+ p+ v+ y( p `" Q* m) I( p actionForEach =
1 Y, `+ \6 y e modelActions.createFActionForEachHomogeneous$call% W! P. H) \7 n5 k" f1 Q9 ~* ]
(heatbugList,2 A& S: r- _% o, U8 u- i
new FCallImpl (this, proto, sel,: W# q8 b# A3 z% U' l3 u
new FArgumentsImpl (this, sel)));# C, I: i; j; K* W9 [4 j
} catch (Exception e) {( k# b) C; q/ s+ B: D' {
e.printStackTrace (System.err);$ C0 W4 g; ~6 M$ _! p5 e( J: _
}
1 \& W! A$ o% H
+ W: q3 X+ a( i5 d0 R syncUpdateOrder ();
8 E% S: E: N7 w* d9 A2 }- L% H9 x; N: }6 F
try {
s/ o/ [' j* }# {& p) i$ D modelActions.createActionTo$message
3 l" o6 n! n" C* H (heat, new Selector (heat.getClass (), "updateLattice", false));, a. f) v/ Q! O' [: a
} catch (Exception e) {
# W* `0 [1 ~3 ^0 F! ] System.err.println("Exception updateLattice: " + e.getMessage ());/ H% N4 m! }8 D7 H2 z t
}
/ ?# m8 y6 D8 V+ h& r. H ' ]# i4 }* L$ A8 A
// Then we create a schedule that executes the- \5 j, H+ k9 {7 a2 H5 w6 _9 J
// modelActions. modelActions is an ActionGroup, by itself it
! Z3 @3 L; ?8 I8 n // has no notion of time. In order to have it executed in
9 W, T5 R! t7 W2 z5 w // time, we create a Schedule that says to use the
6 l! Q, k1 w' l6 m8 K$ { // modelActions ActionGroup at particular times. This7 G% B% Y! z# u' x% F
// schedule has a repeat interval of 1, it will loop every$ `4 n! l4 Q; i* c) U. y
// time step. The action is executed at time 0 relative to. ~* C) \" L! \( c
// the beginning of the loop.4 o% q* K9 a" P1 a* t3 |, m
5 g% P% k5 b, W* J
// This is a simple schedule, with only one action that is
# Q0 p5 g1 y, e7 V* i4 x) p7 q // just repeated every time. See jmousetrap for more" }2 {3 r; t, R7 q! ?
// complicated schedules.
o5 p' {# E5 J* f0 b1 I
* K, _$ ^: e5 i: o6 B3 A; L8 V modelSchedule = new ScheduleImpl (getZone (), 1);# b6 T2 X, ~; U- ?8 m( Z/ ]
modelSchedule.at$createAction (0, modelActions);
2 u$ F1 @* L: D( ^/ P" x
* v9 f8 t: L, A( l return this;
, |3 ], c2 J0 h" c } |