1 | package de.ugoe.cs.eventbench.commands;
|
---|
2 |
|
---|
3 | import java.security.InvalidParameterException;
|
---|
4 | import java.util.HashSet;
|
---|
5 | import java.util.List;
|
---|
6 | import java.util.Set;
|
---|
7 |
|
---|
8 | import de.ugoe.cs.eventbench.CommandHelpers;
|
---|
9 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
10 | import de.ugoe.cs.eventbench.data.Event;
|
---|
11 | import de.ugoe.cs.eventbench.models.IStochasticProcess;
|
---|
12 | import de.ugoe.cs.util.console.Command;
|
---|
13 | import de.ugoe.cs.util.console.Console;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * <p>
|
---|
17 | * Command to generate random sessions.
|
---|
18 | * </p>
|
---|
19 | *
|
---|
20 | * @author Steffen Herbold
|
---|
21 | * @version 1.0
|
---|
22 | */
|
---|
23 | public class CMDgenerateRandomSequences implements Command {
|
---|
24 |
|
---|
25 | /*
|
---|
26 | * (non-Javadoc)
|
---|
27 | *
|
---|
28 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
29 | */
|
---|
30 | @Override
|
---|
31 | public void run(List<Object> parameters) {
|
---|
32 | String modelname;
|
---|
33 | String sequencesName;
|
---|
34 | int numSessions;
|
---|
35 | long maxIter;
|
---|
36 | int minLength = 0;
|
---|
37 | int maxLength = Integer.MAX_VALUE;
|
---|
38 | try {
|
---|
39 | modelname = (String) parameters.get(0);
|
---|
40 | sequencesName = (String) parameters.get(1);
|
---|
41 | numSessions = Integer.parseInt((String) parameters.get(2));
|
---|
42 | maxIter = numSessions * 10;
|
---|
43 | if (parameters.size() >= 4) {
|
---|
44 | maxIter = Long.parseLong((String) parameters.get(3));
|
---|
45 | }
|
---|
46 | if (parameters.size() >= 5) {
|
---|
47 | minLength = Integer.parseInt((String) parameters.get(4));
|
---|
48 | }
|
---|
49 | if (parameters.size() >= 6) {
|
---|
50 | maxLength = Integer.parseInt((String) parameters.get(5));
|
---|
51 | }
|
---|
52 | } catch (Exception e) {
|
---|
53 | throw new InvalidParameterException();
|
---|
54 | }
|
---|
55 |
|
---|
56 | IStochasticProcess model = null;
|
---|
57 | Object dataObject = GlobalDataContainer.getInstance()
|
---|
58 | .getData(modelname);
|
---|
59 | if (dataObject == null) {
|
---|
60 | CommandHelpers.objectNotFoundMessage(modelname);
|
---|
61 | return;
|
---|
62 | }
|
---|
63 | if (!(dataObject instanceof IStochasticProcess)) {
|
---|
64 | CommandHelpers.objectNotType(modelname, "IStochasticProcess");
|
---|
65 | return;
|
---|
66 | }
|
---|
67 | model = (IStochasticProcess) dataObject;
|
---|
68 | Set<List<? extends Event<?>>> sequences = new HashSet<List<? extends Event<?>>>(
|
---|
69 | numSessions);
|
---|
70 | long numIterations = 0;
|
---|
71 | while (sequences.size() < numSessions && numIterations < maxIter) {
|
---|
72 | List<? extends Event<?>> generatedSequence = model.randomSequence();
|
---|
73 | if (generatedSequence.size() >= minLength
|
---|
74 | && generatedSequence.size() <= maxLength) {
|
---|
75 | sequences.add(generatedSequence);
|
---|
76 | }
|
---|
77 | numIterations++;
|
---|
78 | }
|
---|
79 | if (sequences.size() < numSessions) {
|
---|
80 | Console.println("Only " + sequences.size()
|
---|
81 | + " unique sessions generated after " + maxIter
|
---|
82 | + " iterations");
|
---|
83 | }
|
---|
84 | if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
|
---|
85 | CommandHelpers.dataOverwritten(sequencesName);
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | /*
|
---|
90 | * (non-Javadoc)
|
---|
91 | *
|
---|
92 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
93 | */
|
---|
94 | @Override
|
---|
95 | public void help() {
|
---|
96 | Console.println("Usage: generateRandomSequenecs <modelName> <sequencesName> <numSessions> {<maxIter> <minLength> <maxLength>}");
|
---|
97 | }
|
---|
98 |
|
---|
99 | }
|
---|