HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:" X9 a) h4 z7 \ ~5 [) G0 _
# G! e8 ^1 X9 G& n1 S$ y6 b" n
public Object buildActions () {
# u' |" e- t5 C3 p0 l! }% G6 t super.buildActions();
# ^3 q' x, B/ {& A1 }6 ?; H8 ?
7 w! U6 p9 Q4 I+ I6 C9 j' R2 C // Create the list of simulation actions. We put these in
0 B6 Q% b& y3 q |8 \; I8 o Z // an action group, because we want these actions to be, X2 T$ d, e% U- R) ~
// executed in a specific order, but these steps should
+ Y/ ]) Y# F' ?% g( s // take no (simulated) time. The M(foo) means "The message
$ R0 ~) j z; m! V8 p6 v! C+ v3 { // called <foo>". You can send a message To a particular
. ?- T9 \- H3 g // object, or ForEach object in a collection.
' @( b4 x8 f+ J0 u- i 4 B/ d! v0 i/ t" T" {
// Note we update the heatspace in two phases: first run- L7 n% j5 @# q+ Z) J) ^
// diffusion, then run "updateWorld" to actually enact the1 L* g: C; o$ }+ |- u* O7 T
// changes the heatbugs have made. The ordering here is
+ o6 x5 C$ N, |8 @* q( g6 H. W7 q // significant!
5 [1 t5 M+ `( Y7 j- x f ^/ O0 P1 g7 j4 s* L$ e! R7 }
// Note also, that with the additional
" _0 p# {$ G9 r F- t6 _: F/ |: @ // `randomizeHeatbugUpdateOrder' Boolean flag we can
, l2 Q# M1 ?4 N7 N // randomize the order in which the bugs actually run; n& b( e8 Y/ u
// their step rule. This has the effect of removing any' g! y9 r5 k# E( U4 b) _. _2 C+ p
// systematic bias in the iteration throught the heatbug
" \) g: y7 A5 q+ D // list from timestep to timestep6 Z9 A' |8 i: ]% I1 }+ U
" I: `" U. C0 M4 @$ u // By default, all `createActionForEach' modelActions have( W8 ?7 h L2 Q i4 m
// a default order of `Sequential', which means that the
9 ?8 H) Y+ P# x. L3 u" ]" n3 { // order of iteration through the `heatbugList' will be1 n! \' V! E9 H8 R
// identical (assuming the list order is not changed3 M+ u% w6 k. q
// indirectly by some other process).
5 B7 @! d- O$ x0 H. o" V- i- r
3 {6 I3 Q t$ T modelActions = new ActionGroupImpl (getZone ());
' y% f) y/ ~: r3 i. t3 p5 V( t1 j! @ v6 r+ ], |! e5 q1 ]3 A
try {8 Z* }8 d2 i$ d4 q$ R
modelActions.createActionTo$message
$ B) j& h! `) a9 _ (heat, new Selector (heat.getClass (), "stepRule", false));
# C* J7 Y& K% n, }) K } catch (Exception e) {: D% @) b+ c* \# J+ _# J' _
System.err.println ("Exception stepRule: " + e.getMessage ());
' H* V2 J2 U* r. ?" i }. {# ?3 A2 p2 s3 O5 q
0 P; {, s7 c* k( ~! n0 k try {; e6 c# L' x6 t
Heatbug proto = (Heatbug) heatbugList.get (0);3 @5 ~9 R1 w8 q4 ]
Selector sel =
4 I5 M' L2 `$ h8 c new Selector (proto.getClass (), "heatbugStep", false);& X. \) b- n* J7 o2 N; W( I' L0 M
actionForEach =% ~* O+ ~, ]# I/ C8 U% v5 \2 J
modelActions.createFActionForEachHomogeneous$call: T& B# z x; Q( V( `
(heatbugList,
5 h8 F) _. Y2 n; F0 o P new FCallImpl (this, proto, sel,8 z/ G0 c- W* C
new FArgumentsImpl (this, sel)));
# E9 y4 ^6 v& H" T; H2 E } catch (Exception e) {
; L% P/ r+ ]; q2 d# P e.printStackTrace (System.err);% R( B) o; @: q) T8 n6 u* }' g
}
- X. V' C/ r+ p9 i, D 5 X. m5 s7 s) H4 r4 X- ]! e
syncUpdateOrder ();
: t6 b7 f' K/ B3 ~1 b/ d/ S: t* k4 D; B8 i
try {0 o, X% l6 h& `0 t$ z; Y
modelActions.createActionTo$message
3 g) @- _' x7 U3 {! N (heat, new Selector (heat.getClass (), "updateLattice", false));" v! p. ?2 a) h+ X+ I8 C7 B6 f
} catch (Exception e) {& ?- [$ t- ^0 s' U4 }$ k' H. V0 V
System.err.println("Exception updateLattice: " + e.getMessage ());
3 T' i* M+ m& W: d4 m' a }
, O' l1 r: u. o; n/ M4 m# C
1 H E% c/ Y _2 `6 b9 z9 f // Then we create a schedule that executes the
- F2 c/ ]" V5 k9 V9 t3 q // modelActions. modelActions is an ActionGroup, by itself it
/ R9 p' r+ k" J7 e% q' o // has no notion of time. In order to have it executed in0 X6 s7 G' R" o$ ?+ b* ]( L
// time, we create a Schedule that says to use the
; M$ x7 @% m) S C // modelActions ActionGroup at particular times. This1 h H$ b L( N. z) Z$ ^4 ^0 |
// schedule has a repeat interval of 1, it will loop every
+ F4 k+ G0 S; a1 z2 Z // time step. The action is executed at time 0 relative to
6 g7 N" M2 v2 b8 [' b, | // the beginning of the loop.
5 c" b2 b% L7 ~
. ]5 }2 ^* N" f/ D // This is a simple schedule, with only one action that is% M/ T& y( A" |% W
// just repeated every time. See jmousetrap for more
# F( p0 |/ D: |$ f9 i6 A. v // complicated schedules.
" f) E, ]5 F- ?4 C1 Q7 N3 p1 q : |9 M- ]* d" V. i8 [
modelSchedule = new ScheduleImpl (getZone (), 1);7 ]% w$ x$ \9 c. l( t/ f
modelSchedule.at$createAction (0, modelActions);
; T" o& L* f1 T% Y" s 8 q( q- }9 x1 h: Z
return this;7 i4 \& h9 D# C6 K3 C- c5 l7 w
} |