HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 J) z r& k! G3 _3 U
' n" p6 z2 a5 z: f* D- S* z public Object buildActions () {
7 Z0 \! t$ h; C" J super.buildActions();# C; b) J7 V' o7 \
6 e7 _5 `+ j' y1 l% Y
// Create the list of simulation actions. We put these in. Y- d9 r$ ~5 j! M( ]3 @1 e
// an action group, because we want these actions to be* N+ v7 r0 r7 t, k6 `! y
// executed in a specific order, but these steps should
! d: f" X4 G' O. @) t J, B9 @ // take no (simulated) time. The M(foo) means "The message
2 ^+ c# k: {5 S$ e // called <foo>". You can send a message To a particular
7 D8 \' n1 `. r // object, or ForEach object in a collection.
% S( m" e4 W3 X" v# F# {
a' ]4 w& [3 X7 C // Note we update the heatspace in two phases: first run
) l6 [5 S2 a5 G4 {& V4 k, L8 H) g // diffusion, then run "updateWorld" to actually enact the. V' k6 r/ c7 U/ ]
// changes the heatbugs have made. The ordering here is& M6 J4 @8 p! {% R. W5 _3 B: I
// significant!
) L N1 d) ?8 L0 [! V; T6 y" S
/ m4 u5 H0 J& X0 [; c; J8 B // Note also, that with the additional5 w) y5 o/ }+ D' ~$ D2 C
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ t; D8 Y& p! T$ {
// randomize the order in which the bugs actually run- g/ s# @; W6 c7 S6 b' g
// their step rule. This has the effect of removing any! f4 s3 s2 r' x
// systematic bias in the iteration throught the heatbug
0 O+ @! w9 ` [3 @' P' a // list from timestep to timestep* n, F k) ]" ]( O; i/ V5 g) f5 F! e
) l$ X* G0 z* @/ u; a! m, V; z9 D" A5 M // By default, all `createActionForEach' modelActions have
7 Z# @$ ~. h: f& t* D // a default order of `Sequential', which means that the! m* q7 P' P; t$ [' {
// order of iteration through the `heatbugList' will be( t7 k$ v% m" E+ S
// identical (assuming the list order is not changed
& P7 Y! L' G7 ~; V1 ~4 H3 B // indirectly by some other process).7 v- t9 t& @, F6 Z, q' J5 D8 D
1 m3 P( N- M: \# i modelActions = new ActionGroupImpl (getZone ());% m* k4 [( K* H$ a
8 L8 q ]. N* g try {- Y( f6 K4 T* C
modelActions.createActionTo$message
3 W7 o; B" x7 Z( z) K | (heat, new Selector (heat.getClass (), "stepRule", false));
1 P+ Q4 ~% x4 q! i X, E } catch (Exception e) {
, o: w. x0 c2 k4 Z& F System.err.println ("Exception stepRule: " + e.getMessage ());% x2 P& ?) e0 T7 z, \8 `6 G
}
, i: B& l) ?- t7 k7 Q; p m$ u C |1 |/ h( ` g5 X5 c
try {) d: G3 j/ ~1 O0 C" E+ t8 \: }) Z
Heatbug proto = (Heatbug) heatbugList.get (0);9 I# l4 O2 |, L t- h+ ]
Selector sel =
2 A. j6 G# R( R C' U9 n0 W: _ new Selector (proto.getClass (), "heatbugStep", false);
. U9 R3 E9 C! E9 i5 D& G( b actionForEach =
( ]# X6 ~+ ]' F: ~3 C% n modelActions.createFActionForEachHomogeneous$call; R' E4 M2 e% n' j5 l
(heatbugList,
$ F8 h5 l0 i4 l new FCallImpl (this, proto, sel,
* \! u @) M/ h% z new FArgumentsImpl (this, sel)));
0 r9 n8 m5 r" d/ r, ~ } catch (Exception e) {
' A- o3 k* A* z- H, \# ^9 A& {2 Q e.printStackTrace (System.err);
% ~2 O8 ]6 t9 a, J/ N- D }' ]; {, w6 \$ ^8 N3 |2 s/ y8 i
# Z+ C& w" b* Y3 [5 e2 Y1 _
syncUpdateOrder ();
f( w O+ ~1 f! M' z
$ h: A& z$ I9 h) o, X. I# ] try {
* b! d, W3 n/ m$ h" p( y modelActions.createActionTo$message
- \3 l1 ?% \4 V5 H9 P' `( s (heat, new Selector (heat.getClass (), "updateLattice", false));* y! N2 Y* j: c- U
} catch (Exception e) {3 S0 `. ]# n$ i: a
System.err.println("Exception updateLattice: " + e.getMessage ());
6 q/ f; Q! |9 q1 m }
3 H2 P1 _4 y: V' q8 O0 z$ h
$ y) D+ n+ l4 R& w" Z4 Q // Then we create a schedule that executes the
0 L7 d2 L, B3 b Z // modelActions. modelActions is an ActionGroup, by itself it
& V8 d* t4 ~4 [$ t; J // has no notion of time. In order to have it executed in) h. e; {" u5 x# B
// time, we create a Schedule that says to use the
3 H3 O- A* F4 s // modelActions ActionGroup at particular times. This
, V( D3 Q: u7 J# w; l' C! G1 e // schedule has a repeat interval of 1, it will loop every
) R6 ~* g# i" R$ q3 w0 H) k // time step. The action is executed at time 0 relative to
$ s8 ?8 l" J% J# R6 ~! A$ k // the beginning of the loop.9 [- g. x1 w: e: y2 u. S% Z' o
* Y- B9 _! Y% x! q // This is a simple schedule, with only one action that is
1 s" q% N+ I8 }5 e. F' x; | // just repeated every time. See jmousetrap for more
+ Y+ \( V: m# e4 ` // complicated schedules.
f- Z( D. p% i3 W; y
, \& j7 B3 U9 E) U1 S H modelSchedule = new ScheduleImpl (getZone (), 1);
4 [! h8 z, H! j7 `$ w* I modelSchedule.at$createAction (0, modelActions);
" v. p: E: X% p4 A/ V. m! U- Y1 a
: o" k5 n* E/ r) r# D% ]' X return this;, R1 J* ]! m) A
} |