├── .gitignore ├── LICENCE.txt ├── README.md ├── docs ├── README.md ├── images │ ├── README.md │ ├── maths1.jpg │ ├── maths2.jpg │ ├── maths3.jpg │ └── queues.jpg └── manual.adoc ├── examples ├── README.md ├── pom.xml └── src │ ├── main │ └── java │ │ └── org │ │ └── javasim │ │ └── examples │ │ ├── basic │ │ ├── Arrivals.java │ │ ├── Breaks.java │ │ ├── Job.java │ │ ├── Machine.java │ │ ├── MachineShop.java │ │ ├── Main.java │ │ ├── Queue.java │ │ ├── README.md │ │ └── expected_output │ │ ├── interrupt │ │ ├── Arrivals.java │ │ ├── Job.java │ │ ├── MachineShop.java │ │ ├── Main.java │ │ ├── Processor.java │ │ ├── Queue.java │ │ ├── README.md │ │ └── Signaller.java │ │ ├── stats │ │ ├── README.md │ │ └── Stats.java │ │ └── streams │ │ ├── README.md │ │ └── Streams.java │ └── test │ └── java │ └── org │ └── javasim │ └── tests │ └── examples │ ├── basic │ └── BasicExampleUnitTest.java │ ├── interrupt │ └── InterruptExampleUnitTest.java │ ├── stats │ └── StatsExampleUnitTest.java │ └── streams │ ├── StreamsExampleUnitTest.java │ └── TriangularExampleStreamTest.java ├── pom.xml └── src ├── main └── java │ └── org │ └── javasim │ ├── RestartException.java │ ├── Scheduler.java │ ├── Semaphore.java │ ├── Simulation.java │ ├── SimulationEntity.java │ ├── SimulationException.java │ ├── SimulationProcess.java │ ├── TriggerQueue.java │ ├── internal │ ├── SimulationProcessIterator.java │ └── SimulationProcessList.java │ ├── simset │ ├── Head.java │ └── Link.java │ ├── stats │ ├── Bucket.java │ ├── Histogram.java │ ├── Mean.java │ ├── PrecisionHistogram.java │ ├── Quantile.java │ ├── SimpleHistogram.java │ ├── StatisticsException.java │ ├── TimeVariance.java │ └── Variance.java │ └── streams │ ├── Draw.java │ ├── ErlangStream.java │ ├── ExponentialStream.java │ ├── HyperExponentialStream.java │ ├── NormalStream.java │ ├── RandomStream.java │ ├── TriangularStream.java │ └── UniformStream.java └── test └── java └── org └── javasim └── tests ├── SemaphoreUnitTest.java ├── SimulationProcessUnitTest.java ├── internal ├── SimulationProcessIteratorUnitTest.java └── SimulationProcessListUnitTest.java ├── stats ├── BucketUnitTest.java ├── HistogramUnitTest.java ├── MeanUnitTest.java ├── PrecisionHistogramUnitTest.java ├── QuantileUnitTest.java ├── SimpleHistogramUnitTest.java ├── TimeVarianceUnitTest.java └── VarianceUnitTest.java └── streams ├── DrawUnitTest.java ├── ErlangStreamUnitTest.java ├── ExponentialStreamUnitTest.java ├── HyperExponentialStreamUnitTest.java ├── NormalStreamUnitTest.java ├── TriangularStreamUnitTest.java └── UniformStreamUnitTest.java /.gitignore: -------------------------------------------------------------------------------- 1 | target 2 | .project 3 | .classpath 4 | .settings 5 | *.iml 6 | *.class 7 | .idea 8 | .DS_Store 9 | *.out 10 | *~* 11 | -------------------------------------------------------------------------------- /LICENCE.txt: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | JavaSIM is an object-oriented simulation package based upon C++SIM and has been in use since 1997. It provides discrete event process-based simulation similar to SIMULA's simulation class and libraries. A complete list of the capabilities provided follows: 2 | 3 | - The core of the system gives SIMULA-like simulation routines, random number generators, queueing algorithms and in C++SIM there are thread package interfaces, though for Java that's not necessary. 4 | - Entity and set manipulation facilities similar to SIMSET. 5 | - Classes allow "non-causal" events, such as interrupts, to be handled. 6 | - Various routines for gathering statistics, such as histogram and variance classes. 7 | 8 | The system also comes with complete examples and tests which illustrate many of the issues raised in using the simulation package. 9 | 10 | Over the years C++SIM and JavaSim have been used by many commercial and academic organisations. 11 | 12 | Prior to 2007 both C++SIM and JavaSim were freely available in source and binary from Newcastle University, under the University's own licence. However, in late 2007 Newcastle University decided that everything could be released into open source under LGPL. In 2015 the code was moved from Codehaus to github. All JIRAs from there were also recreated as github issues. 13 | 14 | You can find details of the releases in the https://github.com/nmcl/JavaSim/releases section as well as binary downloads for some releases. 15 | 16 | ---- 17 | 18 | To build: 19 | 20 | mvn compile 21 | 22 | Run tests: 23 | 24 | mvn test 25 | 26 | Run tests and create installation: 27 | 28 | mvn install 29 | 30 | To run the examples check the README in that directory. 31 | -------------------------------------------------------------------------------- /docs/README.md: -------------------------------------------------------------------------------- 1 | manual.adoc - the user manual. 2 | 3 | images - the files in this directory are used within the manual so do not move (the URLs used within the doc are relative not absolute). 4 | -------------------------------------------------------------------------------- /docs/images/README.md: -------------------------------------------------------------------------------- 1 | Contains images used within the user manual. DO NOT MOVE. 2 | -------------------------------------------------------------------------------- /docs/images/maths1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nmcl/JavaSim/9ce1fcea4fa9e64d7385fd224c82152ba70ed9e5/docs/images/maths1.jpg -------------------------------------------------------------------------------- /docs/images/maths2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nmcl/JavaSim/9ce1fcea4fa9e64d7385fd224c82152ba70ed9e5/docs/images/maths2.jpg -------------------------------------------------------------------------------- /docs/images/maths3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nmcl/JavaSim/9ce1fcea4fa9e64d7385fd224c82152ba70ed9e5/docs/images/maths3.jpg -------------------------------------------------------------------------------- /docs/images/queues.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/nmcl/JavaSim/9ce1fcea4fa9e64d7385fd224c82152ba70ed9e5/docs/images/queues.jpg -------------------------------------------------------------------------------- /examples/README.md: -------------------------------------------------------------------------------- 1 | To build the examples first ensure that the main source tree has been built using 'mvn install'. Then here: 2 | 3 | mvn compile 4 | 5 | If you want to run the examples as unit tests then: 6 | 7 | mvn test 8 | -------------------------------------------------------------------------------- /examples/pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | javasim-examples 5 | javasim-examples 6 | javasim-examples 7 | 2.3 8 | http://maven.apache.org 9 | 10 | UTF-8 11 | 12 | 13 | 14 | 15 | maven-compiler-plugin 16 | org.apache.maven.plugins 17 | 3.6.0 18 | 19 | 1.8 20 | 1.8 21 | UTF-8 22 | 23 | 24 | 25 | org.apache.maven.plugins 26 | maven-surefire-plugin 27 | 2.17 28 | 29 | 1 30 | false 31 | 34 | -Djdk.net.URLClassPath.disableClassPathURLCheck=true 35 | 36 | 37 | 38 | 39 | 40 | 41 | junit 42 | junit 43 | 4.0 44 | jar 45 | test 46 | true 47 | 48 | 49 | javasim 50 | javasim 51 | 2.3 52 | system 53 | ${basedir}/../target/javasim-${version}.jar 54 | 55 | 56 | 57 | 58 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/basic/Arrivals.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.basic; 22 | 23 | import java.io.IOException; 24 | 25 | import org.javasim.RestartException; 26 | import org.javasim.SimulationException; 27 | import org.javasim.SimulationProcess; 28 | import org.javasim.streams.ExponentialStream; 29 | 30 | public class Arrivals extends SimulationProcess 31 | { 32 | public Arrivals(double mean) 33 | { 34 | InterArrivalTime = new ExponentialStream(mean); 35 | } 36 | 37 | public void run () 38 | { 39 | while (!terminated()) 40 | { 41 | try 42 | { 43 | hold(InterArrivalTime.getNumber()); 44 | } 45 | catch (SimulationException e) 46 | { 47 | } 48 | catch (RestartException e) 49 | { 50 | } 51 | catch (IOException e) 52 | { 53 | } 54 | 55 | new Job(); 56 | } 57 | } 58 | 59 | private ExponentialStream InterArrivalTime; 60 | } 61 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/basic/Breaks.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.basic; 22 | 23 | import java.io.IOException; 24 | 25 | import org.javasim.RestartException; 26 | import org.javasim.SimulationException; 27 | import org.javasim.SimulationProcess; 28 | import org.javasim.streams.UniformStream; 29 | 30 | public class Breaks extends SimulationProcess 31 | { 32 | public Breaks() 33 | { 34 | RepairTime = new UniformStream(10, 100); 35 | OperativeTime = new UniformStream(200, 500); 36 | interrupted_service = false; 37 | } 38 | 39 | public void run () 40 | { 41 | while (!terminated()) 42 | { 43 | try 44 | { 45 | double failedTime = RepairTime.getNumber(); 46 | 47 | hold(OperativeTime.getNumber()); 48 | 49 | MachineShop.M.broken(); 50 | MachineShop.M.cancel(); 51 | 52 | if (!MachineShop.JobQ.isEmpty()) 53 | interrupted_service = true; 54 | 55 | hold(failedTime); 56 | 57 | MachineShop.MachineFailedTime += failedTime; 58 | MachineShop.M.fixed(); 59 | 60 | if (interrupted_service) 61 | MachineShop.M.activateAt(MachineShop.M.serviceTime() 62 | + currentTime()); 63 | else 64 | MachineShop.M.activate(); 65 | 66 | interrupted_service = false; 67 | } 68 | catch (SimulationException e) 69 | { 70 | } 71 | catch (RestartException e) 72 | { 73 | } 74 | catch (IOException e) 75 | { 76 | } 77 | } 78 | } 79 | 80 | private UniformStream RepairTime; 81 | 82 | private UniformStream OperativeTime; 83 | 84 | private boolean interrupted_service; 85 | } 86 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/basic/Job.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.basic; 22 | 23 | import org.javasim.RestartException; 24 | import org.javasim.Scheduler; 25 | import org.javasim.SimulationException; 26 | 27 | public class Job 28 | { 29 | public Job() 30 | { 31 | boolean empty = false; 32 | 33 | ResponseTime = 0.0; 34 | ArrivalTime = Scheduler.currentTime(); 35 | 36 | empty = MachineShop.JobQ.isEmpty(); 37 | MachineShop.JobQ.enqueue(this); 38 | MachineShop.TotalJobs++; 39 | 40 | if (empty && !MachineShop.M.processing() 41 | && MachineShop.M.isOperational()) 42 | { 43 | try 44 | { 45 | MachineShop.M.activate(); 46 | } 47 | catch (SimulationException e) 48 | { 49 | } 50 | catch (RestartException e) 51 | { 52 | } 53 | } 54 | } 55 | 56 | public void finished () 57 | { 58 | ResponseTime = Scheduler.currentTime() - ArrivalTime; 59 | MachineShop.TotalResponseTime += ResponseTime; 60 | } 61 | 62 | private double ResponseTime; 63 | 64 | private double ArrivalTime; 65 | } 66 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/basic/Machine.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.basic; 22 | 23 | import java.io.IOException; 24 | 25 | import org.javasim.RestartException; 26 | import org.javasim.SimulationException; 27 | import org.javasim.SimulationProcess; 28 | import org.javasim.streams.ExponentialStream; 29 | 30 | public class Machine extends SimulationProcess 31 | { 32 | public Machine(double mean) 33 | { 34 | STime = new ExponentialStream(mean); 35 | operational = true; 36 | working = false; 37 | J = null; 38 | } 39 | 40 | public void run () 41 | { 42 | double ActiveStart, ActiveEnd; 43 | 44 | while (!terminated()) 45 | { 46 | working = true; 47 | 48 | while (!MachineShop.JobQ.isEmpty()) 49 | { 50 | ActiveStart = currentTime(); 51 | MachineShop.CheckFreq++; 52 | 53 | MachineShop.JobsInQueue += MachineShop.JobQ.queueSize(); 54 | J = MachineShop.JobQ.dequeue(); 55 | 56 | try 57 | { 58 | hold(serviceTime()); 59 | } 60 | catch (SimulationException e) 61 | { 62 | } 63 | catch (RestartException e) 64 | { 65 | } 66 | 67 | ActiveEnd = currentTime(); 68 | MachineShop.MachineActiveTime += ActiveEnd - ActiveStart; 69 | MachineShop.ProcessedJobs++; 70 | 71 | /* 72 | * Introduce this new method because we usually rely upon the 73 | * destructor of the object to do the work in C++. 74 | */ 75 | 76 | J.finished(); 77 | } 78 | 79 | working = false; 80 | 81 | try 82 | { 83 | cancel(); 84 | } 85 | catch (RestartException e) 86 | { 87 | } 88 | } 89 | } 90 | 91 | public void broken () 92 | { 93 | operational = false; 94 | } 95 | 96 | public void fixed () 97 | { 98 | operational = true; 99 | } 100 | 101 | public boolean isOperational () 102 | { 103 | return operational; 104 | } 105 | 106 | public boolean processing () 107 | { 108 | return working; 109 | } 110 | 111 | public double serviceTime () 112 | { 113 | try 114 | { 115 | return STime.getNumber(); 116 | } 117 | catch (IOException e) 118 | { 119 | return 0.0; 120 | } 121 | } 122 | 123 | private ExponentialStream STime; 124 | 125 | private boolean operational; 126 | 127 | private boolean working; 128 | 129 | private Job J; 130 | } 131 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/basic/MachineShop.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.basic; 22 | 23 | import org.javasim.RestartException; 24 | import org.javasim.Scheduler; 25 | import org.javasim.Simulation; 26 | import org.javasim.SimulationException; 27 | import org.javasim.SimulationProcess; 28 | 29 | public class MachineShop extends SimulationProcess 30 | { 31 | public MachineShop(boolean isBreaks) 32 | { 33 | useBreaks = isBreaks; 34 | 35 | TotalResponseTime = 0.0; 36 | TotalJobs = 0; 37 | ProcessedJobs = 0; 38 | JobsInQueue = 0; 39 | CheckFreq = 0; 40 | MachineActiveTime = 0.0; 41 | MachineFailedTime = 0.0; 42 | } 43 | 44 | public void run () 45 | { 46 | try 47 | { 48 | Breaks B = null; 49 | Arrivals A = new Arrivals(8); 50 | MachineShop.M = new Machine(8); 51 | Job J = new Job(); 52 | 53 | A.activate(); 54 | 55 | if (useBreaks) 56 | { 57 | B = new Breaks(); 58 | B.activate(); 59 | } 60 | 61 | Simulation.start(); 62 | 63 | while (MachineShop.ProcessedJobs < 1000) 64 | hold(1000); 65 | 66 | System.out.println("Current time "+currentTime()); 67 | System.out.println("Total number of jobs present " + TotalJobs); 68 | System.out.println("Total number of jobs processed " 69 | + ProcessedJobs); 70 | System.out.println("Total response time of " + TotalResponseTime); 71 | System.out.println("Average response time = " 72 | + (TotalResponseTime / ProcessedJobs)); 73 | System.out 74 | .println("Probability that machine is working = " 75 | + ((MachineActiveTime - MachineFailedTime) / currentTime())); 76 | System.out.println("Probability that machine has failed = " 77 | + (MachineFailedTime / MachineActiveTime)); 78 | System.out.println("Average number of jobs present = " 79 | + (JobsInQueue / CheckFreq)); 80 | 81 | Simulation.stop(); 82 | 83 | A.terminate(); 84 | MachineShop.M.terminate(); 85 | 86 | if (useBreaks) 87 | B.terminate(); 88 | 89 | SimulationProcess.mainResume(); 90 | } 91 | catch (SimulationException e) 92 | { 93 | } 94 | catch (RestartException e) 95 | { 96 | } 97 | } 98 | 99 | public void await () 100 | { 101 | this.resumeProcess(); 102 | SimulationProcess.mainSuspend(); 103 | } 104 | 105 | public static Machine M = null; 106 | 107 | public static Queue JobQ = new Queue(); 108 | 109 | public static double TotalResponseTime = 0.0; 110 | 111 | public static long TotalJobs = 0; 112 | 113 | public static long ProcessedJobs = 0; 114 | 115 | public static long JobsInQueue = 0; 116 | 117 | public static long CheckFreq = 0; 118 | 119 | public static double MachineActiveTime = 0.0; 120 | 121 | public static double MachineFailedTime = 0.0; 122 | 123 | private boolean useBreaks; 124 | } 125 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/basic/Main.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.basic; 22 | 23 | public class Main 24 | { 25 | public static void main (String[] args) 26 | { 27 | boolean isBreaks = false; 28 | 29 | for (int i = 0; i < args.length; i++) 30 | { 31 | if (args[i].equalsIgnoreCase("-help")) 32 | { 33 | System.out.println("Usage: Main [-breaks] [-help]"); 34 | System.exit(0); 35 | } 36 | if (args[i].equalsIgnoreCase("-breaks")) 37 | isBreaks = true; 38 | } 39 | 40 | MachineShop m = new MachineShop(isBreaks); 41 | 42 | m.await(); 43 | 44 | System.exit(0); 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/basic/Queue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.basic; 22 | 23 | import java.util.NoSuchElementException; 24 | 25 | public class Queue 26 | { 27 | public Queue() 28 | { 29 | head = null; 30 | length = 0; 31 | } 32 | 33 | public boolean isEmpty () 34 | { 35 | if (length == 0) 36 | return true; 37 | else 38 | return false; 39 | } 40 | 41 | public long queueSize () 42 | { 43 | return length; 44 | } 45 | 46 | public Job dequeue () throws NoSuchElementException 47 | { 48 | if (isEmpty()) 49 | throw new NoSuchElementException(); 50 | 51 | List ptr = head; 52 | head = head.next; 53 | 54 | length--; 55 | 56 | return ptr.work; 57 | } 58 | 59 | public void enqueue (Job toadd) 60 | { 61 | if (toadd == null) 62 | return; 63 | 64 | List ptr = head; 65 | 66 | if (isEmpty()) 67 | { 68 | head = new List(); 69 | ptr = head; 70 | } 71 | else 72 | { 73 | while (ptr.next != null) 74 | ptr = ptr.next; 75 | 76 | ptr.next = new List(); 77 | ptr = ptr.next; 78 | } 79 | 80 | ptr.next = null; 81 | ptr.work = toadd; 82 | length++; 83 | } 84 | 85 | private List head; 86 | 87 | private long length; 88 | } 89 | 90 | /* This is the queue on which Jobs are placed before they are used. */ 91 | 92 | class List 93 | { 94 | public List() 95 | { 96 | work = null; 97 | next = null; 98 | } 99 | 100 | public Job work; 101 | 102 | public List next; 103 | } 104 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/basic/README.md: -------------------------------------------------------------------------------- 1 | This is an example of a simulation written using the simulation package. The example is taken from the book by Isi Mitrani (Simulation Techniques for Discrete Event Systems p22). 2 | 3 | The simulation is of a service which attempts to execute as many requests for jobs as possible. The job requests are queued until the service can deal with them. However, the service is prone to failures, and so jobs started will be delayed until the service has been reactivated. 4 | 5 | The classes provided include: 6 | 7 | Arrivals - This class controls the rate at which Jobs arrive at 8 | the service (Machine) 9 | 10 | Breaks - This class controls the availability of the Machine by 11 | "killing" it and restarting it at intervals drawn from 12 | a Uniform distribution. 13 | 14 | Job - This class represents the jobs which the Machine must process. 15 | 16 | Machine - This is the Machine on which the service resides. It obtains 17 | jobs from the job queue for the service and then attempts to 18 | execute them. The machine can fail and so the response time for 19 | jobs is not guaranteed to be the same. 20 | 21 | MachineShop - This is the main part of the simulation which starts the 22 | various processes (Scheduler, Arrivals, Machine, Job) 23 | involved. It also prints out statistics for the response time 24 | for the jobs. 25 | 26 | Queue - This represents the queue which Jobs are placed on prior to being 27 | used by the Machine (service). 28 | 29 | Main - This is the body of the program which initializes the threads package 30 | prior to the simulation starting. 31 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/basic/expected_output: -------------------------------------------------------------------------------- 1 | Output of Main: 2 | 3 | Total number of jobs present 1080 4 | Total number of jobs processed 1079 5 | Total response time of 8999.39 6 | Average response time = 8.3405 7 | Probability that machine is working = 0.999933 8 | Probability that machine has failed = 0 9 | Average number of jobs present = 1 10 | 11 | Output of Main -breaks: 12 | 13 | Total number of jobs present 1190 14 | Total number of jobs processed 1034 15 | Total response time of 704303 16 | Average response time = 681.144 17 | Probability that machine is working = 0.865654 18 | Probability that machine has failed = 0.133096 19 | Average number of jobs present = 80.8097 20 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/interrupt/Arrivals.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.interrupt; 22 | 23 | import java.io.IOException; 24 | 25 | import org.javasim.RestartException; 26 | import org.javasim.SimulationException; 27 | import org.javasim.SimulationProcess; 28 | import org.javasim.streams.ExponentialStream; 29 | 30 | public class Arrivals extends SimulationProcess 31 | { 32 | public Arrivals(double mean) 33 | { 34 | InterArrivalTime = new ExponentialStream(mean); 35 | } 36 | 37 | public void run () 38 | { 39 | while (!terminated()) 40 | { 41 | try 42 | { 43 | hold(InterArrivalTime.getNumber()); 44 | } 45 | catch (SimulationException e) 46 | { 47 | } 48 | catch (RestartException e) 49 | { 50 | } 51 | catch (IOException e) 52 | { 53 | } 54 | 55 | new Job(false); 56 | } 57 | } 58 | 59 | private ExponentialStream InterArrivalTime; 60 | } 61 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/interrupt/Job.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.interrupt; 22 | 23 | public class Job 24 | { 25 | public Job(boolean isSignal) 26 | { 27 | if (isSignal) 28 | MachineShop.SignalQ.enqueue(this); 29 | else 30 | MachineShop.JobQ.enqueue(this); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/interrupt/MachineShop.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.interrupt; 22 | 23 | import org.javasim.RestartException; 24 | import org.javasim.Scheduler; 25 | import org.javasim.Simulation; 26 | import org.javasim.SimulationEntity; 27 | import org.javasim.SimulationException; 28 | import org.javasim.SimulationProcess; 29 | 30 | public class MachineShop extends SimulationEntity 31 | { 32 | public MachineShop() 33 | { 34 | } 35 | 36 | public void run () 37 | { 38 | try 39 | { 40 | Signaller s = new Signaller(1000); 41 | Arrivals A = new Arrivals(2); 42 | MachineShop.cpu = new Processor(10); 43 | Job J = new Job(false); 44 | 45 | MachineShop.cpu.activate(); 46 | A.activate(); 47 | s.activate(); 48 | 49 | Simulation.start(); 50 | 51 | waitFor(cpu); 52 | 53 | System.out.println("Total jobs processed " + ProcessedJobs); 54 | System.out.println("Total signals processed " + SignalledJobs); 55 | 56 | Simulation.stop(); 57 | 58 | MachineShop.cpu.terminate(); 59 | A.terminate(); 60 | s.terminate(); 61 | 62 | SimulationProcess.mainResume(); 63 | } 64 | catch (SimulationException e) 65 | { 66 | } 67 | catch (InterruptedException e) 68 | { 69 | } 70 | catch (RestartException e) 71 | { 72 | } 73 | } 74 | 75 | public void await () 76 | { 77 | this.resumeProcess(); 78 | SimulationProcess.mainSuspend(); 79 | } 80 | 81 | public static Processor cpu = null; 82 | 83 | public static Queue JobQ = new Queue(); 84 | 85 | public static Queue SignalQ = new Queue(); 86 | 87 | public static long ProcessedJobs = 0; 88 | 89 | public static long SignalledJobs = 0; 90 | 91 | public static double TotalResponseTime = 0.0; 92 | 93 | public static double MachineActiveTime = 0.0; 94 | } 95 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/interrupt/Main.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.interrupt; 22 | 23 | public class Main 24 | { 25 | public static void main (String[] args) 26 | { 27 | MachineShop m = new MachineShop(); 28 | 29 | m.await(); 30 | 31 | System.exit(0); 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/interrupt/Processor.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.interrupt; 22 | 23 | import java.io.IOException; 24 | 25 | import org.javasim.RestartException; 26 | import org.javasim.SimulationEntity; 27 | import org.javasim.SimulationException; 28 | import org.javasim.streams.ExponentialStream; 29 | 30 | public class Processor extends SimulationEntity 31 | { 32 | public Processor(double mean) 33 | { 34 | sTime = new ExponentialStream(mean); 35 | } 36 | 37 | public void run () 38 | { 39 | Job j = null; 40 | 41 | while (!terminated()) 42 | { 43 | try 44 | { 45 | try 46 | { 47 | timedWait(sTime.getNumber()); 48 | 49 | if (!MachineShop.JobQ.isEmpty()) 50 | { 51 | j = MachineShop.JobQ.dequeue(); 52 | MachineShop.ProcessedJobs++; 53 | } 54 | } 55 | catch (InterruptedException e) 56 | { 57 | if (MachineShop.SignalQ.isEmpty()) 58 | System.out 59 | .println("Error - signal caught, but no message given!"); 60 | else 61 | { 62 | j = MachineShop.SignalQ.dequeue(); 63 | MachineShop.SignalledJobs++; 64 | } 65 | } 66 | 67 | if (MachineShop.SignalledJobs == 2) 68 | terminate(); 69 | } 70 | catch (SimulationException e) 71 | { 72 | } 73 | catch (RestartException e) 74 | { 75 | } 76 | catch (IOException e) 77 | { 78 | } 79 | } 80 | } 81 | 82 | private ExponentialStream sTime; 83 | } 84 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/interrupt/Queue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.interrupt; 22 | 23 | import java.util.NoSuchElementException; 24 | 25 | public class Queue 26 | { 27 | public Queue() 28 | { 29 | head = null; 30 | length = 0; 31 | } 32 | 33 | public boolean isEmpty () 34 | { 35 | if (length == 0) 36 | return true; 37 | else 38 | return false; 39 | } 40 | 41 | public long queueSize () 42 | { 43 | return length; 44 | } 45 | 46 | public Job dequeue () throws NoSuchElementException 47 | { 48 | if (isEmpty()) 49 | throw new NoSuchElementException(); 50 | 51 | List ptr = head; 52 | head = head.next; 53 | 54 | length--; 55 | 56 | return ptr.work; 57 | } 58 | 59 | public void enqueue (Job toadd) 60 | { 61 | if (toadd == null) 62 | return; 63 | 64 | List ptr = head; 65 | 66 | if (isEmpty()) 67 | { 68 | head = new List(); 69 | ptr = head; 70 | } 71 | else 72 | { 73 | while (ptr.next != null) 74 | ptr = ptr.next; 75 | 76 | ptr.next = new List(); 77 | ptr = ptr.next; 78 | } 79 | 80 | ptr.next = null; 81 | ptr.work = toadd; 82 | length++; 83 | } 84 | 85 | private List head; 86 | 87 | private long length; 88 | } 89 | 90 | /* This is the queue on which Jobs are placed before they are used. */ 91 | 92 | class List 93 | { 94 | public List() 95 | { 96 | work = null; 97 | next = null; 98 | } 99 | 100 | public Job work; 101 | 102 | public List next; 103 | } 104 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/interrupt/README.md: -------------------------------------------------------------------------------- 1 | This is a basic example of the new interrupt mechanisms added in version 1.5. 2 | 3 | A Processor object has two message queue, one for general messages and one for interrupts (signals). The processor will wait for a set period of time and will then inspect its general message queue for work to be done. This will be performed and then it will go back to waiting. A Signal object will periodically wake up and send a signal to the processor and place a message in to its signal queue. 4 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/interrupt/Signaller.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.interrupt; 22 | 23 | import java.io.IOException; 24 | 25 | import org.javasim.RestartException; 26 | import org.javasim.SimulationEntity; 27 | import org.javasim.SimulationException; 28 | import org.javasim.streams.ExponentialStream; 29 | 30 | public class Signaller extends SimulationEntity 31 | { 32 | public Signaller(double mean) 33 | { 34 | sTime = new ExponentialStream(mean); 35 | } 36 | 37 | public void run () 38 | { 39 | while (!terminated()) 40 | { 41 | try 42 | { 43 | hold(sTime.getNumber()); 44 | Job j = new Job(true); 45 | interrupt(MachineShop.cpu, false); 46 | } 47 | catch (SimulationException e) 48 | { 49 | } 50 | catch (RestartException e) 51 | { 52 | } 53 | catch (IOException e) 54 | { 55 | } 56 | } 57 | } 58 | 59 | private ExponentialStream sTime; 60 | } 61 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/stats/README.md: -------------------------------------------------------------------------------- 1 | Shows the distributions produced when using the NormalStream and Quantile implementations. 2 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/stats/Stats.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.stats; 22 | 23 | import org.javasim.streams.NormalStream; 24 | import org.javasim.stats.Quantile; 25 | 26 | public class Stats 27 | { 28 | public static void main (String[] args) throws Exception 29 | { 30 | NormalStream str = new NormalStream(100.0, 2.0); 31 | Quantile hist = new Quantile(); 32 | 33 | for (int i = 0; i < 20; i++) 34 | { 35 | hist.setValue(str.getNumber()); 36 | } 37 | 38 | System.out.println("NormalStream error: "+str.error()); 39 | 40 | hist.print(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/streams/README.md: -------------------------------------------------------------------------------- 1 | Shows the output of the NormalStream and Histogram implementations working together. 2 | -------------------------------------------------------------------------------- /examples/src/main/java/org/javasim/examples/streams/Streams.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.examples.streams; 22 | 23 | import org.javasim.streams.NormalStream; 24 | import org.javasim.stats.Histogram; 25 | 26 | public class Streams 27 | { 28 | public static void main (String[] args) throws Exception 29 | { 30 | NormalStream str = new NormalStream(100.0, 2.0); 31 | Histogram hist = new Histogram(10); 32 | 33 | for (int i = 0; i < 1000; i++) 34 | { 35 | hist.setValue(str.getNumber()); 36 | } 37 | 38 | System.out.println("NormalStream error: "+str.error()); 39 | 40 | hist.print(); 41 | } 42 | } 43 | -------------------------------------------------------------------------------- /examples/src/test/java/org/javasim/tests/examples/basic/BasicExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.examples.basic; 22 | 23 | import org.javasim.Simulation; 24 | import org.javasim.examples.basic.*; 25 | 26 | import org.junit.Test; 27 | 28 | import static org.junit.Assert.*; 29 | 30 | public class BasicExampleUnitTest 31 | { 32 | @Test 33 | public void testNoBreaks () 34 | { 35 | boolean isBreaks = false; 36 | MachineShop m = new MachineShop(isBreaks); 37 | 38 | m.await(); 39 | 40 | try 41 | { 42 | Simulation.reset(); 43 | } 44 | catch (final Throwable ex) 45 | { 46 | } 47 | } 48 | 49 | @Test 50 | public void testBreaks () 51 | { 52 | boolean isBreaks = true; 53 | MachineShop m = new MachineShop(isBreaks); 54 | 55 | m.await(); 56 | 57 | try 58 | { 59 | Simulation.reset(); 60 | } 61 | catch (final Throwable ex) 62 | { 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /examples/src/test/java/org/javasim/tests/examples/interrupt/InterruptExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.examples.interrupt; 22 | 23 | import org.javasim.examples.interrupt.*; 24 | import org.javasim.Simulation; 25 | 26 | import org.junit.Test; 27 | 28 | import static org.junit.Assert.*; 29 | 30 | 31 | public class InterruptExampleUnitTest 32 | { 33 | @Test 34 | public void test () 35 | { 36 | MachineShop m = new MachineShop(); 37 | 38 | m.await(); 39 | 40 | try 41 | { 42 | Simulation.reset(); 43 | } 44 | catch (final Throwable ex) 45 | { 46 | } 47 | } 48 | } 49 | -------------------------------------------------------------------------------- /examples/src/test/java/org/javasim/tests/examples/stats/StatsExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.examples.stats; 22 | 23 | import org.javasim.examples.stats.*; 24 | import org.javasim.Simulation; 25 | 26 | import org.junit.Test; 27 | 28 | import static org.junit.Assert.*; 29 | 30 | public class StatsExampleUnitTest 31 | { 32 | @Test 33 | public void test () 34 | { 35 | try 36 | { 37 | Stats.main(null); 38 | } 39 | catch (final Throwable ex) 40 | { 41 | } 42 | } 43 | } 44 | -------------------------------------------------------------------------------- /examples/src/test/java/org/javasim/tests/examples/streams/StreamsExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.examples.streams; 22 | 23 | import org.javasim.examples.streams.*; 24 | import org.javasim.Simulation; 25 | 26 | import org.junit.Test; 27 | 28 | import static org.junit.Assert.*; 29 | 30 | 31 | public class StreamsExampleUnitTest 32 | { 33 | @Test 34 | public void test () 35 | { 36 | try 37 | { 38 | Streams.main(null); 39 | } 40 | catch (final Throwable ex) 41 | { 42 | } 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /examples/src/test/java/org/javasim/tests/examples/streams/TriangularExampleStreamTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.examples.streams; 22 | 23 | import org.javasim.stats.Histogram; 24 | import org.javasim.streams.TriangularStream; 25 | import org.javasim.examples.streams.*; 26 | 27 | import org.junit.Test; 28 | 29 | public class TriangularExampleStreamTest { 30 | @Test 31 | public void test() { 32 | try { 33 | 34 | TriangularStream triangular = new TriangularStream(0, 20, 7); 35 | 36 | Histogram hist = new Histogram(25); 37 | 38 | for (int i = 0; i < 10000; i++) { 39 | int value = (int) Math.round(triangular.getNumber()); 40 | 41 | System.out.println(" " + value); 42 | 43 | hist.setValue(value); 44 | } 45 | 46 | System.out.println("RandomStream error: " + triangular.error()); 47 | 48 | hist.print(); 49 | 50 | } catch (final Throwable ex) { 51 | } 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4.0.0 4 | 5 | javasim 6 | javasim 7 | 2.3 8 | javasim 9 | 10 | http://maven.apache.org 11 | 12 | 13 | UTF-8 14 | false 15 | 16 | 17 | 18 | 19 | maven-compiler-plugin 20 | org.apache.maven.plugins 21 | 3.6.0 22 | 23 | 1.8 24 | 1.8 25 | UTF-8 26 | 27 | 28 | 29 | org.apache.maven.plugins 30 | maven-surefire-plugin 31 | 2.17 32 | 33 | 36 | ${skipTests} 37 | -Djdk.net.URLClassPath.disableClassPathURLCheck=true 38 | 39 | 40 | 41 | 42 | 43 | 44 | junit 45 | junit 46 | 4.0 47 | jar 48 | test 49 | true 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/RestartException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim; 22 | 23 | /** 24 | * This exception is thrown if the simulation is restarted. Simulation entities 25 | * are expected to catch this and do any tidy up necessary, prior to the restart 26 | * of the simulation later. 27 | * 28 | * @author marklittle 29 | * 30 | */ 31 | public class RestartException extends Exception 32 | { 33 | public static final long serialVersionUID = 0xdeadbeef; 34 | 35 | public RestartException() 36 | { 37 | super(); 38 | } 39 | 40 | public RestartException(String s) 41 | { 42 | super(s); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/Scheduler.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | /* 22 | * Copyright (C) 1996, 1997, 1998, 23 | * 24 | * Department of Computing Science, 25 | * The University, 26 | * Newcastle upon Tyne, 27 | * UK. 28 | * 29 | * $Id: Scheduler.java,v 1.3 1998/12/07 08:28:10 nmcl Exp $ 30 | */ 31 | 32 | package org.javasim; 33 | 34 | import java.util.NoSuchElementException; 35 | 36 | import org.javasim.internal.SimulationProcessIterator; 37 | import org.javasim.internal.SimulationProcessList; 38 | 39 | /** 40 | * This is the scheduler: the heart of the simulation system. 41 | * 42 | * Note: unlike in SIMULA, an active process is removed from the simulation 43 | * queue prior to being activated. 44 | * 45 | * @author marklittle 46 | * 47 | */ 48 | public class Scheduler extends Thread 49 | { 50 | /** 51 | * Get the current simulation time. 52 | * 53 | * @return the current simulation time. 54 | */ 55 | 56 | public static double currentTime () 57 | { 58 | return Scheduler.SimulatedTime; 59 | } 60 | 61 | /** 62 | * This routine resets the simulation time to zero and removes all 63 | * entries from the scheduler queue (as their times may no longer 64 | * be valid). Whatever operation caused the processes to become 65 | * suspended will raise the RestartSimulation exception, which the 66 | * application should catch. It should then perform any work necessary 67 | * to put the process back in a state ready for restarting the simulation 68 | * before calling Cancel on the process. 69 | * 70 | * @throws SimulationException if an error occurs. 71 | */ 72 | 73 | static synchronized void reset () throws SimulationException 74 | { 75 | boolean finished = false; 76 | SimulationProcess tmp = SimulationProcess.current(); 77 | 78 | // set resetting process to idle 79 | 80 | Scheduler.unschedule(tmp); // remove from queue 81 | 82 | do 83 | { 84 | try 85 | { 86 | tmp = Scheduler.ReadyQueue.remove(); 87 | tmp.deactivate(); 88 | } 89 | catch (NoSuchElementException e) 90 | { 91 | finished = true; 92 | } 93 | 94 | } while (!finished); 95 | 96 | finished = false; 97 | 98 | SimulationProcessIterator iter = new SimulationProcessIterator(SimulationProcess.allProcesses); 99 | 100 | do 101 | { 102 | try 103 | { 104 | tmp = iter.get(); 105 | 106 | /* 107 | * Every process must be in Suspend, so we call Resume 108 | * and get each one to check whether the simulation is 109 | * restarting. If it is, it raises an exception and waits 110 | * for the user to cancel the process after setting it 111 | * to become ready to restart. 112 | */ 113 | 114 | tmp.resumeProcess(); 115 | 116 | /* 117 | * Wait for this process to become idle again. 118 | */ 119 | 120 | while (!tmp.idle()) 121 | Thread.yield(); 122 | } 123 | catch (NullPointerException e) 124 | { 125 | finished = true; 126 | } 127 | 128 | } while (!finished); 129 | 130 | Scheduler.SimulatedTime = 0.0; 131 | 132 | SimulationProcess.Current = null; 133 | SimulationProcess.allProcesses = new SimulationProcessList(); 134 | } 135 | 136 | private Scheduler () 137 | { 138 | } 139 | 140 | /** 141 | * It is possible that the currently active process may remove itself 142 | * from the simulation queue. In which case we don't want to suspend the 143 | * process since it needs to continue to run. The return value indicates 144 | * whether or not to call suspend on the currently active process. 145 | */ 146 | 147 | static synchronized boolean schedule () throws SimulationException 148 | { 149 | if (Simulation.isStarted()) 150 | { 151 | SimulationProcess p = SimulationProcess.current(); 152 | 153 | try 154 | { 155 | /* 156 | * For some reason when executing tests in junit an old and dead 157 | * thread appears in the simulation queue. Have only ever seen this 158 | * be a single thread instance, but it is reproducible every time. 159 | * 160 | * https://github.com/nmcl/JavaSim/issues/64 161 | * 162 | * Will try to find out what actually causes this and remove the 163 | * workaround eventually. 164 | * 165 | * https://github.com/nmcl/JavaSim/issues/76 166 | */ 167 | 168 | SimulationProcess.Current = Scheduler.ReadyQueue.remove(); 169 | boolean done = true; 170 | 171 | do 172 | { 173 | if (SimulationProcess.Current != null) 174 | { 175 | if (SimulationProcess.Current.getThreadGroup() == null) 176 | { 177 | SimulationProcess.Current = Scheduler.ReadyQueue.remove(); 178 | p = SimulationProcess.current(); 179 | done = false; 180 | } 181 | else 182 | done = true; 183 | } 184 | else 185 | throw new NoSuchElementException(); 186 | } 187 | while (!done); 188 | } 189 | catch (NoSuchElementException e) 190 | { 191 | System.out.println("Simulation queue empty."); 192 | 193 | return false; 194 | } 195 | catch (NullPointerException e) 196 | { 197 | System.out.println("Simulation queue empty."); 198 | 199 | return false; 200 | } 201 | 202 | if (SimulationProcess.Current.evtime() < 0) 203 | throw new SimulationException("Invalid SimulationProcess wakeup time."); 204 | else 205 | Scheduler.SimulatedTime = SimulationProcess.Current.evtime(); 206 | 207 | if (p != SimulationProcess.Current) 208 | { 209 | // Simulation.printQueue(); 210 | 211 | SimulationProcess.Current.resumeProcess(); 212 | 213 | return true; 214 | } 215 | else 216 | return false; 217 | } 218 | else 219 | throw new SimulationException("Simulation not started."); 220 | } 221 | 222 | static synchronized void unschedule (SimulationProcess p) 223 | { 224 | try 225 | { 226 | Scheduler.ReadyQueue.remove(p); // remove from queue 227 | } 228 | catch (NoSuchElementException e) 229 | { 230 | } 231 | 232 | p.deactivate(); 233 | } 234 | 235 | static SimulationProcessList getQueue () 236 | { 237 | synchronized (theScheduler) 238 | { 239 | return ReadyQueue; 240 | } 241 | } 242 | 243 | static double getSimulationTime () 244 | { 245 | synchronized (theScheduler) 246 | { 247 | return SimulatedTime; 248 | } 249 | } 250 | 251 | private static double SimulatedTime = 0.0; 252 | private static SimulationProcessList ReadyQueue = new SimulationProcessList(); 253 | 254 | static Scheduler theScheduler = new Scheduler(); 255 | } 256 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/Semaphore.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim; 22 | 23 | /** 24 | * Classic semaphores can "accumulate" more resources than the starting 25 | * value. The ceiling parameter is used to indicate whether or not the 26 | * resource count should ever go beyond the initial value - the default 27 | * is that it should. 28 | */ 29 | 30 | public class Semaphore 31 | { 32 | /** 33 | * The result of various operations: 34 | * DONE - succeeded. 35 | * NOTDONE - failed. 36 | * WOULD_BLOCK - the operation would block the thread. 37 | * 38 | * Note that for some operations only DONE and NOTDONE 39 | * could be returned. 40 | */ 41 | 42 | public enum Outcome { DONE, NOTDONE, WOULD_BLOCK }; 43 | 44 | /** 45 | * Create a new mutex (resources = 1). 46 | */ 47 | 48 | public Semaphore() 49 | { 50 | numberWaiting = 0; 51 | numberOfResources = 1; 52 | currentResources = 1; 53 | waitingList = new TriggerQueue(); 54 | } 55 | 56 | /** 57 | * Create a new semaphore (resources = number). 58 | * 59 | * @param number the number of resources. 60 | */ 61 | 62 | public Semaphore(long number) 63 | { 64 | numberWaiting = 0; 65 | numberOfResources = number; 66 | currentResources = number; 67 | waitingList = new TriggerQueue(); 68 | } 69 | 70 | public void finalize () 71 | { 72 | if (numberWaiting != 0) 73 | System.out 74 | .println("Warning: semaphore being removed with clients waiting."); 75 | } 76 | 77 | /** 78 | * Number of entities blocked on the semaphore. 79 | * 80 | * @return the number of entities blocked. 81 | */ 82 | 83 | public long numberWaiting () 84 | { 85 | return numberWaiting; 86 | } 87 | 88 | /** 89 | * Try to acquire the semaphore. Caller will be blocked if there are no free 90 | * resources. 91 | * 92 | * @param toWait the entity that will be blocked. 93 | * @return an indication of the outcome (DONE, NOTDONE) 94 | * @throws RestartException if a reset occurs while an entity is blocked. 95 | */ 96 | 97 | public Outcome get (SimulationEntity toWait) 98 | throws RestartException 99 | { 100 | if (currentResources > 0) 101 | currentResources--; 102 | else 103 | { 104 | numberWaiting++; 105 | 106 | try 107 | { 108 | waitingList.insert(toWait); 109 | } 110 | catch (SimulationException e) 111 | { 112 | } 113 | 114 | toWait.cancel(); 115 | } 116 | 117 | return Outcome.DONE; 118 | } 119 | 120 | /** 121 | * Only acquire the semaphore if it would not block the caller. 122 | * 123 | * @param toWait the entity to block. 124 | * @return the outcome (DONE, NOTDONE or WOULD_BLOCK) 125 | */ 126 | 127 | public Outcome tryGet (SimulationEntity toWait) 128 | throws RestartException 129 | { 130 | if (currentResources == 0) 131 | return Outcome.WOULD_BLOCK; 132 | else 133 | return get(toWait); 134 | } 135 | 136 | /** 137 | * Release the semaphore. No check is made to ensure the caller has 138 | * previously acquired the semaphore. 139 | * 140 | * @return the outcome (DONE or NOTDONE) 141 | */ 142 | 143 | public Outcome release () 144 | { 145 | // if there are things waiting they get triggered right here 146 | // and recomsume the resource that would have been freed otherwise 147 | // by this release call 148 | if (numberWaiting > 0) 149 | { 150 | numberWaiting--; 151 | 152 | waitingList.triggerFirst(true); 153 | 154 | return Outcome.DONE; 155 | } 156 | else 157 | { 158 | // There is nothing waiting so we can free up a resource 159 | currentResources++; 160 | 161 | return Outcome.DONE; 162 | } 163 | } 164 | 165 | private TriggerQueue waitingList; 166 | 167 | private long numberWaiting; 168 | 169 | private long numberOfResources; 170 | 171 | private long currentResources; 172 | } 173 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/Simulation.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 20015, 19 | */ 20 | 21 | package org.javasim; 22 | 23 | import java.util.NoSuchElementException; 24 | 25 | import org.javasim.internal.SimulationProcessIterator; 26 | import org.javasim.internal.SimulationProcessList; 27 | 28 | /** 29 | * A class to encapsulate the various methods to start, stop or 30 | * reset the simulation. 31 | * 32 | * @author marklittle 33 | * 34 | */ 35 | public class Simulation 36 | { 37 | /** 38 | * This routine resets the simulation time to zero and removes all 39 | * entries from the scheduler queue (as their times may no longer 40 | * be valid). Whatever operation caused the processes to become 41 | * suspended will raise the RestartSimulation exception, which the 42 | * application should catch. It should then perform any work necessary 43 | * to put the process back in a state ready for restarting the simulation 44 | * before calling Cancel on the process. 45 | * 46 | * @throws SimulationException if an error occurs. 47 | */ 48 | 49 | public static synchronized void reset () throws SimulationException 50 | { 51 | Simulation._reset = true; 52 | 53 | try 54 | { 55 | Scheduler.reset(); 56 | } 57 | finally 58 | { 59 | Simulation._reset = false; 60 | } 61 | } 62 | 63 | /** 64 | * Is the simulation undergoing a reset? Processes should call this 65 | * method to determine whether the simulation is being reset. If it 66 | * is, then they should act accordingly. 67 | * 68 | * @return true if the simulation is being reset, false otherwise. 69 | */ 70 | 71 | public static synchronized boolean isReset () 72 | { 73 | return Simulation._reset; 74 | } 75 | 76 | /** 77 | * Stop the simulation. Processes should call this 78 | * method to determine whether the simulation is being stopped. If it 79 | * is, then they should act accordingly. 80 | */ 81 | 82 | public static synchronized void stop () 83 | { 84 | Simulation.running = false; 85 | } 86 | 87 | /** 88 | * Start the simulation either from the start or from where it was 89 | * previously stopped. 90 | */ 91 | 92 | public static synchronized void start () 93 | { 94 | Simulation.running = true; 95 | } 96 | 97 | /** 98 | * Print out the contents of the current simulation queue. 99 | */ 100 | 101 | public static synchronized void printQueue () 102 | { 103 | SimulationProcess.allProcesses.print(); 104 | } 105 | 106 | /** 107 | * Has the simulation started? 108 | * 109 | * @return true if the simulation is running, false 110 | * otherwise. 111 | */ 112 | 113 | protected static synchronized boolean isStarted () 114 | { 115 | return Simulation.running; 116 | } 117 | 118 | private static boolean running = false; 119 | private static boolean _reset = false; 120 | } 121 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/SimulationException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim; 22 | 23 | public class SimulationException extends Exception 24 | { 25 | public static final long serialVersionUID = 0xdeadbeef; 26 | 27 | public SimulationException() 28 | { 29 | super(); 30 | } 31 | 32 | public SimulationException(String s) 33 | { 34 | super(s); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/TriggerQueue.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim; 22 | 23 | import java.util.LinkedList; 24 | import java.util.NoSuchElementException; 25 | 26 | /** 27 | * A TriggerQueue is used to hold simulation process instances. 28 | */ 29 | 30 | public class TriggerQueue 31 | { 32 | /** 33 | * Create a new instance. 34 | */ 35 | 36 | public TriggerQueue() 37 | { 38 | head = new LinkedList(); 39 | } 40 | 41 | /** 42 | * If there is anything left on the queue when this goes out-of-scope then 43 | * we will place them back on to the simulation queue, rather than doing 44 | * nothing - this is the safest thing to do. THIS SHOULD NOT HAPPEN THOUGH!* 45 | */ 46 | 47 | public void finalize () 48 | { 49 | if (head != null) 50 | { 51 | if (head.size() > 0) 52 | { 53 | System.out 54 | .println("TriggerQueue destructor called with non-zero list!"); 55 | 56 | try 57 | { 58 | triggerAll(); 59 | } 60 | catch (NoSuchElementException e) 61 | { 62 | } 63 | } 64 | } 65 | } 66 | 67 | /** 68 | * Remove the first entry from the queue, possibly trigger it and then reactivate it 69 | * at the current simulation time. 70 | * 71 | * @param setTrigger indicate whether or not to also trigger the removed process. 72 | * @throws NoSuchElementException thrown if the queue is empty. 73 | */ 74 | 75 | public synchronized void triggerFirst (boolean setTrigger) 76 | throws NoSuchElementException 77 | { 78 | if (head.size() == 0) 79 | throw new NoSuchElementException(); 80 | 81 | SimulationEntity removed = this.remove(); 82 | 83 | if (setTrigger) 84 | removed.trigger(); 85 | 86 | try 87 | { 88 | removed.reactivateAt(SimulationProcess.currentTime()); 89 | } 90 | catch (RestartException e) 91 | { 92 | } 93 | catch (SimulationException e) 94 | { 95 | } 96 | } 97 | 98 | /** 99 | * Remove the first instance from the queue, trigger it and activate it at 100 | * the current time. 101 | * 102 | * @throws NoSuchElementException thrown if the queue is empty. 103 | */ 104 | 105 | public synchronized void triggerFirst () throws NoSuchElementException 106 | { 107 | triggerFirst(true); 108 | } 109 | 110 | /** 111 | * Remove all simulation processes from the queue and trigger them. 112 | * 113 | * @throws NoSuchElementException thrown if the queue is empty. 114 | */ 115 | 116 | public synchronized void triggerAll () throws NoSuchElementException 117 | { 118 | long currentNumber = head.size(); 119 | 120 | if (currentNumber == 0) 121 | throw new NoSuchElementException(); 122 | 123 | for (int i = 0; i < currentNumber; i++) 124 | triggerFirst(); 125 | } 126 | 127 | protected synchronized void insert (SimulationEntity toAdd) 128 | throws SimulationException 129 | { 130 | if (toAdd.isWaiting()) 131 | throw new SimulationException("Entity already waiting on event."); 132 | 133 | head.add(toAdd); 134 | } 135 | 136 | protected synchronized SimulationEntity remove () 137 | throws NoSuchElementException 138 | { 139 | return head.removeFirst(); 140 | } 141 | 142 | private LinkedList head; 143 | } 144 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/internal/SimulationProcessIterator.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.internal; 22 | 23 | import org.javasim.SimulationProcess; 24 | 25 | public class SimulationProcessIterator 26 | { 27 | public SimulationProcessIterator(SimulationProcessList L) 28 | { 29 | ptr = L.Head; 30 | } 31 | 32 | public final synchronized SimulationProcess get () 33 | { 34 | if (ptr != null) 35 | { 36 | SimulationProcessCons p = ptr; 37 | ptr = ptr.cdr(); 38 | return p.car(); 39 | } 40 | 41 | return null; 42 | } 43 | 44 | private SimulationProcessCons ptr; 45 | 46 | }; 47 | 48 | class SimulationProcessCons 49 | { 50 | 51 | public SimulationProcessCons(SimulationProcess p, SimulationProcessCons n) 52 | { 53 | Proc = p; 54 | Next = n; 55 | } 56 | 57 | public final SimulationProcess car () 58 | { 59 | return Proc; 60 | } 61 | 62 | public final SimulationProcessCons cdr () 63 | { 64 | return Next; 65 | } 66 | 67 | public final void setfCdr (SimulationProcessCons n) 68 | { 69 | Next = n; 70 | } 71 | 72 | private SimulationProcess Proc; 73 | 74 | private SimulationProcessCons Next; 75 | } 76 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/internal/SimulationProcessList.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.internal; 22 | 23 | import java.util.NoSuchElementException; 24 | 25 | import org.javasim.SimulationProcess; 26 | 27 | public class SimulationProcessList 28 | { 29 | public SimulationProcessList() 30 | { 31 | Head = null; 32 | } 33 | 34 | public synchronized void insert (SimulationProcess p) 35 | { 36 | insert(p, false); 37 | } 38 | 39 | public synchronized void insert (SimulationProcess p, boolean prior) 40 | { 41 | // If list is empty, insert at head 42 | 43 | if (Head == null) 44 | { 45 | Head = new SimulationProcessCons(p, null); 46 | return; 47 | } 48 | 49 | // Try to insert before (if there is anything scheduled later) 50 | 51 | SimulationProcessIterator iter = new SimulationProcessIterator(this); 52 | SimulationProcess prev = null; 53 | 54 | for (SimulationProcess q = iter.get(); q != null; prev = q, q = iter 55 | .get()) 56 | { 57 | if (prior) 58 | { 59 | if (q.evtime() >= p.evtime()) 60 | { 61 | insertBefore(p, q); 62 | return; 63 | } 64 | } 65 | else 66 | { 67 | if (q.evtime() > p.evtime()) 68 | { 69 | insertBefore(p, q); 70 | return; 71 | } 72 | } 73 | } 74 | 75 | // Got to insert at the end (currently pointed at by 'prev') 76 | 77 | insertAfter(p, prev); 78 | } 79 | 80 | public synchronized boolean insertBefore (SimulationProcess ToInsert, 81 | SimulationProcess Before) 82 | { 83 | for (SimulationProcessCons prev = null, p = Head; p != null; prev = p, p = p 84 | .cdr()) 85 | { 86 | if (p.car() == Before) 87 | { 88 | SimulationProcessCons newcons = new SimulationProcessCons( 89 | ToInsert, p); 90 | if (prev != null) 91 | prev.setfCdr(newcons); 92 | else 93 | Head = newcons; 94 | 95 | return true; 96 | } 97 | } 98 | 99 | return false; 100 | } 101 | 102 | public synchronized boolean insertAfter (SimulationProcess ToInsert, 103 | SimulationProcess After) 104 | { 105 | for (SimulationProcessCons p = Head; p != null; p = p.cdr()) 106 | if (p.car() == After) 107 | { 108 | SimulationProcessCons newcons = new SimulationProcessCons( 109 | ToInsert, p.cdr()); 110 | p.setfCdr(newcons); 111 | return true; 112 | } 113 | 114 | return false; 115 | } 116 | 117 | public synchronized SimulationProcess remove (SimulationProcess element) 118 | throws NoSuchElementException 119 | { 120 | // Take care of boundary condition - empty list 121 | 122 | if (Head == null) 123 | throw new NoSuchElementException(); 124 | 125 | SimulationProcess p = null; 126 | 127 | for (SimulationProcessCons prev = null, ptr = Head; ptr != null; prev = ptr, ptr = ptr 128 | .cdr()) 129 | { 130 | if (ptr.car() == element) 131 | { 132 | SimulationProcessCons oldcons = ptr; 133 | 134 | // unlink the cons cell for the element we're removing 135 | 136 | if (prev != null) 137 | prev.setfCdr(ptr.cdr()); 138 | else 139 | Head = ptr.cdr(); 140 | 141 | // return the pointer to the process 142 | p = ptr.car(); 143 | 144 | return p; 145 | } 146 | } 147 | 148 | throw new NoSuchElementException(); 149 | } 150 | 151 | public synchronized SimulationProcess remove () 152 | throws NoSuchElementException 153 | { 154 | // Change unspecified element to "remove head of list" request 155 | 156 | if (Head != null) 157 | return (remove(Head.car())); 158 | else 159 | throw new NoSuchElementException(); 160 | } 161 | 162 | public synchronized SimulationProcess getNext (SimulationProcess current) 163 | throws NoSuchElementException 164 | { 165 | // take care of boundary condition - empty list. 166 | 167 | if ((Head == null) || (current == null)) 168 | throw new NoSuchElementException(); 169 | 170 | for (SimulationProcessCons ptr = Head; ptr != null; ptr = ptr.cdr()) 171 | { 172 | if (ptr.car() == current) 173 | { 174 | if (ptr.cdr() == null) 175 | return null; 176 | else 177 | return ptr.cdr().car(); 178 | } 179 | else // terminate search - past the point current could be 180 | if (ptr.car().evtime() > current.evtime()) 181 | break; 182 | } 183 | 184 | /* 185 | * If we get here then we have not found current on the list which can 186 | * only mean that it is currently active. 187 | */ 188 | 189 | return Head.car(); 190 | } 191 | 192 | public void print () 193 | { 194 | SimulationProcessIterator iter = new SimulationProcessIterator(this); 195 | SimulationProcess prev = null; 196 | 197 | for (SimulationProcess q = iter.get(); q != null; prev = q, q = iter 198 | .get()) 199 | { 200 | System.out.println(q.evtime()); 201 | } 202 | } 203 | 204 | // package? 205 | 206 | protected SimulationProcessCons Head; 207 | } 208 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/simset/Head.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2010, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2010, 19 | */ 20 | 21 | package org.javasim.simset; 22 | 23 | /* 24 | * This class essentially defines the linked list manager used by the SIMSET 25 | * class in SIMULA. 26 | */ 27 | 28 | // Thanks to Jim Bean for converting the C++SIM classes 29 | 30 | /** 31 | * @deprecated As of release 2.1 use Java's own linked lists. 32 | */ 33 | 34 | public class Head { 35 | 36 | public Head () { 37 | this.first = null; 38 | this.last = null; 39 | } 40 | 41 | /** 42 | * @return the first entry in the list. 43 | */ 44 | 45 | synchronized public Link first () {return first;}; 46 | 47 | /** 48 | * @return the last entry in the list. 49 | */ 50 | 51 | synchronized public Link last () {return last;}; 52 | 53 | /** 54 | * @param element make this the first element in the list. 55 | */ 56 | 57 | public void addFirst (Link element) { 58 | 59 | if (element == null) // nothing to add 60 | return; 61 | 62 | if (element.inList()) // if in another list 63 | element.out(); // pull it out. 64 | 65 | if (first == null) { // Queue presently empty 66 | 67 | first = element; 68 | last = element; 69 | element.theList = this; 70 | 71 | } else { 72 | element.precede(first); 73 | first = element; 74 | } 75 | } 76 | 77 | /** 78 | * @param element make this the last entry in the list. 79 | */ 80 | 81 | public void addLast (Link element) { 82 | 83 | if (element == null) // nothing to add 84 | return; 85 | 86 | if (element.inList()) // if in another list 87 | element.out(); // pull it out. 88 | 89 | if (last == null) { // Queue presently empty 90 | 91 | first = element; 92 | last = element; 93 | element.theList = this; 94 | 95 | } else { 96 | element.follow(last); 97 | last = element; 98 | } 99 | } 100 | 101 | /** 102 | * @return the number of elements in the list. 103 | */ 104 | 105 | synchronized public long cardinal () { 106 | long numberOfElements = 0; 107 | Link tempPtr = first; 108 | 109 | while (tempPtr != null) { 110 | 111 | numberOfElements++; 112 | tempPtr = tempPtr.suc(); 113 | } 114 | 115 | return numberOfElements; 116 | } 117 | 118 | /** 119 | * @return true if the list is empty, false otherwise. 120 | */ 121 | 122 | synchronized public boolean empty () {return cardinal() == 0;} 123 | 124 | /** 125 | * Empty the list. 126 | */ 127 | 128 | synchronized public void clear () { 129 | Link tempPtr = first, marker = null; 130 | 131 | while (tempPtr != null) { 132 | 133 | marker = tempPtr; 134 | tempPtr = tempPtr.suc(); 135 | marker = null; 136 | } 137 | 138 | first = null; 139 | last = null; 140 | } 141 | 142 | protected Link first; 143 | protected Link last; 144 | } 145 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/simset/Link.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2010, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2010, 19 | */ 20 | 21 | package org.javasim.simset; 22 | 23 | /* 24 | * This class defines the elements of the linked lists within SIMSET. 25 | */ 26 | 27 | // Thanks to Jim Bean for converting the C++SIM classes 28 | 29 | /** 30 | * @deprecated As of release 2.1 use Java's own linked lists. 31 | */ 32 | 33 | public class Link { 34 | 35 | public Link () { 36 | this.next = null; 37 | this.prev = null; 38 | this.theList = null; 39 | } 40 | 41 | /** 42 | * @return the successor to this list element. 43 | */ 44 | 45 | synchronized public Link suc () { 46 | return next; 47 | } 48 | 49 | /** 50 | * @return the predecessor to this list element. 51 | */ 52 | 53 | synchronized public Link pred () { 54 | return prev; 55 | } 56 | 57 | /** 58 | * @return this element in the list and remove it from the list. 59 | */ 60 | 61 | synchronized public Link out () { 62 | RemoveElement (); 63 | return this; 64 | } 65 | 66 | /** 67 | * Add this entrt to the list. 68 | * 69 | * @param list the list to add this element. 70 | */ 71 | 72 | synchronized public void inTo (Head list) { 73 | if (list != null) { 74 | 75 | list.addLast (this); 76 | theList = list; 77 | return; 78 | 79 | } 80 | } 81 | 82 | /** 83 | * Add this list element to the same list as the element provided. Make 84 | * sure elements are only on one list. 85 | * 86 | * @param toPrecede the element to add before in its list. If null, or the element 87 | * is not in a list, then remove this element from any list it may be in to 88 | * match. 89 | */ 90 | 91 | synchronized public void precede (Link toPrecede) { 92 | if ((toPrecede == null) || ( ! toPrecede.inList())) 93 | 94 | RemoveElement(); 95 | 96 | else { 97 | if (inList()) 98 | RemoveElement(); 99 | 100 | toPrecede.addBefore(this); 101 | } 102 | }; 103 | 104 | /** 105 | * Add this list element to the same list as the element provided. Make 106 | * sure elements are only on one list. 107 | * 108 | * @param toFollow the element to add after in its list. If null, or the element 109 | * is not in a list, then remove this element from any list it may be in to 110 | * match. 111 | */ 112 | 113 | synchronized public void follow (Link toFollow) { 114 | if ((toFollow == null) || ( ! toFollow.inList())) 115 | 116 | RemoveElement(); 117 | 118 | else { 119 | if (inList()) 120 | RemoveElement(); 121 | 122 | toFollow.addAfter(this); 123 | } 124 | } 125 | 126 | /** 127 | * Add this element to the head of a list. 128 | * 129 | * @param list the list to use. 130 | */ 131 | 132 | synchronized public void follow (Head list) { 133 | if (list != null) 134 | list.addFirst(this); 135 | } 136 | 137 | /** 138 | * @return true if this element is in a list, false otherwise. 139 | */ 140 | 141 | synchronized public boolean inList () { 142 | return (boolean) (theList != null); 143 | } 144 | 145 | private void RemoveElement () { 146 | 147 | // can't have prev and next if we are not on a list 148 | if (theList == null) 149 | return; 150 | 151 | if (prev != null) 152 | prev.next = next; 153 | 154 | if (next != null) 155 | next.prev = prev; 156 | 157 | if (theList.first == this) 158 | theList.first = next; 159 | 160 | if (theList.last == this) 161 | theList.last = prev; 162 | 163 | theList = null; 164 | prev = null; 165 | next = null; 166 | } 167 | 168 | private void addAfter (Link toAdd) { 169 | toAdd.prev = this; 170 | toAdd.theList = theList; 171 | 172 | if (next == null) 173 | next = toAdd; 174 | 175 | else { 176 | next.prev = toAdd; 177 | toAdd.next = next; 178 | next = toAdd; 179 | } 180 | 181 | if (theList.last == this) 182 | theList.last = toAdd; 183 | } 184 | 185 | private void addBefore (Link toAdd) { 186 | toAdd.theList = theList; 187 | toAdd.next = this; 188 | 189 | if (prev == null) 190 | prev = toAdd; 191 | 192 | else { 193 | prev.next = toAdd; 194 | toAdd.prev = prev; 195 | prev = toAdd; 196 | } 197 | 198 | if (theList.first == this) 199 | theList.first = toAdd; 200 | } 201 | 202 | protected Link next; 203 | protected Link prev; 204 | protected Head theList = new Head(); 205 | } 206 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/stats/Bucket.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.stats; 22 | 23 | public class Bucket 24 | { 25 | /** 26 | * Create bucket with name 'n' and number of entries 'initEntries'. 27 | */ 28 | 29 | public Bucket(double n, long initEntries) 30 | { 31 | numberOfEntries = initEntries; 32 | name = n; 33 | next = null; 34 | } 35 | 36 | /** 37 | * Create bucket with name 'n' and 1 entry. 38 | */ 39 | 40 | public Bucket(double n) 41 | { 42 | numberOfEntries = 1; 43 | name = n; 44 | next = null; 45 | } 46 | 47 | /** 48 | * Copy constructor. 49 | */ 50 | 51 | public Bucket(Bucket b) 52 | { 53 | numberOfEntries = b.size(); 54 | name = b.name(); 55 | next = null; 56 | } 57 | 58 | /** 59 | * @return whether or not the name of the bucket equal to 'value'? 60 | */ 61 | 62 | public boolean equals (double value) 63 | { 64 | if (name == value) 65 | return true; 66 | else 67 | return false; 68 | } 69 | 70 | /** 71 | * @return whether or not the name of the bucket greater than 'value'? 72 | */ 73 | 74 | public boolean greaterThan (double value) 75 | { 76 | if (name > value) 77 | return true; 78 | else 79 | return false; 80 | } 81 | 82 | /** 83 | * @return whether or not the name of the bucket greater than or equal to 'value'? 84 | */ 85 | 86 | public boolean greaterThanOrEqual (double value) 87 | { 88 | if (name >= value) 89 | return true; 90 | else 91 | return false; 92 | } 93 | 94 | /** 95 | * @return whether or not the name of the bucket less than 'value'? 96 | */ 97 | 98 | public boolean lessThan (double value) 99 | { 100 | if (name < value) 101 | return true; 102 | else 103 | return false; 104 | } 105 | 106 | /** 107 | * @return whether or not the name of the bucket less than or equal to 'value'? 108 | */ 109 | 110 | public boolean lessThanOrEqual (double value) 111 | { 112 | if (name <= value) 113 | return true; 114 | else 115 | return false; 116 | } 117 | 118 | /** 119 | * @return the name of the bucket. 120 | */ 121 | 122 | public double name () 123 | { 124 | return name; 125 | } 126 | 127 | /** 128 | * Increment the number of entries by 'value'. 129 | * 130 | * @param value the increment. 131 | */ 132 | 133 | public void incrementSize (long value) 134 | { 135 | numberOfEntries += value; 136 | } 137 | 138 | /** 139 | * Set the number of entries to 'value'. 140 | * 141 | * @param value the size of the bucket. 142 | */ 143 | 144 | public void size (long value) 145 | { 146 | numberOfEntries = value; 147 | } 148 | 149 | /** 150 | * @return the number of entries. 151 | */ 152 | 153 | public long size () 154 | { 155 | return numberOfEntries; 156 | } 157 | 158 | /** 159 | * @return the next bucket. 160 | */ 161 | 162 | public Bucket cdr () 163 | { 164 | return next; 165 | } 166 | 167 | /** 168 | * Set the next bucket. 169 | * 170 | * @param n the next bucket. 171 | */ 172 | 173 | public void setCdr (Bucket n) 174 | { 175 | next = n; 176 | } 177 | 178 | private long numberOfEntries; 179 | 180 | private double name; 181 | 182 | private Bucket next; 183 | } 184 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/stats/Histogram.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.stats; 22 | 23 | import java.io.DataInputStream; 24 | import java.io.DataOutputStream; 25 | import java.io.FileInputStream; 26 | import java.io.FileNotFoundException; 27 | import java.io.FileOutputStream; 28 | import java.io.IOException; 29 | 30 | /** 31 | * This histogram class maintains a fixed number of buckets. When the number of 32 | * buckets required to maintain all of the values given is about to be exceeded 33 | * a merge operation is performed. This takes a pair of buckets and merges their 34 | * values according to the policy selected when the histogram was created. The 35 | * policies are: (1) ACCUMULATE - this creates a new bucket with the same name 36 | * as the largest of the buckets, and it has the sum of the two old bucket 37 | * entries as its entry number. (2) MEAN - this creates a new bucket with the 38 | * name as the mean of the two old buckets, and it has the sum of the two old 39 | * bucket entries as its entry number. (3) MAX - this creates a new bucket with 40 | * the name as the largest of the buckets, and it has the same number of 41 | * entries. (4) MIN - this creates a new bucket with the name as the smallest of 42 | * the two buckets, and it has the same number of entries. 43 | */ 44 | 45 | public class Histogram extends PrecisionHistogram 46 | { 47 | public static final int ACCUMULATE = 0; 48 | 49 | public static final int MEAN = 1; 50 | 51 | public static final int MAX = 2; 52 | 53 | public static final int MIN = 3; 54 | 55 | /** 56 | * Create with maximum index 'maxIndex' and specified 'mergeChoice', which 57 | * will be used if the buckets must be merged. 58 | */ 59 | 60 | public Histogram(long maxIndex, int mergeChoice) 61 | { 62 | if (maxIndex > 1) 63 | maxSize = maxIndex; 64 | else 65 | maxSize = 2; 66 | 67 | merge = mergeChoice; 68 | } 69 | 70 | /** 71 | * Create with maximum index 'maxIndex'. Merge choice is MEAN. 72 | */ 73 | 74 | public Histogram(long maxIndex) 75 | { 76 | if (maxIndex > 1) 77 | maxSize = maxIndex; 78 | else 79 | maxSize = 2; 80 | 81 | merge = Histogram.MEAN; 82 | } 83 | 84 | /** 85 | * Add 'value' to the histogram. If a bucket already exists for this then it 86 | * is incremented, otherwise a new bucket will be created. This may require 87 | * the existing buckets to be merged to make room. 88 | * 89 | * @param value the number to use. 90 | */ 91 | 92 | public void setValue (double value) throws IllegalArgumentException 93 | { 94 | if ((numberOfBuckets() == maxSize) && (!isPresent(value))) 95 | { 96 | try 97 | { 98 | mergeBuckets(); 99 | } 100 | catch (StatisticsException e) 101 | { 102 | } 103 | } 104 | 105 | super.setValue(value); 106 | } 107 | 108 | /** 109 | * Save the state of the histogram to the file named 'fileName'. 110 | * 111 | * @param fileName the name of the file to use to save the state. 112 | * @return true if the state was written, false otherwise. 113 | */ 114 | 115 | public boolean saveState (String fileName) throws IOException 116 | { 117 | FileOutputStream f = new FileOutputStream(fileName); 118 | DataOutputStream iFile = new DataOutputStream(f); 119 | 120 | boolean res = saveState(iFile); 121 | 122 | f.close(); 123 | 124 | return res; 125 | } 126 | 127 | /** 128 | * Save the state of the histogram to the stream 'oFile'. 129 | * 130 | * @param oFile the name of the DataOutputStream to use to save the state. 131 | * @return true if the state was written, false otherwise. 132 | */ 133 | 134 | public boolean saveState (DataOutputStream oFile) throws IOException 135 | { 136 | oFile.writeLong(maxSize); 137 | oFile.writeInt(merge); 138 | 139 | return super.saveState(oFile); 140 | } 141 | 142 | /** 143 | * Restore the histogram state from the file 'fileName'. 144 | * 145 | * @param fileName the name of the file to use to read the state. 146 | * @return true if the state was read, false otherwise. 147 | */ 148 | 149 | public boolean restoreState (String fileName) throws FileNotFoundException, 150 | IOException 151 | { 152 | FileInputStream f = new FileInputStream(fileName); 153 | DataInputStream oFile = new DataInputStream(f); 154 | 155 | boolean res = restoreState(oFile); 156 | 157 | f.close(); 158 | 159 | return res; 160 | } 161 | 162 | /** 163 | * Restore the histogram state from the stream 'iFile'. 164 | * 165 | * @param iFile the name of the DataInputStream to use to read the state. 166 | * @return true if the state was read, false otherwise. 167 | */ 168 | 169 | public boolean restoreState (DataInputStream iFile) throws IOException 170 | { 171 | maxSize = iFile.readLong(); 172 | merge = iFile.readInt(); 173 | 174 | return super.restoreState(iFile); 175 | } 176 | 177 | /** 178 | * Print the contents of the histogram. 179 | */ 180 | 181 | public void print () 182 | { 183 | System.out.println("Maximum number of buckets " + maxSize); 184 | System.out.print("Merge choice is "); 185 | 186 | switch (merge) 187 | { 188 | case Histogram.ACCUMULATE: 189 | System.out.println("ACCUMULATE"); 190 | break; 191 | case Histogram.MEAN: 192 | System.out.println("MEAN"); 193 | break; 194 | case Histogram.MAX: 195 | System.out.println("MAX"); 196 | break; 197 | case Histogram.MIN: 198 | System.out.println("MIN"); 199 | break; 200 | } 201 | 202 | super.print(); 203 | } 204 | 205 | /** 206 | * Merge the existing buckets according to the merge criteria specified when 207 | * the histogram was created. 208 | */ 209 | 210 | protected void mergeBuckets () throws StatisticsException 211 | { 212 | Bucket newHead = null, ptr = null; 213 | Bucket index = Head; 214 | long newLength = 0; 215 | 216 | index = super.Head; 217 | 218 | while (index != null) 219 | { 220 | Bucket newElement = null; 221 | 222 | // merge buckets in pairs 223 | 224 | if (index.cdr() != null) 225 | { 226 | newElement = new Bucket(compositeName(index, index.cdr())); 227 | newElement.size(compositeSize(index, index.cdr())); 228 | 229 | // move on to next pair of buckets 230 | 231 | index = (index.cdr()).cdr(); 232 | } 233 | else 234 | newElement = new Bucket(index); 235 | 236 | newLength++; 237 | if (newHead != null) 238 | ptr.setCdr(newElement); 239 | else 240 | newHead = newElement; 241 | 242 | ptr = newElement; 243 | } 244 | 245 | index = super.Head; 246 | ptr = index; 247 | 248 | while (index != null) 249 | { 250 | ptr = index.cdr(); 251 | index = ptr; 252 | } 253 | 254 | super.Head = newHead; 255 | super.length = newLength; 256 | } 257 | 258 | private double compositeName (Bucket a, Bucket b) 259 | { 260 | switch (merge) 261 | { 262 | case ACCUMULATE: 263 | case MAX: 264 | return b.name(); 265 | case MEAN: 266 | return ((a.name() * a.size() + b.name() * b.size()) / (a.size() + b 267 | .size())); 268 | case MIN: 269 | return a.name(); 270 | default: 271 | break; 272 | } 273 | 274 | return 0.0; 275 | } 276 | 277 | private long compositeSize (Bucket a, Bucket b) throws StatisticsException 278 | { 279 | switch (merge) 280 | { 281 | case ACCUMULATE: 282 | return (a.size() + b.size()); 283 | case MEAN: 284 | return (a.size() + b.size()); 285 | case MAX: 286 | return b.size(); 287 | case MIN: 288 | return a.size(); 289 | default: 290 | break; 291 | } 292 | 293 | // shouldn't get here! 294 | 295 | throw new StatisticsException("compositeSize switch error."); 296 | } 297 | 298 | protected long maxSize; 299 | 300 | protected int merge; 301 | } 302 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/stats/Mean.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.stats; 22 | 23 | import java.io.*; 24 | 25 | import java.io.FileNotFoundException; 26 | import java.io.IOException; 27 | 28 | public class Mean 29 | { 30 | public Mean() 31 | { 32 | reset(); 33 | } 34 | 35 | /** 36 | * @param value add to this instance, incrementing the number of samples, the sum, mean, etc. 37 | */ 38 | 39 | public void setValue (double value) throws IllegalArgumentException 40 | { 41 | if (value > _Max) 42 | _Max = value; 43 | if (value < _Min) 44 | _Min = value; 45 | _Sum += value; 46 | _Number++; 47 | _Mean = (double) (_Sum / _Number); 48 | } 49 | 50 | /** 51 | * Reset the object. 52 | */ 53 | 54 | public void reset () 55 | { 56 | _Max = Float.MIN_VALUE; 57 | _Min = Float.MAX_VALUE; 58 | _Sum = _Mean = 0.0; 59 | _Number = 0; 60 | } 61 | 62 | /** 63 | * @return the number of samples. 64 | */ 65 | 66 | public int numberOfSamples () 67 | { 68 | return _Number; 69 | } 70 | 71 | /** 72 | * @return the minimum value given. 73 | */ 74 | 75 | public double min () 76 | { 77 | return _Min; 78 | } 79 | 80 | /** 81 | * @return the maximum value given. 82 | */ 83 | 84 | public double max () 85 | { 86 | return _Max; 87 | } 88 | 89 | /** 90 | * @return the sum of all values. 91 | */ 92 | 93 | public double sum () 94 | { 95 | return _Sum; 96 | } 97 | 98 | /** 99 | * @return the mean value. 100 | */ 101 | 102 | public double mean () 103 | { 104 | return _Mean; 105 | } 106 | 107 | /** 108 | * Save the state of the histogram to the file named 'fileName'. 109 | * 110 | * @param fileName the file to use. 111 | * @return true if save succeeded, false otherwise. 112 | */ 113 | 114 | public boolean saveState (String fileName) throws IOException 115 | { 116 | FileOutputStream f = new FileOutputStream(fileName); 117 | DataOutputStream oFile = new DataOutputStream(f); 118 | 119 | boolean res = saveState(oFile); 120 | 121 | f.close(); 122 | 123 | return res; 124 | } 125 | 126 | /** 127 | * Save the state of the histogram to the stream 'oFile'. 128 | * 129 | * @param oFile the stream to use. 130 | * @return true if save succeeded, false otherwise. 131 | */ 132 | 133 | public boolean saveState (DataOutputStream oFile) throws IOException 134 | { 135 | oFile.writeDouble(_Max); 136 | oFile.writeDouble(_Min); 137 | oFile.writeDouble(_Sum); 138 | oFile.writeDouble(_Mean); 139 | oFile.writeInt(_Number); 140 | 141 | return true; 142 | } 143 | 144 | /** 145 | * Restore the histogram state from the file 'fileName'. 146 | * 147 | * @param fileName the file to use. 148 | * @return true if the restore succeeded, false otherwise. 149 | */ 150 | 151 | public boolean restoreState (String fileName) throws FileNotFoundException, 152 | IOException 153 | { 154 | FileInputStream f = new FileInputStream(fileName); 155 | DataInputStream iFile = new DataInputStream(f); 156 | 157 | boolean res = restoreState(iFile); 158 | 159 | f.close(); 160 | 161 | return res; 162 | } 163 | 164 | /** 165 | * Restore the histogram state from the stream 'iFile'. 166 | * 167 | * @param iFile the file to use. 168 | * @return true if the restore succeeded, false otherwise. 169 | */ 170 | 171 | public boolean restoreState (DataInputStream iFile) throws IOException 172 | { 173 | _Max = iFile.readDouble(); 174 | _Min = iFile.readDouble(); 175 | _Sum = iFile.readDouble(); 176 | _Mean = iFile.readDouble(); 177 | _Number = iFile.readInt(); 178 | 179 | return true; 180 | } 181 | 182 | /** 183 | * Print out the statistics compiled on the data given. 184 | */ 185 | 186 | public void print () 187 | { 188 | System.out.println("Number of samples : " + numberOfSamples()); 189 | System.out.println("Minimum : " + min()); 190 | System.out.println("Maximum : " + max()); 191 | System.out.println("Sum : " + sum()); 192 | System.out.println("Mean : " + mean()); 193 | } 194 | 195 | protected double _Max; 196 | 197 | protected double _Min; 198 | 199 | protected double _Sum; 200 | 201 | protected double _Mean; 202 | 203 | protected int _Number; 204 | } 205 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/stats/Quantile.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.stats; 22 | 23 | /** 24 | * Provides a means of obtaining the p-quantile of a distribution, i.e., the 25 | * value below which p-percent of the distribution lies. 26 | */ 27 | 28 | public class Quantile extends PrecisionHistogram 29 | { 30 | /** 31 | * Create with 95% probability. 32 | */ 33 | 34 | public Quantile() 35 | { 36 | qProb = 0.95; 37 | } 38 | 39 | /** 40 | * Create with the specified probability. If the probability it greater than 41 | * 100% (1.0) or less than or equal to 0% then throw an exception. 42 | * 43 | * @param q the probability to use for this instance. 44 | */ 45 | 46 | public Quantile(double q) throws IllegalArgumentException 47 | { 48 | qProb = q; 49 | 50 | if ((q <= 0.0) || (q > 1.0)) 51 | throw new IllegalArgumentException("Quantile::Quantile ( " + q 52 | + " ) : bad value."); 53 | } 54 | 55 | /** 56 | * @return the p-quantile. 57 | */ 58 | 59 | public double getValue () 60 | { 61 | double pSamples = numberOfSamples() * qProb; 62 | long nEntries = 0; 63 | Bucket ptr = Head, trail = null; 64 | 65 | while ((nEntries < pSamples) && (ptr != null)) 66 | { 67 | nEntries += ptr.size(); 68 | trail = ptr; 69 | ptr = ptr.cdr(); 70 | } 71 | 72 | return trail.name(); 73 | } 74 | 75 | /** 76 | * @return the p-quantile percentage. 77 | */ 78 | 79 | public double range () 80 | { 81 | return qProb; 82 | } 83 | 84 | /** 85 | * Print out the quantile information. 86 | */ 87 | 88 | public void print () 89 | { 90 | System.out.println("Quantile precentage : " + qProb); 91 | System.out.println("Value below which percentage occurs " 92 | + this.getValue()); 93 | super.print(); 94 | } 95 | 96 | private double qProb; 97 | } 98 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/stats/SimpleHistogram.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.stats; 22 | 23 | import java.io.*; 24 | 25 | import java.io.FileNotFoundException; 26 | import java.io.IOException; 27 | 28 | /** 29 | * A simple histogram with a set number of buckets. 30 | */ 31 | 32 | public class SimpleHistogram extends PrecisionHistogram 33 | { 34 | /** 35 | * Create with 'nbuckets' evenly distributed over the range 'min' to 'max'. 36 | */ 37 | 38 | public SimpleHistogram(double min, double max, long nbuckets) 39 | { 40 | if (min < max) 41 | { 42 | minIndex = min; 43 | maxIndex = max; 44 | } 45 | else 46 | { 47 | minIndex = max; 48 | maxIndex = min; 49 | } 50 | 51 | if (nbuckets > 0) 52 | numberBuckets = nbuckets; 53 | else 54 | nbuckets = 1; 55 | 56 | width = (max - min) / numberBuckets; 57 | reset(); 58 | } 59 | 60 | /** 61 | * Create a number of buckets with width 'w' evenly distributed over the 62 | * range 'min' to 'max'. 63 | */ 64 | 65 | public SimpleHistogram(double min, double max, double w) 66 | { 67 | if (min < max) 68 | { 69 | minIndex = min; 70 | maxIndex = max; 71 | } 72 | else 73 | { 74 | minIndex = max; 75 | maxIndex = min; 76 | } 77 | 78 | if (w > 0) 79 | width = w; 80 | else 81 | width = 2.0; 82 | 83 | numberBuckets = (long) ((max - min) / width); 84 | 85 | if ((max - min) / width - numberBuckets > 0) 86 | numberBuckets++; 87 | 88 | reset(); 89 | } 90 | 91 | /** 92 | * @param value add to the histogram. If it is outside the range of the histogram 93 | * then raise an exception, otherwise find the appropriate bucket and 94 | * increment it. 95 | */ 96 | 97 | public void setValue (double value) throws IllegalArgumentException 98 | { 99 | if ((value < minIndex) || (value > maxIndex)) 100 | throw new IllegalArgumentException("Value " + value 101 | + " is beyond histogram range [ " + minIndex + ", " 102 | + maxIndex + " ]"); 103 | 104 | for (Bucket ptr = Head; ptr != null; ptr = ptr.cdr()) 105 | { 106 | double bucketValue = ptr.name(); 107 | 108 | if ((value == bucketValue) || (value <= bucketValue + width)) 109 | { 110 | super.setValue(ptr.name()); 111 | return; 112 | } 113 | } 114 | 115 | // shouldn't get here!! 116 | 117 | throw new IllegalArgumentException("Something went wrong with " 118 | + value); 119 | } 120 | 121 | /** 122 | * Empty the histogram. Always keep the number of buckets that 123 | * were originally specified though. 124 | */ 125 | 126 | public void reset () 127 | { 128 | double value = minIndex; 129 | 130 | super.reset(); 131 | 132 | // pre-create buckets with given width 133 | 134 | for (int i = 0; i < numberBuckets; value += width, i++) 135 | create(value); 136 | } 137 | 138 | /** 139 | * Get the number of entries in bucket 'name'. 140 | * 141 | * @param name the id of the bucket. 142 | * @return the number of entries in the bucket. 143 | */ 144 | 145 | public double sizeByName (double name) throws IllegalArgumentException 146 | { 147 | if ((name < minIndex) || (name > maxIndex)) 148 | throw new IllegalArgumentException("Argument out of range."); 149 | 150 | for (Bucket ptr = Head; ptr != null; ptr = ptr.cdr()) 151 | { 152 | double bucketValue = ptr.name(); 153 | 154 | if ((name == bucketValue) || (name <= bucketValue + width)) 155 | return ptr.size(); 156 | } 157 | 158 | throw new IllegalArgumentException("Name " + name + " out of range."); 159 | } 160 | 161 | /** 162 | * @return the width of each bucket. 163 | */ 164 | 165 | public double Width () 166 | { 167 | return width; 168 | } 169 | 170 | /** 171 | * Print out information about the histogram. 172 | */ 173 | 174 | public void print () 175 | { 176 | System.out.println("SimpleHistogram Data:"); 177 | System.out.println("Maximum index range : " + maxIndex); 178 | System.out.println("Minimum index range : " + minIndex); 179 | System.out.println("Number of buckets : " + numberBuckets); 180 | System.out.println("Width of each bucket : " + width); 181 | 182 | super.print(); 183 | } 184 | 185 | /** 186 | * Save the state of the histogram to the file named 'fileName'. 187 | * 188 | * @param fileName the file to use. 189 | * @return true if it succeeded, false otherwise. 190 | */ 191 | 192 | public boolean saveState (String fileName) throws IOException 193 | { 194 | FileOutputStream f = new FileOutputStream(fileName); 195 | DataOutputStream oFile = new DataOutputStream(f); 196 | 197 | boolean res = saveState(oFile); 198 | 199 | f.close(); 200 | 201 | return res; 202 | } 203 | 204 | /** 205 | * Save the state of the histogram to the stream 'oFile'. 206 | * 207 | * @param oFile the stream to use. 208 | * @return true if it succeeded, false otherwise. 209 | */ 210 | 211 | public boolean saveState (DataOutputStream oFile) throws IOException 212 | { 213 | oFile.writeDouble(minIndex); 214 | oFile.writeDouble(maxIndex); 215 | oFile.writeDouble(width); 216 | oFile.writeLong(numberBuckets); 217 | 218 | return super.saveState(oFile); 219 | } 220 | 221 | /** 222 | * Restore the histogram state from the file 'fileName'. 223 | * 224 | * @param fileName the file to use. 225 | * @return true if it succeeded, false otherwise. 226 | */ 227 | 228 | public boolean restoreState (String fileName) throws FileNotFoundException, 229 | IOException 230 | { 231 | FileInputStream f = new FileInputStream(fileName); 232 | DataInputStream iFile = new DataInputStream(f); 233 | 234 | boolean res = restoreState(iFile); 235 | 236 | f.close(); 237 | 238 | return res; 239 | } 240 | 241 | /** 242 | * Restore the histogram state from the stream 'iFile'. 243 | * 244 | * @param iFile the stream to use. 245 | * @return true if it succeeded, false otherwise. 246 | */ 247 | 248 | public boolean restoreState (DataInputStream iFile) throws IOException 249 | { 250 | minIndex = iFile.readDouble(); 251 | maxIndex = iFile.readDouble(); 252 | width = iFile.readDouble(); 253 | numberBuckets = iFile.readLong(); 254 | 255 | return super.restoreState(iFile); 256 | } 257 | 258 | private double minIndex; 259 | 260 | private double maxIndex; 261 | 262 | private double width; 263 | 264 | private long numberBuckets; 265 | } 266 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/stats/StatisticsException.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.stats; 22 | 23 | /** 24 | * General exception thrown by the statistics gathering classes. 25 | */ 26 | 27 | public class StatisticsException extends Exception 28 | { 29 | public static final long serialVersionUID = 0xdeadbeef; 30 | 31 | public StatisticsException() 32 | { 33 | super(); 34 | } 35 | 36 | public StatisticsException(String s) 37 | { 38 | super(s); 39 | } 40 | } -------------------------------------------------------------------------------- /src/main/java/org/javasim/stats/TimeVariance.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.stats; 22 | 23 | import java.io.*; 24 | 25 | import org.javasim.SimulationProcess; 26 | 27 | /** 28 | * Obtain the average value given during the simulation time. 29 | */ 30 | 31 | public class TimeVariance extends Variance 32 | { 33 | public TimeVariance() 34 | { 35 | reset(); 36 | } 37 | 38 | /** 39 | * Zero the statistics. 40 | */ 41 | 42 | public void reset () 43 | { 44 | first = true; 45 | startTime = currentValue = 0.0; 46 | stime = total = 0.0; 47 | super.reset(); 48 | } 49 | 50 | /** 51 | * @param value Add to the instance, updating the statistics. 52 | */ 53 | 54 | public void setValue (double value) throws IllegalArgumentException 55 | { 56 | super.setValue(value); 57 | 58 | if (!first) 59 | { 60 | total += area(); 61 | if (value == currentValue) 62 | return; 63 | } 64 | else 65 | { 66 | first = false; 67 | startTime = SimulationProcess.currentTime(); 68 | } 69 | 70 | store(value); 71 | } 72 | 73 | /** 74 | * @return the average value given up to the current simulation time. 75 | */ 76 | 77 | public double timeAverage () 78 | { 79 | if (first || (SimulationProcess.currentTime() - startTime) == 0) 80 | return 0.0; 81 | 82 | return ((total + area()) / (SimulationProcess.currentTime() - startTime)); 83 | } 84 | 85 | /** 86 | * Save the state of the histogram to the file named 'fileName'. 87 | * 88 | * @param fileName the file to use. 89 | * @return true if it succeeds, false otherwise. 90 | */ 91 | 92 | public boolean saveState (String fileName) throws IOException 93 | { 94 | FileOutputStream f = new FileOutputStream(fileName); 95 | DataOutputStream oFile = new DataOutputStream(f); 96 | 97 | boolean res = saveState(oFile); 98 | 99 | f.close(); 100 | 101 | return res; 102 | } 103 | 104 | /** 105 | * Save the state of the histogram to the stream 'oFile'. 106 | * 107 | * @param oFile the stream to use. 108 | * @return true if it succeeds, false otherwise. 109 | */ 110 | 111 | public boolean saveState (DataOutputStream oFile) throws IOException 112 | { 113 | oFile.writeBoolean(first); 114 | oFile.writeDouble(startTime); 115 | oFile.writeDouble(currentValue); 116 | oFile.writeDouble(stime); 117 | oFile.writeDouble(total); 118 | 119 | return super.saveState(oFile); 120 | } 121 | 122 | /** 123 | * Restore the histogram state from the file 'fileName'. 124 | * 125 | * @param fileName the file to use. 126 | * @return true if it succeeds, false otherwise. 127 | */ 128 | 129 | public boolean restoreState (String fileName) throws FileNotFoundException, 130 | IOException 131 | { 132 | FileInputStream f = new FileInputStream(fileName); 133 | DataInputStream iFile = new DataInputStream(f); 134 | 135 | boolean res = restoreState(iFile); 136 | 137 | f.close(); 138 | 139 | return res; 140 | } 141 | 142 | /** 143 | * Restore the histogram state from the stream 'iFile'. 144 | * 145 | * @param iFile the stream to use. 146 | * @return true if it succeeds, false otherwise. 147 | */ 148 | 149 | public boolean restoreState (DataInputStream iFile) throws IOException 150 | { 151 | first = iFile.readBoolean(); 152 | startTime = iFile.readDouble(); 153 | currentValue = iFile.readDouble(); 154 | stime = iFile.readDouble(); 155 | total = iFile.readDouble(); 156 | 157 | return super.restoreState(iFile); 158 | } 159 | 160 | private double area () 161 | { 162 | return (currentValue * (SimulationProcess.currentTime() - stime)); 163 | } 164 | 165 | private void store (double value) 166 | { 167 | currentValue = value; 168 | stime = SimulationProcess.currentTime(); 169 | } 170 | 171 | private boolean first; 172 | 173 | private double startTime; 174 | 175 | private double currentValue; 176 | 177 | private double stime; 178 | 179 | private double total; 180 | } 181 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/stats/Variance.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.stats; 22 | 23 | import java.io.DataInputStream; 24 | import java.io.DataOutputStream; 25 | import java.io.FileInputStream; 26 | import java.io.FileNotFoundException; 27 | import java.io.FileOutputStream; 28 | import java.io.IOException; 29 | 30 | /** 31 | * Used to obtain the variance of the samples given. 32 | */ 33 | 34 | public class Variance extends Mean 35 | { 36 | public Variance() 37 | { 38 | reset(); 39 | } 40 | 41 | /** 42 | * @param Add to the instance, updating the variance. 43 | */ 44 | 45 | public void setValue (double value) throws IllegalArgumentException 46 | { 47 | _sqr += value * value; 48 | super.setValue(value); 49 | } 50 | 51 | /** 52 | * Zero the statistics. 53 | */ 54 | 55 | public void reset () 56 | { 57 | _sqr = 0.0; 58 | super.reset(); 59 | } 60 | 61 | /** 62 | * @return the variance. 63 | */ 64 | 65 | public double variance () 66 | { 67 | if (_Number > 1) 68 | return ((_sqr - ((_Sum * _Sum) / _Number)) / (_Number - 1)); 69 | else 70 | return 0.0; 71 | } 72 | 73 | /** 74 | * @return the standard deviation of the samples. 75 | */ 76 | 77 | public double stdDev () 78 | { 79 | if (_Number == 0 || variance() <= 0) 80 | return 0.0; 81 | else 82 | return Math.sqrt(variance()); 83 | } 84 | 85 | /** 86 | * @return the confidence. 87 | * 88 | * @param value the confidence range should be between 0 and 0.9999 89 | */ 90 | 91 | public double confidence (double value) throws IllegalArgumentException 92 | { 93 | if ((value > 1) || (value < 0)) 94 | throw new IllegalArgumentException(); 95 | 96 | return mean() + (1+value)*stdDev(); 97 | } 98 | 99 | /** 100 | * Prints out the statistics information. 101 | */ 102 | 103 | public void print () 104 | { 105 | System.out.println("Variance : " + variance()); 106 | System.out.println("Standard Deviation: " + stdDev()); 107 | 108 | super.print(); 109 | } 110 | 111 | /** 112 | * Save the state of the histogram to the file named 'fileName'. 113 | * 114 | * @param fileName the file to use. 115 | * @return true if it succeeds, false otherwise. 116 | */ 117 | 118 | public boolean saveState (String fileName) throws IOException 119 | { 120 | FileOutputStream f = new FileOutputStream(fileName); 121 | DataOutputStream oFile = new DataOutputStream(f); 122 | 123 | boolean res = saveState(oFile); 124 | 125 | f.close(); 126 | 127 | return res; 128 | } 129 | 130 | /** 131 | * Save the state of the histogram to the stream 'oFile'. 132 | * 133 | * @param oFile the stream to use. 134 | * @return true if it succeeds, false otherwise. 135 | */ 136 | 137 | public boolean saveState (DataOutputStream oFile) throws IOException 138 | { 139 | oFile.writeDouble(_sqr); 140 | return super.saveState(oFile); 141 | } 142 | 143 | /** 144 | * Restore the histogram state from the file 'fileName'. 145 | * 146 | * @param fileName the file to use. 147 | * @return true if it succeeds, false otherwise. 148 | */ 149 | 150 | public boolean restoreState (String fileName) throws FileNotFoundException, 151 | IOException 152 | { 153 | FileInputStream f = new FileInputStream(fileName); 154 | DataInputStream iFile = new DataInputStream(f); 155 | 156 | boolean res = restoreState(iFile); 157 | 158 | f.close(); 159 | 160 | return res; 161 | } 162 | 163 | /** 164 | * Restore the histogram state from the stream 'iFile'. 165 | * 166 | * @param iFile the stream to use. 167 | * @return true if it succeeds, false otherwise. 168 | */ 169 | 170 | public boolean restoreState (DataInputStream iFile) throws IOException 171 | { 172 | _sqr = iFile.readDouble(); 173 | 174 | return super.restoreState(iFile); 175 | } 176 | 177 | private double _sqr; 178 | } 179 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/streams/Draw.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.streams; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * Return true or false with probability given when constructed. Uses a 27 | * UniformStream. 28 | */ 29 | 30 | public class Draw 31 | { 32 | /** 33 | * Probability of true is 'p'. 34 | */ 35 | 36 | public Draw(double p) 37 | { 38 | s = new UniformStream(0, 1); 39 | prob = p; 40 | } 41 | 42 | /** 43 | * Probability 'p'. Ignore the first 'StreamSelect' values before starting 44 | * to return values. 45 | */ 46 | 47 | public Draw(double p, int StreamSelect) 48 | { 49 | s = new UniformStream(0, 1, StreamSelect); 50 | prob = p; 51 | } 52 | 53 | /** 54 | * Probability 'p'. Ignore the first 'StreamSelect' values before starting 55 | * to return values. The seeds to the UniformStream are 'MGSeed' and 56 | * 'LGSeed'. 57 | */ 58 | 59 | public Draw(double p, int StreamSelect, long MGSeed, long LCGSeed) 60 | { 61 | s = new UniformStream(0, 1, StreamSelect, MGSeed, LCGSeed); 62 | prob = p; 63 | } 64 | 65 | /** 66 | * @return true with specified probability. 67 | */ 68 | 69 | public boolean getBoolean () throws IOException 70 | { 71 | if (s.getNumber() >= prob) 72 | return true; 73 | else 74 | return false; 75 | } 76 | 77 | private UniformStream s; 78 | 79 | private double prob; 80 | } 81 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/streams/ErlangStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.streams; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * Returns a number from an Erlang distribution with the given mean and standard 27 | * deviation. 28 | */ 29 | 30 | public class ErlangStream extends RandomStream 31 | { 32 | /** 33 | * Create a stream with mean 'm' and standard deviation 'sd'. 34 | */ 35 | 36 | public ErlangStream(double m, double sd) 37 | { 38 | super(); 39 | 40 | Mean = m; 41 | StandardDeviation = sd; 42 | 43 | double z = Mean / StandardDeviation; 44 | k = (long) (z * z); 45 | } 46 | 47 | /** 48 | * Create a stream with mean 'm' and standard deviation 'sd'. Ignore the 49 | * first 'StreamSelect' values before starting to return values. 50 | */ 51 | 52 | public ErlangStream(double m, double sd, int StreamSelect) 53 | { 54 | super(); 55 | 56 | Mean = m; 57 | StandardDeviation = sd; 58 | 59 | double z = Mean / StandardDeviation; 60 | k = (long) (z * z); 61 | for (int ss = 0; ss < StreamSelect * 1000; ss++) 62 | uniform(); 63 | } 64 | 65 | /** 66 | * Create a stream with mean 'm' and standard deviation 'sd'. Ignore the 67 | * first 'StreamSelect' values before starting to return values. The seeds 68 | * to the RandomStream are 'MGSeed' and 'LGSeed'. 69 | */ 70 | 71 | public ErlangStream(double m, double sd, int StreamSelect, long MGSeed, 72 | long LCGSeed) 73 | { 74 | super(MGSeed, LCGSeed); 75 | 76 | Mean = m; 77 | StandardDeviation = sd; 78 | 79 | double z = Mean / StandardDeviation; 80 | k = (long) (z * z); 81 | for (int ss = 0; ss < StreamSelect * 1000; ss++) 82 | uniform(); 83 | } 84 | 85 | /** 86 | * @return a stream number. 87 | */ 88 | 89 | public double getNumber () throws IOException, ArithmeticException 90 | { 91 | double z = 1.0; 92 | for (int i = 0; i < k; i++) 93 | z *= uniform(); 94 | 95 | return -(Mean / k) * Math.log(z); 96 | } 97 | 98 | private double Mean; 99 | 100 | private double StandardDeviation; 101 | 102 | private long k; 103 | } 104 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/streams/ExponentialStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.streams; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * Returns a number from an exponential distribution with the given mean. 27 | */ 28 | 29 | public class ExponentialStream extends RandomStream 30 | { 31 | /** 32 | * Create stream with mean 'm'. 33 | */ 34 | 35 | public ExponentialStream(double m) 36 | { 37 | super(); 38 | 39 | Mean = m; 40 | } 41 | 42 | /** 43 | * Create stream with mean 'm'. Skip the first 'StreamSelect' stream values. 44 | */ 45 | 46 | public ExponentialStream(double m, int StreamSelect) 47 | { 48 | super(); 49 | 50 | Mean = m; 51 | 52 | for (int i = 0; i < StreamSelect * 1000; i++) 53 | uniform(); 54 | } 55 | 56 | /** 57 | * Create stream with mean 'm'. Skip the first 'StreamSelect' stream values. 58 | * Pass seeds 'MGSeed' and 'LCGSeed' to the base class. 59 | */ 60 | 61 | public ExponentialStream(double m, int StreamSelect, long MGSeed, 62 | long LCGSeed) 63 | { 64 | super(MGSeed, LCGSeed); 65 | 66 | Mean = m; 67 | 68 | for (int i = 0; i < StreamSelect * 1000; i++) 69 | uniform(); 70 | } 71 | 72 | /** 73 | * @return stream number. 74 | */ 75 | 76 | public double getNumber () throws IOException, ArithmeticException 77 | { 78 | return -Mean * Math.log(uniform()); 79 | } 80 | 81 | private double Mean; 82 | } 83 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/streams/HyperExponentialStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.streams; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * Returns a number from a hyperexpontial distribution with the given mean and 27 | * standard deviation. 28 | */ 29 | 30 | public class HyperExponentialStream extends RandomStream 31 | { 32 | /** 33 | * Create stream with mean 'm' and standard deviation 'sd'. 34 | */ 35 | 36 | public HyperExponentialStream(double m, double sd) 37 | { 38 | super(); 39 | 40 | mean = m; 41 | standardDeviation = sd; 42 | 43 | double cv, z; 44 | cv = standardDeviation / mean; 45 | z = cv * cv; 46 | p = 0.5 * (1.0 - Math.sqrt((z - 1.0) / (z + 1.0))); 47 | } 48 | 49 | /** 50 | * Create stream with mean 'm' and standard deviation 'sd'. Skip the first 51 | * 'StreamSelect' values. 52 | */ 53 | 54 | public HyperExponentialStream(double m, double sd, int StreamSelect) 55 | { 56 | super(); 57 | 58 | mean = m; 59 | standardDeviation = sd; 60 | 61 | double cv, z; 62 | cv = standardDeviation / mean; 63 | z = cv * cv; 64 | p = 0.5 * (1.0 - Math.sqrt((z - 1.0) / (z + 1.0))); 65 | 66 | for (int ss = 0; ss < StreamSelect * 1000; ss++) 67 | uniform(); 68 | } 69 | 70 | /** 71 | * Create stream with mean 'm' and standard deviation 'sd'. Skip the first 72 | * 'StreamSelect' values. Pass seeds 'MGSeed' and 'LCGSeed' to the base 73 | * class. 74 | */ 75 | 76 | public HyperExponentialStream(double m, double sd, int StreamSelect, 77 | long MGSeed, long LCGSeed) 78 | { 79 | super(MGSeed, LCGSeed); 80 | 81 | mean = m; 82 | standardDeviation = sd; 83 | 84 | double cv, z; 85 | cv = standardDeviation / mean; 86 | z = cv * cv; 87 | p = 0.5 * (1.0 - Math.sqrt((z - 1.0) / (z + 1.0))); 88 | 89 | for (int ss = 0; ss < StreamSelect * 1000; ss++) 90 | uniform(); 91 | } 92 | 93 | /** 94 | * @return a value from the stream. 95 | */ 96 | 97 | public double getNumber () throws IOException, ArithmeticException 98 | { 99 | double z = 0; 100 | 101 | if (uniform() > p) 102 | z = mean / (1.0 - p); 103 | else 104 | z = mean / p; 105 | 106 | return -0.5 * z * Math.log(uniform()); 107 | } 108 | 109 | private double mean; 110 | 111 | private double standardDeviation; 112 | 113 | private double p; 114 | } 115 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/streams/NormalStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.streams; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * Returns a number from a normal distribution with the given mean and standard 27 | * deviation. 28 | */ 29 | 30 | public class NormalStream extends RandomStream 31 | { 32 | /** 33 | * Create stream with mean 'm' and standard deviation 'sd'. 34 | */ 35 | 36 | public NormalStream(double m, double sd) 37 | { 38 | super(); 39 | 40 | mean = m; 41 | standardDeviation = sd; 42 | z = 0.0; 43 | } 44 | 45 | /** 46 | * Create stream with mean 'm' and standard deviation 'sd'. Skip the first 47 | * 'StreamSelect' values. 48 | */ 49 | 50 | public NormalStream(double m, double sd, int StreamSelect) 51 | { 52 | super(); 53 | 54 | mean = m; 55 | standardDeviation = sd; 56 | z = 0.0; 57 | 58 | for (int ss = 0; ss < StreamSelect * 1000; ss++) 59 | uniform(); 60 | } 61 | 62 | /** 63 | * Create stream with mean 'm' and standard deviation 'sd'. Skip the first 64 | * 'StreamSelect' values. Pass seeds 'MGSeed' and 'LCGSeed' to the base 65 | * class. 66 | */ 67 | 68 | public NormalStream(double m, double sd, int StreamSelect, long MGSeed, 69 | long LCGSeed) 70 | { 71 | super(MGSeed, LCGSeed); 72 | 73 | mean = m; 74 | standardDeviation = sd; 75 | z = 0.0; 76 | 77 | for (int ss = 0; ss < StreamSelect * 1000; ss++) 78 | uniform(); 79 | } 80 | 81 | /** 82 | * Use the polar method, due to Box, Muller and 83 | * Marsaglia.Taken from Seminumerical Algorithms, Knuth, Addison-Wesley, 84 | * p.117. 85 | * 86 | * @return a stream number. 87 | */ 88 | 89 | public double getNumber () throws IOException, ArithmeticException 90 | { 91 | // Use the polar method, due to Box, Muller and Marsaglia 92 | // Taken from Seminumerical Algorithms, Knuth, Addison-Wesley, p.117 93 | 94 | double X2; 95 | 96 | if (z != 0.0) 97 | { 98 | X2 = z; 99 | z = 0.0; 100 | } 101 | else 102 | { 103 | double S, v1, v2; 104 | do 105 | { 106 | v1 = 2.0 * uniform() - 1.0; 107 | v2 = 2.0 * uniform() - 1.0; 108 | S = v1 * v1 + v2 * v2; 109 | } 110 | while (S >= 1.0); 111 | 112 | S = Math.sqrt((-2.0 * Math.log(S)) / S); 113 | X2 = v1 * S; 114 | z = v2 * S; 115 | } 116 | 117 | return mean + X2 * standardDeviation; 118 | } 119 | 120 | private double mean; 121 | 122 | private double standardDeviation; 123 | 124 | private double z; 125 | } 126 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/streams/RandomStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.streams; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * The class RandomStream is the base class from which the other distribution 27 | * classes are derived. It uses a linear congruential generator based on the 28 | * algorithm from "Algorithms", R. Sedgewick, Addison-Wesley, Reading MA, 1983 29 | * pp. 36-38. The results of the LC generator are shuffled with a multiplicative 30 | * generator as suggested by Maclaren and Marsaglia (See Knuth Vol2, 31 | * Seminumerical Algorithms). The multiplicative generator is courtesy I. 32 | * Mitrani 1992, private correspondence: Y[i+1] = Y[i] * 5^5 mod 2^26, period is 33 | * 2^24, initial seed must be odd 34 | */ 35 | 36 | public abstract class RandomStream 37 | { 38 | /** 39 | * @return In derived classes this method returns the value obtained by the stream. 40 | * It must be redefined by the deriving class. 41 | */ 42 | 43 | public abstract double getNumber () throws IOException, ArithmeticException; 44 | 45 | /** 46 | * @return a chi-square error measure on the uniform distribution function. 47 | */ 48 | 49 | public final double error () 50 | { 51 | long r = 100; 52 | long N = 100 * r; 53 | long f[] = new long[100]; 54 | int i; 55 | 56 | for (i = 0; i < r; i++) 57 | f[i] = 0; 58 | for (i = 0; i < N; i++) 59 | f[(int) (uniform() * r)]++; 60 | long t = 0; 61 | for (i = 0; i < r; i++) 62 | t += f[i] * f[i]; 63 | double rt = (double) r * t; 64 | double rtN = rt / (double) N - (double) N; 65 | return 1.0 - (rtN / r); 66 | } 67 | 68 | protected RandomStream() 69 | { 70 | if (series == null) 71 | { 72 | series = new double[128]; 73 | 74 | mSeed = 772531; 75 | lSeed = 1878892440; 76 | 77 | for (int i = 0; i < RandomStream.sizeOfSeries 78 | / RandomStream.sizeOfDouble; i++) 79 | series[i] = mgen(); 80 | } 81 | } 82 | 83 | protected RandomStream(long MGSeed, long LCGSeed) 84 | { 85 | series = new double[128]; 86 | 87 | // Clean up input parameters 88 | 89 | if ((MGSeed & 1) == 0) 90 | MGSeed--; 91 | if (MGSeed < 0) 92 | MGSeed = -MGSeed; 93 | if (LCGSeed < 0) 94 | LCGSeed = -LCGSeed; 95 | 96 | // Initialise state 97 | 98 | mSeed = MGSeed; 99 | lSeed = LCGSeed; 100 | 101 | for (int i = 0; i < RandomStream.sizeOfSeries 102 | / RandomStream.sizeOfDouble; i++) 103 | series[i] = mgen(); 104 | } 105 | 106 | protected final double uniform () 107 | { 108 | // A linear congruential generator based on the algorithm from 109 | // "Algorithms", R. Sedgewick, Addison-Wesley, Reading MA, 1983. 110 | // pp. 36-38. 111 | 112 | long m = 100000000; 113 | long b = 31415821; 114 | long m1 = 10000; 115 | 116 | // Do the multiplication in pieces to avoid overflow 117 | 118 | long p0 = lSeed % m1, p1 = lSeed / m1, q0 = b % m1, q1 = b / m1; 119 | 120 | lSeed = (((((p0 * q1 + p1 * q0) % m1) * m1 + p0 * q0) % m) + 1) % m; 121 | 122 | // The results of the LC generator are shuffled with 123 | // the multiplicative generator as suggested by 124 | // Maclaren and Marsaglia (See Knuth Vol2, Seminumerical Algorithms) 125 | 126 | long choose = lSeed 127 | % (RandomStream.sizeOfSeries / RandomStream.sizeOfDouble); 128 | 129 | double result = series[(int) choose]; 130 | series[(int) choose] = mgen(); 131 | 132 | return result; 133 | } 134 | 135 | private double mgen () 136 | { 137 | // A multiplicative generator, courtesy I. Mitrani 1992, 138 | // private correspondence 139 | // Y[i+1] = Y[i] * 5^5 mod 2^26 140 | // period is 2^24, initial seed must be odd 141 | 142 | long two2the26th = 67108864; // 2**26 143 | 144 | mSeed = (mSeed * 25) % two2the26th; 145 | mSeed = (mSeed * 25) % two2the26th; 146 | mSeed = (mSeed * 5) % two2the26th; 147 | 148 | return (double) mSeed / (double) two2the26th; 149 | } 150 | 151 | private static long mSeed = 0; 152 | 153 | private static long lSeed = 0; 154 | 155 | private static double[] series = null; 156 | 157 | /* 158 | * We do this so that we can have the same results when running on most Unix 159 | * boxes with C++. It doesn't make any difference to the randomness of a 160 | * distribution. 161 | */ 162 | 163 | static private final long sizeOfSeries = 1024; 164 | 165 | static private final long sizeOfDouble = 8; 166 | } 167 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/streams/TriangularStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.streams; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * Returns a number drawn from a triangular distribution with lower limit a, upper limit b and mode c, where a < b and a ≤ c ≤ b. 27 | */ 28 | 29 | public class TriangularStream extends RandomStream { 30 | /** 31 | * Create stream with low bound 'l'(a) and high bound 'h'(b) and 'm'(c) value. 32 | */ 33 | 34 | public TriangularStream(double a, double b, double c) { 35 | super(); 36 | 37 | this.a = a; 38 | this.b = b; 39 | this.c = c; 40 | } 41 | 42 | /** 43 | * Create stream with low bound 'l'(a) and high bound 'h'(b) and 'm'(c) value. Skip the first 'StreamSelect' values before returning numbers from the stream. 44 | */ 45 | 46 | public TriangularStream(double a, double b, double c, int StreamSelect) { 47 | super(); 48 | 49 | this.a = a; 50 | this.b = b; 51 | this.c = c; 52 | 53 | for (int i = 0; i < StreamSelect * 1000; i++) 54 | uniform(); 55 | } 56 | 57 | /** 58 | * Create stream with low bound 'l'(a) and high bound 'h'(b) and 'm'(c) value. Skip the first 'StreamSelect' values before returning numbers from the stream. Pass the seeds 'MGSeed' and 'LCGSeed' to 59 | * the base class. 60 | */ 61 | 62 | public TriangularStream(double a, double b, double c, int StreamSelect, long MGSeed, long LCGSeed) { 63 | super(MGSeed, LCGSeed); 64 | 65 | this.a = a; 66 | this.b = b; 67 | this.c = c; 68 | 69 | for (int i = 0; i < StreamSelect * 1000; i++) 70 | uniform(); 71 | } 72 | 73 | /** 74 | * @return a number from the stream. 75 | */ 76 | 77 | public double getNumber() throws IOException, ArithmeticException { 78 | 79 | double F = (c - a) / (b - a); 80 | double rand = uniform(); 81 | if (rand < F) { 82 | return a + Math.sqrt(rand * (b - a) * (c - a)); 83 | } else { 84 | return b - Math.sqrt((1 - rand) * (b - a) * (b - c)); 85 | } 86 | 87 | } 88 | 89 | private double a; 90 | private double b; 91 | private double c; 92 | } 93 | -------------------------------------------------------------------------------- /src/main/java/org/javasim/streams/UniformStream.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.streams; 22 | 23 | import java.io.IOException; 24 | 25 | /** 26 | * Returns a number drawn from a uniform distribution with the given lower and 27 | * upper bounds. 28 | */ 29 | 30 | public class UniformStream extends RandomStream 31 | { 32 | /** 33 | * Create stream with low bound 'l' and high bound 'h'. 34 | */ 35 | 36 | public UniformStream(double l, double h) 37 | { 38 | super(); 39 | 40 | lo = l; 41 | hi = h; 42 | range = hi - lo; 43 | } 44 | 45 | /** 46 | * Create stream with low bound 'l' and high bound 'h'. Skip the first 47 | * 'StreamSelect' values before returning numbers from the stream. 48 | */ 49 | 50 | public UniformStream(double l, double h, int StreamSelect) 51 | { 52 | super(); 53 | 54 | lo = l; 55 | hi = h; 56 | range = hi - lo; 57 | 58 | for (int i = 0; i < StreamSelect * 1000; i++) 59 | uniform(); 60 | } 61 | 62 | /** 63 | * Create stream with low bound 'l' and high bound 'h'. Skip the first 64 | * 'StreamSelect' values before returning numbers from the stream. Pass the 65 | * seeds 'MGSeed' and 'LCGSeed' to the base class. 66 | */ 67 | 68 | public UniformStream(double l, double h, int StreamSelect, long MGSeed, 69 | long LCGSeed) 70 | { 71 | super(MGSeed, LCGSeed); 72 | 73 | lo = l; 74 | hi = h; 75 | range = hi - lo; 76 | 77 | for (int i = 0; i < StreamSelect * 1000; i++) 78 | uniform(); 79 | } 80 | 81 | /** 82 | * @return a number from the stream. 83 | */ 84 | 85 | public double getNumber () throws IOException, ArithmeticException 86 | { 87 | return lo + (range * uniform()); 88 | } 89 | 90 | private double lo; 91 | 92 | private double hi; 93 | 94 | private double range; 95 | } 96 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/SemaphoreUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests; 22 | 23 | import org.javasim.RestartException; 24 | import org.javasim.Semaphore; 25 | import org.javasim.SimulationEntity; 26 | import org.javasim.streams.ExponentialStream; 27 | import org.junit.Test; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | class DummyEntity extends SimulationEntity 32 | { 33 | public DummyEntity (double mean) 34 | { 35 | InterArrivalTime = new ExponentialStream(mean); 36 | } 37 | 38 | public void run () 39 | { 40 | try 41 | { 42 | hold(InterArrivalTime.getNumber()); 43 | } 44 | catch (final Exception ex) 45 | { 46 | } 47 | } 48 | 49 | private ExponentialStream InterArrivalTime; 50 | } 51 | 52 | 53 | public class SemaphoreUnitTest 54 | { 55 | @Test 56 | public void test () throws Exception 57 | { 58 | Semaphore sem = new Semaphore(2); 59 | DummyEntity e1 = new DummyEntity(10); 60 | DummyEntity e2 = new DummyEntity(20); 61 | DummyEntity e3 = new DummyEntity(30); 62 | 63 | assertTrue(sem.numberWaiting() == 0); 64 | 65 | Semaphore.Outcome result = sem.get(e1); 66 | 67 | assertTrue(result == Semaphore.Outcome.DONE); 68 | 69 | result = sem.get(e2); 70 | 71 | assertTrue(result == Semaphore.Outcome.DONE); 72 | 73 | result = sem.tryGet(e3); 74 | 75 | assertTrue(result == Semaphore.Outcome.WOULD_BLOCK); 76 | 77 | result = sem.get(e3); 78 | 79 | assertTrue(result == Semaphore.Outcome.DONE); 80 | assertTrue(sem.numberWaiting() == 1); 81 | } 82 | } 83 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/SimulationProcessUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests; 22 | 23 | import org.javasim.RestartException; 24 | import org.javasim.Simulation; 25 | import org.javasim.SimulationException; 26 | import org.javasim.SimulationProcess; 27 | import org.javasim.streams.ExponentialStream; 28 | import org.junit.Test; 29 | 30 | import static org.junit.Assert.*; 31 | 32 | class Dummy extends SimulationProcess 33 | { 34 | public Dummy (double mean) 35 | { 36 | InterArrivalTime = new ExponentialStream(mean); 37 | } 38 | 39 | public void run () 40 | { 41 | try 42 | { 43 | hold(InterArrivalTime.getNumber()); 44 | } 45 | catch (final Exception ex) 46 | { 47 | } 48 | } 49 | 50 | private ExponentialStream InterArrivalTime; 51 | } 52 | 53 | class Runner extends SimulationProcess 54 | { 55 | public Runner () 56 | { 57 | } 58 | 59 | public void run () 60 | { 61 | try 62 | { 63 | Dummy A = new Dummy(8); 64 | 65 | A.activateDelay(2000); 66 | 67 | Simulation.start(); 68 | 69 | hold(1000); 70 | 71 | Simulation.stop(); 72 | 73 | A.terminate(); 74 | 75 | SimulationProcess.mainResume(); 76 | } 77 | catch (final Exception e) 78 | { 79 | } 80 | } 81 | 82 | public void await () 83 | { 84 | this.resumeProcess(); 85 | SimulationProcess.mainSuspend(); 86 | } 87 | } 88 | 89 | 90 | public class SimulationProcessUnitTest 91 | { 92 | @Test 93 | public void test () throws Exception 94 | { 95 | Runner proc = new Runner(); 96 | 97 | proc.await(); 98 | 99 | assertTrue(proc.time() > 0.0); 100 | assertTrue(proc.nextEv() != null); 101 | assertTrue(proc.evtime() == 1000.0); 102 | 103 | assertFalse(proc.idle()); 104 | assertFalse(proc.passivated()); 105 | assertFalse(proc.terminated()); 106 | 107 | assertTrue(SimulationProcess.current() != null); 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/internal/SimulationProcessIteratorUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.internal; 22 | 23 | import org.javasim.SimulationProcess; 24 | import org.javasim.internal.SimulationProcessIterator; 25 | import org.javasim.internal.SimulationProcessList; 26 | import org.javasim.streams.ExponentialStream; 27 | import org.junit.Test; 28 | 29 | import static org.junit.Assert.*; 30 | 31 | class DummyProcess extends SimulationProcess 32 | { 33 | public DummyProcess (double mean) 34 | { 35 | InterArrivalTime = new ExponentialStream(mean); 36 | } 37 | 38 | public void run () 39 | { 40 | try 41 | { 42 | hold(InterArrivalTime.getNumber()); 43 | } 44 | catch (final Exception ex) 45 | { 46 | } 47 | } 48 | 49 | private ExponentialStream InterArrivalTime; 50 | } 51 | 52 | public class SimulationProcessIteratorUnitTest 53 | { 54 | @Test 55 | public void test () throws Exception 56 | { 57 | SimulationProcessList list = new SimulationProcessList(); 58 | DummyProcess d1 = new DummyProcess(0.0); 59 | DummyProcess d2 = new DummyProcess(1.0); 60 | 61 | list.insert(d1); 62 | list.insert(d2, true); 63 | 64 | SimulationProcessIterator iter = new SimulationProcessIterator(list); 65 | 66 | assertEquals(iter.get(), d2); 67 | assertEquals(iter.get(), d1); 68 | assertEquals(iter.get(), null); 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/internal/SimulationProcessListUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.internal; 22 | 23 | import org.javasim.SimulationProcess; 24 | import org.javasim.internal.SimulationProcessList; 25 | import org.javasim.streams.ExponentialStream; 26 | import org.junit.Test; 27 | 28 | import static org.junit.Assert.*; 29 | 30 | class Dummy extends SimulationProcess 31 | { 32 | public Dummy (double mean) 33 | { 34 | InterArrivalTime = new ExponentialStream(mean); 35 | } 36 | 37 | public void run () 38 | { 39 | try 40 | { 41 | hold(InterArrivalTime.getNumber()); 42 | } 43 | catch (final Exception ex) 44 | { 45 | } 46 | } 47 | 48 | private ExponentialStream InterArrivalTime; 49 | } 50 | 51 | public class SimulationProcessListUnitTest 52 | { 53 | @Test 54 | public void test () throws Exception 55 | { 56 | SimulationProcessList list = new SimulationProcessList(); 57 | Dummy d1 = new Dummy(0.0); 58 | Dummy d2 = new Dummy(1.0); 59 | 60 | try 61 | { 62 | list.remove(); 63 | 64 | fail(); 65 | } 66 | catch (final Exception ex) 67 | { 68 | } 69 | 70 | list.insert(d1); 71 | list.insert(d2, true); 72 | 73 | assertEquals(list.getNext(d1), null); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/stats/BucketUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.stats; 22 | 23 | import org.javasim.stats.Bucket; 24 | import org.junit.Test; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class BucketUnitTest 29 | { 30 | @Test 31 | public void test () throws Exception 32 | { 33 | Bucket b1 = new Bucket(20.0); 34 | Bucket b2 = new Bucket(20.0, 1000); 35 | 36 | assertEquals(b2.size(), (long) 1000); 37 | 38 | assertTrue(b1.equals(b2.name())); 39 | 40 | Bucket b3 = new Bucket(b1); 41 | 42 | assertTrue(b3.equals(b1.name())); 43 | 44 | Bucket b4 = new Bucket(40.0); 45 | 46 | assertTrue(b4.greaterThan(b2.name())); 47 | assertTrue(b4.greaterThanOrEqual(b4.name())); 48 | 49 | assertTrue(b1.lessThan(b4.name())); 50 | assertTrue(b1.lessThanOrEqual(b1.name())); 51 | } 52 | } 53 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/stats/HistogramUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.stats; 22 | 23 | import org.javasim.stats.Histogram; 24 | import org.junit.Test; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class HistogramUnitTest 29 | { 30 | @Test 31 | public void test () throws Exception 32 | { 33 | Histogram hist = new Histogram(1); 34 | 35 | hist.setValue(10.0); 36 | hist.setValue(100.0); 37 | 38 | assertEquals(hist.numberOfBuckets(), (long) 2); 39 | 40 | hist.saveState("target/hist.temp"); 41 | 42 | hist.reset(); 43 | 44 | assertEquals(hist.numberOfBuckets(), (long) 0); 45 | 46 | hist.restoreState("target/hist.temp"); 47 | 48 | assertEquals(hist.numberOfBuckets(), (long) 2); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/stats/MeanUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.stats; 22 | 23 | import org.javasim.stats.Mean; 24 | import org.junit.Test; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class MeanUnitTest 29 | { 30 | @Test 31 | public void test () throws Exception 32 | { 33 | final double MAX = 1000.0; 34 | final double MIN = 0.0; 35 | 36 | Mean mn = new Mean(); 37 | 38 | mn.setValue(MAX); 39 | mn.setValue(MIN); 40 | 41 | assertTrue(mn.max() == MAX); 42 | assertTrue(mn.min() == MIN); 43 | assertEquals(mn.numberOfSamples(), 2); 44 | assertTrue(mn.sum() == MAX+MIN); 45 | assertTrue(mn.mean() == (MAX+MIN)/2); 46 | 47 | mn.saveState("target/mean.tmp"); 48 | 49 | mn.reset(); 50 | 51 | assertTrue(mn.mean() == 0.0); 52 | 53 | Mean theMean = new Mean(); 54 | 55 | theMean.restoreState("target/mean.tmp"); 56 | 57 | assertTrue(theMean.max() == MAX); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/stats/PrecisionHistogramUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.stats; 22 | 23 | import org.javasim.stats.PrecisionHistogram; 24 | import org.junit.Test; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class PrecisionHistogramUnitTest 29 | { 30 | @Test 31 | public void test () throws Exception 32 | { 33 | PrecisionHistogram hist = new PrecisionHistogram(); 34 | 35 | hist.setValue(10.0); 36 | hist.setValue(100.0); 37 | 38 | assertEquals(hist.numberOfBuckets(), (long) 2); 39 | assertEquals(hist.numberOfSamples(), 2); 40 | assertTrue(hist.sizeByIndex(0) == 1.0); 41 | assertTrue(hist.sizeByName(100.0) == 1.0); 42 | 43 | hist.saveState("target/hist.temp"); 44 | 45 | hist.reset(); 46 | 47 | assertEquals(hist.numberOfBuckets(), (long) 0); 48 | 49 | hist.restoreState("target/hist.temp"); 50 | 51 | assertEquals(hist.numberOfBuckets(), (long) 2); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/stats/QuantileUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.stats; 22 | 23 | import org.javasim.stats.Quantile; 24 | import org.javasim.stats.Variance; 25 | import org.junit.Test; 26 | 27 | import static org.junit.Assert.*; 28 | 29 | public class QuantileUnitTest 30 | { 31 | @Test 32 | public void test () throws Exception 33 | { 34 | Quantile q; 35 | 36 | try 37 | { 38 | q = new Quantile(1.1); 39 | 40 | fail(); 41 | } 42 | catch (final Exception ex) 43 | { 44 | } 45 | 46 | try 47 | { 48 | q = new Quantile(-1.1); 49 | 50 | fail(); 51 | } 52 | catch (final Exception ex) 53 | { 54 | } 55 | 56 | q = new Quantile(); 57 | 58 | assertTrue(q.range() == 0.95); 59 | 60 | for (int i = 0; i < 100; i++) 61 | q.setValue(i); 62 | 63 | assertTrue(q.getValue() == 94.0); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/stats/SimpleHistogramUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.stats; 22 | 23 | import org.javasim.stats.SimpleHistogram; 24 | import org.junit.Test; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class SimpleHistogramUnitTest 29 | { 30 | @Test 31 | public void test () throws Exception 32 | { 33 | SimpleHistogram hist = new SimpleHistogram(1.0, 100.0, 20); 34 | 35 | hist.setValue(10.0); 36 | hist.setValue(100.0); 37 | 38 | assertEquals(hist.numberOfBuckets(), (long) 20); 39 | 40 | assertTrue(hist.sizeByName(10.0) == 1.0); 41 | 42 | hist.print(); 43 | 44 | hist.saveState("target/hist.temp"); 45 | 46 | hist.reset(); 47 | 48 | assertEquals(hist.numberOfBuckets(), (long) 20); 49 | 50 | hist.restoreState("target/hist.temp"); 51 | 52 | assertEquals(hist.numberOfBuckets(), (long) 20); 53 | 54 | hist = new SimpleHistogram(10.0, 1000.0, 4.0); 55 | 56 | assertTrue(hist.Width() == 4.0); 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/stats/TimeVarianceUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.stats; 22 | 23 | import org.javasim.stats.TimeVariance; 24 | import org.junit.Test; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class TimeVarianceUnitTest 29 | { 30 | @Test 31 | public void test () throws Exception 32 | { 33 | TimeVariance vn = new TimeVariance(); 34 | 35 | vn.setValue(10.0); 36 | 37 | assertTrue(vn.timeAverage() == 0.0); 38 | 39 | for (int i = 0; i < 1000; i++) 40 | vn.setValue(i); 41 | 42 | assertTrue(vn.timeAverage() == 0.0); // because simulation time has not advanced. 43 | 44 | double v = vn.variance(); 45 | 46 | assertTrue(v > 0.0); 47 | assertTrue(vn.stdDev() == Math.sqrt(vn.variance())); 48 | 49 | vn.saveState("target/variance.temp"); 50 | 51 | vn.reset(); 52 | 53 | assertTrue(vn.variance() == 0.0); 54 | 55 | vn.restoreState("target/variance.temp"); 56 | 57 | assertTrue(v == vn.variance()); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/stats/VarianceUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.stats; 22 | 23 | import org.javasim.stats.Variance; 24 | import org.junit.Test; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class VarianceUnitTest 29 | { 30 | @Test 31 | public void test () throws Exception 32 | { 33 | Variance vn = new Variance(); 34 | 35 | vn.setValue(10.0); 36 | vn.setValue(20.0); 37 | 38 | double v = vn.variance(); 39 | 40 | assertTrue(v > 0.0); 41 | assertTrue(vn.stdDev() == Math.sqrt(vn.variance())); 42 | 43 | vn.saveState("target/variance.temp"); 44 | 45 | vn.reset(); 46 | 47 | assertTrue(vn.variance() == 0.0); 48 | 49 | vn.restoreState("target/variance.temp"); 50 | 51 | assertTrue(v == vn.variance()); 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/streams/DrawUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.streams; 22 | 23 | import org.javasim.streams.Draw; 24 | import org.junit.Test; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class DrawUnitTest 29 | { 30 | @Test 31 | public void test () throws Exception 32 | { 33 | Draw d = new Draw(0); 34 | 35 | assertTrue(d.getBoolean()); 36 | 37 | d = new Draw(1.0, 1000); 38 | 39 | assertFalse(d.getBoolean()); 40 | 41 | d = new Draw(1.0, 1000, 772532, 1878892441); 42 | 43 | assertFalse(d.getBoolean()); 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/streams/ErlangStreamUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.streams; 22 | 23 | import org.javasim.streams.ErlangStream; 24 | import org.junit.Test; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class ErlangStreamUnitTest 29 | { 30 | @Test 31 | public void test () throws Exception 32 | { 33 | ErlangStream str = new ErlangStream(10.0, 4.0); 34 | 35 | try 36 | { 37 | str.getNumber(); 38 | } 39 | catch (final Exception ex) 40 | { 41 | fail(); 42 | } 43 | 44 | str = new ErlangStream(10.0, 1.0, 1000); 45 | 46 | try 47 | { 48 | str.getNumber(); 49 | } 50 | catch (final Exception ex) 51 | { 52 | fail(); 53 | } 54 | 55 | str = new ErlangStream(20.0, 2.0, 1000, 772532, 1878892441); 56 | 57 | try 58 | { 59 | str.getNumber(); 60 | } 61 | catch (final Exception ex) 62 | { 63 | fail(); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/streams/ExponentialStreamUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.streams; 22 | 23 | import org.javasim.streams.ExponentialStream; 24 | import org.junit.Test; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class ExponentialStreamUnitTest 29 | { 30 | @Test 31 | public void test () throws Exception 32 | { 33 | ExponentialStream str = new ExponentialStream(10.0); 34 | 35 | try 36 | { 37 | str.getNumber(); 38 | } 39 | catch (final Exception ex) 40 | { 41 | fail(); 42 | } 43 | 44 | str = new ExponentialStream(10.0, 1000); 45 | 46 | try 47 | { 48 | str.getNumber(); 49 | str.error(); 50 | } 51 | catch (final Exception ex) 52 | { 53 | fail(); 54 | } 55 | 56 | str = new ExponentialStream(20.0, 1000, 772532, 1878892441); 57 | 58 | try 59 | { 60 | str.getNumber(); 61 | } 62 | catch (final Exception ex) 63 | { 64 | fail(); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/streams/HyperExponentialStreamUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.streams; 22 | 23 | import org.javasim.streams.HyperExponentialStream; 24 | import org.junit.Test; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class HyperExponentialStreamUnitTest 29 | { 30 | @Test 31 | public void test () throws Exception 32 | { 33 | HyperExponentialStream str = new HyperExponentialStream(10.0, 2.0); 34 | 35 | try 36 | { 37 | str.getNumber(); 38 | } 39 | catch (final Exception ex) 40 | { 41 | fail(); 42 | } 43 | 44 | str = new HyperExponentialStream(10.0, 1.0, 1000); 45 | 46 | try 47 | { 48 | str.getNumber(); 49 | } 50 | catch (final Exception ex) 51 | { 52 | fail(); 53 | } 54 | 55 | str = new HyperExponentialStream(20.0, 5.0, 1000, 772532, 1878892441); 56 | 57 | try 58 | { 59 | str.getNumber(); 60 | } 61 | catch (final Exception ex) 62 | { 63 | fail(); 64 | } 65 | } 66 | } 67 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/streams/NormalStreamUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.streams; 22 | 23 | import org.javasim.streams.NormalStream; 24 | import org.junit.Test; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class NormalStreamUnitTest 29 | { 30 | @Test 31 | public void test () throws Exception 32 | { 33 | NormalStream str = new NormalStream(10.0, 2.0); 34 | 35 | try 36 | { 37 | str.getNumber(); 38 | } 39 | catch (final Exception ex) 40 | { 41 | fail(); 42 | } 43 | 44 | str = new NormalStream(10.0, 1.0, 1000); 45 | 46 | try 47 | { 48 | str.getNumber(); 49 | } 50 | catch (final Exception ex) 51 | { 52 | fail(); 53 | } 54 | 55 | str = new NormalStream(20.0, 5.0, 1000, 772532, 1878892441); 56 | 57 | try 58 | { 59 | str.getNumber(); 60 | str.error(); 61 | } 62 | catch (final Exception ex) 63 | { 64 | fail(); 65 | } 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/streams/TriangularStreamUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.streams; 22 | 23 | import static org.junit.Assert.fail; 24 | 25 | import org.javasim.streams.TriangularStream; 26 | import org.junit.Test; 27 | 28 | public class TriangularStreamUnitTest { 29 | @Test 30 | public void test() throws Exception { 31 | TriangularStream str = new TriangularStream(0, 10, 6); 32 | 33 | try { 34 | str.getNumber(); 35 | str.error(); 36 | } catch (final Exception ex) { 37 | fail(); 38 | } 39 | 40 | str = new TriangularStream(0, 10, 6, 1000); 41 | 42 | try { 43 | str.getNumber(); 44 | } catch (final Exception ex) { 45 | fail(); 46 | } 47 | 48 | str = new TriangularStream(0, 10, 6, 1000, 772532, 1878892441); 49 | 50 | try { 51 | str.getNumber(); 52 | str.error(); 53 | } catch (final Exception ex) { 54 | fail(); 55 | } 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /src/test/java/org/javasim/tests/streams/UniformStreamUnitTest.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 1990-2008, Mark Little, University of Newcastle upon Tyne 3 | * and others contributors as indicated 4 | * by the @authors tag. All rights reserved. 5 | * See the copyright.txt in the distribution for a 6 | * full listing of individual contributors. 7 | * This copyrighted material is made available to anyone wishing to use, 8 | * modify, copy, or redistribute it subject to the terms and conditions 9 | * of the GNU Lesser General Public License, v. 2.1. 10 | * This program is distributed in the hope that it will be useful, but WITHOUT A 11 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 12 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. 13 | * You should have received a copy of the GNU Lesser General Public License, 14 | * v.2.1 along with this distribution; if not, write to the Free Software 15 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 16 | * MA 02110-1301, USA. 17 | * 18 | * (C) 1990-2008, 19 | */ 20 | 21 | package org.javasim.tests.streams; 22 | 23 | import org.javasim.streams.UniformStream; 24 | import org.junit.Test; 25 | 26 | import static org.junit.Assert.*; 27 | 28 | public class UniformStreamUnitTest 29 | { 30 | @Test 31 | public void test () throws Exception 32 | { 33 | UniformStream str = new UniformStream(10.0, 2.0); 34 | 35 | try 36 | { 37 | str.getNumber(); 38 | str.error(); 39 | } 40 | catch (final Exception ex) 41 | { 42 | fail(); 43 | } 44 | 45 | str = new UniformStream(10.0, 1.0, 1000); 46 | 47 | try 48 | { 49 | str.getNumber(); 50 | } 51 | catch (final Exception ex) 52 | { 53 | fail(); 54 | } 55 | 56 | str = new UniformStream(20.0, 5.0, 1000, 772532, 1878892441); 57 | 58 | try 59 | { 60 | str.getNumber(); 61 | str.error(); 62 | } 63 | catch (final Exception ex) 64 | { 65 | fail(); 66 | } 67 | } 68 | } 69 | --------------------------------------------------------------------------------