HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% m6 ^# S* \8 P% M& d8 l6 F: y2 k* H2 K" R
public Object buildActions () {7 M1 _$ |2 P/ X$ y6 ~! J$ b
super.buildActions();5 h: ^: D7 ~7 _6 f! L
8 B& a! m% r: J // Create the list of simulation actions. We put these in
; b$ }3 V- t( _" E* O4 G1 |3 O // an action group, because we want these actions to be! S6 z# q& i+ ^" g1 W
// executed in a specific order, but these steps should
5 U( b% W7 e3 u& H. d // take no (simulated) time. The M(foo) means "The message
* j2 ^$ K6 A: h/ d+ [: e5 N // called <foo>". You can send a message To a particular
7 C! X" |& a9 o! p+ v" n% S // object, or ForEach object in a collection.
8 [+ \9 g+ s7 m/ v( d 7 r& j" Y) f, X. M" W' ~
// Note we update the heatspace in two phases: first run- `- N% O/ Q, z8 J2 p/ V
// diffusion, then run "updateWorld" to actually enact the
* j2 h7 S9 ` q! P7 a0 U. ^% p* A // changes the heatbugs have made. The ordering here is, O u- T) ?* c, I9 j
// significant!
! `5 e6 a" n. n2 i+ a( }
! ]4 k, T D, S8 e, J: p2 B: q // Note also, that with the additional
9 ?8 G$ M3 W. H; A+ z( _2 P // `randomizeHeatbugUpdateOrder' Boolean flag we can
( x) e6 P( ^: Y5 |3 m5 O* Z // randomize the order in which the bugs actually run1 o# ^ A6 \( Q9 n9 h0 P' P
// their step rule. This has the effect of removing any2 r) w, e! L/ k( {) ]2 b
// systematic bias in the iteration throught the heatbug
5 ?" t+ W# ]2 k: _7 H2 C$ l // list from timestep to timestep
) s% m2 W; u' B$ S; o 0 M2 i$ ^) ~9 v6 Z: J
// By default, all `createActionForEach' modelActions have; z" u, _. V" j# t9 |
// a default order of `Sequential', which means that the% m( }) X p+ u4 O
// order of iteration through the `heatbugList' will be
% X2 k$ o! I+ J v // identical (assuming the list order is not changed$ @( @1 n9 N; ~7 ~% F" o
// indirectly by some other process).
! L) L5 }9 K% i9 {0 i! {6 g
, ~. C% `$ U8 T, x) L1 d# F modelActions = new ActionGroupImpl (getZone ());+ ]3 T% z: I# ^! H% c p# g2 ?1 C
# c$ O8 H# U$ W; d- ?$ D
try {
: {' @' b9 Q. l5 }2 l modelActions.createActionTo$message
9 A$ [2 {& P% a/ B J (heat, new Selector (heat.getClass (), "stepRule", false));
+ b/ i7 z5 ~4 \ } catch (Exception e) {( j2 b6 N2 p, z$ h; A
System.err.println ("Exception stepRule: " + e.getMessage ());
. z8 P3 ?6 l2 {! c! H }) H2 }" O& _1 R
6 z0 | J5 t1 u2 z! ~ try {: V: m5 u0 B, L% b3 L' p O4 D( Z
Heatbug proto = (Heatbug) heatbugList.get (0);0 E8 I8 r# H4 c( f
Selector sel = z C9 u Z" z$ ~0 G" x. v
new Selector (proto.getClass (), "heatbugStep", false);3 V' K% i, p( e# R+ A6 c
actionForEach =8 S/ c1 [! J: V
modelActions.createFActionForEachHomogeneous$call
t( j& W3 r1 n2 Z (heatbugList,
* p2 X/ H$ Z8 y8 z+ d5 ^, ~ new FCallImpl (this, proto, sel,
8 w7 V0 e9 f# @ new FArgumentsImpl (this, sel)));! R& k4 ^4 L3 P0 F# J
} catch (Exception e) {9 Q: Q' ?7 d3 ~" k5 m
e.printStackTrace (System.err);
5 j0 ]' _- Y) p- K( e: Y7 ]* { }
( f# v5 m! f# B
% w& s" W& O" d$ z syncUpdateOrder ();0 O5 q0 q( M9 K, G( p. f. c7 t# k9 |0 p
7 w( ~% t. [( B% s/ m, K try {' ?+ d" e" F& W( ]4 a# m5 L
modelActions.createActionTo$message " H- E3 F+ p! y& o0 g' x# q
(heat, new Selector (heat.getClass (), "updateLattice", false));
4 c5 }" d' B9 H2 A! p; l } catch (Exception e) {
* b; H5 y' A" K6 P3 U' @: x2 V System.err.println("Exception updateLattice: " + e.getMessage ());% J& u' s1 Q, G
}
8 \0 }* p# S& k: J' C: { h" D( I7 s/ S* E
' f! F( m* J$ U; A1 M! ? // Then we create a schedule that executes the
, [0 x) r9 E+ P: G9 {/ ~, U // modelActions. modelActions is an ActionGroup, by itself it' [: u' C, w8 N1 e2 N' E% x
// has no notion of time. In order to have it executed in
9 O+ m% x# q2 _' M6 u+ x // time, we create a Schedule that says to use the
; R, I0 |; `9 ]' S! D // modelActions ActionGroup at particular times. This: b" v2 h+ h% [% x, }3 F
// schedule has a repeat interval of 1, it will loop every+ z0 j5 i4 s, D1 j4 m* }5 ~, P
// time step. The action is executed at time 0 relative to
3 t7 u/ s* ?3 s2 W // the beginning of the loop.+ Y a- ]2 _: @6 z& J$ u
- Y) S( z5 H5 ~7 q r- P' z" A // This is a simple schedule, with only one action that is9 N+ s* ^4 b6 c6 ?9 U
// just repeated every time. See jmousetrap for more; l5 N, h" C" D, P
// complicated schedules.. \$ ]; }# b" h
/ l) v' T' n) I; q6 k+ l3 m modelSchedule = new ScheduleImpl (getZone (), 1);
, J5 @& T* I2 v& E6 B modelSchedule.at$createAction (0, modelActions);
6 [. T' b8 Q0 t3 r9 X7 J
- G) h% U8 T, b- W" k return this;0 b* d. M+ d E) O- H2 K
} |