HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
; r* y- }# y( O% U/ d$ K$ f4 {( s0 J, k8 A0 L1 w u( L
public Object buildActions () {
& \! R1 S) h! p! c; W% h% x super.buildActions();- }8 R, `! U9 T b1 q7 |
1 E+ u* T% j9 P+ S4 H8 c- w // Create the list of simulation actions. We put these in
0 i7 K; o. G7 B // an action group, because we want these actions to be2 U3 t3 K& b/ K( S, w
// executed in a specific order, but these steps should7 {. I7 ^ _. r( Q. |
// take no (simulated) time. The M(foo) means "The message& |' w* c- f) Q. X8 o
// called <foo>". You can send a message To a particular
' c4 u& Y/ e& o2 G* Z# l8 `* f# ]8 c // object, or ForEach object in a collection.
3 ?* A' n# L2 J ( Q4 l) c' W% E# t
// Note we update the heatspace in two phases: first run
; z9 R2 f, E; O5 X8 E // diffusion, then run "updateWorld" to actually enact the
/ H( g' ^5 i: ^3 u$ `. W! s$ o // changes the heatbugs have made. The ordering here is+ U! M m9 }# {3 s* I/ E5 \
// significant!: C4 ]$ P, H" B& N$ p y3 e
. M& X, d) {( G P( [( P
// Note also, that with the additional
6 f6 l6 r, ~3 L // `randomizeHeatbugUpdateOrder' Boolean flag we can! R" s n6 G! |' w3 Q" C
// randomize the order in which the bugs actually run# n# k0 q0 `+ a
// their step rule. This has the effect of removing any+ ]* E0 S Y% F& Q
// systematic bias in the iteration throught the heatbug7 x2 e, m& R7 z$ E
// list from timestep to timestep
" V5 i0 m3 A+ t! c5 v 7 b. G- X! G" F( k7 O% O
// By default, all `createActionForEach' modelActions have
# x, H$ |& |- b; Y5 q: l& D // a default order of `Sequential', which means that the
0 S& W1 Y$ ^6 ^0 N! I" f // order of iteration through the `heatbugList' will be
4 o9 p7 ^9 A& k! i // identical (assuming the list order is not changed
0 q% G& a3 t, F# K- {2 D // indirectly by some other process).1 S" q. \# V* o' q; t* Z
5 k5 U# k4 X% y3 E: h, j
modelActions = new ActionGroupImpl (getZone ());8 V1 ?) y8 d1 Y7 w
2 G8 s% _3 G/ T$ U; Z! Z, c, ]
try {
' ]9 P: r! J& J D( _ s. i( Y: B modelActions.createActionTo$message
& T+ g, z) X* [ (heat, new Selector (heat.getClass (), "stepRule", false));0 L! {$ |/ l" q/ v
} catch (Exception e) {) a& B9 i7 p( |" L |% n1 E
System.err.println ("Exception stepRule: " + e.getMessage ());
8 D- I2 L4 p2 D2 d, S# Z }4 g% q! @3 X; t: |6 J x$ E
- H% X! ?; q+ H- B; k* b' m try {' u; V5 ~7 L1 p
Heatbug proto = (Heatbug) heatbugList.get (0);
5 O6 u7 g- ?9 | Selector sel =
$ c9 z' F# _9 T* D new Selector (proto.getClass (), "heatbugStep", false);+ L1 E2 a2 h! z% U% w. A
actionForEach =# l7 I1 v- S3 Y; c2 b4 b# d
modelActions.createFActionForEachHomogeneous$call
9 f5 ~9 W8 l8 v& F L. s (heatbugList, ^0 j0 ?! c- A) ?+ S U" V
new FCallImpl (this, proto, sel,
# Z* q9 V! t$ u+ e* m, i new FArgumentsImpl (this, sel)));( d; v/ E1 L. A* }8 g1 V, l
} catch (Exception e) {9 b, n+ O9 S. G# n. z/ K1 T% M/ f
e.printStackTrace (System.err);
% c/ s/ K1 D3 [ B+ | } X! ^, R; L9 ~" t9 k- |% ?/ a
! q) B! x1 q6 Y" w syncUpdateOrder ();
! N& G- ?. @' m5 U5 l8 d! Y2 v I) E0 l4 A) L3 \3 k
try {
- Q6 X9 |/ H+ k7 L4 x! Z modelActions.createActionTo$message
1 O5 I3 O' E$ A" { (heat, new Selector (heat.getClass (), "updateLattice", false));& O" I' {$ e. K+ e3 c
} catch (Exception e) {. O" g9 S" M3 W; {5 J6 f
System.err.println("Exception updateLattice: " + e.getMessage ());
P" S! L( _) e5 U! t }5 J6 z. ~1 S; Q& [/ j7 b+ }9 G6 ]
. N4 I4 E. b {8 g" q$ f // Then we create a schedule that executes the
3 S' M; p0 g- r$ t8 L$ ^8 q5 G // modelActions. modelActions is an ActionGroup, by itself it* Y2 ]: e1 U' e
// has no notion of time. In order to have it executed in4 S% ?4 g$ ? a5 a$ r
// time, we create a Schedule that says to use the
. o% ~! a7 ^. R, d7 F1 E. a // modelActions ActionGroup at particular times. This/ y9 I4 M/ \9 V& y9 t) [/ U$ P
// schedule has a repeat interval of 1, it will loop every4 x* r. i% N8 D2 C
// time step. The action is executed at time 0 relative to
# \1 h( v* R# }6 E // the beginning of the loop.; f0 o% Y/ K" l: K# ^ n& Z
b5 g! Q, z& H3 o* J4 q9 v/ w
// This is a simple schedule, with only one action that is
0 ]7 c4 `; L3 f4 C' y" ^( u // just repeated every time. See jmousetrap for more: H W' l+ e$ G& v' o c
// complicated schedules.5 D! \+ D8 K1 K# {
, l* A6 d( }4 U, ?! s
modelSchedule = new ScheduleImpl (getZone (), 1);: c1 A( L" I, f1 ^/ r
modelSchedule.at$createAction (0, modelActions);; ]" N* G, t6 Q. h$ U7 P
5 V1 {% D$ p# ^* A4 W# y0 W* c/ I
return this;. v! R+ h) x& Q' J
} |