HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
% q6 T/ m' l$ A& E7 Q$ m2 [9 L! ]3 R# G# b8 [# i S+ Z: w w
public Object buildActions () {
& ^3 {% h9 q! |$ M, v9 `. x; G super.buildActions();
( ]$ N! f# _- n) { 1 Q( J6 j& J2 g4 X
// Create the list of simulation actions. We put these in9 |3 J; \0 k" y8 E2 N- z, x
// an action group, because we want these actions to be
+ \8 X- f* _% ` // executed in a specific order, but these steps should: {# X3 P7 @. L- Q) f3 B! K& I
// take no (simulated) time. The M(foo) means "The message
B; V9 b$ C, j# }6 {/ Z- ~ // called <foo>". You can send a message To a particular6 r" q2 K/ T3 n0 y2 q
// object, or ForEach object in a collection.6 k- @0 }# ]- w9 Z0 I1 D/ @1 Q
/ }0 F2 n9 V5 m% ^, J. b
// Note we update the heatspace in two phases: first run1 @# F: |4 B u! o- F6 a
// diffusion, then run "updateWorld" to actually enact the
- E) Z7 Q$ O+ S3 q // changes the heatbugs have made. The ordering here is
0 d2 z2 v$ k m) y5 }, D1 l. Z // significant!
$ o+ }5 n1 S# [3 n P. |/ E
2 ~: w! c1 A2 O& L6 w. m, h( L // Note also, that with the additional+ ~ L1 f. h) E
// `randomizeHeatbugUpdateOrder' Boolean flag we can) _* h) }# ?$ K) |+ B5 |) M
// randomize the order in which the bugs actually run7 Y, ?1 `" ?2 w; v3 ~
// their step rule. This has the effect of removing any" R, ?8 R6 Q, f8 N- d- i$ ?0 D
// systematic bias in the iteration throught the heatbug
6 Q1 P8 y7 G$ F0 \ // list from timestep to timestep
% \/ s! S& j" F) F& i
) M9 L% G4 O6 Z2 e // By default, all `createActionForEach' modelActions have9 \# Q, @+ d5 g* H7 S) _. i* M3 W3 w
// a default order of `Sequential', which means that the
5 H- h$ B/ P8 C7 o // order of iteration through the `heatbugList' will be6 N6 d$ b' d0 t: m& H7 m; J7 S
// identical (assuming the list order is not changed
& ]$ P: @3 i, ?: ]4 ~ q: _8 k // indirectly by some other process).! t2 ]" K4 L" M' e$ s3 i' u% c
* ]8 ~% D, O9 y. O! A3 C modelActions = new ActionGroupImpl (getZone ());+ ]: o8 `8 \0 `5 a# N2 r6 n1 ?
5 r' h- x7 ?& c1 @ try {% L: t+ o: r! G' W
modelActions.createActionTo$message
% s, C+ O6 v6 |5 q0 m, Q/ F/ D; T (heat, new Selector (heat.getClass (), "stepRule", false));" |( u3 }( X/ F( H! L3 W
} catch (Exception e) {
' X& B9 O' m0 W! @) d System.err.println ("Exception stepRule: " + e.getMessage ());/ ]" B9 M7 ]' q1 F& Y9 K
}- e! e/ K- J" z" }7 Z! p
" \5 e$ I5 Z& n try {
' s* Q* G9 C: [% h+ o0 Q Heatbug proto = (Heatbug) heatbugList.get (0);
3 f3 Q: B, A' c0 J$ X/ ~ Selector sel =
0 o* p8 T. F/ v! x8 M( d: a new Selector (proto.getClass (), "heatbugStep", false);* a* Q/ b/ O/ x) e' [; J) [
actionForEach =* S: E: P2 Y$ F; m! _
modelActions.createFActionForEachHomogeneous$call
) A5 w; J" y) o (heatbugList,
: P/ j3 b; c. X7 c" ]1 M# A5 c new FCallImpl (this, proto, sel,0 Z/ l7 I# x, l$ x7 j) `( I
new FArgumentsImpl (this, sel)));" U2 }( r7 k' a* X
} catch (Exception e) {0 }( w" y5 m, B& Y' Q, X
e.printStackTrace (System.err);3 B, C6 v: c. x7 {+ I" `
}+ M, c5 d; O% B3 v
: [1 \, [$ L4 H A- _3 a3 ? syncUpdateOrder ();1 ~% c+ ^' e {2 Y
+ S5 R, ?0 e, o" B+ `4 g x, s* G try {/ r% f4 \" y8 l
modelActions.createActionTo$message 4 A! H% ^. O: c8 D: p
(heat, new Selector (heat.getClass (), "updateLattice", false));' C. k2 y: u) j. d# g$ k
} catch (Exception e) {5 D. [" D/ h) k, n, N: a; T _. ]
System.err.println("Exception updateLattice: " + e.getMessage ());
: u: o& ]' Y. @) D8 b& k }
* b M( L; Y/ z ' E1 t, C) ~+ B; G2 w/ W
// Then we create a schedule that executes the
( a. E* }, [3 O( Z9 T // modelActions. modelActions is an ActionGroup, by itself it
- W; ~1 f$ g% K, v) z& X // has no notion of time. In order to have it executed in
) f& ^5 U7 k% @6 z // time, we create a Schedule that says to use the! Z; n5 Q7 \7 H4 Q
// modelActions ActionGroup at particular times. This9 ]) P S4 ^. N# l# W6 _' Q
// schedule has a repeat interval of 1, it will loop every
9 _0 k6 c+ L1 O2 G // time step. The action is executed at time 0 relative to
4 V- ?! h9 F) d! F: l% F // the beginning of the loop.
5 Y. D2 ^0 G8 ?& a7 H" q9 }7 g8 k4 t6 f# e
// This is a simple schedule, with only one action that is
( {1 H# \: m# d! B, O // just repeated every time. See jmousetrap for more
7 h9 j- C {0 i; [ // complicated schedules.& m# x6 |" K7 i! ^$ o- {
; s/ Q* U. P0 z& y G; E6 | j' c; B( J modelSchedule = new ScheduleImpl (getZone (), 1);, V# e# {; t: G! P
modelSchedule.at$createAction (0, modelActions);: U3 U, ]$ U0 R# X
7 U: U: o. r5 S* [& Z$ |1 o return this;
2 Q$ h, X2 o2 Y7 U1 G* N } |