1 | package de.ugoe.cs.eventbench.commands;
|
---|
2 |
|
---|
3 | import java.security.InvalidParameterException;
|
---|
4 | import java.util.ArrayList;
|
---|
5 | import java.util.Collection;
|
---|
6 | import java.util.LinkedHashSet;
|
---|
7 | import java.util.LinkedList;
|
---|
8 | import java.util.List;
|
---|
9 | import java.util.Random;
|
---|
10 |
|
---|
11 | import de.ugoe.cs.eventbench.CommandHelpers;
|
---|
12 | import de.ugoe.cs.eventbench.data.Event;
|
---|
13 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
14 | import de.ugoe.cs.eventbench.models.IStochasticProcess;
|
---|
15 | import de.ugoe.cs.util.console.Command;
|
---|
16 | import de.ugoe.cs.util.console.Console;
|
---|
17 |
|
---|
18 | /**
|
---|
19 | * <p>
|
---|
20 | * Command to generate sequences of a given length.
|
---|
21 | * </p>
|
---|
22 | *
|
---|
23 | * @author Steffen Herbold
|
---|
24 | * @version 1.0
|
---|
25 | */
|
---|
26 | public class CMDgenerateHybrid implements Command {
|
---|
27 |
|
---|
28 | /*
|
---|
29 | * (non-Javadoc)
|
---|
30 | *
|
---|
31 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
32 | */
|
---|
33 | @Override
|
---|
34 | public void run(List<Object> parameters) {
|
---|
35 | String modelname;
|
---|
36 | String sequencesName;
|
---|
37 | int length;
|
---|
38 | int maxLengthAll;
|
---|
39 | int numSequences;
|
---|
40 | boolean validEnd = true;
|
---|
41 | try {
|
---|
42 | modelname = (String) parameters.get(0);
|
---|
43 | sequencesName = (String) parameters.get(1);
|
---|
44 | length = Integer.parseInt((String) parameters.get(2));
|
---|
45 | maxLengthAll = Integer.parseInt((String) parameters.get(3));
|
---|
46 | numSequences = Integer.parseInt((String) parameters.get(4));
|
---|
47 | if (parameters.size() >= 6) {
|
---|
48 | validEnd = Boolean.parseBoolean((String) parameters.get(5));
|
---|
49 | }
|
---|
50 | } catch (Exception e) {
|
---|
51 | throw new InvalidParameterException();
|
---|
52 | }
|
---|
53 |
|
---|
54 | if (length <= maxLengthAll) {
|
---|
55 | // indirectly call command generateFixedLengthSequences
|
---|
56 | List<Object> parameters2 = new LinkedList<Object>();
|
---|
57 | parameters2.add(modelname);
|
---|
58 | parameters2.add(sequencesName);
|
---|
59 | parameters2.add(Integer.toString(length));
|
---|
60 | parameters2.add(Integer.toString(length));
|
---|
61 | parameters2.add(Boolean.toString(false));
|
---|
62 | parameters2.add(Integer.toString(numSequences));
|
---|
63 | parameters2.add(Boolean.toString(validEnd));
|
---|
64 | CMDgenerateFixedLengthSequences cmd = new CMDgenerateFixedLengthSequences();
|
---|
65 | cmd.run(parameters2);
|
---|
66 | return;
|
---|
67 | }
|
---|
68 |
|
---|
69 | IStochasticProcess model = null;
|
---|
70 | Object dataObject = GlobalDataContainer.getInstance()
|
---|
71 | .getData(modelname);
|
---|
72 | if (dataObject == null) {
|
---|
73 | CommandHelpers.objectNotFoundMessage(modelname);
|
---|
74 | return;
|
---|
75 | } else if (!(dataObject instanceof IStochasticProcess)) {
|
---|
76 | CommandHelpers.objectNotType(modelname, "IStochasticProcess");
|
---|
77 | return;
|
---|
78 | }
|
---|
79 | model = (IStochasticProcess) dataObject;
|
---|
80 | Collection<List<? extends Event<?>>> sequences = new LinkedHashSet<List<? extends Event<?>>>();
|
---|
81 |
|
---|
82 | List<List<? extends Event<?>>> seqsTmp = new ArrayList<List<? extends Event<?>>>(
|
---|
83 | model.generateSequences(maxLengthAll + 1, true));
|
---|
84 |
|
---|
85 | Console.traceln("" + seqsTmp.size() + " of length " + maxLengthAll
|
---|
86 | + " possible");
|
---|
87 | List<Double> probabilities = new ArrayList<Double>(seqsTmp.size());
|
---|
88 | double probSum = 0.0;
|
---|
89 | for (List<? extends Event<?>> sequence : seqsTmp) {
|
---|
90 | double prob = model.getProbability(sequence);
|
---|
91 | probabilities.add(prob);
|
---|
92 | probSum += prob;
|
---|
93 | }
|
---|
94 |
|
---|
95 | Random r = new Random();
|
---|
96 | int j = 0;
|
---|
97 | while (sequences.size() < numSequences && j <= numSequences * 100) {
|
---|
98 | j++;
|
---|
99 | double randVal = r.nextDouble() * probSum;
|
---|
100 | double sum = 0.0d;
|
---|
101 | int index = -1;
|
---|
102 | while (sum < randVal) {
|
---|
103 | index++;
|
---|
104 | double currentProb = probabilities.get(index);
|
---|
105 | sum += currentProb;
|
---|
106 | }
|
---|
107 | List<? extends Event<?>> seqTmp = seqsTmp.get(index);
|
---|
108 | if (!Event.ENDEVENT.equals(seqTmp.get(seqTmp.size() - 1))) {
|
---|
109 | List<? extends Event<?>> sequence;
|
---|
110 | if (validEnd) {
|
---|
111 | sequence = finishSequence(seqTmp, model, length + 2,
|
---|
112 | validEnd);
|
---|
113 | } else {
|
---|
114 | sequence = finishSequence(seqTmp, model, length + 1,
|
---|
115 | validEnd);
|
---|
116 | }
|
---|
117 | sequences.add(sequence);
|
---|
118 | }
|
---|
119 | }
|
---|
120 | if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
|
---|
121 | CommandHelpers.dataOverwritten(sequencesName);
|
---|
122 | }
|
---|
123 | Console.println("" + sequences.size() + " sequences generated");
|
---|
124 | }
|
---|
125 |
|
---|
126 | public List<? extends Event<?>> finishSequence(
|
---|
127 | List<? extends Event<?>> sequence, IStochasticProcess model,
|
---|
128 | int maxLength, boolean validEnd) {
|
---|
129 | Random r = new Random();
|
---|
130 | boolean endFound = false;
|
---|
131 | List<Event<?>> sequenceCopy = new LinkedList<Event<?>>(sequence);
|
---|
132 |
|
---|
133 | while (!endFound) {
|
---|
134 | sequenceCopy = new LinkedList<Event<?>>(sequence);
|
---|
135 | while (!endFound && sequenceCopy.size() <= maxLength) {
|
---|
136 | double randVal = r.nextDouble();
|
---|
137 | double probSum = 0.0;
|
---|
138 | for (Event<?> symbol : model.getEvents()) {
|
---|
139 | probSum += model.getProbability(sequenceCopy, symbol);
|
---|
140 | if (probSum >= randVal) {
|
---|
141 | if (!(Event.STARTEVENT.equals(symbol) || Event.ENDEVENT.equals(symbol))) {
|
---|
142 | // only add the symbol the sequence if it is not
|
---|
143 | // START
|
---|
144 | // or END
|
---|
145 | sequenceCopy.add(symbol);
|
---|
146 | }
|
---|
147 | endFound = Event.ENDEVENT.equals(symbol)
|
---|
148 | || (!validEnd && sequenceCopy.size() == maxLength);
|
---|
149 | break;
|
---|
150 | }
|
---|
151 | }
|
---|
152 | }
|
---|
153 | }
|
---|
154 | return sequenceCopy;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /*
|
---|
158 | * (non-Javadoc)
|
---|
159 | *
|
---|
160 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
161 | */
|
---|
162 | @Override
|
---|
163 | public void help() {
|
---|
164 | Console.println("Usage: generateFixedLengthSequences <modelname> <sequencesName> <lenght> <maxlengthAll> <numSequences> {<validEnd>}");
|
---|
165 | }
|
---|
166 |
|
---|
167 | }
|
---|