HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:6 w% T8 F, J" s- Z* d, F; A2 M& U8 x4 `
1 m' {- y$ r1 b+ ]: p# B public Object buildActions () {2 m! X( C L5 G5 T4 H3 C
super.buildActions(); I5 D, r# k2 Y. @/ \1 z9 h1 v) n, m
; U* O6 ? k. w4 v- x
// Create the list of simulation actions. We put these in9 _, {! |, k1 I( B/ f
// an action group, because we want these actions to be h' N$ P k# ?2 j+ i; U
// executed in a specific order, but these steps should- B6 j) p& M6 W$ u& b7 c1 w
// take no (simulated) time. The M(foo) means "The message
* R0 [7 U& T3 D! X" ]) g, K0 ~+ R, P4 [ // called <foo>". You can send a message To a particular6 z# k' \) B" L" i8 M6 e" Y
// object, or ForEach object in a collection. j& F; D3 e- {) E
, {. c3 q2 ]/ o2 e // Note we update the heatspace in two phases: first run4 t% ^; A; b& F! b2 \
// diffusion, then run "updateWorld" to actually enact the1 F# X, a$ @# P" ]" o' [
// changes the heatbugs have made. The ordering here is% U. j, D V8 R# B+ E8 L/ @9 E# ^) n
// significant!* G4 b. G& L! m, n2 D
5 i) D# C" o& V' a% N0 }( m x
// Note also, that with the additional+ w9 J7 O; L% [4 g+ b! u% ^
// `randomizeHeatbugUpdateOrder' Boolean flag we can
. \" ]5 E4 g# r: r3 b$ I) B // randomize the order in which the bugs actually run
! N( ?8 y- F% X, a: P1 H/ K // their step rule. This has the effect of removing any5 U3 Z+ G1 n! v# \3 v1 R6 N
// systematic bias in the iteration throught the heatbug
5 ]) l7 y5 Z3 i# Y4 C // list from timestep to timestep
' z. r Z7 u8 s+ U; N5 [
# s9 h7 R0 |/ q' O% I: `' X. b% g* s! B // By default, all `createActionForEach' modelActions have9 e$ w" |- [4 u- N; w& c5 b
// a default order of `Sequential', which means that the
, {1 q& s- ^6 r. f // order of iteration through the `heatbugList' will be6 {5 A) z- G* J* o4 D
// identical (assuming the list order is not changed9 K" x8 g& s" m3 ^ L" S7 ^
// indirectly by some other process)., Z6 U% ~4 N/ e' Y5 J; _
8 c/ D/ T! ?$ c9 U- `. p) n* E modelActions = new ActionGroupImpl (getZone ());& q$ w, u$ i$ Y" ]0 j5 n
, Q2 S2 h7 T' R2 A8 q try {
+ e% M5 }$ f' s+ v# W' v L( @ modelActions.createActionTo$message4 K: s8 y" l8 }$ v% Z! d: Y3 P& I
(heat, new Selector (heat.getClass (), "stepRule", false));$ u3 K/ m, e L6 P; l5 ~- X3 r( k
} catch (Exception e) {
6 Z+ F. ^4 i, H/ `% g System.err.println ("Exception stepRule: " + e.getMessage ());
/ _" Q1 M- N$ Z8 C1 C# w }
' X2 Z( _3 j K5 p" N2 [
" X4 c! ]& m: e+ g1 b try {
: ~7 S4 Q' m1 a1 o Heatbug proto = (Heatbug) heatbugList.get (0);
4 H @2 b3 Y5 y& a i7 m Selector sel =
6 g* L' b* |6 t; q' L new Selector (proto.getClass (), "heatbugStep", false);7 v% I% F7 w; q0 R" Y8 r( [, R
actionForEach =5 v6 w% U) M1 R, F
modelActions.createFActionForEachHomogeneous$call# e4 W: s5 }( i$ S, g
(heatbugList,' s" B. K4 M; m+ F
new FCallImpl (this, proto, sel,
5 X# d* F# b& g) z$ b/ y new FArgumentsImpl (this, sel)));
' G* j0 [: ^# q: P, H/ _ } catch (Exception e) {
9 T1 j1 E9 H j' l. m7 x e.printStackTrace (System.err);, T# S1 ~4 ]! i* G" }! e0 n& H
}" z% Q' K( ]# ~9 T3 _
' D1 n+ Y: g) d* Z2 L- A: V% F0 ~, E0 g
syncUpdateOrder ();
. M9 V8 W; r% S( _% @% \- s7 ?2 b/ K) M; }: N( ]
try {
/ f. ~- L, _& J modelActions.createActionTo$message 2 E' N+ ?7 ?( ?' w! x
(heat, new Selector (heat.getClass (), "updateLattice", false));
9 e0 V/ b% I3 g1 Q. g- P/ B1 r" U } catch (Exception e) {
9 [' l' B; z2 t; C, e# o& z System.err.println("Exception updateLattice: " + e.getMessage ());
7 S4 L3 P# f) A; G; Q! K- `6 J }/ k! p, C" Y$ c# B( T8 l4 K1 {
2 Q- @5 x5 N9 j( N
// Then we create a schedule that executes the- z" S& t, i; C& J, J
// modelActions. modelActions is an ActionGroup, by itself it- B: ^8 g, L( q$ C
// has no notion of time. In order to have it executed in
5 F% v) x# y- q // time, we create a Schedule that says to use the
3 k8 Y/ h N8 \ // modelActions ActionGroup at particular times. This$ r5 b# l h" H& n; Y8 @1 E
// schedule has a repeat interval of 1, it will loop every
/ G8 R/ Y2 ]$ i // time step. The action is executed at time 0 relative to$ P( o' W0 L' f6 o; U& u
// the beginning of the loop.0 V* H% E9 S1 ]8 t6 D e* w- L& n
9 i. g# R! U4 }6 g( R* F& y
// This is a simple schedule, with only one action that is: W" T7 V3 o3 S, ?$ F! s+ e) C# |( k
// just repeated every time. See jmousetrap for more; L0 ]) V+ n+ U9 f
// complicated schedules.
+ a4 x) l# u+ H! u4 o( i) ]& v( J 2 ^. m/ P0 m1 C& Z
modelSchedule = new ScheduleImpl (getZone (), 1);
1 Q. P1 W, \7 A- {: k* G# X modelSchedule.at$createAction (0, modelActions);
5 S- m- P3 j3 I
8 }6 c7 P0 {- U2 ]0 ^# D$ | return this;5 N# o' Q$ i2 W- z; N: t
} |