HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
2 }# }, A, a$ q6 Y7 i; o1 X$ @6 P
public Object buildActions () {6 r, w; p8 v8 V7 }7 N/ i5 i+ g
super.buildActions();% P6 c# Y* L' c2 N L {/ C
7 s; ?+ e( ?7 L/ [- Z9 R* p) F5 r' W
// Create the list of simulation actions. We put these in
; ^. X1 ?$ Q' ~, q& \6 T, a1 [. V // an action group, because we want these actions to be' I5 P5 H- I4 Y6 |
// executed in a specific order, but these steps should
/ T5 f5 K/ z3 r- P, L7 T c // take no (simulated) time. The M(foo) means "The message
! \1 D7 F+ |+ }" C* i // called <foo>". You can send a message To a particular, Y1 `; i7 H% z. L% n
// object, or ForEach object in a collection.8 k- {$ C+ ^3 J0 w
& y- h# e3 ]$ | j
// Note we update the heatspace in two phases: first run
1 e1 I$ j' L# |! S Q3 j; ?' V( C // diffusion, then run "updateWorld" to actually enact the0 I1 M' M' _ @! P9 Y, a% _
// changes the heatbugs have made. The ordering here is0 B( d# I* V% L) M- W9 T
// significant!
* Q2 M- d) i3 V2 [+ G, U; E $ N$ x- Z( \7 b6 e+ W
// Note also, that with the additional3 t! ]% U. a4 J. r
// `randomizeHeatbugUpdateOrder' Boolean flag we can7 G7 U* j" \: w$ ]! u
// randomize the order in which the bugs actually run5 b. W6 S$ v9 ~, o: x
// their step rule. This has the effect of removing any
X- A: z% w( y* u& U' o2 m // systematic bias in the iteration throught the heatbug$ C# O) c0 g2 D5 T
// list from timestep to timestep
4 q' r, Z% M# R9 _/ G0 e 7 u$ K) @: m5 \
// By default, all `createActionForEach' modelActions have& |! k' [ s* v0 a' s
// a default order of `Sequential', which means that the
$ F" ?+ m( J2 i2 C0 [ // order of iteration through the `heatbugList' will be) b. y+ T1 p; @( Z
// identical (assuming the list order is not changed: B$ L* N1 Z. p% g( z
// indirectly by some other process).
" Y3 Q2 L$ H" K+ m % J: f$ M5 U$ ]& u' G, g$ ~3 e
modelActions = new ActionGroupImpl (getZone ());
* p1 M+ b- u% l, r3 r
- @- g; l* {- e3 m9 j, [ try { v3 r1 p$ z- F W1 q4 F
modelActions.createActionTo$message7 L& V+ g. @- X4 ]9 j2 \' R* t8 ~; O
(heat, new Selector (heat.getClass (), "stepRule", false));
: z* p& i: \6 }; p( k } catch (Exception e) {7 n+ g% z. t& @- S( f
System.err.println ("Exception stepRule: " + e.getMessage ());. o1 g: O& A/ X' m
}7 O2 {7 a: ]; v4 P/ w) i1 j
* ~* S$ q0 {4 d& W2 k M: s* n! l try {
; H& m; e& ]7 C Heatbug proto = (Heatbug) heatbugList.get (0);( F7 v7 \2 g( }1 N: }
Selector sel =
" {- ~4 R2 Y* h4 h new Selector (proto.getClass (), "heatbugStep", false);; w0 [/ l3 J* x, ?/ @' v
actionForEach =' s* C5 l0 _* Q) O& e
modelActions.createFActionForEachHomogeneous$call
6 g' e! b* f/ E- w' { (heatbugList,9 h; V, n& W' @) }! N/ `
new FCallImpl (this, proto, sel,
9 T: q/ I" J. Z new FArgumentsImpl (this, sel)));
8 @- H0 C: u" ~. T! U! h } catch (Exception e) {% ^( O m k+ x6 E; C
e.printStackTrace (System.err);0 p$ t* V; q# k1 `: `
}
" e2 H/ @3 E2 u t
; ]) y: r. X6 `8 e' m6 K syncUpdateOrder ();9 A# g7 K! p7 E2 ]& ~ W
6 r( c5 ?3 X& `
try {
' w4 m, Z. r& S modelActions.createActionTo$message * C* p, U6 e D" S5 y
(heat, new Selector (heat.getClass (), "updateLattice", false));
! z3 z1 U4 z* b: v b5 j } catch (Exception e) {
( ]5 L d5 t: t' C9 V System.err.println("Exception updateLattice: " + e.getMessage ());- U$ e7 o& q8 `* x
}
) z' ~0 @4 L! C ) d6 V1 i$ o0 N X
// Then we create a schedule that executes the6 f0 W8 P4 Q2 e* E% f* M, `/ g
// modelActions. modelActions is an ActionGroup, by itself it" y% {* y- }/ w7 N5 |, E! o
// has no notion of time. In order to have it executed in
' P: J. p) g$ i; J# d3 o // time, we create a Schedule that says to use the; S+ H7 T# t. u d5 V
// modelActions ActionGroup at particular times. This
& Y8 C- [& J4 s+ y! C- C3 t // schedule has a repeat interval of 1, it will loop every; Q0 }7 \8 }. q) N5 E& y; B' w7 G
// time step. The action is executed at time 0 relative to0 \6 E+ ?/ o$ ?" [
// the beginning of the loop.: h# G9 z- o) b i5 w! X
% `/ N8 [9 O2 T8 j, l- I# s
// This is a simple schedule, with only one action that is
: {1 Z0 i, E7 k$ Q: ~2 U // just repeated every time. See jmousetrap for more
+ q4 E- D" _$ o // complicated schedules.0 f9 @3 I& u7 j# j% P
7 R+ V3 w3 d: w) K
modelSchedule = new ScheduleImpl (getZone (), 1);8 o# C& z' ~: j# L8 Z l
modelSchedule.at$createAction (0, modelActions);" K ]5 w/ d5 x: I6 V( G
% R; G9 Z4 }( R, q E4 ^9 b* M$ N return this;3 S3 j# |/ O. U9 |# C
} |