HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% X% @5 q: j8 }8 B+ @6 j1 D$ D7 Y
% x9 O' }# \* z! v' C- y: T public Object buildActions () {( C, o9 s" m) V" N* d' ]7 u' P
super.buildActions();. S' O7 O& P6 P4 q4 H
. a `* g+ n$ K+ Q
// Create the list of simulation actions. We put these in
; H9 f" }, o: H) D4 q: I) ~ N // an action group, because we want these actions to be! I+ ^6 ]) E9 ?8 g
// executed in a specific order, but these steps should
* Z2 a( m. u+ a# w: C1 K // take no (simulated) time. The M(foo) means "The message
+ ?9 y- S% m* r8 }6 r& |+ J/ l% n // called <foo>". You can send a message To a particular
3 E6 A$ H" O, R& b8 q // object, or ForEach object in a collection.
" \, n1 G/ b* Y4 N( \6 M
8 P# r& v( h$ h$ O9 W# z: t' H // Note we update the heatspace in two phases: first run# I( P4 k/ k- n \& q
// diffusion, then run "updateWorld" to actually enact the/ B1 Z4 \0 _3 B1 R1 n: \( H6 I
// changes the heatbugs have made. The ordering here is
9 H. j; ] Z8 O, q$ J2 B // significant!) H0 ]% C1 L% N2 i
6 N* t. ?1 k: p. ]
// Note also, that with the additional
4 M' j' ~# A2 e# B! w1 C. ^2 p+ G // `randomizeHeatbugUpdateOrder' Boolean flag we can
2 `! Q+ C* n5 F% t" B/ i: i // randomize the order in which the bugs actually run5 U0 P: V1 l7 I6 g% }, n; [
// their step rule. This has the effect of removing any
9 O- c, }5 i0 |' i ^0 f // systematic bias in the iteration throught the heatbug
. M* E" l9 H) r9 Z' W) d# F, ^ // list from timestep to timestep9 c4 a. Z$ L4 H$ Q) c
) D, ]4 k6 M7 k% Z7 v4 c7 l/ R // By default, all `createActionForEach' modelActions have/ \2 V! O* [, k6 a
// a default order of `Sequential', which means that the3 o* U8 }; ~7 A" ?6 u1 A! O1 a
// order of iteration through the `heatbugList' will be6 B( m- J1 `+ c1 `! v
// identical (assuming the list order is not changed4 b% q3 V/ {8 B- W+ m
// indirectly by some other process).. {6 F) F* a' _4 J0 ?
; h& ~6 D: u' O" H* B! ?5 r
modelActions = new ActionGroupImpl (getZone ());) M. u1 \/ v# u8 e# ^9 j. n7 Z
/ E) \/ F; t# I1 d3 e try {
' g+ v' U; J& h/ c" s3 k modelActions.createActionTo$message
! i5 h$ A6 M4 i0 X, g. e+ e (heat, new Selector (heat.getClass (), "stepRule", false));
9 E3 w( y1 e t1 b } catch (Exception e) {% ]4 y! F$ O) d7 Z3 a0 y/ M8 T
System.err.println ("Exception stepRule: " + e.getMessage ());2 k' Q/ }$ W' B4 ]4 z; ]* ?
}
9 t2 n0 _$ q& j5 i7 W6 E3 q0 m$ x# n$ k7 K$ k, m4 a( K7 m7 [
try {3 n, ]- h+ f" b
Heatbug proto = (Heatbug) heatbugList.get (0);" k) D+ O8 ] {, ]$ @# U
Selector sel =
6 T8 } E+ e; U: g! Z; o$ | new Selector (proto.getClass (), "heatbugStep", false);( _0 z0 ?! R( i+ Y G' c. k7 u
actionForEach =
3 \) A( P2 C( m0 D5 A modelActions.createFActionForEachHomogeneous$call* d! g' z7 I( M
(heatbugList,
/ H: r7 @4 {3 [ new FCallImpl (this, proto, sel,( H, B# N! {, m1 G! i) l
new FArgumentsImpl (this, sel)));. k9 A/ i. i# I4 |8 A
} catch (Exception e) {' m. j+ v7 ~# j- H+ v
e.printStackTrace (System.err);
6 ]1 N# b+ w) T# b& `8 \ ^ }
4 E8 @8 U$ N* Z) X. @) q* r9 t $ F2 }9 C, B+ [
syncUpdateOrder ();# M5 Q. S7 P* y
) x! E! d8 O% {! U( B2 _0 A try {8 ^& h( L: o$ q+ m& b* K7 q8 M
modelActions.createActionTo$message . v1 u5 [9 O |" ~. r8 w
(heat, new Selector (heat.getClass (), "updateLattice", false));1 ~2 M S' n! Q2 T' ^4 S% J0 k
} catch (Exception e) {
1 ?+ O6 ^. ]5 K5 f, J! n3 L+ e System.err.println("Exception updateLattice: " + e.getMessage ());
: i0 z* r9 W- t/ }3 C( { }6 ^ ]% F2 B5 N0 L
; v, h" y! a# x9 C6 B& Q
// Then we create a schedule that executes the# `* Q' e$ T* o% `9 e0 o
// modelActions. modelActions is an ActionGroup, by itself it
- {) V! N) P! J/ o // has no notion of time. In order to have it executed in
" g. i- k7 t7 O3 e5 C" f // time, we create a Schedule that says to use the
) Z/ k% i2 C, E" Z; t // modelActions ActionGroup at particular times. This; y/ C' J5 i: F( \/ t
// schedule has a repeat interval of 1, it will loop every; S- M: E. u' N5 y4 n; s% O. f
// time step. The action is executed at time 0 relative to& h0 r: |" [; T2 z3 O, W
// the beginning of the loop.
! c7 Z. g" k, K5 ]6 P) \3 |9 b- M9 K# F+ t* {2 f$ l& @! B) k
// This is a simple schedule, with only one action that is
% o9 f0 `9 I C$ W m // just repeated every time. See jmousetrap for more
' [4 v- T; J+ j! s' }# g // complicated schedules." L, p- C s- S t( c0 s
/ d }% ?9 s! l% j) H e8 }0 W: U5 r
modelSchedule = new ScheduleImpl (getZone (), 1);3 H ]. s3 z9 B# C
modelSchedule.at$createAction (0, modelActions);
0 {5 ]& c! _* Y* T$ j" }% h* b
9 L6 d" z6 [) @ return this;) t3 l: u, S+ L4 S0 ]. G* O
} |