1 package nl.tudelft.simulation.dsol.tutorial.section42;
2
3 import nl.tudelft.simulation.dsol.formalisms.eventscheduling.SimEvent;
4 import nl.tudelft.simulation.dsol.logger.Cat;
5 import nl.tudelft.simulation.dsol.simulators.DevsSimulatorInterface;
6 import nl.tudelft.simulation.jstats.distributions.DistContinuous;
7 import nl.tudelft.simulation.jstats.distributions.DistDiscrete;
8 import nl.tudelft.simulation.jstats.distributions.DistEmpiricalDiscreteLong;
9 import nl.tudelft.simulation.jstats.distributions.DistExponential;
10 import nl.tudelft.simulation.jstats.distributions.empirical.DiscreteEmpiricalDistribution;
11 import nl.tudelft.simulation.jstats.distributions.empirical.ProbabilityDensities;
12 import nl.tudelft.simulation.jstats.streams.StreamInterface;
13
14
15
16
17
18
19
20
21
22
23
24
25 public class Customer implements BuyerInterface
26 {
27
28 private DevsSimulatorInterface<Double> simulator = null;
29
30
31 private SellerInterface retailer = null;
32
33
34 private DistContinuous intervalTime = null;
35
36
37 private DistDiscrete orderBatchSize = null;
38
39
40
41
42
43
44
45 public Customer(final DevsSimulatorInterface<Double> simulator, final SellerInterface retailer)
46 {
47 super();
48 this.simulator = simulator;
49 this.retailer = retailer;
50 StreamInterface stream = this.simulator.getModel().getStream("default");
51 this.intervalTime = new DistExponential(stream, 0.1);
52 DiscreteEmpiricalDistribution empDist = ProbabilityDensities.createDiscreteDistribution(new long[] {1, 2, 3, 4},
53 new double[] {1.0 / 6.0, 1.0 / 3.0, 1.0 / 3.0, 1.0 / 6.0});
54 this.orderBatchSize = new DistEmpiricalDiscreteLong(stream, empDist);
55 this.createOrder();
56 }
57
58
59 @Override
60 public void receiveProduct(final long amount)
61 {
62 this.simulator.getLogger().filter(Cat.DSOL).trace("receiveProduct: received " + amount);
63 }
64
65
66
67
68 private void createOrder()
69 {
70 this.retailer.order(this, this.orderBatchSize.draw());
71 try
72 {
73 this.simulator.scheduleEvent(new SimEvent<Double>(this.simulator.getSimulatorTime() + this.intervalTime.draw(),
74 this,"createOrder", null));
75 }
76 catch (Exception exception)
77 {
78 this.simulator.getLogger().always().error(exception, "createOrder");
79 }
80 }
81 }