HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 ?4 @! R3 A' g8 a5 T6 a
6 X( {/ s" T" H# a- _* h2 b5 o
public Object buildActions () {
( ^+ I. q- b# t/ N4 R super.buildActions();
+ q3 L# T* M: z. x0 h1 |
% f& S3 u1 c% S g8 \0 b // Create the list of simulation actions. We put these in: v2 @+ {" f0 b7 L6 b2 I8 C
// an action group, because we want these actions to be9 ?& o0 K" c0 w: j
// executed in a specific order, but these steps should
! b1 p' D7 ]0 o9 b& B; C$ ]/ a1 A // take no (simulated) time. The M(foo) means "The message
0 |( _. |# ?" t# c7 P // called <foo>". You can send a message To a particular
- K) |" {+ D) n: R4 M0 {9 [ // object, or ForEach object in a collection.
+ m9 L% s1 I9 |$ O) J8 b: v% T/ B $ }% D4 ~ D4 j: @4 y4 b
// Note we update the heatspace in two phases: first run7 S; M9 J$ p/ D* J# o
// diffusion, then run "updateWorld" to actually enact the4 R9 q c1 }! Z5 N1 R
// changes the heatbugs have made. The ordering here is
7 |" X- q- ` F // significant!
6 {" ]$ O8 M3 j4 o2 z& s + _! y) e) r- C
// Note also, that with the additional% |# A* R; L6 |- r$ X8 k
// `randomizeHeatbugUpdateOrder' Boolean flag we can
& J* ~4 o4 g% B- ?$ g1 E // randomize the order in which the bugs actually run* I$ y( r) E4 \3 j% F: d
// their step rule. This has the effect of removing any
' C' b& h4 |6 N1 z // systematic bias in the iteration throught the heatbug5 ^; m8 n5 c* C- `9 H6 O
// list from timestep to timestep F' j# e) y9 e5 g9 a
8 a: G. i5 ]( o7 {& }) a) _ // By default, all `createActionForEach' modelActions have! w9 C9 `0 K) w
// a default order of `Sequential', which means that the- j, m" B! g5 \# G$ X ^! u
// order of iteration through the `heatbugList' will be
; L- Y w' |- s, a* b% |1 a- s // identical (assuming the list order is not changed4 A2 x4 @4 r5 @8 k; S2 A% @/ w; ?
// indirectly by some other process).
4 ]# B# u, ^$ ^! E# |; z
2 z" B1 X5 j& G$ P7 k4 k& U; L) u modelActions = new ActionGroupImpl (getZone ());
- U! t. C0 { I) X1 b2 S- O2 a {# g/ ~6 v) T3 C
try {
0 Y+ d; E, l l" r" | modelActions.createActionTo$message
9 l s) d$ f0 |* t (heat, new Selector (heat.getClass (), "stepRule", false));
+ s7 R# z. z6 n$ ^) Q } catch (Exception e) {
! S! W6 N, b v System.err.println ("Exception stepRule: " + e.getMessage ());
3 o" S% S# ^4 N3 B% y" [ } l1 ^/ S% O R; L D& r, c' O0 x: ^
% U$ D0 Y! {- ^( b9 [- k try {
7 W5 E( O+ b/ n8 ? Heatbug proto = (Heatbug) heatbugList.get (0);6 u: f& l; j2 v1 j& ~+ F
Selector sel = 6 \- C! E# n: O) L/ w$ r
new Selector (proto.getClass (), "heatbugStep", false);
- G+ |' W1 y3 \' t/ _3 Q3 N actionForEach =, K3 A6 B. ^0 A/ o- @
modelActions.createFActionForEachHomogeneous$call
! E3 ~" e' D& M. G/ b3 [: ~ (heatbugList,
. u# w% }* n* s v new FCallImpl (this, proto, sel,
5 \ c) ? L) V: r- {8 I4 I new FArgumentsImpl (this, sel)));
: }6 K/ i! v0 O3 a( G0 H9 @* ` } catch (Exception e) {# S1 d* y% N0 H. r' t
e.printStackTrace (System.err);; y& D# c# r* k/ y& X6 {
}
- ?8 K I. f$ l* G6 g% O- I6 T % F7 F5 A/ d* j! V7 L
syncUpdateOrder ();
- E# H- K i( ~4 g8 q
6 R! ^" O; l3 j i' j try {
: B `+ E9 x- a& Q2 i! Z modelActions.createActionTo$message
6 C2 ^+ g7 S# Z5 d# A (heat, new Selector (heat.getClass (), "updateLattice", false));
: _5 c* K7 C- ^ } catch (Exception e) {
* G! H* I6 C; D- Y; q# u7 o System.err.println("Exception updateLattice: " + e.getMessage ());1 g! w& X& e7 u) z* x' w
}
3 Z3 K% s: h1 q( C; i
- b/ @) b+ e+ m: i' e2 Q, U // Then we create a schedule that executes the
2 I: ^5 [" }; i$ N, t // modelActions. modelActions is an ActionGroup, by itself it
) T& C! Y1 s3 m, f: \# S3 W // has no notion of time. In order to have it executed in
. x% @9 b) V: h* w) d8 M' H // time, we create a Schedule that says to use the
4 m1 d+ }2 Y8 `' e: _$ n5 B+ D // modelActions ActionGroup at particular times. This
' I& b! }" P U5 C3 G" j" I/ |& U // schedule has a repeat interval of 1, it will loop every; F" e3 B2 w7 m
// time step. The action is executed at time 0 relative to! F$ L+ }" O+ [) S$ T# F$ d
// the beginning of the loop.2 _6 M& q/ k3 R+ l- z( X" O
9 q& Q' `9 I/ S4 q, r // This is a simple schedule, with only one action that is, Q) C0 G, d! i* ?& x, S: w
// just repeated every time. See jmousetrap for more
! x2 m( B& [* L // complicated schedules.
0 C6 ^" C7 r0 G
5 b; C# W! V) g modelSchedule = new ScheduleImpl (getZone (), 1);
# f! `) `% O( R7 G( E) R modelSchedule.at$createAction (0, modelActions);( |1 c Z$ J' |/ c" P/ ?5 a( x- n% d
+ J$ Q% r/ J x6 Z$ I' u
return this;9 f1 E+ O+ J1 c+ R- N- H
} |