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 | int minLength = 0;
|
---|
36 | int maxLength = Integer.MAX_VALUE;
|
---|
37 | long maxIter;
|
---|
38 | try {
|
---|
39 | modelname = (String) parameters.get(0);
|
---|
40 | sequencesName = (String) parameters.get(1);
|
---|
41 | numSessions = Integer.parseInt((String) parameters.get(2));
|
---|
42 | minLength = Integer.parseInt((String) parameters.get(3));
|
---|
43 | maxLength = Integer.parseInt((String) parameters.get(4));
|
---|
44 | maxIter = numSessions * 10;
|
---|
45 | if (parameters.size() >= 5) {
|
---|
46 | maxIter = Long.parseLong((String) parameters.get(5));
|
---|
47 | }
|
---|
48 | } catch (Exception e) {
|
---|
49 | throw new InvalidParameterException();
|
---|
50 | }
|
---|
51 |
|
---|
52 | IStochasticProcess model = null;
|
---|
53 | Object dataObject = GlobalDataContainer.getInstance()
|
---|
54 | .getData(modelname);
|
---|
55 | if (dataObject == null) {
|
---|
56 | CommandHelpers.objectNotFoundMessage(modelname);
|
---|
57 | return;
|
---|
58 | }
|
---|
59 | if (!(dataObject instanceof IStochasticProcess)) {
|
---|
60 | CommandHelpers.objectNotType(modelname, "IStochasticProcess");
|
---|
61 | return;
|
---|
62 | }
|
---|
63 | model = (IStochasticProcess) dataObject;
|
---|
64 | Set<List<? extends Event<?>>> sequences = new HashSet<List<? extends Event<?>>>(
|
---|
65 | numSessions);
|
---|
66 | long numIterations = 0;
|
---|
67 | while (sequences.size() < numSessions && numIterations < maxIter) {
|
---|
68 | List<? extends Event<?>> generatedSequence = model.randomSequence();
|
---|
69 | if (generatedSequence.size() >= minLength
|
---|
70 | && generatedSequence.size() <= maxLength) {
|
---|
71 | sequences.add(generatedSequence);
|
---|
72 | }
|
---|
73 | numIterations++;
|
---|
74 | }
|
---|
75 | if (sequences.size() < numSessions) {
|
---|
76 | Console.println("Only " + sequences.size()
|
---|
77 | + " unique sessions generated after " + maxIter
|
---|
78 | + " iterations");
|
---|
79 | }
|
---|
80 | if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
|
---|
81 | CommandHelpers.dataOverwritten(sequencesName);
|
---|
82 | }
|
---|
83 | }
|
---|
84 |
|
---|
85 | /*
|
---|
86 | * (non-Javadoc)
|
---|
87 | *
|
---|
88 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
89 | */
|
---|
90 | @Override
|
---|
91 | public void help() {
|
---|
92 | Console.println("Usage: generateRandomSequenecs <modelName> <sequencesName> <numSessions> <minLength> <maxLength> {<maxIter>}");
|
---|
93 | }
|
---|
94 |
|
---|
95 | }
|
---|