HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
4 q: s: S" g, b3 `
9 O: m* N* V9 h2 O+ \8 H+ } public Object buildActions () {
* H+ D/ b& X- l# o Z4 t super.buildActions();; k1 j9 ~6 N% B' L
+ Z; |' w/ W# z6 o+ I6 S6 G, J
// Create the list of simulation actions. We put these in
( a9 Q7 n1 P$ i' @6 H5 L // an action group, because we want these actions to be: M$ m4 Y6 l" }4 v9 d. T! X# J
// executed in a specific order, but these steps should
6 ~5 Q0 R/ z: C' L% }, U g+ N: l // take no (simulated) time. The M(foo) means "The message
s# U1 e* p. _6 {" E // called <foo>". You can send a message To a particular1 a2 Z9 y* O R' Z. k
// object, or ForEach object in a collection.
8 Y( w6 g- ^* l, o6 R5 l+ h
/ {, O; ^( ?6 y6 f) s. d: |! {& ^ // Note we update the heatspace in two phases: first run2 m" r( |6 \# w& f7 b! z* H, O, R- H. ]
// diffusion, then run "updateWorld" to actually enact the! y" q7 p. ?) ]) D/ _& n7 } Z: o
// changes the heatbugs have made. The ordering here is. a) L' ~1 y. v# D- k
// significant!
6 N9 _( ~1 A. {8 E0 x2 S
6 Y4 V' g4 F# X2 x- p* v9 v // Note also, that with the additional' C6 T; E9 \/ a% A% q
// `randomizeHeatbugUpdateOrder' Boolean flag we can
T( j2 u2 H5 K // randomize the order in which the bugs actually run
, z4 X8 l) |9 |9 m- j // their step rule. This has the effect of removing any
% j, b$ }) Q* R& \+ Q // systematic bias in the iteration throught the heatbug }/ w& Y& @7 \2 ]5 Y
// list from timestep to timestep
0 G4 E) A" P% \1 D {7 p9 Y) W+ H
7 ^' a# Y7 s$ } // By default, all `createActionForEach' modelActions have H& w# y1 H" ?* u$ K. W
// a default order of `Sequential', which means that the- M& S& H9 p# G* f
// order of iteration through the `heatbugList' will be6 h$ S& w2 ^5 n/ P# u
// identical (assuming the list order is not changed
7 @, i+ m( |- r& | // indirectly by some other process).; V( F2 ]& o0 }/ h
' a) M9 I1 X9 s0 V# S* a) _" q
modelActions = new ActionGroupImpl (getZone ());
) T0 v2 c3 n" ?5 X0 l4 W: b+ @5 [8 g# B6 o u; M
try {1 a1 X# w. d9 O6 C5 M
modelActions.createActionTo$message
# h5 E! R0 S n: X, D* _/ ? N! |- V (heat, new Selector (heat.getClass (), "stepRule", false));- h; F" z4 \8 i
} catch (Exception e) {* a" f: K" B2 T/ P
System.err.println ("Exception stepRule: " + e.getMessage ());
8 A. ?" a% f6 ?) H( _9 `1 \ }
$ r4 W' e) g! P) v6 P/ U8 I0 w% X( @
try {
+ p4 L# d) b N, t c; W8 L% C Heatbug proto = (Heatbug) heatbugList.get (0);1 F4 V" r( g ]$ M/ w2 S) q( v
Selector sel =
# c; y/ _* z7 {; H O" H new Selector (proto.getClass (), "heatbugStep", false);" l/ j1 F' o0 I
actionForEach =
" i$ M. U; X0 Q5 n modelActions.createFActionForEachHomogeneous$call
o) T& g' E( X5 u% o7 d (heatbugList,
! e* G6 u/ v; J+ Q" }! x1 |' m new FCallImpl (this, proto, sel,
Z: u9 m U; M( D2 b new FArgumentsImpl (this, sel)));0 [& l: ]! `$ [) p0 i9 y0 }4 s9 |
} catch (Exception e) {
! N5 j$ P, C" u; K2 i: h" e0 @9 Q e.printStackTrace (System.err);; {* W7 }. l: g P
}/ \9 p9 f- G" {$ L3 H0 x! p
8 E& k8 l/ g( T syncUpdateOrder ();
8 S" i; d# C' B! C$ o( G
5 A3 `& K1 d& }- Z- e6 ~2 t0 t5 Y( h try {
! P; ~3 x- ?) `/ J- ~/ K modelActions.createActionTo$message
$ L1 b- v" ]# w6 H( M, t: ~ (heat, new Selector (heat.getClass (), "updateLattice", false));2 [" G9 P# \" l$ R
} catch (Exception e) {( R% T2 R4 A9 T9 e& i- |6 f
System.err.println("Exception updateLattice: " + e.getMessage ());
" H/ `$ c7 I& y* n }) O4 A# ]' _0 z: j% f5 p/ V; r
' M( S9 F5 y! u, }1 o5 ~ // Then we create a schedule that executes the
- i" b/ |* U; ]" g+ J, b // modelActions. modelActions is an ActionGroup, by itself it; z. R4 d, C+ y7 a( j
// has no notion of time. In order to have it executed in& ]) y+ _' p- {3 l$ Y# q
// time, we create a Schedule that says to use the
+ \) l4 X5 Q: n // modelActions ActionGroup at particular times. This! F ]' ]& Z; L" \% [; h* V& n
// schedule has a repeat interval of 1, it will loop every V# `7 @9 V4 b B
// time step. The action is executed at time 0 relative to! H" m3 P3 c: R' t- |
// the beginning of the loop.1 j* b# O* R' Y' j+ W$ u+ n, Q
& S# d% g% s3 k0 Z
// This is a simple schedule, with only one action that is( |- I$ z, Z* ^& \: M0 c1 L
// just repeated every time. See jmousetrap for more0 x( ?$ M- M) A% M
// complicated schedules.
( Q% Y* l# l. W# E& J4 Z8 V1 h( r
7 T$ ^' I9 w1 Q' v6 H @+ x1 V modelSchedule = new ScheduleImpl (getZone (), 1);
' ?, |! f6 `- t' h- K modelSchedule.at$createAction (0, modelActions);+ Y/ \/ T" R* Z& r8 P0 R% `
; ^+ [, ]. K+ Z2 F0 z0 c0 }9 T
return this;; e1 _8 P2 f9 ]' x L
} |