HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:/ s, r" |1 G* d
) M2 [3 H% ^7 |6 w: }5 i8 i* ~
public Object buildActions () {
' b$ t% O0 e: o# V super.buildActions();
* t9 ]3 v8 z, ]
; D2 m0 b' A! ?9 W9 Y/ I1 m // Create the list of simulation actions. We put these in7 y6 l: ?/ G8 E0 P4 G/ M* i6 k4 W
// an action group, because we want these actions to be6 ^6 Z) c& X# @6 |7 u
// executed in a specific order, but these steps should3 a( z( j9 g: A' M
// take no (simulated) time. The M(foo) means "The message6 `. n1 u) E; r$ m7 U$ c/ n/ K1 X2 G
// called <foo>". You can send a message To a particular% }9 i' p7 |. \( r2 r
// object, or ForEach object in a collection.
7 h; H' G+ A+ w8 ^ ) e- D& Y8 o K# Y1 B0 P
// Note we update the heatspace in two phases: first run
! p. A' Y: i$ f) R // diffusion, then run "updateWorld" to actually enact the# N! t( a4 C! |( G. Q' E
// changes the heatbugs have made. The ordering here is
9 J G$ p0 e7 |' {2 E // significant!& D0 V# L5 Q, F& L1 ?
9 v8 g G. j4 O" n6 ]" v/ [
// Note also, that with the additional$ s) d5 {4 A5 R0 Y F
// `randomizeHeatbugUpdateOrder' Boolean flag we can; s: n6 G, B4 [& @. v
// randomize the order in which the bugs actually run; Y1 M! I. ?9 [. k6 y9 H4 _
// their step rule. This has the effect of removing any
5 P7 F; [2 F7 B3 f4 y- z9 _2 @) E // systematic bias in the iteration throught the heatbug
: E1 N% p! b/ W# B; r9 a( n // list from timestep to timestep
' ]5 i; U6 |5 O3 C* C 0 o2 F4 N# G& x3 p- B
// By default, all `createActionForEach' modelActions have8 ?+ L2 z$ ?# T8 u% g) c) ?0 X- [
// a default order of `Sequential', which means that the
% x! g7 g2 |1 S& l+ Q# f // order of iteration through the `heatbugList' will be
; @( f' T4 |5 ? // identical (assuming the list order is not changed
^+ l1 O1 D! P) b+ O: X // indirectly by some other process).
" p8 q b( v; v6 X+ {8 s% \" L7 { 4 G8 ?1 q9 g8 Z9 ?* F5 Q$ R
modelActions = new ActionGroupImpl (getZone ());
( e# T( w& g2 E1 K2 E4 }! Z2 D
8 G8 @& C3 j7 y9 q try {
) F# Q# y& O) j+ A+ D) U5 a% J: E0 D modelActions.createActionTo$message
( F, W9 x# ]) g8 S7 W% p (heat, new Selector (heat.getClass (), "stepRule", false));
# k+ a0 ^; \ H5 R+ ^& O } catch (Exception e) {, j C; X' A7 z1 h7 [, x0 _
System.err.println ("Exception stepRule: " + e.getMessage ());
6 V) b7 w. M+ o }% y2 J/ N# |' B3 L& l! F; P
# d$ W2 k+ F# O8 ~ ~* V& u4 J4 l
try {
; D6 x3 A7 {. @! s Heatbug proto = (Heatbug) heatbugList.get (0);) i2 p9 E+ D7 j( j7 s
Selector sel = : v+ Q* P# M, o: o
new Selector (proto.getClass (), "heatbugStep", false);
" M9 q9 m" P8 v. ] actionForEach =
1 q, g c2 F8 S# h. b" A modelActions.createFActionForEachHomogeneous$call
: X+ R: Q# @2 y# J (heatbugList,4 N/ O, r% Y. r1 C6 O
new FCallImpl (this, proto, sel,3 S. `7 o" E5 h) W5 Z3 b6 f1 X
new FArgumentsImpl (this, sel)));
' q( V+ V6 Z9 ? } catch (Exception e) {
, S* ?7 m4 e$ K7 e e.printStackTrace (System.err);$ U8 _7 e: q& q7 L+ G; V0 O9 P
}3 c6 |1 b! S+ [$ M. a" ^
0 e+ Q0 B( S$ z4 i2 L) d syncUpdateOrder ();
* J: j8 k& k# _
( c+ ~3 S. a0 A! p. A: o ~ try {
/ r1 n' }5 R+ V7 v7 i8 N modelActions.createActionTo$message
5 Z; ]$ [. u# F1 N (heat, new Selector (heat.getClass (), "updateLattice", false));
1 n! n! q0 \1 E3 q9 ?3 h* c/ l } catch (Exception e) {) M/ j9 ?; ^7 {
System.err.println("Exception updateLattice: " + e.getMessage ());, q) V) {9 d: \6 G1 z/ M+ [6 ?% g
}$ R" C Z- Y8 L
" N2 t. G7 N: Q9 T: P // Then we create a schedule that executes the% U4 }& j, B8 n+ ]0 x
// modelActions. modelActions is an ActionGroup, by itself it
8 \2 L. n U- G* F5 M! n // has no notion of time. In order to have it executed in1 T7 V7 Q; I: j( h6 ]! w" c: L4 l7 u
// time, we create a Schedule that says to use the
" U! Y" c$ ^9 G! v# M& Z4 d // modelActions ActionGroup at particular times. This3 C6 o/ W# b r& L4 q4 M
// schedule has a repeat interval of 1, it will loop every: x3 w. _7 O& @- a
// time step. The action is executed at time 0 relative to. k" U4 k l& ~; {& ~. ?/ K
// the beginning of the loop.
4 {- u( ~; f2 m% m& I2 a. e2 \ G
// This is a simple schedule, with only one action that is' r! ?: _. V$ X9 I' m
// just repeated every time. See jmousetrap for more
0 h \! F* W) n. D+ q* j% p4 H0 a // complicated schedules.
1 `' o) b! e0 {, M; a2 b; y' w . A/ [1 Q2 O7 @6 G5 M+ E* }
modelSchedule = new ScheduleImpl (getZone (), 1);
7 q. y1 {: K) O* p+ M modelSchedule.at$createAction (0, modelActions);7 N8 D6 w6 \: g/ R
. G9 u8 w' `$ u6 T, } return this;
3 k- V3 R' P7 f# A3 Y' @# p, g+ U6 E } |