HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
6 `0 x! S; {/ d8 D
9 S: z/ q6 Y3 }' D- g; Y public Object buildActions () {
6 p; Z( j) T3 [1 P3 U9 R" }+ i. u super.buildActions();3 n$ d2 J" z7 D) r( ]5 @8 I
6 M7 Y) N" r* y7 _( s) O% E
// Create the list of simulation actions. We put these in5 f \, x/ {3 Z! i- p4 h
// an action group, because we want these actions to be
& ^! K X. ]4 h" w7 y3 o; o // executed in a specific order, but these steps should- |: J* I* }2 Q8 c1 { T
// take no (simulated) time. The M(foo) means "The message: S. w D x4 i3 ?, ^
// called <foo>". You can send a message To a particular, D! ?( V+ n# l$ d7 ]* Z" O
// object, or ForEach object in a collection.
, N q1 H3 n. u0 \, h
! Q' L/ ]% r- R" z& ~ // Note we update the heatspace in two phases: first run
4 ^4 d6 Q3 p! K- ^ // diffusion, then run "updateWorld" to actually enact the
6 }* y2 i8 Z) ]7 F4 v0 Y // changes the heatbugs have made. The ordering here is
' p& s7 `/ O+ }! o \) ?$ X // significant!* N0 f: P4 n% q$ X
& Y3 X$ n# \9 H8 p5 t7 G; ?% _ // Note also, that with the additional3 o1 \6 O# V8 t
// `randomizeHeatbugUpdateOrder' Boolean flag we can& E& V% Y, P0 E* @6 E( m4 C: C
// randomize the order in which the bugs actually run# q. E7 C# O1 \! @6 Y8 l# X
// their step rule. This has the effect of removing any2 F5 d( g) F; p* p1 F
// systematic bias in the iteration throught the heatbug6 a7 p. h" B4 }# o$ _. \) J4 ]! W, I
// list from timestep to timestep
) F7 m( {4 d' O+ N8 c 1 t0 p9 f* i- D
// By default, all `createActionForEach' modelActions have& n8 }9 q) R# s1 y9 Q
// a default order of `Sequential', which means that the# t6 @& B8 x" P) d+ [# C' l
// order of iteration through the `heatbugList' will be' x6 O3 A$ e: Z* J2 R; U& g
// identical (assuming the list order is not changed: Q) T) _- f" p2 T A
// indirectly by some other process).! H! q2 u @9 y% L
1 a8 Z1 e6 P" G" z$ ^7 _8 F5 X modelActions = new ActionGroupImpl (getZone ());
* E' G4 p$ K S$ E# }+ h7 |
4 Y. f5 G0 |+ i( F: x/ L- y try {# D/ |, N; \0 @- l2 _& s/ {
modelActions.createActionTo$message& O% n& s) o L! `$ ?2 z3 v1 i& c/ T
(heat, new Selector (heat.getClass (), "stepRule", false));
5 O; s# B) J( C, |- C1 ^ } catch (Exception e) {
( L5 B* `4 M" m Q( A) H' f System.err.println ("Exception stepRule: " + e.getMessage ());
. ?, `( k3 @! S i9 [2 ? }
" W: j0 i, R# |5 V0 o9 {' i& z0 a% ?$ c/ ]6 \
try {% r9 w/ d& {+ U, S8 U
Heatbug proto = (Heatbug) heatbugList.get (0);
% ^0 w+ k1 |8 H8 i, m7 a Selector sel = & E4 L, R; {4 Q2 L" `. l! m/ H
new Selector (proto.getClass (), "heatbugStep", false);
: l5 G. d, w" m9 |7 a" q, M actionForEach =
% Z+ I6 J: U O; @9 q) \0 Y; g modelActions.createFActionForEachHomogeneous$call7 u/ a5 F1 ?3 E9 _" a
(heatbugList,
6 z( B3 d: g+ w7 | new FCallImpl (this, proto, sel,
, D" u P/ g2 {5 y1 @3 d* @ new FArgumentsImpl (this, sel)));8 `# n# ]: f* x ]$ \
} catch (Exception e) {
& q( S; v* D& A$ v# G, X e.printStackTrace (System.err);# _) s& F: c+ j9 }/ ?
}/ I1 h% F: i* @( T
6 X, m2 S \ f$ n9 v$ i+ U; u/ X! Q
syncUpdateOrder ();
& o k' Y& E; G6 c: _+ [- C8 w$ l+ l; W$ {$ k
try {4 Q- R- t% m0 f( j, X9 Q+ `2 w3 D2 Y
modelActions.createActionTo$message $ k6 }, r* D/ T" Z `+ Z
(heat, new Selector (heat.getClass (), "updateLattice", false));% M3 d+ G; t( c( w Q F/ S
} catch (Exception e) {' F0 U+ ^9 i W8 P7 S
System.err.println("Exception updateLattice: " + e.getMessage ());/ q% Y. Y& L* f+ u5 r: p( S w
}( r" V4 P4 O0 _1 P8 `$ L) e$ U
. K4 ^3 n+ Z8 R // Then we create a schedule that executes the7 \0 j+ o4 ^8 Y" {2 w
// modelActions. modelActions is an ActionGroup, by itself it! x" [) [: E( Z& q' \: x
// has no notion of time. In order to have it executed in i+ i, b2 C4 @" L l1 V1 W
// time, we create a Schedule that says to use the' w! w* t" K# y: G U6 O1 |
// modelActions ActionGroup at particular times. This
% |: o& R8 q! E1 \" }" \+ @ // schedule has a repeat interval of 1, it will loop every) J7 v5 q& g3 d8 A3 D: i
// time step. The action is executed at time 0 relative to7 V' W n! t/ A9 @6 y2 D
// the beginning of the loop.
# u) L5 p4 Q' q2 o3 G
7 b% @$ h* F- T4 W- r9 {% d // This is a simple schedule, with only one action that is
+ W0 w2 ~1 P3 c$ v. W# I* ]* ~3 n# J7 X // just repeated every time. See jmousetrap for more4 l" G3 z* A# v z
// complicated schedules.
' s2 v0 R! D3 D' s4 g2 G4 | [
5 c! G1 @: ?$ k8 t. e1 m% [ modelSchedule = new ScheduleImpl (getZone (), 1);
; d, G, f0 \9 p2 f modelSchedule.at$createAction (0, modelActions);, G2 n) O+ b/ |+ V
4 y6 P9 Y9 b ?6 i# o5 J
return this;9 N( `/ E7 b' q' D& a& c0 `3 N
} |