Initial
This commit is contained in:
commit
ba2fb856fa
14 changed files with 618 additions and 0 deletions
44
app/build.gradle.kts
Normal file
44
app/build.gradle.kts
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
/*
|
||||
* This file was generated by the Gradle 'init' task.
|
||||
*
|
||||
* This generated file contains a sample Java application project to get you started.
|
||||
* For more details on building Java & JVM projects, please refer to https://docs.gradle.org/8.3/userguide/building_java_projects.html in the Gradle documentation.
|
||||
* This project uses @Incubating APIs which are subject to change.
|
||||
*/
|
||||
|
||||
plugins {
|
||||
// Apply the application plugin to add support for building a CLI application in Java.
|
||||
application
|
||||
}
|
||||
|
||||
repositories {
|
||||
// Use Maven Central for resolving dependencies.
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
dependencies {
|
||||
// This dependency is used by the application.
|
||||
implementation("com.google.guava:guava:32.1.1-jre")
|
||||
}
|
||||
|
||||
testing {
|
||||
suites {
|
||||
// Configure the built-in test suite
|
||||
val test by getting(JvmTestSuite::class) {
|
||||
// Use JUnit Jupiter test framework
|
||||
useJUnitJupiter("5.9.3")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Apply a specific Java toolchain to ease working on different environments.
|
||||
java {
|
||||
toolchain {
|
||||
languageVersion.set(JavaLanguageVersion.of(17))
|
||||
}
|
||||
}
|
||||
|
||||
application {
|
||||
// Define the main class for the application.
|
||||
mainClass.set("simulation.MainSimulation")
|
||||
}
|
||||
7
app/src/main/java/simulation/Event.java
Normal file
7
app/src/main/java/simulation/Event.java
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
package simulation;
|
||||
|
||||
class Event{
|
||||
public double eventTime;
|
||||
public int eventType;
|
||||
public Event next;
|
||||
}
|
||||
35
app/src/main/java/simulation/EventList.java
Normal file
35
app/src/main/java/simulation/EventList.java
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
package simulation;
|
||||
|
||||
public class EventList{
|
||||
|
||||
private Event list, last;
|
||||
|
||||
EventList(){
|
||||
list = new Event();
|
||||
last = new Event();
|
||||
list.next = last;
|
||||
}
|
||||
|
||||
public void InsertEvent(int type, double TimeOfEvent){
|
||||
Event dummy, predummy;
|
||||
Event newEvent = new Event();
|
||||
newEvent.eventType = type;
|
||||
newEvent.eventTime = TimeOfEvent;
|
||||
predummy = list;
|
||||
dummy = list.next;
|
||||
while ((dummy.eventTime < newEvent.eventTime) & (dummy != last)){
|
||||
predummy = dummy;
|
||||
dummy = dummy.next;
|
||||
}
|
||||
predummy.next = newEvent;
|
||||
newEvent.next = dummy;
|
||||
}
|
||||
|
||||
public Event FetchEvent(){
|
||||
Event dummy;
|
||||
dummy = list.next;
|
||||
list.next = dummy.next;
|
||||
dummy.next = null;
|
||||
return dummy;
|
||||
}
|
||||
}
|
||||
6
app/src/main/java/simulation/GlobalSimulation.java
Normal file
6
app/src/main/java/simulation/GlobalSimulation.java
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
package simulation;
|
||||
|
||||
public class GlobalSimulation{
|
||||
public static final int ARRIVAL = 1, READY = 2, MEASURE = 3;
|
||||
public static double time = 0;
|
||||
}
|
||||
21
app/src/main/java/simulation/MainSimulation.java
Normal file
21
app/src/main/java/simulation/MainSimulation.java
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
package simulation;
|
||||
|
||||
import java.io.*;
|
||||
|
||||
public class MainSimulation extends GlobalSimulation{
|
||||
|
||||
public static void main(String[] args) throws IOException {
|
||||
Event actEvent;
|
||||
EventList myEventList = new EventList();
|
||||
State actState = new State(myEventList);
|
||||
myEventList.InsertEvent(ARRIVAL, 0);
|
||||
myEventList.InsertEvent(MEASURE, 5);
|
||||
while (time < 50000){
|
||||
actEvent = myEventList.FetchEvent();
|
||||
time = actEvent.eventTime;
|
||||
actState.TreatEvent(actEvent);
|
||||
}
|
||||
System.out.println("Mean number of customers: " + 1.0*actState.accumulated/actState.noMeasurements);
|
||||
System.out.println("Number of measurements done: " + actState.noMeasurements);
|
||||
}
|
||||
}
|
||||
58
app/src/main/java/simulation/State.java
Normal file
58
app/src/main/java/simulation/State.java
Normal file
|
|
@ -0,0 +1,58 @@
|
|||
package simulation;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class State extends GlobalSimulation{
|
||||
|
||||
public int numberInQueue = 0, accumulated = 0, noMeasurements = 0;
|
||||
|
||||
private EventList myEventList;
|
||||
|
||||
Random slump = new Random();
|
||||
|
||||
State(EventList x){
|
||||
myEventList = x;
|
||||
}
|
||||
|
||||
private void InsertEvent(int event, double timeOfEvent){
|
||||
myEventList.InsertEvent(event, timeOfEvent);
|
||||
}
|
||||
|
||||
|
||||
public void TreatEvent(Event x){
|
||||
switch (x.eventType){
|
||||
case ARRIVAL:
|
||||
arrival();
|
||||
break;
|
||||
case READY:
|
||||
ready();
|
||||
break;
|
||||
case MEASURE:
|
||||
measure();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
private double generateMean(double mean){
|
||||
return 2*mean*slump.nextDouble();
|
||||
}
|
||||
|
||||
private void arrival(){
|
||||
if (numberInQueue == 0)
|
||||
InsertEvent(READY, time + generateMean(1));
|
||||
numberInQueue++;
|
||||
InsertEvent(ARRIVAL, time + generateMean(2));
|
||||
}
|
||||
|
||||
private void ready(){
|
||||
numberInQueue--;
|
||||
if (numberInQueue > 0)
|
||||
InsertEvent(READY, time + generateMean(1));
|
||||
}
|
||||
|
||||
private void measure(){
|
||||
accumulated = accumulated + numberInQueue;
|
||||
noMeasurements++;
|
||||
InsertEvent(MEASURE, time + generateMean(5));
|
||||
}
|
||||
}
|
||||
12
app/src/test/java/simulation/AppTest.java
Normal file
12
app/src/test/java/simulation/AppTest.java
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
package simulation;
|
||||
|
||||
// import org.junit.jupiter.api.Test;
|
||||
// import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
// No defined tests yet
|
||||
class AppTest {
|
||||
// @Test void appHasAGreeting() {
|
||||
// App classUnderTest = new App();
|
||||
// assertNotNull(classUnderTest.getGreeting(), "app should have a greeting");
|
||||
// }
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue