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.data.GlobalDataContainer;
|
---|
9 | import de.ugoe.cs.eventbench.data.Event;
|
---|
10 | import de.ugoe.cs.eventbench.models.IStochasticProcess;
|
---|
11 | import de.ugoe.cs.util.console.Command;
|
---|
12 | import de.ugoe.cs.util.console.Console;
|
---|
13 |
|
---|
14 | public class CMDgenerateRandomSequences implements Command {
|
---|
15 |
|
---|
16 | @Override
|
---|
17 | public void run(List<Object> parameters) {
|
---|
18 | String modelname;
|
---|
19 | String sequencesName;
|
---|
20 | int numSessions;
|
---|
21 | long maxIter;
|
---|
22 | int minLength = 0;
|
---|
23 | int maxLength = Integer.MAX_VALUE;
|
---|
24 | try {
|
---|
25 | modelname = (String) parameters.get(0);
|
---|
26 | sequencesName = (String) parameters.get(1);
|
---|
27 | numSessions = Integer.parseInt((String) parameters.get(2));
|
---|
28 | maxIter = numSessions*10;
|
---|
29 | if( parameters.size()>=4 ) {
|
---|
30 | maxIter = Long.parseLong((String) parameters.get(3));
|
---|
31 | }
|
---|
32 | if( parameters.size()>=5 ) {
|
---|
33 | minLength = Integer.parseInt((String) parameters.get(4));
|
---|
34 | }
|
---|
35 | if( parameters.size()>=6 ) {
|
---|
36 | maxLength = Integer.parseInt((String) parameters.get(5));
|
---|
37 | }
|
---|
38 | }
|
---|
39 | catch (Exception e) {
|
---|
40 | throw new InvalidParameterException();
|
---|
41 | }
|
---|
42 |
|
---|
43 | IStochasticProcess model = null;
|
---|
44 | Object dataObject = GlobalDataContainer.getInstance().getData(modelname);
|
---|
45 | if( dataObject==null ) {
|
---|
46 | Console.println("Model " + modelname + " not found in storage.");
|
---|
47 | }
|
---|
48 | else if( !(dataObject instanceof IStochasticProcess) ) {
|
---|
49 | Console.println("Object " + modelname + " not of type MarkovModel!");
|
---|
50 | } else {
|
---|
51 | model = (IStochasticProcess) dataObject;
|
---|
52 | Set<List<? extends Event<?>>> sequences = new HashSet<List<? extends Event<?>>>(numSessions);
|
---|
53 | long numIterations = 0;
|
---|
54 | while( sequences.size()<numSessions && numIterations<maxIter ) {
|
---|
55 | List<? extends Event<?>> generatedSequence = model.randomSequence();
|
---|
56 | if( generatedSequence.size()>=minLength && generatedSequence.size()<=maxLength ) {
|
---|
57 | sequences.add(generatedSequence);
|
---|
58 | }
|
---|
59 | numIterations++;
|
---|
60 | }
|
---|
61 | if( sequences.size() < numSessions ) {
|
---|
62 | Console.println("Only " + sequences.size() + " unique sessions generated after " + maxIter + " iterations");
|
---|
63 | }
|
---|
64 | if( GlobalDataContainer.getInstance().addData(sequencesName, sequences) ) {
|
---|
65 | Console.traceln("Old data \"" + sequencesName + "\" overwritten");
|
---|
66 | }
|
---|
67 | }
|
---|
68 | }
|
---|
69 |
|
---|
70 | @Override
|
---|
71 | public void help() {
|
---|
72 | Console.println("Usage: generateRandomSequenecs <modelName> <sequencesName> <numSessions> {<maxIter> <minLength> <maxLength>}");
|
---|
73 | }
|
---|
74 |
|
---|
75 | }
|
---|