HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:
) S! Q; u U7 N" t
3 g8 g5 L8 p# v public Object buildActions () {
: F& k3 T! V# v" }3 F super.buildActions();
# C/ Z3 k, Q5 b# Z+ q+ U
3 J( {2 A1 l; B) p8 B // Create the list of simulation actions. We put these in
8 P+ E$ C- W0 X9 U' a1 P: L B- I // an action group, because we want these actions to be
, ?# r' y# O3 z8 n* \7 i // executed in a specific order, but these steps should9 k- R3 [2 a( R0 J
// take no (simulated) time. The M(foo) means "The message# g# R4 f% A7 C6 ^+ }2 H
// called <foo>". You can send a message To a particular
7 ]2 Z. U6 @! p) S // object, or ForEach object in a collection.
4 w: r. D$ h# |' V1 S O0 {
" D* {" R3 x" }7 m. p // Note we update the heatspace in two phases: first run" x3 E* Y/ Q- ?; ? i
// diffusion, then run "updateWorld" to actually enact the0 D9 T( V6 Y7 a: U: ]! ?
// changes the heatbugs have made. The ordering here is
B- r: l# U3 U* q% N5 P // significant! P$ t0 j2 N& i5 k6 Q6 B) R
x1 \4 j* @' \1 N' o( X
// Note also, that with the additional
: w4 o) E; ~0 u% n$ ?" w // `randomizeHeatbugUpdateOrder' Boolean flag we can
7 L+ \- H) C; ~: m% n) \% Z, f: o // randomize the order in which the bugs actually run
/ y$ m4 F5 P5 }6 f // their step rule. This has the effect of removing any
" v) g4 U6 Q9 Z3 v // systematic bias in the iteration throught the heatbug
" c/ d% P) ?. ] // list from timestep to timestep
+ a$ m/ V U6 u5 m
* H: v' P# R( `: o% q6 M2 Z) r // By default, all `createActionForEach' modelActions have8 M/ K2 H' F. v2 _- J
// a default order of `Sequential', which means that the
( B' q1 S+ ?9 I7 |, S8 c/ n' _( S // order of iteration through the `heatbugList' will be
6 q8 E% E O2 F' o3 B // identical (assuming the list order is not changed& t" \6 o. t; f' G7 a/ O
// indirectly by some other process).5 c: z, g! j# K$ l4 t1 c
0 v8 `7 O" O& ]3 M; i6 N modelActions = new ActionGroupImpl (getZone ());
3 g& C" [" Y7 }
; i1 g" p2 ~+ B! ?/ u, ~) \* J try {3 g: m9 u2 C( }7 M9 V
modelActions.createActionTo$message
( d- r1 d4 A% w! j$ ?& Z( q (heat, new Selector (heat.getClass (), "stepRule", false));
1 a2 ?, |" G$ S, M% Q } catch (Exception e) {
( ?* q( h$ e& Z4 o* q- n System.err.println ("Exception stepRule: " + e.getMessage ());0 S2 L- W5 } Q6 F. E% K
}
# j, V3 d4 w* s, \3 j( b" r7 ^; _1 g/ \
try {
3 a. x" ^1 F9 s0 d0 D" o9 I! F$ A Heatbug proto = (Heatbug) heatbugList.get (0);, |! A3 V5 `1 |7 Y
Selector sel = $ M% c( l+ \1 s7 A9 Y; T" A% C
new Selector (proto.getClass (), "heatbugStep", false);
: Z9 V6 j4 T# j$ q5 r2 M/ W actionForEach =7 s/ z2 v7 w% J ^) b- _+ ?
modelActions.createFActionForEachHomogeneous$call
$ a* V9 Z- M/ p/ u (heatbugList,! ]( L4 s+ U. x1 x/ ]. ?* S
new FCallImpl (this, proto, sel,/ b: c! `1 T$ U. w
new FArgumentsImpl (this, sel)));3 q: s, m$ q( V( x/ x, {
} catch (Exception e) {
& o6 R4 y! G1 q1 h# z: T9 ^ e.printStackTrace (System.err);! I) L3 @5 v, g2 N& I0 i
}% S5 V2 B# r0 y' o$ b+ h! }
" u4 [4 r3 W2 L
syncUpdateOrder ();
2 b/ |; O7 f3 l6 c3 D9 l
) i; K& Y, P7 J; N, I7 D try {1 U( ~5 \' m. j& l- o( R9 ]; D0 `, P
modelActions.createActionTo$message
) ^4 t/ S. Q* s+ N2 m" L: s (heat, new Selector (heat.getClass (), "updateLattice", false));- f5 b6 L$ Y' {- p! Z
} catch (Exception e) {
" j/ S9 H3 C$ i. o% R- H' F System.err.println("Exception updateLattice: " + e.getMessage ());
& c9 i3 q' k% i" B; x8 x$ c }
, O3 Z$ h. R) m) L: o0 J , p( \' e% Q: o, E1 q3 {: J! u
// Then we create a schedule that executes the
; e$ `' k0 b# @2 C) w // modelActions. modelActions is an ActionGroup, by itself it
* d0 V, c% c$ s5 }, Q8 m // has no notion of time. In order to have it executed in p+ X# F; ^ f! a7 O
// time, we create a Schedule that says to use the
. ~2 |* J5 {& q( l // modelActions ActionGroup at particular times. This3 P# I b# |* v, x$ g4 Y) F
// schedule has a repeat interval of 1, it will loop every6 O% x8 e* g. c: f$ n3 T
// time step. The action is executed at time 0 relative to5 r9 c+ l4 v" }( E& W. a2 q9 @0 _
// the beginning of the loop.
3 ?1 ^% h- H6 a7 n" ~
" w3 m3 ~0 T& E: X // This is a simple schedule, with only one action that is
; z6 {) d( @$ \ // just repeated every time. See jmousetrap for more
& K) B" T; a: M& D( p) F7 W // complicated schedules.
' N- H% @9 O5 x* g. A6 h b6 Q3 V; x ' o# Y0 h/ s* O0 J
modelSchedule = new ScheduleImpl (getZone (), 1);5 ^2 H3 N, l2 e- i H
modelSchedule.at$createAction (0, modelActions);
! C2 ^7 Y0 |5 A$ D' P . c( V% K* u: i
return this;+ O5 X& ?4 T" x# t Z+ ~3 }/ v
} |