HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下: T% S. }+ t8 s. v& y: ~
/ i8 Q5 W2 q$ }% D5 P9 D8 Y9 |
public Object buildActions () {
F, f: h& U. q- \5 p super.buildActions();& u0 q! j/ m# d( Q% b! Q5 Y
; g/ f/ n( ^ |0 `# d" n
// Create the list of simulation actions. We put these in9 N0 G' k2 S4 | w- ]
// an action group, because we want these actions to be4 n' V) w; @7 _; U: j+ T
// executed in a specific order, but these steps should
% U6 C* V$ v& |9 F' o. [ // take no (simulated) time. The M(foo) means "The message" w$ R- n! J6 |/ |7 a
// called <foo>". You can send a message To a particular
) w. B3 F, d( S3 w9 X' m // object, or ForEach object in a collection.
) W/ v4 F0 t/ t' U1 D- g
& @7 f' c; P6 a% V8 p# G) o // Note we update the heatspace in two phases: first run8 Q; e1 y; a" ]- Y! b# r o
// diffusion, then run "updateWorld" to actually enact the: S' R$ q3 A7 z4 i. _
// changes the heatbugs have made. The ordering here is1 l; a1 u* W: @3 I; V8 \
// significant!
( F) d4 B$ T0 m& @
$ J. `) c+ }3 c$ z* h$ p% w9 r // Note also, that with the additional
( \: r( b( x7 b5 c6 @8 m // `randomizeHeatbugUpdateOrder' Boolean flag we can
) W7 A2 S+ L; T2 S: v // randomize the order in which the bugs actually run& |8 a! w3 w4 j* x7 `. a& j
// their step rule. This has the effect of removing any. o5 @+ y* Z P$ j' n
// systematic bias in the iteration throught the heatbug
% P; B& P/ j$ E4 K' u( u. j // list from timestep to timestep: ` I1 o; s5 v7 Z0 `1 L+ a! k
! X' @5 B3 v( }" v6 ^/ B6 c
// By default, all `createActionForEach' modelActions have5 ~, g! q) Q* h- \2 L. Q
// a default order of `Sequential', which means that the; a1 {1 X% |) p2 J+ H- h6 j; @; d
// order of iteration through the `heatbugList' will be- t Q8 |4 ?. S( l* p f3 P
// identical (assuming the list order is not changed6 S; `9 L: v- I. o5 K% v9 q% i! m
// indirectly by some other process).
5 [* m% X- y: |6 x2 G
8 m2 i. t+ n3 v0 C: T) n6 e3 p modelActions = new ActionGroupImpl (getZone ());
; M4 l$ q$ `& p: V* D: S+ d: B3 A" }( o: R6 Z
try {
2 s3 p4 Y% }5 B( l) }( p1 ?. q modelActions.createActionTo$message/ N% x ]: Q9 M# X. w4 E" ]! _
(heat, new Selector (heat.getClass (), "stepRule", false));/ C @1 b8 T- h4 r1 k: G. C
} catch (Exception e) {
; [+ P. |) c8 R7 k# n* { System.err.println ("Exception stepRule: " + e.getMessage ());
. X: P& T2 E; F3 W: O3 ? }0 b+ i' h. N2 [- g
6 n$ Q# C& a9 E4 B try {1 V0 V% c) N" |; C
Heatbug proto = (Heatbug) heatbugList.get (0);+ M6 N B$ N) w" n# n
Selector sel = + n6 D0 L. M) N# t* Z
new Selector (proto.getClass (), "heatbugStep", false);
( T) V; I! n* a1 X6 \) K actionForEach =
* ^+ A" p3 g. u* I modelActions.createFActionForEachHomogeneous$call
# n$ l2 J, A! Z0 L9 F# Q (heatbugList,
$ u1 q9 |2 E6 n* h% V1 j% X new FCallImpl (this, proto, sel,; D3 ^2 u* R& i% h$ j7 Z) j5 _
new FArgumentsImpl (this, sel)));
& k7 R9 u) v, c! \+ c } catch (Exception e) {
# _5 d6 m) C! g& ^: Q5 l e.printStackTrace (System.err);* ]+ {3 ~3 _9 [4 d |4 K
}
% s% n" [) b" b
! w# |- a9 L5 b- p: i7 t; k2 y; @ syncUpdateOrder ();, S4 x" w+ Z0 M. k
) Z+ a1 j5 M3 I H
try {5 Q& [! j7 v" m
modelActions.createActionTo$message / {7 s; Q5 J9 H
(heat, new Selector (heat.getClass (), "updateLattice", false));
& z% v! {4 ^) t" Q } catch (Exception e) {7 D+ _. w5 |. [4 h
System.err.println("Exception updateLattice: " + e.getMessage ());, m+ D- ?1 D0 s; E& j
}" |! P# Q) P' E" \) N$ x
& Q9 `' K4 g2 p, T // Then we create a schedule that executes the
, R% a2 E$ E$ `) C, B- i5 m // modelActions. modelActions is an ActionGroup, by itself it
+ n0 Q6 q) z4 J0 |& ~( ] // has no notion of time. In order to have it executed in7 B _9 m5 Z: j% \$ r# n8 R
// time, we create a Schedule that says to use the
( x. @5 q8 ^3 {* ^& O // modelActions ActionGroup at particular times. This/ n" f8 Q7 F0 E/ W6 I; `
// schedule has a repeat interval of 1, it will loop every1 C' J! |% }0 q( k7 J0 j$ V: D
// time step. The action is executed at time 0 relative to6 g- a4 Y* i7 h- J2 M. M7 s$ Q
// the beginning of the loop.& r" {+ G/ H2 s, q. j4 A( e6 p& |
, G8 C0 G3 b3 ~4 I // This is a simple schedule, with only one action that is
5 c( {: w$ @1 x# Q. E+ O // just repeated every time. See jmousetrap for more8 R$ ~+ ]. ~% K( r x& Y. f) C. D# ?
// complicated schedules. ?) h1 U. R! Z8 k
" p4 M$ {5 N' \6 R5 t* o modelSchedule = new ScheduleImpl (getZone (), 1);
9 X: B1 z& [ L3 ]- f# N8 C* h modelSchedule.at$createAction (0, modelActions);# h+ k: E1 R5 C0 L9 D
7 z$ a( y! ^# ]5 Y2 I
return this; x+ n4 y# n2 H W( `# b
} |