[96] | 1 | package de.ugoe.cs.eventbench.commands;
|
---|
| 2 |
|
---|
| 3 | import java.security.InvalidParameterException;
|
---|
[120] | 4 | import java.util.ArrayList;
|
---|
[103] | 5 | import java.util.Collection;
|
---|
[120] | 6 | import java.util.HashSet;
|
---|
| 7 | import java.util.LinkedHashSet;
|
---|
| 8 | import java.util.LinkedList;
|
---|
[96] | 9 | import java.util.List;
|
---|
[120] | 10 | import java.util.Random;
|
---|
| 11 | import java.util.Set;
|
---|
[96] | 12 |
|
---|
[240] | 13 | import de.ugoe.cs.eventbench.CommandHelpers;
|
---|
[96] | 14 | import de.ugoe.cs.eventbench.data.Event;
|
---|
| 15 | import de.ugoe.cs.eventbench.data.GlobalDataContainer;
|
---|
| 16 | import de.ugoe.cs.eventbench.models.IStochasticProcess;
|
---|
| 17 | import de.ugoe.cs.util.console.Command;
|
---|
| 18 | import de.ugoe.cs.util.console.Console;
|
---|
| 19 |
|
---|
[171] | 20 | /**
|
---|
| 21 | * <p>
|
---|
| 22 | * Command to generate all sequences of a given length.
|
---|
| 23 | * </p>
|
---|
| 24 | *
|
---|
| 25 | * @author Steffen Herbold
|
---|
| 26 | * @version 1.0
|
---|
| 27 | */
|
---|
[96] | 28 | public class CMDgenerateFixedLengthSequences implements Command {
|
---|
| 29 |
|
---|
[171] | 30 | /*
|
---|
| 31 | * (non-Javadoc)
|
---|
| 32 | *
|
---|
| 33 | * @see de.ugoe.cs.util.console.Command#run(java.util.List)
|
---|
| 34 | */
|
---|
[96] | 35 | @Override
|
---|
| 36 | public void run(List<Object> parameters) {
|
---|
| 37 | String modelname;
|
---|
| 38 | String sequencesName;
|
---|
[120] | 39 | int minLength;
|
---|
| 40 | int maxLength;
|
---|
| 41 | boolean all = true;
|
---|
| 42 | int numSequences = -1;
|
---|
[393] | 43 | boolean validEnd = true;
|
---|
[96] | 44 | try {
|
---|
| 45 | modelname = (String) parameters.get(0);
|
---|
| 46 | sequencesName = (String) parameters.get(1);
|
---|
[120] | 47 | minLength = Integer.parseInt((String) parameters.get(2));
|
---|
| 48 | maxLength = Integer.parseInt((String) parameters.get(3));
|
---|
[171] | 49 | if (parameters.size() >= 5) {
|
---|
[120] | 50 | all = Boolean.parseBoolean((String) parameters.get(4));
|
---|
| 51 | }
|
---|
[393] | 52 | if (parameters.size() >= 6) {
|
---|
[171] | 53 | numSequences = Integer.parseInt((String) parameters.get(5));
|
---|
[120] | 54 | }
|
---|
[393] | 55 | if (parameters.size() >= 7) {
|
---|
| 56 | validEnd = Boolean.parseBoolean((String) parameters.get(6));
|
---|
| 57 | }
|
---|
[171] | 58 | } catch (Exception e) {
|
---|
[96] | 59 | throw new InvalidParameterException();
|
---|
| 60 | }
|
---|
[171] | 61 |
|
---|
| 62 | IStochasticProcess model = null;
|
---|
| 63 | Object dataObject = GlobalDataContainer.getInstance()
|
---|
| 64 | .getData(modelname);
|
---|
| 65 | if (dataObject == null) {
|
---|
[240] | 66 | CommandHelpers.objectNotFoundMessage(modelname);
|
---|
[209] | 67 | return;
|
---|
[171] | 68 | } else if (!(dataObject instanceof IStochasticProcess)) {
|
---|
[240] | 69 | CommandHelpers.objectNotType(modelname, "IStochasticProcess");
|
---|
[209] | 70 | return;
|
---|
| 71 | }
|
---|
| 72 | model = (IStochasticProcess) dataObject;
|
---|
| 73 | Collection<List<? extends Event<?>>> sequences = new LinkedHashSet<List<? extends Event<?>>>();
|
---|
| 74 | for (int length = minLength; length <= maxLength; length++) {
|
---|
[393] | 75 | if( validEnd ) {
|
---|
| 76 | sequences.addAll(model.generateValidSequences(length + 2));
|
---|
| 77 | } else {
|
---|
| 78 | sequences.addAll(model.generateSequences(length + 1, true));
|
---|
| 79 | }
|
---|
[209] | 80 | }
|
---|
| 81 | Console.traceln("" + sequences.size() + " possible");
|
---|
| 82 | if (!all && numSequences < sequences.size()) {
|
---|
| 83 | List<Double> probabilities = new ArrayList<Double>(sequences.size());
|
---|
| 84 | double probSum = 0.0;
|
---|
| 85 | for (List<? extends Event<?>> sequence : sequences) {
|
---|
| 86 | double prob = model.getProbability(sequence);
|
---|
| 87 | probabilities.add(prob);
|
---|
| 88 | probSum += prob;
|
---|
[120] | 89 | }
|
---|
[209] | 90 | Set<Integer> drawnSequences = new HashSet<Integer>(numSequences);
|
---|
| 91 | Random r = new Random();
|
---|
| 92 | while (drawnSequences.size() < numSequences) {
|
---|
| 93 | double randVal = r.nextDouble() * probSum;
|
---|
| 94 | double sum = 0.0d;
|
---|
| 95 | int index = -1;
|
---|
| 96 | while (sum < randVal) {
|
---|
| 97 | index++;
|
---|
| 98 | double currentProb = probabilities.get(index);
|
---|
| 99 | sum += currentProb;
|
---|
[120] | 100 | }
|
---|
[209] | 101 | if (!drawnSequences.contains(index)) {
|
---|
| 102 | drawnSequences.add(index);
|
---|
| 103 | probSum -= probabilities.get(index);
|
---|
| 104 | probabilities.set(index, 0.0d);
|
---|
[120] | 105 | }
|
---|
[209] | 106 | }
|
---|
| 107 | Collection<List<? extends Event<?>>> retainedSequences = new LinkedList<List<? extends Event<?>>>();
|
---|
| 108 | int index = 0;
|
---|
| 109 | for (List<? extends Event<?>> sequence : sequences) {
|
---|
| 110 | if (drawnSequences.contains(index)) {
|
---|
| 111 | retainedSequences.add(sequence);
|
---|
[120] | 112 | }
|
---|
[209] | 113 | index++;
|
---|
[120] | 114 | }
|
---|
[209] | 115 | sequences = retainedSequences;
|
---|
[96] | 116 | }
|
---|
[209] | 117 | if (GlobalDataContainer.getInstance().addData(sequencesName, sequences)) {
|
---|
[240] | 118 | CommandHelpers.dataOverwritten(sequencesName);
|
---|
[209] | 119 | }
|
---|
| 120 | Console.println("" + sequences.size() + " sequences generated");
|
---|
[96] | 121 | }
|
---|
| 122 |
|
---|
[171] | 123 | /*
|
---|
| 124 | * (non-Javadoc)
|
---|
| 125 | *
|
---|
| 126 | * @see de.ugoe.cs.util.console.Command#help()
|
---|
| 127 | */
|
---|
[96] | 128 | @Override
|
---|
| 129 | public void help() {
|
---|
[393] | 130 | Console.println("Usage: generateFixedLengthSequences <modelname> <sequencesName> <minlenght> <maxlength> {<all>} {<numSequences>} {<validEnd>}");
|
---|
[96] | 131 | }
|
---|
| 132 |
|
---|
| 133 | }
|
---|