HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 U( O! G1 U- \6 f6 t4 {& n
* J* N L6 h# A# \( V
public Object buildActions () {
8 k' }6 \' |) |$ a, b5 a/ x# J# y super.buildActions();
/ n2 H* y/ O: m0 y: t ( P q8 ^5 F' x1 S) K, U) j
// Create the list of simulation actions. We put these in- R; S+ O' z1 F& _" B3 @
// an action group, because we want these actions to be
f1 I0 D, W# D* [. m // executed in a specific order, but these steps should" {* O) O& s$ ]
// take no (simulated) time. The M(foo) means "The message$ y }/ |+ p1 t9 N
// called <foo>". You can send a message To a particular5 Y* p* L( G( a: M- p& p- _
// object, or ForEach object in a collection.
7 ^' P6 V9 [) C1 Y( m8 p" d- q" d7 m8 ` * F% B% D: i! H& Q# v
// Note we update the heatspace in two phases: first run+ ?3 p2 t5 A6 f
// diffusion, then run "updateWorld" to actually enact the, b z1 W" o' Q7 G& [3 ^/ B) d: ?( ?+ h
// changes the heatbugs have made. The ordering here is% K/ {0 R4 ], l
// significant!2 @# R5 l- W8 u& _ G# E
$ T6 F, M* n$ c3 U+ ]/ u: a
// Note also, that with the additional; s( d5 L% ^* E) f3 }2 Q
// `randomizeHeatbugUpdateOrder' Boolean flag we can
4 w, X: ~/ Y/ N- x% ^5 P) Y3 V. a // randomize the order in which the bugs actually run
6 ?; Y% B& I a3 a: |; E0 |* P# B/ B" [ // their step rule. This has the effect of removing any
7 X) _. P4 ]; s* }5 |: V- a0 e& [ // systematic bias in the iteration throught the heatbug+ ]! x6 u" T0 f
// list from timestep to timestep
4 _/ f/ w: ?3 X# I6 i/ [
" Q, p+ |3 P* W0 z/ i+ i% z5 } // By default, all `createActionForEach' modelActions have
. m2 v0 Z; Y6 @' i% v1 [9 ` // a default order of `Sequential', which means that the
' Y4 I/ u# l1 M# F // order of iteration through the `heatbugList' will be0 _, ^% u5 V# q$ Q; Z/ c! [
// identical (assuming the list order is not changed& c' J: [. o0 \ j- Q4 y: A
// indirectly by some other process).0 u2 @. v, M$ I2 c4 B5 m1 y! V
" c9 u) \" D$ s! \& }4 p
modelActions = new ActionGroupImpl (getZone ());+ _- A( s' Q( u+ F" @8 J
! w6 p# {5 p3 z0 p try {
5 _% s- L4 G6 G4 M! ` modelActions.createActionTo$message
" `; _6 m0 j2 {7 S (heat, new Selector (heat.getClass (), "stepRule", false));- o- u) n* o& y
} catch (Exception e) {
& T! U4 H0 L8 D System.err.println ("Exception stepRule: " + e.getMessage ());; w3 u2 V$ G1 M/ k0 D' d# {
}
( F4 r( F& _; k$ h0 z7 F" @+ @& U
' i+ b2 R( v, y try {' y8 y! B& L! V2 e
Heatbug proto = (Heatbug) heatbugList.get (0);
" p4 H. I+ n7 l7 g Selector sel =
) E' `. M1 J3 o0 P, n+ C new Selector (proto.getClass (), "heatbugStep", false);) \" o0 Y! ~% b1 n! i6 r8 m7 S5 [7 y
actionForEach =
$ B* s' }0 e$ m O modelActions.createFActionForEachHomogeneous$call, v$ k# L$ h% C/ H: X
(heatbugList,0 z& S: f9 i9 S* W( O
new FCallImpl (this, proto, sel,1 W" m5 O7 J; ~. m! \9 C
new FArgumentsImpl (this, sel)));/ d7 o+ t5 n: }
} catch (Exception e) {0 ?9 g4 C7 z$ a1 ^, A' C: c9 P8 |
e.printStackTrace (System.err);
- w, W+ J2 _4 H# h+ L! I+ m( g }
1 X3 O4 x3 i( q+ ?. v4 m- \' f% { c
& v# z* O: g" p) l& B1 ]! d" s syncUpdateOrder ();
) Y) r) d5 ?3 R6 J9 E' q2 z7 U5 H* c0 ^' P
try {0 x) S" N9 }' e! ~, b" a
modelActions.createActionTo$message 9 v( n @( B+ K! v% i
(heat, new Selector (heat.getClass (), "updateLattice", false));/ i5 M. z% f( Z" ~- g( x8 c
} catch (Exception e) {+ l) C# i( P3 i0 O, T
System.err.println("Exception updateLattice: " + e.getMessage ());& V# J$ J# W8 q0 n+ H
}* n" ~6 F7 L1 f" ~# z- H! s
: f+ o# E' M: `, {7 `' I S$ u& ^# F
// Then we create a schedule that executes the
3 P6 B' F8 ?* ^0 l. S* \; I // modelActions. modelActions is an ActionGroup, by itself it2 [( [* f6 d* `) y
// has no notion of time. In order to have it executed in
6 L5 q8 @9 ^/ k' C5 D- T. c // time, we create a Schedule that says to use the/ j$ V8 J) m9 ?: v1 U6 p
// modelActions ActionGroup at particular times. This
* s& g/ F% M+ i$ ^ // schedule has a repeat interval of 1, it will loop every/ o. h( d9 j) z4 r/ |$ e
// time step. The action is executed at time 0 relative to
$ M* }& {1 d. L% |, Z* f. F) f // the beginning of the loop.
% T/ t9 i* | W7 d5 G7 {( {
9 r: I/ N+ U& u' ]) `/ E* l // This is a simple schedule, with only one action that is0 m* ?' u- t$ |, W# d; z
// just repeated every time. See jmousetrap for more
" S4 t, l( k6 ` O+ w3 O // complicated schedules.
: f7 _4 y: n6 L: X/ s
5 W, L! M& W$ |6 j3 ]2 ?5 B# ]; n modelSchedule = new ScheduleImpl (getZone (), 1);# V! d9 d+ K- k; s' I
modelSchedule.at$createAction (0, modelActions);
- i6 [6 |! h- C2 s( i! ~ + w+ A; d8 d. ^6 S: f9 z8 w7 p
return this;6 g) ~) x/ R% m0 B% e* o7 J
} |