HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:; a' [9 P. Z6 K' R3 I8 n
5 b' A. }% v4 t! W
public Object buildActions () {& @$ s# M9 ?3 p' K+ A8 L" k
super.buildActions();
" t9 f6 k6 u% c k u, G % X) d& z0 s& u7 z7 A
// Create the list of simulation actions. We put these in8 S" N7 C" ]6 M# P
// an action group, because we want these actions to be
2 M0 g$ v( K) r4 C! z- P* E Z8 W // executed in a specific order, but these steps should9 j' d$ l0 ^4 _4 H- \" F
// take no (simulated) time. The M(foo) means "The message
- W# O2 }- [3 t, f8 l" V // called <foo>". You can send a message To a particular5 s1 a% O P9 ~
// object, or ForEach object in a collection.6 o5 d- e9 a7 [/ R! W5 g* v
' d+ J, C2 E! [! F$ n // Note we update the heatspace in two phases: first run1 A: \+ ~' k" o% Z7 V8 b
// diffusion, then run "updateWorld" to actually enact the0 G5 u! v3 `& n, }( g& e
// changes the heatbugs have made. The ordering here is L9 j, \3 e# m* J( w7 J
// significant!
- }/ H5 x, h( j: u8 F - C6 u1 t$ C7 y1 {7 ?8 @
// Note also, that with the additional( Y4 u" w& O" R) ?- B
// `randomizeHeatbugUpdateOrder' Boolean flag we can: q7 z7 x g( O
// randomize the order in which the bugs actually run% P1 D" a" A3 F1 [# _" y, ^
// their step rule. This has the effect of removing any
/ Z9 g! S+ U0 _1 g; D0 l // systematic bias in the iteration throught the heatbug0 u# _1 \: D4 u* m; _8 i9 ?
// list from timestep to timestep
3 A. J8 J( A$ [- b$ M y @8 a* |) z7 L- _# j3 v
// By default, all `createActionForEach' modelActions have
( i! Y# E( J) Q$ A, }; R // a default order of `Sequential', which means that the( u# T: I: g& ^
// order of iteration through the `heatbugList' will be
$ [8 ~! P8 D! h2 ^7 S+ @: V9 f // identical (assuming the list order is not changed5 n$ M- R& r K# M
// indirectly by some other process).
. B2 K( H2 y7 d* A
d i+ o& f2 V6 Z modelActions = new ActionGroupImpl (getZone ());) s) f% J4 w8 i V
/ F7 i$ b) i! r; V) e6 A2 }( b1 ~ try {
& I2 w! P. s1 o- I modelActions.createActionTo$message
5 |1 G. c& R4 l; R" P6 @ (heat, new Selector (heat.getClass (), "stepRule", false));
( D% K8 x E8 y0 t } catch (Exception e) {
, V6 c6 ^, e) J" B+ W+ f System.err.println ("Exception stepRule: " + e.getMessage ());" `9 K9 t T, k6 i2 U: N" y
}/ N k- a- j5 h: x
1 u3 J) U( p1 T
try {
( H$ N1 s/ l2 x Heatbug proto = (Heatbug) heatbugList.get (0);5 c$ R, P% w$ d! n1 J- C
Selector sel =
* c, O% w. _! I# Z new Selector (proto.getClass (), "heatbugStep", false);
5 m# ~1 ?) ~. [6 V2 i actionForEach =
4 D: }8 y" @& ?- ?* \$ t. K1 \ modelActions.createFActionForEachHomogeneous$call% j1 R0 C( U3 K v
(heatbugList,
* ~+ ^" d4 \. z4 t/ \4 @: y2 F new FCallImpl (this, proto, sel,: ~( v% ]1 ~* g7 B5 S) d2 D$ b
new FArgumentsImpl (this, sel)));
) f6 @7 M9 w* ]" E5 u9 x/ X9 f6 X } catch (Exception e) {6 l1 u2 ^4 h7 [ b) y
e.printStackTrace (System.err);* U) B, H4 u7 j& e4 b1 M. ?7 H
}8 Y( s7 T1 z2 r5 r. ~
" j; D2 G+ k% G) i* O
syncUpdateOrder ();
! Z/ Z" s$ Z4 ?* w) F; `' i
5 D- [# K+ Z( J! C8 P try {
, G4 u1 t! r8 I! u& n; m5 Y. l$ } modelActions.createActionTo$message 6 F/ u& Q7 B3 ~6 D" d, |+ E
(heat, new Selector (heat.getClass (), "updateLattice", false));
, C# \( F( R2 S- j/ {4 o } catch (Exception e) {
9 }3 F4 e# Z. [( r3 q9 `$ ^$ E# ~ System.err.println("Exception updateLattice: " + e.getMessage ());
( f3 _; ?2 Z5 K: f* c2 ]* I* u }! r) s F) w1 n7 w
/ [. b5 m& I; P
// Then we create a schedule that executes the
" r$ G# X$ m5 [1 s7 O% N1 Z+ o // modelActions. modelActions is an ActionGroup, by itself it- g9 A9 d% D0 z" ~7 X' u' c
// has no notion of time. In order to have it executed in
+ U- [6 Q& F: U, h% }9 u1 J9 ] // time, we create a Schedule that says to use the& P+ i* D; \5 r2 `: M0 V! H! L# p4 w
// modelActions ActionGroup at particular times. This
! `, f, y" L4 F4 E // schedule has a repeat interval of 1, it will loop every% l! d9 S7 T) \5 P* t# e
// time step. The action is executed at time 0 relative to
1 h9 W3 t! ~, y6 u, R- T8 i: L9 D // the beginning of the loop.: @: ~/ x& j& ]1 o
2 m( e8 |$ C" w' t" S' q$ L
// This is a simple schedule, with only one action that is4 r( I* F. a w/ t' a, `
// just repeated every time. See jmousetrap for more
0 J7 l& c' W8 K' \3 g // complicated schedules.
/ j/ ~! C' n: N& N+ \
& n' n# u0 u) Q! f modelSchedule = new ScheduleImpl (getZone (), 1);
' Z+ V& f7 Z7 s4 C6 e# j/ x. x modelSchedule.at$createAction (0, modelActions);0 ^9 z, ~7 v* [/ V5 E! W
D2 T, {5 i5 L( e return this;
+ m; _) ~$ W+ \ } |