HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:9 `6 \/ k. I; r( R6 M; p
' k; @8 u2 J) M: W public Object buildActions () {
8 r, d4 _3 ~5 p6 E super.buildActions();. _8 d7 j- s2 B8 y% E4 K- k
/ T( }# {: \! p* s/ \ O& Z
// Create the list of simulation actions. We put these in
/ ~4 B8 z! B' F. E9 g1 P // an action group, because we want these actions to be5 `" D* v. `, y% l0 b
// executed in a specific order, but these steps should
, Q3 q. O! S G! f- J% k // take no (simulated) time. The M(foo) means "The message
8 T8 E) V9 r: a; a! r! l D& o) \ // called <foo>". You can send a message To a particular7 ]* R: A+ K+ E4 G2 m. _1 ?# i
// object, or ForEach object in a collection.
. j2 n* o- z. D$ r2 F7 N# M$ _
2 ]/ T* J* B! F+ z3 X7 Z% g' l // Note we update the heatspace in two phases: first run, V( r* R5 j/ j w# ?3 l
// diffusion, then run "updateWorld" to actually enact the
0 [4 w$ a" v& Z+ u4 X) } // changes the heatbugs have made. The ordering here is) t L6 ?" j6 v8 O6 O
// significant!
; ]! z- P6 d& W% l7 K/ {! f" ? 8 a2 j: U# g: y, o2 l4 f( U. ?
// Note also, that with the additional# V; m/ V8 y* v0 C( L$ e1 h3 ~
// `randomizeHeatbugUpdateOrder' Boolean flag we can' S% J% n: T* e: ~( P, f+ q/ u5 h
// randomize the order in which the bugs actually run# n8 T( {4 E. I" Z
// their step rule. This has the effect of removing any
( N( e- n8 {8 j: E // systematic bias in the iteration throught the heatbug
; x+ b V- M. R9 o8 M# s) O // list from timestep to timestep
- ^( z& M' L+ V9 E/ Q; d 1 d! I2 i3 B6 J8 r
// By default, all `createActionForEach' modelActions have/ E9 R) F; n3 F& {; ^
// a default order of `Sequential', which means that the( G; O8 S! a1 O
// order of iteration through the `heatbugList' will be
: k+ H! P. G% _2 O c // identical (assuming the list order is not changed: ?( w! ^, c( `) N9 g2 B0 M
// indirectly by some other process).! V2 Z+ j/ O* Z7 G+ W, _
4 \/ T7 ^8 K! g5 ]1 q( N
modelActions = new ActionGroupImpl (getZone ());
- P7 P, [) A3 ~# r! ]8 b& h- m, T3 {% W) }
try {
7 Q$ a" z" e7 Q4 t7 D* W modelActions.createActionTo$message
+ D$ V/ M3 M+ g: A (heat, new Selector (heat.getClass (), "stepRule", false));
. u @2 j# G8 o# ~ } catch (Exception e) {
1 k' n9 d( }8 a5 |( s' N System.err.println ("Exception stepRule: " + e.getMessage ());
; K8 m+ e5 t) T# a: ]7 y0 n }
" N9 Y3 U( v5 M: y
: f2 F) i+ y: ? try {
' x9 j6 l) u# U% ^7 E Heatbug proto = (Heatbug) heatbugList.get (0);
1 ~, a6 z: \: r1 l3 e Selector sel = 8 V5 C# } J3 C) n9 O8 ?
new Selector (proto.getClass (), "heatbugStep", false);- K: U S5 _, q4 Z
actionForEach =' l& |6 K* y1 ~1 a) Q; J r
modelActions.createFActionForEachHomogeneous$call
0 S* G7 K" t" I: c (heatbugList,
" w9 c1 G8 ]8 L5 G new FCallImpl (this, proto, sel,1 o$ @' _2 _" l5 j2 t ^
new FArgumentsImpl (this, sel)));' L& `0 ?3 ^; a7 N( Y
} catch (Exception e) {0 O2 n D6 M' c) U. x! E( G5 [
e.printStackTrace (System.err);1 `5 X5 b4 N8 b$ Y- v
}$ @$ Y* v$ I# G* K- b
# Q" Z7 D/ b- l# {: j+ W/ g
syncUpdateOrder ();
# L! Q( F4 t ^8 c! G% M j
; v1 R3 M( j% C/ i8 L) U try {) B. V6 h% q* o- G1 u; k1 \. H
modelActions.createActionTo$message
+ _3 x6 ^$ o* K. S, F4 M% E (heat, new Selector (heat.getClass (), "updateLattice", false));: N# B* @- `2 b! j
} catch (Exception e) {8 e n# n" g- I% S9 `! W
System.err.println("Exception updateLattice: " + e.getMessage ());) @% c H" u$ t
}$ Q# f. o. r3 E$ J
- y. X$ @! |2 V; u // Then we create a schedule that executes the/ ~6 O( ^ {5 K4 b
// modelActions. modelActions is an ActionGroup, by itself it% c: k: k% [% B5 }8 y# ?' j
// has no notion of time. In order to have it executed in
; M6 N1 P$ g. F- \ // time, we create a Schedule that says to use the
- d: y4 i2 ~4 N( ~ // modelActions ActionGroup at particular times. This* h3 j/ c L' w
// schedule has a repeat interval of 1, it will loop every! Y$ s+ o" P9 w* Y8 \
// time step. The action is executed at time 0 relative to
0 b7 H" H& O3 {" `; [0 r4 J3 R // the beginning of the loop.+ P8 _( m8 \. Z) z6 x" X
5 ^1 V6 \/ J6 p2 J6 Z l6 Y9 X% K // This is a simple schedule, with only one action that is3 b7 {% y; U) Y! D
// just repeated every time. See jmousetrap for more6 v2 ]# E1 O1 L2 F5 j6 u
// complicated schedules.
6 x% x" Y0 R$ [& G! o$ [3 G' \ 2 V1 k) T4 L! V1 A
modelSchedule = new ScheduleImpl (getZone (), 1);" b1 v* j( A [, a5 c6 u
modelSchedule.at$createAction (0, modelActions);' \; U/ V7 V& v8 m& n5 D
+ k7 i9 e7 |, `; n( C: ?
return this;. c: d1 W9 O+ d5 N# }
} |