HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:1 |" s7 o; h& e; M. Z
. S' A! Z1 G2 _' T0 l; |4 I4 _/ S public Object buildActions () {
# A* a! I& A; U) `8 W, x super.buildActions();
9 m$ R# M" g1 O* ~2 Q2 ~
6 z4 `9 l9 \4 U1 S* \ // Create the list of simulation actions. We put these in
a/ w3 f g$ L* j' z; k // an action group, because we want these actions to be8 L3 H N0 @$ f- S
// executed in a specific order, but these steps should
& F7 E' _2 W5 a' s) _ // take no (simulated) time. The M(foo) means "The message5 c2 X5 E; K8 q& d9 D8 F p [ R% N
// called <foo>". You can send a message To a particular2 N7 \1 g3 W, v% M7 m# B
// object, or ForEach object in a collection.
, |, }. M/ n8 N/ T6 y' {
( Q. R9 D. R2 x9 \% A- ]3 i6 @& h // Note we update the heatspace in two phases: first run
# ~# h7 s l9 S0 m; P" y s o1 R // diffusion, then run "updateWorld" to actually enact the1 |; x$ ?$ e o5 t6 c
// changes the heatbugs have made. The ordering here is
. M$ D2 C' _7 Q) h // significant!
1 F/ @. Q. K- Z6 Y7 c - @4 H4 @4 U9 J" C- f, d% I
// Note also, that with the additional+ \' C9 v X! B$ A7 G8 {8 ?5 L
// `randomizeHeatbugUpdateOrder' Boolean flag we can t5 o" X6 o- H# v& G
// randomize the order in which the bugs actually run2 ?) F ^3 z- w/ z1 P
// their step rule. This has the effect of removing any3 g U1 q4 j$ d; L$ o: k
// systematic bias in the iteration throught the heatbug
& c7 G) d3 B( B // list from timestep to timestep
8 C0 S+ L; e: s: t' z ) n& A/ _ K; ]
// By default, all `createActionForEach' modelActions have
% O) Z& k. z. Y! { // a default order of `Sequential', which means that the
8 `3 x6 B1 ]! i# b3 R, h // order of iteration through the `heatbugList' will be6 j4 R& A1 E" y0 E6 H8 E' [
// identical (assuming the list order is not changed
1 r1 X; {7 X. Z- L; O2 q& e // indirectly by some other process).
3 a, D7 A) E6 l+ l 4 \! ^7 x" Q3 v8 e- F$ G4 H
modelActions = new ActionGroupImpl (getZone ());# j2 B' ~* e" y
) _ |0 X1 `5 p R0 c+ N
try {
% A& m$ b) F! J modelActions.createActionTo$message
' j! c1 t( A- {! F% d (heat, new Selector (heat.getClass (), "stepRule", false));
9 Q' @5 P' Z( X0 ~( L7 Z6 q% I- U } catch (Exception e) {
) J1 p! T& I& v' F) |0 `& D System.err.println ("Exception stepRule: " + e.getMessage ());8 g# a$ S3 g- v
}0 v* V( N* s% t- k6 B/ o: \/ q
2 ], F' Q/ {6 F
try {: f+ B5 B/ E7 V2 `2 m8 l
Heatbug proto = (Heatbug) heatbugList.get (0);) S* l$ Z. r- A( F5 ]: | j1 B
Selector sel = + y+ y0 f. Y+ N+ T5 Z! G$ y
new Selector (proto.getClass (), "heatbugStep", false);
* C: a# K# ?8 }; h( z0 @ actionForEach =* C/ V0 i/ {' d) Q
modelActions.createFActionForEachHomogeneous$call
9 t5 t# s) G! Y0 W3 p3 c: |4 G (heatbugList,
1 W( l, _0 W6 A( t& f9 }! w9 k new FCallImpl (this, proto, sel,+ _/ M7 s+ B j# ~+ T# [
new FArgumentsImpl (this, sel)));
2 o' w) F" o( m) G } catch (Exception e) {/ ~1 U, T( J2 F& _
e.printStackTrace (System.err);* o, e Z, p! _+ K: k9 t
}" k, b9 P5 D% w. j2 v& U
+ h$ U+ C0 a7 x) i; r syncUpdateOrder ();! f1 _, ?" x6 E6 U+ @2 s6 d/ b# U7 f$ I
' c- [- W1 ?* b2 w! ~8 g
try {% s- x3 `0 Q/ K2 J# X
modelActions.createActionTo$message
- i, H6 |7 M3 u6 ~+ H0 m (heat, new Selector (heat.getClass (), "updateLattice", false)); \" n* ^0 k3 E7 D' F
} catch (Exception e) {$ G% j& ?. o& w/ p! s3 f2 m
System.err.println("Exception updateLattice: " + e.getMessage ());
& j4 u q7 [* g& N }
* z. G) b$ P- z" g% J1 [# I2 D
! p- [& Y6 {3 H# U8 r9 V X1 k% V w // Then we create a schedule that executes the+ @4 Y+ U a% [& N% G+ ]
// modelActions. modelActions is an ActionGroup, by itself it
. k8 r/ |7 X* l% a5 q) y // has no notion of time. In order to have it executed in! M5 o A N( U; }$ `# W5 d
// time, we create a Schedule that says to use the
3 [2 o0 i* Y$ H; n // modelActions ActionGroup at particular times. This
9 x) U# B& x$ i) f; b // schedule has a repeat interval of 1, it will loop every
z3 }' {. O. a5 ]/ i; T // time step. The action is executed at time 0 relative to
5 c" d; a: |" n1 z9 s* v0 S // the beginning of the loop.
/ \% @0 q5 ` G1 z( Y. V3 y9 n4 m
8 }# i$ @4 Z3 T+ N7 A // This is a simple schedule, with only one action that is2 v' r$ B, J; o$ S' G5 Z
// just repeated every time. See jmousetrap for more
! p6 U2 O+ v3 A& f# X // complicated schedules.
6 }/ V8 Y/ V. F! [3 e# W. }7 y
; X' o( e, p( R% K modelSchedule = new ScheduleImpl (getZone (), 1);
5 q' N( i, z: H& S! [0 ^ modelSchedule.at$createAction (0, modelActions);
( c5 S k5 [- x; `. j0 P& |4 m: M+ x ( V: ~/ p4 }9 D; I+ K! O
return this;* c" }6 {! L6 g: D$ E7 A7 P! B& x
} |