Hello this question definite falls under Advanced. I had to repost the question so that it was properly formatted. So I hope its looks better. I've created the code to to the following. Its actually 3 classes of code. Any help would be appreciated.
here what it does and hereare the problems am having with it. Thanks again for reading:
Explanation of code
This code is for Discrete Event Simulation Techniques. The code written creates a number of classes. Mainly it has the classes MS1 , MS2 and BTS1 and BTS2,
MS1 sends signals to BTS1 and MS2 sends signals to BTS2. MS1and BTS1 has no connection to MS2 and MTS2.
How does signals go from MS1 to BTS1? It sends the signals to the class Signals. Signals then sends the signals o the class Queue which is like a cable or line in real life. Hence a cable called queueMStoBTS which is an object of class Queue connects MS1 and BTS1.
Now the for MS2 and BTS2 the same thing happens. Except the class Signals is replaced by SignalsMS2 and Queue is replaced by class QueueMS2 and its object is queueMS2toBTS2.
Now the only connection between MS1/BTS1 and MS2/BTS2 is that when the simulation is being performed signals running from MS1 to BTS1 and from MS2 toBTS2 occurs in parallel ( at the same time).
Now the Simulation is performed for 610 time units. And every 10 time units a sample of the amount of bytes ( signals are measured in bytes ) flowing through each queue is measured and that value is put into an array which stores the value. This is done for both MS1/BTS1 and MS2.BTS2.
In the end a graph is plotted with bytes at each 10 units time VS time.
Hence am suppose to get 2 graphs at the end. One for MS1/BTS1 and MS2/BTs2.
My problem is that the simulation does not run for the full 610 time units. It stop sometimes at 3 units. And the only time the graphs can be printed is when the time unit exceeds 600 time units and since the time units never exceeds 600 the graphs are never plotted.
Could you give it a look please. I worked really hard on this one.
import java.util.Random.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.util.*;
import DESimGraph.*;
import MS2.*;
class DESim extends Simulator
{
publicstaticvoid main(String[] args)
{
new DESim().start();
}
void start()
{
events = new ListQueue();
/* We have to create the Mobile station, Queue and BTS */
BTS1 bts1 = new BTS1();
Queue queueMStoBTS = new Queue();
Signals sig = new Signals() ;
MS1 ms1 = new MS1();
BTS2 bts2 = new BTS2();
QueueMS2 queueMS2toBTS2 = new QueueMS2();
SignalsMS2 sigMS2 = new SignalsMS2();
MS2 ms2 = new MS2();
/*After creating them we have to connect all of them*/
sig.queueMStoBTS = queueMStoBTS;
queueMStoBTS.bts1 = bts1;
bts1.queueMStoBTS = queueMStoBTS;
sigMS2.queueMS2toBTS2 =queueMS2toBTS2 ;
queueMS2toBTS2.bts2 = bts2 ;
bts2.queueMS2toBTS2 = queueMS2toBTS2 ;
/*Make the mobile station start creating signals immediately */
sig.time = 0.0;
ms1.time = 0.0 ;
sigMS2.time =0.0 ;
ms2.time = 0.0;
insert(sig);
insert(ms1);
insert(sigMS2);
insert(ms2);
doAllEvents();
}
}
/*------------MOBILE STATION ONE------------------------------------*/
/*This class generates signals and puts it in the Signals Class.
The Signals class then puts it into the Queue.
And the Queue then transports these signals to the BTS1 class.
This code is an extension of the "Build a Discete Event Simulation
Class MS2 is a basic replica of this class.
in Ten Easy Steps.. thats why it looks familar.
*/
class MS1 extends Event
{
private Vector msSignals = new Vector();
String handoff = "120";
String locationUpdate = "1250";
String callSetup = "3000";
String callEnd = "350" ;
public MS1()
{
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 size = (String) msSignals.elementAt(number);
Signals.method1(size);
simulator.insert(this);
}
}
}
/*--------------MS1 signals are put here---------------------*/
class Signals extends Event
{
Queue queueMStoBTS ;
staticint i =0;
staticprivate java.util.Vector inputSignals = new java.util.Vector();
public Signals()
{
}
void execute(AbstractSimulator simulator)
{
queueMStoBTS.insert(simulator ,Signals.method());
time += Random.exponential(1.0);
if (time <= 610.0)
{
simulator.insert(this);
}
}
staticpublicvoid method1(String input)
{
inputSignals.addElement(input);
}
staticpublic String method()
{
String signal = (String) inputSignals.firstElement();
inputSignals.removeElementAt(0);
return signal ;
}
}
/*------------------------------------------------*/
class Queue
{
BTS1 bts1 ;
staticint totalBytes = 0;
staticdouble timeNow =0 ;
staticdouble timeBelow =0;
/* A vector is cerated to hold siganals */
staticprivate java.util.Vector signals = new java.util.Vector();
void insert( AbstractSimulator simulator ,String signal)
{
if(bts1.isAvaiable())
{
//int bytes = Integer.valueOf(signal).intValue();
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 tp 2 matrices
int timeNowInt = (int)(timeNow);
DynamicArray.arrayMethod1(totalBytes);
DynamicArray.arrayMethod(timeNowInt);
totalBytes = 0;
timeBelow = timeBelow + 10 ;
}
if(timeNow > 600)
{
DynamicArray.print();
}
bts1.insert(simulator , signal);
}
else
{
signals.addElement(signal);
}
}
int size()
{
return signals.size();
}
String remove()
{
String signal = (String) signals.firstElement();
signals.removeElementAt(0);
return signal;
}
}
/*-----------------------------------------------------------------*/
cla ss BTS1 extends Event
{
private String signalNumberServe ;
Queue queueMStoBTS ;
void execute(AbstractSimulator simulator)
{
System.out.println("Finished processing bytes " + signalNumberServe + " at time " + time);
signalNumberServe = null ;
if( queueMStoBTS.size()> 0)
{
String signalNumber = queueMStoBTS.remove();
insert((Simulator) simulator, signalNumber);
}
}
boolean isAvaiable()
{
return (signalNumberServe == null);
}
void insert(AbstractSimulator simulator ,String signal)
{
signalNumberServe = signal ;
double serviceTime = Random.exponential(0);
time = ((Simulator)simulator).now() + serviceTime;
simulator.insert(this);
}
}
/*---------------------Global---------------------------*/
class Global
{
staticpublicint totalSize =0 ;
staticpublicint totalSizeMS2 =0 ;
}
/*---------------------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]);
}
DESimGraph graph = new DESimGraph(data , data1) ;
}
}
Well, just as the writer writes, and having writ, moves on, the poster posts, and have posted, moves on. The poster can preview, and having previewed, can edit. But once the poster has punched the post message button, the posters post is preserved for posterity.
The reason I don't offer a way to edit a posting is because I figured people might go back and change their posts, or delete them, and then the replies wouldn't make sense. So my recommendation is just to use the preview function to check things out before you post, then take a deep breath and post your message. If you later realize you want to make a correction, then just post a reply to your original message.
I was looking through your code yesterday. There are several problems with a Random method.
You use a method Random.exponential(1.0) and Random.exponential(0.0) These methods do not exist in the API that I can find. I susbstituted: Random random = new Random(); double number = random.nextDouble(); in the appropriate places. But I can't tell if that is what is required.
The code has > in several places which probably should be >
In the method: void doAllEvents() {Event e;while ( (e= (Event) events.removeFirst()) != null) {time = e.time;e.execute(this);
events.removeFirst() removes all the elements and is not programmed properly to return null when there are no more elements.
You have to fix these before proceeding. I can only guess what the fix is.
The lines:
if(timeNow > 600){DynamicArray.print();}
control when the plots are going to occur. You would have to modify the code here to ensure a plot every run.