HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
5 N* Z- I$ r! _6 y4 \! ?" _4 g. T' f0 M3 V# G; H3 X: @; b+ |$ L
public Object buildActions () {$ P. H- u1 @6 l- J3 J- i
super.buildActions();
3 R% A$ E( s! V" J2 u; S( x
& f* Y# ~; _& {& E# M // Create the list of simulation actions. We put these in1 j4 H, a2 G7 d" d3 E) k
// an action group, because we want these actions to be9 J2 R$ b, ~/ S E5 o/ Y: S# |
// executed in a specific order, but these steps should' G' {7 T# t: Q1 v/ ~1 X" ^
// take no (simulated) time. The M(foo) means "The message
0 G. S! g5 a* Y6 b9 n // called <foo>". You can send a message To a particular
" i1 T) Q/ O1 q/ w' c // object, or ForEach object in a collection.5 T5 _ r1 M' G6 b! ^- a
1 y9 ^: }4 M& @
// Note we update the heatspace in two phases: first run
4 P' p% V& ^, W+ c6 @5 I& l // diffusion, then run "updateWorld" to actually enact the
G" j, s }/ j) @! S8 z' B // changes the heatbugs have made. The ordering here is, o5 p& \$ s3 l) e8 ^" O! P9 i
// significant!$ p7 y& v9 _" `; w. a3 U4 Z# B$ ?
' b( j3 }2 E) p5 ?4 J$ ]& S // Note also, that with the additional5 {$ m6 a8 k; c( A2 F
// `randomizeHeatbugUpdateOrder' Boolean flag we can
3 d7 a& E h ~; G' F3 [7 z // randomize the order in which the bugs actually run
, {4 H% m) e; E1 v* ] // their step rule. This has the effect of removing any) O0 f! E% x6 u
// systematic bias in the iteration throught the heatbug
' x. U% h4 E3 Y3 R, X% U // list from timestep to timestep& f: g9 m* I- E; c$ `& e
; y, M; x/ B3 I1 \- z# ^ // By default, all `createActionForEach' modelActions have. I4 O0 C4 P: w: T2 w
// a default order of `Sequential', which means that the
3 |- q# K8 G" d8 f/ T, } // order of iteration through the `heatbugList' will be
2 u+ r3 R- { D7 y. \# ]) Q2 y. m // identical (assuming the list order is not changed
9 V3 H* y* A7 o, ? // indirectly by some other process).; ~; T% _* a# M$ B$ d
8 }/ G+ T2 |; \, G C3 x5 h& M
modelActions = new ActionGroupImpl (getZone ());1 a, l9 Y$ ~, H- n! w; ~
9 }+ `( b$ a& z$ Y% a! j
try {
9 S4 z3 d/ |6 ~3 y+ p, { modelActions.createActionTo$message
1 G7 v% H2 \- Y0 L3 c (heat, new Selector (heat.getClass (), "stepRule", false));
7 B" a+ }) j* m, P& d } catch (Exception e) {1 J1 c9 k: Y0 m5 N1 J, c2 r) E
System.err.println ("Exception stepRule: " + e.getMessage ());
, m+ U. `( m# I/ |# M }
5 ~' W* |6 T3 @: @7 F7 h/ g" J/ B, U% |4 t" x! N( W# C
try {
! K4 Q1 J4 s: T+ ^" x' c9 @ Heatbug proto = (Heatbug) heatbugList.get (0);& U% d+ W% P6 _ D
Selector sel = 9 ?" S/ G [5 Q; u2 ]2 e
new Selector (proto.getClass (), "heatbugStep", false);8 ]5 \- j0 e* ?7 ^& o
actionForEach =
! c6 U4 [- }$ }4 L/ h1 s& u modelActions.createFActionForEachHomogeneous$call, q5 M+ _3 H$ Q1 y: _
(heatbugList,% ?8 i5 R0 ]! B' x; I' w8 K/ y
new FCallImpl (this, proto, sel,' o) y9 s# W$ _+ f! L4 @3 d, B ]
new FArgumentsImpl (this, sel)));3 f6 I# }. u. Z2 j6 s2 B2 r: u- X( _, m: q
} catch (Exception e) {
5 [$ O2 A. x- H' D% {, x- W* \8 q e.printStackTrace (System.err);
2 i7 O T# M$ \: W6 {) y }
" w( J8 r; h6 X5 f( r& R
8 Y9 w8 q( j6 {1 [4 J syncUpdateOrder ();/ q; A0 f+ n2 n1 r
& w, d6 X" U& S try {
8 J, C! o! ?5 E1 R I modelActions.createActionTo$message
( L; `0 h: W: W8 C f, G0 r (heat, new Selector (heat.getClass (), "updateLattice", false));
7 p Z. D* b( | } catch (Exception e) {
3 X& Z% l1 B3 k System.err.println("Exception updateLattice: " + e.getMessage ());* ^& E* e7 g# l/ `! d4 P
}% r% i e& g1 Y9 {7 `
+ `0 m0 x, L) I7 h8 T
// Then we create a schedule that executes the4 v; p; G4 n% |* D" C
// modelActions. modelActions is an ActionGroup, by itself it( m) F( G+ }, h/ n
// has no notion of time. In order to have it executed in' \' Z% i! V4 N7 [+ Z
// time, we create a Schedule that says to use the1 ]$ o0 n/ l' [. b6 l4 ^
// modelActions ActionGroup at particular times. This v$ R. l7 G4 {" B$ i0 E
// schedule has a repeat interval of 1, it will loop every
( q7 s- X% j: L* m // time step. The action is executed at time 0 relative to
/ A2 n5 F0 P2 f; k // the beginning of the loop.
2 ]$ o" s, O* J6 v: |2 Q# e* r5 M/ L' t g* _- X! ]- @8 w6 X
// This is a simple schedule, with only one action that is
" D( A$ r: d; _ // just repeated every time. See jmousetrap for more
$ \. k( m& Q- a // complicated schedules.
+ s3 [5 B1 u" \* `
, n9 U: j& K- d" N. M modelSchedule = new ScheduleImpl (getZone (), 1);
2 q& z6 z! K4 m$ @, S% `" }& C! K modelSchedule.at$createAction (0, modelActions);
/ D- V8 ~( k1 N4 Z, B ' l4 e" O p2 |
return this;
K& h; O9 z* Z! M& w( o } |