HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:! b/ u6 ^' B+ B+ ` I. V S
7 ?- t- T a5 }
public Object buildActions () {& S6 b6 `& z( z) G) L! f. J; b
super.buildActions();* u" `! x7 y2 c7 H1 R5 E
0 K5 y; B0 R' j: m& _; I // Create the list of simulation actions. We put these in2 N; f+ d- H7 A* O$ [: g2 E3 a
// an action group, because we want these actions to be
! c+ H @' y8 }, Y# N9 K // executed in a specific order, but these steps should
2 S7 R1 l1 Y8 g8 f/ ]- g) Q4 x& r$ U // take no (simulated) time. The M(foo) means "The message- F0 w/ M1 f- ^9 J4 E) x
// called <foo>". You can send a message To a particular
1 h5 R$ L) M1 P. ?$ D- _ // object, or ForEach object in a collection.4 w6 E. G6 L5 A- P* M6 B) F
! L$ o% n5 x8 v% F$ O // Note we update the heatspace in two phases: first run
, a" ~% X8 u& a2 X( ~$ E // diffusion, then run "updateWorld" to actually enact the
+ f- v5 g) g$ _ // changes the heatbugs have made. The ordering here is$ V7 G% U2 |/ o& T. k# i$ Q# K
// significant!
2 q! c8 r& w+ Y$ n: U
$ o; J6 u% G" X8 i" F // Note also, that with the additional9 V( \ M2 @, E) r. r
// `randomizeHeatbugUpdateOrder' Boolean flag we can+ e j! ]- |. x# N+ D
// randomize the order in which the bugs actually run
/ X1 t5 f# K \+ u, p // their step rule. This has the effect of removing any
0 }8 N. w% m; R- }* Y0 s // systematic bias in the iteration throught the heatbug$ }1 y. `/ N+ V0 z n: I5 b
// list from timestep to timestep$ h, w( h2 G6 _9 F/ Y$ V# j5 _ c
! \( u6 z* [( K o$ [" W // By default, all `createActionForEach' modelActions have+ a; q* ]1 ~4 A0 D# T
// a default order of `Sequential', which means that the
2 `, S. B; r/ W3 K$ i5 I+ Q // order of iteration through the `heatbugList' will be
& F4 g: e" K2 ~% g* b1 Z // identical (assuming the list order is not changed# h' y# |) ~0 V! T% P
// indirectly by some other process).
' G3 M. Y- F+ Z4 G: B" i + E& g/ X: ^7 l2 ^' N7 o
modelActions = new ActionGroupImpl (getZone ());
/ d3 K) d( a: U1 H$ C
( W6 M: S, c/ {( Z# `+ k3 B try {
3 }2 g9 o6 X; l8 t' B2 I6 ?$ i! g! ?7 _ modelActions.createActionTo$message
) Z0 Y& a. A2 v (heat, new Selector (heat.getClass (), "stepRule", false));
$ u8 x( B5 [& z } catch (Exception e) {/ N: e! [- S; p+ \
System.err.println ("Exception stepRule: " + e.getMessage ());
% Y5 k* ?' \+ R0 V; d }
0 ` h" b6 V2 g4 }
/ \5 _. A0 E8 M8 x, X/ Y* {, l' x try {- e" b+ S$ ^" r+ f
Heatbug proto = (Heatbug) heatbugList.get (0);; z) S4 k6 ^5 b# `
Selector sel =
( W. d' `" @! W1 M new Selector (proto.getClass (), "heatbugStep", false);1 z; |1 C9 T/ |/ m! s, I
actionForEach =
4 P {* R R% ^& U' E5 W1 R modelActions.createFActionForEachHomogeneous$call
6 D1 d5 G9 M" r6 e( E (heatbugList,
T1 M g) W7 X+ O% z( z4 {6 H new FCallImpl (this, proto, sel,
8 P0 }9 s+ ]! o. z9 l* u new FArgumentsImpl (this, sel)));
1 D# Q1 _# o% {/ B8 _ } catch (Exception e) {( V3 m7 }) y2 H, v
e.printStackTrace (System.err);
( }5 k) }5 b- v% N. C5 S( y/ \ }, t2 M% z6 K% r( `# g: U: V) p
& S1 Q& G* d% O! `0 w) }8 c syncUpdateOrder ();
. S/ T# p, V! d b# t, B. f1 u4 `0 S, S
try {; ?3 L7 {- x L3 r% p# |& q) _6 I
modelActions.createActionTo$message 5 N+ D( r% R- ^) c& ?) h* C8 o
(heat, new Selector (heat.getClass (), "updateLattice", false));
, ~6 Y, B$ ?0 t } catch (Exception e) {" Q- P$ u+ |8 {; f" q3 r! m
System.err.println("Exception updateLattice: " + e.getMessage ());8 `$ k8 Z9 E+ f3 G( i8 [0 o
}
! X) h! Q4 p0 b 0 Q2 G% e* |: _( m
// Then we create a schedule that executes the
5 g5 Y4 W( C! _! t( G // modelActions. modelActions is an ActionGroup, by itself it6 x: A3 V5 l) X1 }: n
// has no notion of time. In order to have it executed in
& P" _1 `% a+ ]& M' c! h6 o // time, we create a Schedule that says to use the
& K8 I3 r+ V1 t // modelActions ActionGroup at particular times. This+ k% [7 R N' i" N: b* r- ~
// schedule has a repeat interval of 1, it will loop every9 t/ A4 B9 `+ k
// time step. The action is executed at time 0 relative to( T: i+ z6 }1 s# y/ f/ ^6 R, P Y
// the beginning of the loop.
; _ |( T$ \! {! R6 n0 w" }' a
2 Z4 l G2 {# R // This is a simple schedule, with only one action that is' r: {/ J2 V" F1 E8 Q& e/ u
// just repeated every time. See jmousetrap for more
6 r! ]6 @6 y1 @; R- a5 x0 o9 ? // complicated schedules.: T) t: a6 ~3 k
# g/ T% |9 |! Q. D: a; W
modelSchedule = new ScheduleImpl (getZone (), 1);5 r' {* B$ _: d& C7 W
modelSchedule.at$createAction (0, modelActions); Z7 n0 ^6 r# z8 W* y- Y* N
1 T. @$ \7 |! L' ]2 H. C
return this;
( B9 R+ ?: W) v9 A: x } |