HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
j! v0 X$ J% R e% N' d: @ T- `% P9 J! L2 n/ T! u; `( Z* ]
public Object buildActions () {2 r, Z; K4 c2 l* t2 p- x
super.buildActions();
9 t& C; l4 t: Y1 N6 C- |
; B( W( `! b e, s9 B, u. B- S // Create the list of simulation actions. We put these in
6 d1 c# P( |% l; e5 O! a5 y" @ // an action group, because we want these actions to be! u6 \9 x7 }- _$ t* S* {% M+ l
// executed in a specific order, but these steps should
. H+ u% ]3 \+ q* l1 z2 ^ // take no (simulated) time. The M(foo) means "The message
1 |6 { Q. V* r! g$ T# J // called <foo>". You can send a message To a particular$ g; |: s/ ^/ ^, u/ |
// object, or ForEach object in a collection.
/ T: P# ?) w+ d* J( V" s
0 j; z" n: B" n# Y. ~) b4 s, R/ q // Note we update the heatspace in two phases: first run
- c( V( b* }, M4 l& e5 E // diffusion, then run "updateWorld" to actually enact the
& k+ o% w# T9 {9 w // changes the heatbugs have made. The ordering here is% h1 T* T6 @/ z9 X2 E" r
// significant!& E1 \: R* C$ t# [5 [2 @" \( A
' |/ I! q% n3 @ // Note also, that with the additional
/ f/ y \: ]+ @; W$ ~6 y // `randomizeHeatbugUpdateOrder' Boolean flag we can7 ]4 R7 h) R' l5 e
// randomize the order in which the bugs actually run0 u1 B0 Q) p. B' _/ R, J
// their step rule. This has the effect of removing any1 K2 ?- f$ z: e
// systematic bias in the iteration throught the heatbug
2 \ y2 z6 _7 v // list from timestep to timestep
0 x. E1 O0 j, w * w; }: a5 Z4 [+ e& w& m4 t2 g- l
// By default, all `createActionForEach' modelActions have
% z- D2 Z% w$ A/ z // a default order of `Sequential', which means that the
4 j$ {% x! L U* K( ` // order of iteration through the `heatbugList' will be+ G5 q5 w7 i: W7 O
// identical (assuming the list order is not changed
6 n* K6 i; U3 D* q: F$ K8 D // indirectly by some other process).0 ]& V: w4 b; I6 g( `
3 |2 k7 c& \# A2 I" U4 P+ ^
modelActions = new ActionGroupImpl (getZone ()); Q: K0 O+ J/ s( E% r& N6 u9 G: d$ K
& j5 P8 u6 K) u; g try {' d& D& o, g; T& s& U9 A2 W( v2 |9 i+ \
modelActions.createActionTo$message) S9 L- F/ K) ?7 P& Y6 o- [9 n
(heat, new Selector (heat.getClass (), "stepRule", false));- @* e% q; p5 C x, ^; I
} catch (Exception e) {
$ ^' c+ F6 |% P- h j; G System.err.println ("Exception stepRule: " + e.getMessage ());
- Q4 k2 z& N; K* ? }
4 b; `1 `$ ?" |( m7 t4 G6 G
5 q* L/ c$ } g/ t try {
, U6 D+ Z& z) a& u5 P! I6 l Heatbug proto = (Heatbug) heatbugList.get (0);! U* s' w( n: F, C4 c$ f
Selector sel =
& U2 x! d. r$ S6 j% K. _ new Selector (proto.getClass (), "heatbugStep", false);
+ T4 v( E5 f) I7 y0 M5 d actionForEach =
4 h3 N T( g+ z8 c& Q- Y' k modelActions.createFActionForEachHomogeneous$call
% z, i$ i6 L) j* Q) b (heatbugList,! w+ Q+ |; O" s! q& C
new FCallImpl (this, proto, sel,. c% p" c3 s' Z; j' l
new FArgumentsImpl (this, sel)));
^- {3 _) A2 \ } catch (Exception e) {% D9 h& d# y# l1 U+ F
e.printStackTrace (System.err);
0 H$ Q8 k9 v1 r7 [2 V. r }, `% u( J) P! z9 U1 Z& B0 W
* a: O" O2 h' E
syncUpdateOrder ();
& R+ I3 H3 N% c& \) L2 ^! t' ~. i+ |
try {
3 r3 I3 x c/ A+ ~1 A modelActions.createActionTo$message 3 P' s" }; f. o2 Y- q2 v5 [0 ~
(heat, new Selector (heat.getClass (), "updateLattice", false));
{3 j# F+ j6 E6 s5 V" z } catch (Exception e) {! B# d4 u( r! s9 ~2 u. _+ d
System.err.println("Exception updateLattice: " + e.getMessage ());1 Z7 Z |8 v- z% K
}
! q; w. f% L+ W! A4 g; j8 U2 { " @; x" Y6 }3 E, x9 `( l# B
// Then we create a schedule that executes the& t) s {) e! {& n! Z: s
// modelActions. modelActions is an ActionGroup, by itself it
! S4 b# m& \! e( c1 T5 y* K7 U // has no notion of time. In order to have it executed in/ Q4 b4 I$ V# [& X4 W2 \- h
// time, we create a Schedule that says to use the ] [' q3 n! y# Q3 `8 z
// modelActions ActionGroup at particular times. This g$ n. \) A; e
// schedule has a repeat interval of 1, it will loop every3 R3 H& z2 Q' f
// time step. The action is executed at time 0 relative to
) O, Q5 j$ Y. f7 W R- S' g5 G // the beginning of the loop.
7 d4 }8 v0 V4 q9 z7 l$ `7 E x& u! @- v- e% l. W! y6 P P
// This is a simple schedule, with only one action that is0 S# x/ l7 |3 q0 }+ C4 m" g$ v
// just repeated every time. See jmousetrap for more
: N! K; U( s) Q ^1 B // complicated schedules.
. t8 l5 k2 d# @5 E8 ^5 N/ K ' o1 z7 b& ]0 F" h% m, a! a% T
modelSchedule = new ScheduleImpl (getZone (), 1);4 o2 G, P n7 J% g* [5 I8 I7 r8 T
modelSchedule.at$createAction (0, modelActions);
1 D3 O$ K. ?8 ?. p+ V
$ \+ R: t% b2 z, l( n return this;! B- u3 U8 I3 D6 `) z
} |