Hello I've included some code below. Its about discrete Event Simulation techniques in Java. I posted some similar code before. But this time the code actually complies. It mostly does what it is suppose to do. Here is what it is suppose to do. When you look at the code you'll see the classes:
MobileStation1 MobileStation2 BTS1 BTS2
now signals are transfered between MobileStation1 and BTS1 while signals are tranfered between MobileStation2 and BTS2 in parallel. At the end of the simulation its plots to graphs one for the amount of signals passed between MobileStation1 and BTS1 Vs time and another graph for MobileStation2 and BTS2 vs time.
Its really simple and it compiles and everything. The problem is that only one graph is printed.
You see I tried to do some efficient programming. If you look at the code you'll also see the classes
Link Plot DynamicArray
I tried to get MobileStation1 MobileStation2 and BTS1 and BTS2 to kinda share the classes above.
But for some reason only one graph is being plotted. I think that only one dynamic array is being made and all the values from both simulations are being thrown into the same array and hence I get one graph. I know the code is long but could you give it a look if you have time please... thanks a million.
Here is the code
import java.util.Random.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
class DES5March2002 extends Simulator
{
publicstaticvoid main(String[] args)
{
new DES5March2002().start();
}
void start()
{
events = new ListQueue();
BTS2 bts2 = new BTS2();
MobileStation2 ms2 = new MobileStation2();
Link link2 = new Link();
BTS1 bts1 = new BTS1();
MobileStation1 ms1 = new MobileStation1();
Link link1 = new Link();
bts1.link1 = link1;
ms1.link1 = link1;
bts2.link2 = link2;
ms2.link2 = link2;
bts2.time = 0.0;
link2.time =0.0;
ms2.time = 0.0 ;
bts1.time = 0.0;
link1.time =0.0;
ms1.time = 0.0 ;
insert(bts2);
insert(ms2);
insert(link2);
insert(bts1);
insert(ms1);
insert(link1);
doAllEvents();
}
}
/*-----------------------------------------------------------*/
class MobileStation1 extends Event
{
Link link1 ;
private Vector msSignals = new Vector();
String handoff = "100";
String locationUpdate = "0";
String callSetup = "0";
String callEnd = "0" ;
public MobileStation1()
{
msSignals.addElement(handoff);
msSignals.addElement(locationUpdate);
msSignals.addElement(callSetup);
msSignals.addElement(callEnd);
}
void execute(AbstractSimulator simulator)
{
time += Random.exponential(1.0);
if (time <= 610.0)
{
int number =(int)(Math.random()*3);
String callSetupSignal = (String) msSignals.elementAt(number);
link1.addSignalToVector(callSetupSignal);
simulator.insert(this);
}
}
}
/*-----------------------------------------------------------*/
class MobileStation2 extends Event
{
Link link2 ;
private Vector msSignals = new Vector();
String handoff = "0";
String locationUpdate = "0";
String callSetup = "0";
String callEnd = "0" ;
public MobileStation2()
{
msSignals.addElement(handoff);
msSignals.addElement(locationUpdate);
msSignals.addElement(callSetup);
msSignals.addElement(callEnd);
}
void execute(AbstractSimulator simulator)
{
time += Random.exponential(1.0);
if (time <= 610.0)
{
int number =(int)(Math.random()*3);
String callSetupSignal = (String) msSignals.elementAt(number);
link2.addSignalToVector(callSetupSignal);
simulator.insert(this);
}
}
}
/*-----------------------------------------------------------*/
class Link extends Event
{
staticint totalBytes = 0;
staticdouble timeNow =0 ;
staticdouble timeBelow =0;
staticint i =0;
staticprivate java.util.Vector inputSignals = new java.util.Vector();
public Link()
{
}
void execute(AbstractSimulator simulator)
{
insert(simulator , removeFirstSignal());
time += Random.exponential(0.1);
if (time <= 610.0)
{
simulator.insert(this);
}
}
void insert( AbstractSimulator simulator ,String signal)
{
int bytes = Integer.parseInt(signal);
totalBytes = totalBytes + bytes ;
timeNow = ((Simulator)simulator).now();
if(timeNow <= (timeBelow + 10))
{
System.out.println("Total Bytes" + totalBytes);
}
if(timeNow > (timeBelow + 10))
{
//Send total byes and time now to matrices
int timeNowInt = (int)(timeNow);
DynamicArray.arrayMethod1(totalBytes);
DynamicArray.arrayMethod(timeNowInt);
totalBytes = 0;
timeBelow = timeBelow + 10 ;
}
if(timeNow > 600)
{
DynamicArray.print();
}
inputSignals.addElement(signal);
}
int size()
{
return inputSignals.size();
}
staticpublicvoid addSignalToVector(String inputSignal)
{
inputSignals.addElement(inputSignal);
}
staticpublic String removeFirstSignal()
{
String signal = "yo" ;
if(inputSignals.size() != 0)
{
signal = (String) inputSignals.firstElement();
inputSignals.removeElementAt(0);
}
if(inputSignals.size() == 0)
{
System.out.println("hello");
signal = "0" ;
}
return signal ;
}
}
/*-----------------------------------------------------------*/
class BTS1 extends Event
{
String signalNumberServe ;
Link link1 ;
public BTS1()
{
}
void execute(AbstractSimulator simulator)
{
time += Random.exponential(10.0);
if (time <= 610.0)
{
signalNumberServe = null ;
if( link1.size()> 0)
{
String signalNumber = link1.removeFirstSignal();
signalNumberServe = signalNumber ;
double serviceTime = Random.exponential(1.0);
time = ((Simulator)simulator).now() + serviceTime;
System.out.println("Finished processing bytes " + signalNumberServe + " at time " + time);
}
if(link1.size()==0)
{
System.out.println("hello");
String signalNumber = "0";
signalNumberServe = signalNumber ;
double serviceTime = Random.exponential(1.0);
time = ((Simulator)simulator).now() + serviceTime;
}
simulator.insert(this);
}
}
boolean isAvaiable()
{
return (signalNumberServe == null);
}
}
/*---------------------------------------------------*/
class BTS2 extends Event
{
String signalNumberServe ;
Link link2 ;
public BTS2()
{
}
void execute(AbstractSimulator simulator)
{
time += Random.exponential(10.0);
if (time <= 610.0)
{
signalNumberServe = null ;
if( link2.size()> 0)
{
String signalNumber = link2.removeFirstSignal();
signalNumberServe = signalNumber ;
double serviceTime = Random.exponential(1.0);
time = ((Simulator)simulator).now() + serviceTime;
System.out.println("Finished at" + signalNumberServe + " at time " + time);
}
if(link2.size()==0)
{
System.out.println("hello");
String signalNumber = "0";
signalNumberServe = signalNumber ;
double serviceTime = Random.exponential(1.0);
time = ((Simulator)simulator).now() + serviceTime;
}
simulator.insert(this);
}
}
boolean isAvaiable()
{
return (signalNumberServe == null);
}
}
/*---------------------MATRIX---------------------------*/
class DynamicArray
{
staticprivateint i ;
staticprivateint l;
staticprivateint data[] = newint[1];
staticprivateint data1[] = newint[1];
staticprivateint newdata[];
staticprivateint newdata1[];
staticprivateint k=0;
staticprivateint q=0;
staticprivateint newSize;
staticprivateint newSize1;
public DynamicArray(){}
staticvoid arrayMethod(int j)
{
i = j ;
if(k==data.length)
{
newSize = data.length +1;
int[] newData = newint[newSize];
System.arraycopy(data, 0, newData, 0, data.length);
data = newData;
data[k] = i;
}
else
{
data[k]=i;
}
k++;
}
staticvoid arrayMethod1(int m)
{
l = m ;
if(q==data1.length)
{
newSize1 = data1.length+1;
Global.totalSize = newSize1 ;
int[] newData1 = newint[newSize1];
System.arraycopy(data1, 0, newData1, 0, data1.length);
data1 = newData1;
data1[q] = l;
}
else
{
data1[q]=l;
System.out.println("Nope ");
}
q++;
}
staticvoid print()
{
System.out.println("here ");
int ee = data.length ;
int ff = data1.length ;
System.out.println("" + ee );
System.out.println("" + ff );
for(int ll =0 ; ll < ee ; ll++)
{
System.out.println("" + data[ll]);
}
for(int hh =0 ; hh < ff ; hh++)
{
System.out.println("" + data1[hh]);
}
new DESimGraph(data , data1) ;
}
}
/*-----------------------------------------------------------*/
class DESimGraph
{
privateint[] data ;
privateint[] data1;
public DESimGraph(int[] dataEx , int[] data1Ex)
{
data = dataEx ;
data1 = data1Ex;
Plot plot = new Plot(data , data1);
plot.setLayout(null);
JFrame frame = new JFrame();
frame.setBackground(Color.white);
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
frame.addWindowListener(new WindowAdapter() {
publicvoid windowClosing(WindowEvent we) {
System.exit(0);
}
});
frame.getContentPane().add(plot);
plot.updateUI();
frame.setSize(800,800);
frame.setVisible(true);
System.out.println("Now");
try {
Thread.sleep(5000);
} catch (Exception e) {}
}
}
class Plot extends JPanel
{
int[] x ;
int[] y ;
int size = Global.totalSize ;
int[] xx = newint[size];
int[] yy = newint[size];
String xlabel, ylabel, title;
int xdim, ydim, yzero, xzero, xdraw, ydraw;
double xtic, ytic, xpoint, ypoint;
double xmax, xmin, ymax, ymin;
staticdouble x1max ;
staticdouble x1min ;
public Plot(int[] dataEx , int[] data1Ex)
{
System.out.println("Yep ");
x = dataEx ;
y = data1Ex ;
xdim = 600;
ydim = 600;
xtic = 100;
ytic = 5000;
xlabel = ("Time");
ylabel = ("Bytes");
title = ylabel + " versus " + xlabel;
xmax = x[0];
xmin = x[0];
ymax = y[0];
ymin = y[0];
x1min= x[0];
x1max= x[0];
for (int i=0; i < size; i++){
if (x[i] > xmax) {
xmax = x[i];
}
if (x[i] < xmin) {
xmin = x[i];
}
if (y[i] > ymax) {
ymax = y[i];
x1max = x[i];
}
if (y[i] < ymin) {
ymin = y[i];
x1min = x[i];
}
}
//xx and yy are the scaled x and y used for plotting
for (int i=0; i < size; i++){
xx[i] = (int) (50 + (((x[i]-xmin)/(xmax-xmin)) * (xdim-100)));
yy[i] = (int) ((ydim - 50) - (((y[i]-ymin)/(ymax-ymin)) * (ydim-100)));
}
//Find Zero point on y-axis required for drawing the axes
if ((ymax*ymin) < 0){
yzero = (int) ((ydim - 50) - (((0-ymin)/(ymax-ymin)) * (ydim-100)));
}
else{
yzero = (int) ((ydim - 50) - ((0/(ymax-ymin)) * (ydim-100)));
}
//Find zero point on x-axis required for drawing the axes
if ((xmax*xmin) < 0) {
xzero = (int) (50 + (((0-xmin)/(xmax-xmin)) * (xdim-100)));
}
else{
xzero = (int) (50 + ((0/(xmax-xmin)) * (xdim-100)));
}
//Now ready to plot the results
repaint();
}
publicvoid paint(Graphics g){
Font f1 = new Font("TimesRoman", Font.PLAIN, 10);
g.setFont(f1);
//First draw the axes
//y-axis
g.drawLine(xzero, 50, xzero, ydim-50);
g.drawLine(xzero, 50, (xzero - 5), 55);
g.drawLine(xzero, 50, (xzero + 5), 55);
//x-axis
g.drawLine(50, yzero, xdim-50, yzero);
g.drawLine((xdim-50), yzero, (xdim-55), (yzero + 5));
g.drawLine((xdim-50), yzero, (xdim-55), (yzero - 5));
//Initialise the labelling taking into account the xtic and ytic values
//x-axis labels
if (xmin <= 0){
xpoint = xmin - (xmin%xtic);
}else{
xpoint = xmin - (xmin%xtic) + xtic;
}
do{
xdraw = (int) (50 + (((xpoint-xmin)/(xmax-xmin))*(xdim-100)));
g.drawString(xpoint + "", xdraw, (yzero+10));
xpoint = xpoint + xtic;
}while (xpoint <= xmax);
if (ymin <= 0){
ypoint = ymin - (ymin%ytic);
}else{
ypoint = ymin - (ymin%ytic) + ytic;
}
do{
ydraw = (int) ((ydim - 50) - (((ypoint - ymin)/(ymax-ymin))*(ydim-100)));
g.drawString(ypoint + "", (xzero - 20), ydraw);
ypoint = ypoint + ytic;
}while (ypoint <= ymax);
//Titles and labels
Font f2 = new Font("TimesRoman", Font.BOLD, 14);
g.setFont(f2);
g.drawString(xlabel, (xdim - 100), (yzero + 25));
g.drawString(ylabel, (xzero - 25), 40);
g.drawString(title, (xdim/2 - 75), 20);
//Finding Maximum and Minimun values for the curve
// Draw Lines
g.drawString("Maximum point" + "("+ x1max + "," + ymax + ")", (xdim/2 + 250), 170);
g.drawString("Minimum point" + "("+ x1min + "," + ymin + ")", (xdim/2 + 250), 200);
for (int j = 0; j < size-1; j++)
{
g.drawLine(xx[j], yy[j], xx[j+1], yy[j+1]);
}
}
}
/*----------------------------------------*/
abstractclass Event extends AbstractEvent {
double time;
publicboolean lessThan(Comparable y) {
Event e = (Event) y; // Will throw an exception if y is not an Event
return this.time < e.time;
}
}
class Simulator extends AbstractSimulator {
double time;
double now() {
return time;
}
void doAllEvents() {
Event e;
while ( (e= (Event) events.removeFirst()) != null ) {
time = e.time;
e.execute(this);
}
}
}
/*----------------------------------------------*/
class ListQueue extends OrderedSet {
java.util.Vector elements = new java.util.Vector();
void insert(Comparable x) {
int i = 0;
while (i < elements.size() && ((Comparable) elements.elementAt(i)).lessThan(x)) {
i++;
}
elements.insertElementAt(x,i);
}
Comparable removeFirst() {
if (elements.size() ==0) returnnull;
Comparable x = (Comparable) elements.firstElement();
elements.removeElementAt(0);
return x;
}
Object remove(Object x) {
for (int i = 0; i < elements.size(); i++) {
if (elements.elementAt(i).equals(x)) {
Object y = elements.elementAt(i);
elements.removeElementAt(i);
return y;
}
}
returnnull;
}
publicint size() {
return elements.size();
}
}
/*---------------------------------------------------------*/
interface Comparable {
boolean lessThan(Comparable y);
}
abstractclass AbstractEvent implements Comparable {
abstractvoid execute(AbstractSimulator simulator);
}
abstractclass OrderedSet {
abstractvoid insert(Comparable x);
abstract Comparable removeFirst();
abstractint size();
}
class AbstractSimulator {
OrderedSet events;
void insert(AbstractEvent e) {
events.insert(e);
}
AbstractEvent cancel(AbstractEvent e) {
thrownew java.lang.RuntimeException("Method not implemented");
}
}