HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:8 |. V- f- P) r) M
5 x7 R, Q9 J# X+ F7 o+ J- t$ @$ N public Object buildActions () {
) v# e1 ~8 k, Y( a9 |2 d* i super.buildActions();
) D( m% u+ R1 X6 f ) C' ]: S5 X* h, {9 U. e
// Create the list of simulation actions. We put these in; T- L; Z- z4 i, S' `7 A
// an action group, because we want these actions to be
* R; c5 `& q* I& q0 j$ E6 Z+ v // executed in a specific order, but these steps should' s) M' ~/ m- m2 O' i8 J* {
// take no (simulated) time. The M(foo) means "The message( y/ c7 ?' L# }& c
// called <foo>". You can send a message To a particular
; v) q' V+ P1 _/ H0 a1 P" y7 @ // object, or ForEach object in a collection.$ I5 C- J0 G! w+ Z& w6 M
3 S- K7 K: X$ Q7 i$ H( q3 V0 H // Note we update the heatspace in two phases: first run
' e' ]# j8 u5 c // diffusion, then run "updateWorld" to actually enact the% x4 c" l: Z' e, {' z
// changes the heatbugs have made. The ordering here is
$ |, ?$ l A( C9 C/ ^! G // significant!- |+ V+ g, N/ O8 @+ o {
+ v* q5 w, O, u% h2 @
// Note also, that with the additional0 a% Z6 E, S* Q) w6 n0 U
// `randomizeHeatbugUpdateOrder' Boolean flag we can. K$ r( V) q g" x) c
// randomize the order in which the bugs actually run* I( I8 M) ~" W0 N( \! F, e L
// their step rule. This has the effect of removing any
$ |% n. @0 w% N7 Q // systematic bias in the iteration throught the heatbug/ E. r5 B# q( E0 e
// list from timestep to timestep6 x( |7 W" i$ N9 @! L( M; ]
@( ~' p. q% y
// By default, all `createActionForEach' modelActions have- {) K. ]2 e. T% {) d7 F/ F
// a default order of `Sequential', which means that the) `, J# [3 Z+ H4 B2 E
// order of iteration through the `heatbugList' will be
' R: `; _4 `- ?4 F8 x // identical (assuming the list order is not changed
7 g' N' Q/ f+ I. j3 |' y5 _+ \1 {$ u // indirectly by some other process).# e, j4 j5 ^# E4 s
1 l, I" P: g4 h+ @& J
modelActions = new ActionGroupImpl (getZone ());
, i% y" G d6 N* b$ U. w$ x; t" T. A) M( T" N5 S
try {
3 i7 B2 {3 Y4 i$ N. u" |+ T modelActions.createActionTo$message) s0 R- P$ d# Q" l2 @! O; p4 j
(heat, new Selector (heat.getClass (), "stepRule", false));( ~6 z x6 @/ f% F# _
} catch (Exception e) {
+ c# ~5 y( g% W$ I4 E1 Q System.err.println ("Exception stepRule: " + e.getMessage ());
- H `' e# ~5 ^, J* ?! ?6 \ }- |( u. U% t5 I7 B+ B
3 m8 W0 x! e" O2 u7 A
try {: o6 L2 {0 h4 e# ?) E
Heatbug proto = (Heatbug) heatbugList.get (0);
9 D; j8 d5 S* [: h! Q# J# M Selector sel = ' Z) y: O \' ^5 P( }. @" Y
new Selector (proto.getClass (), "heatbugStep", false);
, D1 s4 z6 i% {9 P! E. I* @' {# S actionForEach =3 V1 V+ {3 p5 F5 C( j& u3 k
modelActions.createFActionForEachHomogeneous$call
% x% b# y% U) ] g/ X7 s (heatbugList,4 R2 V: b2 f% e% \ D
new FCallImpl (this, proto, sel,
' h8 j C8 i$ d2 B9 ? new FArgumentsImpl (this, sel)));1 h' S+ ]/ B# X7 w) f
} catch (Exception e) {- C+ c9 D y/ b% }# s
e.printStackTrace (System.err);
' F7 y" }. W, U, i }4 w1 [* V5 T5 `! @; m
2 `7 x$ @0 b- n2 z syncUpdateOrder ();
5 C; o6 S8 a: r' j; t( H6 Y7 |9 f3 |: z A+ ~* E
try {
8 d% v- @% ^3 R6 k' P$ Y2 x modelActions.createActionTo$message 5 E& K: @) u% x7 ^# i
(heat, new Selector (heat.getClass (), "updateLattice", false));
. ?6 H% a- J* x; B/ ~ } catch (Exception e) {
" a1 _' W5 v; h q5 T# ] System.err.println("Exception updateLattice: " + e.getMessage ());
# y3 [' v. ]2 G: v/ J/ v# f }* ]9 ]/ V+ @. t5 h, w8 Z- x
D B' X3 L/ S
// Then we create a schedule that executes the* ]" {8 |# s/ {+ A) P
// modelActions. modelActions is an ActionGroup, by itself it3 n6 m) O( ~* u+ \7 U8 m" n5 C
// has no notion of time. In order to have it executed in' B. F& A# i7 i& g" j
// time, we create a Schedule that says to use the
4 Y5 B( @! j1 C2 d& L$ U // modelActions ActionGroup at particular times. This* Z% R( {7 T/ t! p! a4 S5 a: K
// schedule has a repeat interval of 1, it will loop every& j) ` \- ~6 Z
// time step. The action is executed at time 0 relative to
8 P: a8 ^! @4 S, w$ X* ` // the beginning of the loop.
# e$ {- a: B+ q* C; P( q; ^2 e" D* R( {& G2 w& H( q
// This is a simple schedule, with only one action that is z X8 o* j) p( W9 P/ l
// just repeated every time. See jmousetrap for more
7 e; X' U: T: V& L Q // complicated schedules.
% R5 q& o% Q9 Y. i
+ d& I% X _* A+ T modelSchedule = new ScheduleImpl (getZone (), 1);, o$ x* g0 Z$ h: d
modelSchedule.at$createAction (0, modelActions);
3 r* c5 j, ~; X 2 J& m& }7 }! m& U5 G6 G* C1 {
return this;. h( \# K: W A9 ^" n* B7 X
} |