source: trunk/src/org/expeditee/agents/DisplayTree.java@ 25

Last change on this file since 25 was 25, checked in by ra33, 16 years ago

Added forced garbage collection into DisplayTree when memory gets low.

Removed another method which was for dealing with frameset names ending in a number

File size: 4.2 KB
Line 
1package org.expeditee.agents;
2
3import org.expeditee.gui.DisplayIO;
4import org.expeditee.gui.Frame;
5import org.expeditee.gui.FrameGraphics;
6import org.expeditee.gui.FrameUtils;
7import org.expeditee.gui.TimeKeeper;
8import org.expeditee.items.Item;
9import org.expeditee.items.ItemUtils;
10import org.expeditee.items.Text;
11
12/**
13 * Displays the Tree of Frames starting from the given frame. The delay between
14 * frames can be adjusted, either per-frame with the
15 *
16 * @DisplayFramePause tag, or mid-stream with the
17 * @DisplayTreePause tag, both tags should be followed with the desired delay in
18 * ms. The default delay between Frames is 50ms.
19 * @author jdm18
20 *
21 */
22public class DisplayTree extends TreeProcessor {
23
24 // delay between frames, in ms
25 private long _delay = 50;
26
27 // The shortest delay between frames
28 private static final long TIMER_RESOLUTION = 10;
29
30 private Runtime _runtime = Runtime.getRuntime();
31
32 private long _timeRemaining = 0;
33
34 private int _frameCount = 0;
35
36 private TimeKeeper _timer;
37
38 public DisplayTree(String delay) {
39 super();
40 try {
41 _delay = Long.parseLong(delay);
42 } catch (Exception e) {
43 }
44 }
45
46 public DisplayTree() {
47 super();
48 }
49
50 @Override
51 public boolean initialise(Frame start) {
52 _timer = new TimeKeeper();
53
54 // push current frame on to back-stack
55 DisplayIO.addToBack(start);
56
57 // MIKE: Doesnt think the line below is needed
58 // update the delays (if necessary)
59 // delay(start);
60
61 return true;
62 }
63
64 @Override
65 protected void processFrame(Frame toProcess) {
66 long freeMemory = _runtime.freeMemory();
67 if(freeMemory < 100000){
68 _runtime.gc();
69 FrameGraphics.DisplayMessage("GARBAGE COLLECT!");
70 } else if (freeMemory % 10 == 0)
71 System.out.println(_runtime.freeMemory());
72
73 FrameUtils.ResponseTimer.restart();
74
75 // ignore loops
76 if (toProcess != DisplayIO.getCurrentFrame())
77 FrameUtils.DisplayFrame(toProcess, false);
78
79 // parse the frame for any pause settings
80 delay(toProcess);
81
82 _frameCount++;
83 }
84
85 /**
86 * Parses through the given Frame to find any
87 *
88 * @DisplayTreePause or
89 * @DisplayFramePause tags, and updates the delay or pause as necessary
90 * @param toSearch
91 */
92 private void delay(Frame toSearch) {
93 // check for change in globaly delay time
94 Item delay = ItemUtils.FindTag(toSearch.getItems(),
95 "@DisplayTreePause:");
96 if (delay != null) {
97 try {
98 // attempt to read in the delay value
99 _delay = Long.parseLong(ItemUtils.StripTag(((Text) delay)
100 .getFirstLine(), "@DisplayTreePause:"));
101 message("DisplayTree delay changed to: " + _delay + "ms");
102 } catch (NumberFormatException nfe) {
103 message("Incorrect paramter for DisplayTreePause");
104 }
105
106 }
107
108 // check for change in delay for this frame only
109 delay = ItemUtils.FindTag(toSearch.getItems(), "@DisplayFramePause:");
110 if (delay != null) {
111 try {
112 // attempt to read in the delay value
113 long pause = Long.parseLong(ItemUtils.StripTag(((Text) delay)
114 .getFirstLine(), "@DisplayFramePause:"));
115 pause(pause);
116 } catch (NumberFormatException nfe) {
117 message("Incorrect paramter for DisplayFramePause");
118 }
119 } else
120 pause(_delay);
121 }
122
123 @Override
124 protected void finalise(Frame start) {
125 if (_frameCount == 0)
126 _frameCount++;
127
128 long avg = _timer.getElapsedMillis() / _frameCount;
129 String stats = "Frames: " + _frameCount + ", Time: "
130 + _timer.getElapsedStringMillis() + ", Average: " + avg;
131 String msg = "Tree successfully displayed. ";
132
133 // return the user to the Frame they started on
134 if (!_stop) {
135 DisplayIO.Back();
136 } else {
137 msg = "Agent halted by user. ";
138 }
139
140 message(msg + stats);
141 }
142
143 /**
144 * Pauses the execution of this Agent for the given time period (in ms.)
145 * This is used for the delay between displaying frames.
146 *
147 * @param time
148 */
149 private void pause(long time) {
150 try {
151 if (time < 0)
152 _timeRemaining = Long.MAX_VALUE;
153 else
154 _timeRemaining = time;
155
156 while (_timeRemaining > 0) {
157 Thread.yield();
158 Thread.sleep(TIMER_RESOLUTION);
159 _timeRemaining -= TIMER_RESOLUTION;
160 }
161 } catch (InterruptedException e) {
162 }
163 }
164
165 @Override
166 public void interrupt() {
167 _timeRemaining = 0;
168 }
169
170 @Override
171 public void stop() {
172 _timeRemaining = 0;
173 super.stop();
174 }
175
176}
Note: See TracBrowser for help on using the repository browser.