HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: t" I. ]2 ?. z$ _8 B: N7 u
. n2 [( S' G/ T6 v2 H- D/ m; N public Object buildActions () {) ]: o' m' b V6 a
super.buildActions();
" y( h) R/ O: z' j
, z# B8 Y. _$ H, u // Create the list of simulation actions. We put these in
; o( f% S; g& H$ P7 Y) J; U // an action group, because we want these actions to be
9 ]9 f- w# y X' e3 j // executed in a specific order, but these steps should
7 w: ?/ M, Q" u |: a: P // take no (simulated) time. The M(foo) means "The message0 C# i6 S! ^+ P. Y7 C1 V
// called <foo>". You can send a message To a particular
# ^! B; m" W; ^ // object, or ForEach object in a collection.5 J( b! h7 G; I( ]: ~
; z# V* \; h, w. Y! }+ S
// Note we update the heatspace in two phases: first run
7 P/ v, U! t% N. X7 ?$ A // diffusion, then run "updateWorld" to actually enact the" O6 F/ ~) g0 F4 o# v1 f# w6 \
// changes the heatbugs have made. The ordering here is
+ O9 _: {; U. m // significant!
9 c. f: |: C! B# a/ [' ~2 _
' E" b/ a% B- ~, j; n4 k5 M, G$ A // Note also, that with the additional
4 l' j- x# v7 A! e9 b9 ]5 Z // `randomizeHeatbugUpdateOrder' Boolean flag we can) K0 ]- C2 r' N
// randomize the order in which the bugs actually run
. a# l: I" K. `" h# U // their step rule. This has the effect of removing any
6 X+ @. i+ ~' C/ f // systematic bias in the iteration throught the heatbug6 d# q- j" ~4 h
// list from timestep to timestep
: W+ V5 k( |2 B
+ n `# F, @# u# l6 Q; @5 a3 y // By default, all `createActionForEach' modelActions have
6 c" ^: f% e: s g! ^6 u& y# S' N // a default order of `Sequential', which means that the; Q# i! j8 R; E0 K
// order of iteration through the `heatbugList' will be: o9 j3 ]$ q. M1 \& E$ Q% D8 \4 ]9 U1 w
// identical (assuming the list order is not changed" b7 M# S; L% a6 `8 T" K+ a1 k e
// indirectly by some other process).' f. ~8 ^: q6 m( c& D
2 |9 i) V9 e7 r( y) D" p
modelActions = new ActionGroupImpl (getZone ());, ]8 {3 I3 l. K# q% I% z
- g( q2 C/ E$ D# t# [
try {
+ D7 h* ^; n8 V6 X+ S! E9 T modelActions.createActionTo$message7 t1 R% R) O4 f. S/ ~) t x8 [" B
(heat, new Selector (heat.getClass (), "stepRule", false)); A3 n# x# P' r9 {. O! R$ S5 u
} catch (Exception e) {2 B5 v5 o# o: _- @, Q* l
System.err.println ("Exception stepRule: " + e.getMessage ());9 m) }( \/ f8 W9 A' I
}
]5 V$ j8 V- j- q" y d; d1 v ?8 \! J( ^5 ?7 k; p( j
try {9 S; E1 i3 O9 N
Heatbug proto = (Heatbug) heatbugList.get (0);* l' z& H+ d' {: D; G9 [* ?8 ~
Selector sel = - h3 H; _! {2 X
new Selector (proto.getClass (), "heatbugStep", false);
, T) T" ~4 l7 S; ~" b+ P actionForEach =
' y3 |9 w( [% l% {- h7 @1 I modelActions.createFActionForEachHomogeneous$call4 ]2 }; b- J# M- g( m
(heatbugList,
7 N( e' x0 g, F new FCallImpl (this, proto, sel,, L9 ?1 \ @/ |6 b$ h6 ]
new FArgumentsImpl (this, sel)));
5 \# e) y# M Q: p. Y1 q! `5 | } catch (Exception e) {
x( K3 ~) J! Z5 t3 d' p! o e.printStackTrace (System.err);' C* N: y: b6 _
}
3 V+ A* u9 |# d" i$ s 8 W) n6 |. S0 v. p: V& K* n, \+ T$ N
syncUpdateOrder ();) y- d: o& g! }3 l3 e
+ Y0 v0 l3 c) ~$ Z
try {7 h( x3 R5 U$ ?8 _0 m
modelActions.createActionTo$message & W, u0 i! G9 p3 h8 G# O+ }
(heat, new Selector (heat.getClass (), "updateLattice", false));7 s1 R) W! Q0 {
} catch (Exception e) {- V6 ~: S% W1 p9 _& M- [) k
System.err.println("Exception updateLattice: " + e.getMessage ());3 J# ~9 b2 D! R8 g9 ?+ `# @4 r
}
- T3 h8 c9 S3 r, T& Q' W
- @ H+ u" w! V% k% Y // Then we create a schedule that executes the" {$ _5 @& H' X5 m, [) a3 C
// modelActions. modelActions is an ActionGroup, by itself it
) J& H" n! T. J // has no notion of time. In order to have it executed in: A: f, o @, P5 N2 l( l: x
// time, we create a Schedule that says to use the
1 ]( h# u0 M6 |% f0 R$ {' O // modelActions ActionGroup at particular times. This
$ U! G% Y# B# m$ [ // schedule has a repeat interval of 1, it will loop every
' [" W% i3 a- m" v o) H // time step. The action is executed at time 0 relative to
; y! @3 [+ R! F- _ R. R; { // the beginning of the loop.
* ]9 |6 M8 ` [4 z8 |6 ] w" R/ U/ n6 P0 {
// This is a simple schedule, with only one action that is$ C" N9 n' a) U' @2 Y" @& _
// just repeated every time. See jmousetrap for more
% M4 `' l/ I7 N$ g) ^ |1 t // complicated schedules.
$ ]5 n" T* A5 x! _- i3 C( g( ~
S+ [9 u* p, N/ @; }" n& [ modelSchedule = new ScheduleImpl (getZone (), 1);
: Z* M6 M" I p' A t0 v modelSchedule.at$createAction (0, modelActions);) y) }/ \- ^* D9 V. ]% P4 r0 X# A) Z' U
1 S7 C* B8 G$ V8 L return this;
' A4 S6 V' \# i( L# `, B0 Z! { } |