HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
9 u3 N( t6 Z$ b
" N2 Y$ X& B) D- X. P public Object buildActions () {' y+ I8 b; ?& V8 I* {2 D
super.buildActions();
4 g5 r2 v/ R, G# f# h 5 w( n( c) ]- W9 b. _
// Create the list of simulation actions. We put these in
* c% d |' t9 } // an action group, because we want these actions to be* ^/ s' |9 F, m: K. r# `* d9 l& q
// executed in a specific order, but these steps should3 H; D8 J( ]; d# N, `
// take no (simulated) time. The M(foo) means "The message
) ?+ ^0 O" U% Z7 K3 p // called <foo>". You can send a message To a particular
: G+ p0 R; q3 ] // object, or ForEach object in a collection.0 ?- J/ X5 }, C* H/ R
. L" u7 ?8 v- d2 h1 z // Note we update the heatspace in two phases: first run
/ N" k' l: [, N# o1 u5 m: D // diffusion, then run "updateWorld" to actually enact the! `' z6 i" P- {6 @6 ?
// changes the heatbugs have made. The ordering here is) Y+ ?/ W9 ^1 P( c. K8 M
// significant!
! O2 e) S( o+ s ! ]$ D) | V3 ^! n. ]
// Note also, that with the additional
- d6 H+ E6 p$ J; K // `randomizeHeatbugUpdateOrder' Boolean flag we can" h9 ]3 D' n& \4 C# V3 ~
// randomize the order in which the bugs actually run3 S* [4 z9 i& L4 G
// their step rule. This has the effect of removing any
: w `+ [4 t4 T/ s // systematic bias in the iteration throught the heatbug
& P% C7 u1 b: v, z9 l // list from timestep to timestep
( S: n7 G/ o2 ?; z
' T' a& t; S; m! r // By default, all `createActionForEach' modelActions have
3 S s# w3 g& w j" x) p' F // a default order of `Sequential', which means that the
1 T; ~( h6 }/ A. o3 Q7 Q // order of iteration through the `heatbugList' will be
8 u0 N: M; }) ?' Q3 q) U8 s // identical (assuming the list order is not changed5 \ a* Z& I6 B. k6 t! ^
// indirectly by some other process)." z& @0 D0 C: y/ {- V0 w q
% y6 p6 X8 `* H6 A+ m modelActions = new ActionGroupImpl (getZone ());
5 ^. S7 E8 B8 q" G' w/ _1 j+ w. X j/ \! A
try {
+ n _$ n. e* D: \* W modelActions.createActionTo$message
1 `% y6 H7 A# s0 l (heat, new Selector (heat.getClass (), "stepRule", false));# q" V% S7 R9 B8 f0 {7 x y* l
} catch (Exception e) {
; x8 R: Q; B' c; V System.err.println ("Exception stepRule: " + e.getMessage ());& H% T: u8 f) J+ g) i
}1 l4 x& i1 I5 G5 H- P
0 k) l7 C* E0 s* A$ p3 m' S/ ~
try {% w: J; @- t# I$ f6 S5 M0 E; m& V. p
Heatbug proto = (Heatbug) heatbugList.get (0);
: d! }/ b( z& Z4 M) R" f Selector sel =
" A* G' r! w; [5 p+ T3 U new Selector (proto.getClass (), "heatbugStep", false);0 [4 w" Q: F3 P4 M
actionForEach =
' q9 a# {, d! z; s5 }* ^! R modelActions.createFActionForEachHomogeneous$call
$ T! a/ v% U* F9 e3 v+ I (heatbugList,& \) u8 _- r& B7 R% ]2 L- B
new FCallImpl (this, proto, sel,
7 ^6 R9 R* n! h new FArgumentsImpl (this, sel)));
( `4 M& E: R. n$ A! f+ i+ N } catch (Exception e) {
7 x" x, A0 H$ u# t% t e.printStackTrace (System.err);/ ?; M# a- Q `5 C4 M
}
/ m7 b3 @* e/ X) C 2 P' V2 M+ }) Z# f0 T
syncUpdateOrder ();. p+ G6 s; i: _( I/ t$ o( ^3 M
3 ]! ]2 J' s) `% r& _- p. e try {" G6 `; K# K5 E
modelActions.createActionTo$message
& m! J# S( n+ }) J. J1 _* o u (heat, new Selector (heat.getClass (), "updateLattice", false));- z9 ], c4 U+ z: X4 n' i+ ^' o* T* ^
} catch (Exception e) {6 E* c5 t) c2 q, a0 o7 v/ U4 ]8 \
System.err.println("Exception updateLattice: " + e.getMessage ());" B0 d4 I) c4 v! x% g& c9 h+ R1 N
}
/ T$ }7 u6 }# x5 V/ g , ^/ X+ f a- |3 u
// Then we create a schedule that executes the0 }) i1 t4 @8 U- Z; \2 L5 n7 ~
// modelActions. modelActions is an ActionGroup, by itself it
3 m# o" Y7 B3 { U5 x5 A/ ~ // has no notion of time. In order to have it executed in9 h. K9 B! y8 [1 L9 n1 a
// time, we create a Schedule that says to use the" W, O8 |& O/ E4 i* M: w) b
// modelActions ActionGroup at particular times. This
7 U' R8 @0 `4 u( N6 c5 ^ // schedule has a repeat interval of 1, it will loop every
/ ^, ? q# m4 K& J. v // time step. The action is executed at time 0 relative to
$ x5 n* N5 e& y- E // the beginning of the loop. a; i" @# R8 f' D
' c; C( m0 q0 ?8 K
// This is a simple schedule, with only one action that is. s; }, Q; z8 a9 M z- h/ @3 c! B6 S
// just repeated every time. See jmousetrap for more
/ U9 m& f1 t& K; k M1 p }3 k // complicated schedules.6 G% D* h& R* X& H0 ]' \
7 A3 }9 O! @4 }/ u% A6 t o
modelSchedule = new ScheduleImpl (getZone (), 1);
, M! Y* J4 Q+ V2 M. C) E modelSchedule.at$createAction (0, modelActions);$ X1 f: d# E( Z9 `, E( o3 N' D7 n
) w2 B% e' H* l$ ]3 q; @
return this;
1 [6 W3 L' a$ [; o/ t8 b! X } |