HeatbugModelSwarm中buildActions部分,3个try分别是做什么?查了下refbook-java-2.2,解释太简略,还是不懂,高手指点,谢谢!代码如下:4 I4 w5 [/ W& U
% M7 e: u) Q% ^ b, d' q) X) U
public Object buildActions () {1 r( M% o. P/ m- p* s
super.buildActions();$ h7 L+ Q; L; U( t
9 v+ j8 e+ x2 N- ~* d
// Create the list of simulation actions. We put these in6 p& H4 p: Q1 I9 h& ^# o1 j
// an action group, because we want these actions to be; w/ Q2 i- Q/ o, ?$ l0 |
// executed in a specific order, but these steps should& ]# e- @! Y9 e8 Y. N% ]9 U- H+ J
// take no (simulated) time. The M(foo) means "The message
6 [6 y2 H h# K* b // called <foo>". You can send a message To a particular
. F( U4 B+ C7 o/ }) R4 E7 g4 ?& ?6 Y // object, or ForEach object in a collection.
- u6 F" U1 K2 p7 e: n" \* T
x& S& w; X( q- r; x; a // Note we update the heatspace in two phases: first run3 H+ v9 A/ b8 n# j* d. a9 c
// diffusion, then run "updateWorld" to actually enact the
7 x: h6 R6 L, ~* t. F$ r // changes the heatbugs have made. The ordering here is
2 C" `8 J6 Q2 R1 Y" J+ V# g2 [ // significant!
! m7 P% d- J _' z" o3 c
! I7 G6 V! F$ w2 s // Note also, that with the additional
( [8 F% Y) v% o4 q3 ~ // `randomizeHeatbugUpdateOrder' Boolean flag we can
& L O" z7 J6 ]6 p4 s // randomize the order in which the bugs actually run
' L/ a' r3 q2 R( P1 }$ i. a' \1 G // their step rule. This has the effect of removing any
" u5 q$ {- S! B, q) C: K // systematic bias in the iteration throught the heatbug
a: D1 D4 d$ ^* [ // list from timestep to timestep: ^! ~# S9 _; u
8 w2 q# k: d% l+ J
// By default, all `createActionForEach' modelActions have8 k- E- I6 x e5 t
// a default order of `Sequential', which means that the9 n1 ]& Q" K2 P5 N K
// order of iteration through the `heatbugList' will be
' z# {6 ?, |! U4 ^4 ~. o+ r) M // identical (assuming the list order is not changed7 O! \7 T1 a! x! O
// indirectly by some other process).
1 u0 Y; g9 O; f$ j4 n
: k$ Y! ?$ s7 D. q' X/ h+ B modelActions = new ActionGroupImpl (getZone ());: [; p: C; j8 _
% i8 U3 k0 `- U6 n# F6 j3 @$ V" x
try {
* `6 w1 x7 T* N! T0 S# | _ modelActions.createActionTo$message
9 L; u6 e# v7 b- F A8 k (heat, new Selector (heat.getClass (), "stepRule", false));3 X7 i- x C, o. I# k
} catch (Exception e) {( X; l+ }1 I0 `0 V) j
System.err.println ("Exception stepRule: " + e.getMessage ());3 @; \3 ] R$ _1 k+ Z* _
} }2 n/ I* |' o$ r6 S
# @2 |4 |+ A6 u! |) e! B
try {
& X6 ]8 {) O: Y Heatbug proto = (Heatbug) heatbugList.get (0);
$ r) n2 S f8 @ Selector sel =
) ?; w3 v; ]+ Y+ h# C" q7 T. }; u new Selector (proto.getClass (), "heatbugStep", false);; U7 K) c9 s6 O: M2 W% ?4 F0 h) w
actionForEach =
2 O, M: W% t* e! _5 x# Q modelActions.createFActionForEachHomogeneous$call# C8 d" b3 B/ B
(heatbugList,
2 v: e0 r: J- ^ F J' w new FCallImpl (this, proto, sel,2 K6 T; f! P+ R% k- S/ ?- X
new FArgumentsImpl (this, sel)));
/ ~3 J8 a9 N6 B& @; W e } catch (Exception e) {8 Q0 K. o9 t9 c
e.printStackTrace (System.err);1 ^' o8 I& M7 L: l5 q6 @# o3 f" d$ e, I
}9 T( X8 X; S- f
# a: r4 d* e& e2 ^1 d9 P syncUpdateOrder ();0 u5 M9 y/ ~; a' C4 j
! i. C' n1 u# M$ T
try {
9 i& {, ?$ Q- @7 Q modelActions.createActionTo$message * q. _1 u) |" Z5 J
(heat, new Selector (heat.getClass (), "updateLattice", false));2 L: ~8 \1 x, L r) R6 d+ X' o! V
} catch (Exception e) { A. G+ A1 N5 R8 O; f# K
System.err.println("Exception updateLattice: " + e.getMessage ());. w4 J' f* L8 E8 J! _
}3 }* C# E3 _% E: e
+ f! W3 _7 m& f6 n+ l Q4 s; O // Then we create a schedule that executes the
3 M! [8 r Q! v7 m X; K // modelActions. modelActions is an ActionGroup, by itself it
% X. H" D1 b6 D# ] D // has no notion of time. In order to have it executed in
0 s3 i9 Z5 Z9 ^* H- A // time, we create a Schedule that says to use the; }: j5 [) R( x) N" H
// modelActions ActionGroup at particular times. This0 }( A( g- W3 z5 l9 e4 \
// schedule has a repeat interval of 1, it will loop every: ^' l3 k. m+ e% a7 n1 u& M
// time step. The action is executed at time 0 relative to' `1 a9 U5 i" a m* M+ ], w
// the beginning of the loop.
: I' l Z8 r* g' i9 v& G- e7 Q+ z" t6 m5 _
// This is a simple schedule, with only one action that is
% \# \' q% K4 P( x! a8 H // just repeated every time. See jmousetrap for more
" _4 J4 F; ?- ~7 i // complicated schedules.
2 D) u N m# z/ F: F8 y. G( T' N, u / f1 _2 N4 A8 f4 y
modelSchedule = new ScheduleImpl (getZone (), 1);
& t1 R, Y3 u6 J% E D6 ~, H modelSchedule.at$createAction (0, modelActions);
+ y; ]" U( N; u* w+ r
! @. K: ~% E% _* g return this;% [3 x% q1 o; [( `" ]% m' A
} |