HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
: N* |+ Q7 z# j0 z9 A4 S
. M/ C% c& P `7 T+ t public Object buildActions () {
8 X' {8 f. ]0 F4 A super.buildActions();: E. d5 Z- r# F7 b$ \
; q7 A* l0 L2 r/ t0 T/ h // Create the list of simulation actions. We put these in1 p( m5 y1 k$ x3 ^, F% o
// an action group, because we want these actions to be
$ b- f8 ]4 j: C" u: A1 T // executed in a specific order, but these steps should
5 e9 ?7 ]$ W1 O3 B" i* c7 x // take no (simulated) time. The M(foo) means "The message0 @- r' y+ Z7 i, L
// called <foo>". You can send a message To a particular
" v/ Y' X/ ]6 j# l0 U9 N% {! o# Y // object, or ForEach object in a collection.
- b" z/ _0 x0 {% _( n
) H" _& O' Z& ^. B/ I$ a, Y" Z // Note we update the heatspace in two phases: first run' u! \7 j2 ~) [0 L" [! X. ~# Z& w
// diffusion, then run "updateWorld" to actually enact the& t }4 p2 H2 I& z$ p/ K
// changes the heatbugs have made. The ordering here is
; S6 t4 g4 F8 |- h0 O; ] // significant!
" s% O! w9 [( C0 m
8 N2 d% B1 t" a: U: m3 f // Note also, that with the additional
& V# W" A" O8 t( i0 H" \ // `randomizeHeatbugUpdateOrder' Boolean flag we can
; i6 F L6 q5 L9 w* H% L // randomize the order in which the bugs actually run
4 C# S- C6 P K& ]6 r1 D // their step rule. This has the effect of removing any
$ \5 h$ o& e9 F' _' G" O1 H // systematic bias in the iteration throught the heatbug3 N7 ~/ K6 ~2 y, y: J% i
// list from timestep to timestep
' [2 ^8 a- W' r* W7 k , m! m2 B5 O' \7 S% G
// By default, all `createActionForEach' modelActions have& N, O, `/ M, R
// a default order of `Sequential', which means that the, g! r3 c* ~. Q& P( z! L* Y$ _. E
// order of iteration through the `heatbugList' will be
' N) z: u- ], W% _- \) d& f // identical (assuming the list order is not changed
0 { R, X w. F0 n: L9 `9 { // indirectly by some other process).
( T& L- Z: O( V h1 @$ _7 Y0 B, h 0 U' q4 y8 Q' ?0 n7 A' N0 C
modelActions = new ActionGroupImpl (getZone ());( e3 y0 A% ]& _' m1 _2 H
0 `9 F% K+ G }; E( @" [
try { R/ _9 D+ z0 A3 O2 s! {3 t
modelActions.createActionTo$message4 O" c5 _2 {# a8 c
(heat, new Selector (heat.getClass (), "stepRule", false));+ c6 W" y. u: c+ B' q
} catch (Exception e) {
6 j5 l/ \1 [: g7 Z' e System.err.println ("Exception stepRule: " + e.getMessage ());5 L: ^ e1 x0 Q; Y
}* {; ^+ j' ], A! e
. v- G: g1 F: n2 G; @5 H ~
try {
" `; V& e! U8 o% [ Heatbug proto = (Heatbug) heatbugList.get (0);
! N% j8 o! u9 p; e: o1 A: _ Selector sel = + j$ P/ w4 b* O% ?
new Selector (proto.getClass (), "heatbugStep", false);
0 f! M# W( H! Q+ ` actionForEach =
; M$ ]' C8 F5 k/ z modelActions.createFActionForEachHomogeneous$call* J6 Q) g7 ]1 P# r( B
(heatbugList,
) y( w$ P; [% ^- V: m6 {+ b new FCallImpl (this, proto, sel,2 p+ m; s8 B3 U; ^7 V4 _8 ^/ D
new FArgumentsImpl (this, sel)));- I W3 o% R* K' r, m5 E) p, _1 B2 ?
} catch (Exception e) {
! U, ]8 x U F! v: x e.printStackTrace (System.err);3 [! m8 Z$ \2 C; I
}; H, w2 ?( \; ]6 P9 E) Y
: u+ N* D& [6 j3 Z4 h* | syncUpdateOrder ();0 n0 s; z7 m2 g: b9 A" J5 ~
3 @( r: y; x' N9 |$ P6 R- i7 l. @
try { n( `# W+ B# f
modelActions.createActionTo$message ; W) T1 ?5 b% l' b$ k( C
(heat, new Selector (heat.getClass (), "updateLattice", false));
8 Q$ l0 S) z' V5 G% J } catch (Exception e) {
, y: Y* n' f1 J9 G h System.err.println("Exception updateLattice: " + e.getMessage ());0 X' N' b# }& R( D# p7 S
}
- w6 A' `9 V" }5 C& w- ^
: P8 ]( i S0 D. d I7 S- G* f // Then we create a schedule that executes the
- ` d7 G* i8 D' g // modelActions. modelActions is an ActionGroup, by itself it
0 R. N6 R- [9 _) p6 r // has no notion of time. In order to have it executed in, x8 [. ]% b/ m% }
// time, we create a Schedule that says to use the
! _" F4 M+ ^% b/ w // modelActions ActionGroup at particular times. This6 U& `/ L+ ? W3 D* Y
// schedule has a repeat interval of 1, it will loop every7 p- l* U5 z" ?* V
// time step. The action is executed at time 0 relative to$ u. n1 ?- a" M' l6 p
// the beginning of the loop.) Y. y! i1 O8 J+ j* G9 P, t# _5 ]2 J
+ ]6 i! W6 C* h G" E) f' {; {
// This is a simple schedule, with only one action that is2 d$ P. O) z5 A
// just repeated every time. See jmousetrap for more2 X/ U6 \- g6 G' ]$ S2 a
// complicated schedules.
6 \+ k8 f0 Y$ v, i$ ] 0 H8 T. G$ s$ A
modelSchedule = new ScheduleImpl (getZone (), 1);
0 O* n' L) E& ` P# U$ `' [$ G. L modelSchedule.at$createAction (0, modelActions);
9 L+ q1 x3 t. S+ ?( m* Z. R
9 i) P' Y& H* ^) D4 x( y% a7 j return this;5 U, o7 m0 V2 n. T% _
} |