Menu

[r66]: / ls / graph / script / Algorithm.java  Maximize  Restore  History

Download this file

112 lines (97 with data), 2.3 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
package ls.graph.script;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import ls.graph.Controller;
import ls.script.Scriptable;
import ls.util.Utils;
public class Algorithm implements Scriptable, Runnable
{
private List<AlgorithmStep> steps = null;
private int delay = 1000; //in ms
private List<AlgorithmListener> listeners = null;
private boolean stop;
public Algorithm()
{
super();
this.steps = new ArrayList<AlgorithmStep>();
this.listeners = new LinkedList<AlgorithmListener>();
String sDelay = Controller.getProperty("ALGORITHM_STEP_DELAY", String.valueOf(this.delay));
try
{ this.delay = Integer.parseInt(sDelay); }
catch (NumberFormatException exn)
{ Utils.exn(exn); }
}
public synchronized void addListener(AlgorithmListener l)
{
if (l != null) this.listeners.add(l);
}
private void fireExecutionStarted()
{
for (AlgorithmListener l : this.listeners)
{
l.executionStarted(this);
}
}
private void fireExecutionEnded()
{
for (AlgorithmListener l : this.listeners)
l.executionEnded(this);
}
private void fireStepStarted(int ind)
{
for (AlgorithmListener l : this.listeners)
l.startingStep(ind);
}
public String getBindingName()
{
return "Algorithm";
}
public void addStep(AlgorithmStep step)
{
if (step != null)
this.steps.add(step);
}
public void run()
{
fireExecutionStarted();
stop = steps.size() <= 0;
int nextStepIndex = 0;
while (!stop)
{
AlgorithmStep nextStep = steps.get(nextStepIndex);
if (nextStep != null)
{
fireStepStarted(nextStepIndex);
stop = nextStep.execute();
if (!stop)
{
nextStepIndex = nextStep.getNextStep();
try
{
synchronized (this)
{ this.wait(getDelay()); }
}
catch (InterruptedException exn)
{
Utils.exn(exn);
}
}
}
else stop = true; //no next step
}
fireExecutionEnded();
}
public void setStop(boolean st)
{
this.stop = st;
}
//private Random random = new Random();
private int getDelay()
{
return this.delay;
//int d = random.nextInt(delay);
//d = Math.max(d/4, d);
//return d;
}
}
MongoDB Logo MongoDB