Index: trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/commands/CMDgenerateHybrid.java
===================================================================
--- trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/commands/CMDgenerateHybrid.java	(revision 261)
+++ trunk/EventBenchConsole/src/de/ugoe/cs/eventbench/commands/CMDgenerateHybrid.java	(revision 394)
@@ -36,16 +36,33 @@
 		String sequencesName;
 		int length;
-		int maxLengthall;
+		int maxLengthAll;
 		int numSequences;
-		int maxIterOuter = 100;
-		int maxIterInner = 1000;
+		boolean validEnd = true;
 		try {
 			modelname = (String) parameters.get(0);
 			sequencesName = (String) parameters.get(1);
 			length = Integer.parseInt((String) parameters.get(2));
-			maxLengthall = Integer.parseInt((String) parameters.get(3));
+			maxLengthAll = Integer.parseInt((String) parameters.get(3));
 			numSequences = Integer.parseInt((String) parameters.get(4));
+			if (parameters.size() >= 6) {
+				validEnd = Boolean.parseBoolean((String) parameters.get(5));
+			}
 		} catch (Exception e) {
 			throw new InvalidParameterException();
+		}
+
+		if (length <= maxLengthAll) {
+			// indirectly call command generateFixedLengthSequences
+			List<Object> parameters2 = new LinkedList<Object>();
+			parameters2.add(modelname);
+			parameters2.add(sequencesName);
+			parameters2.add(Integer.toString(length));
+			parameters2.add(Integer.toString(length));
+			parameters2.add(Boolean.toString(false));
+			parameters2.add(Integer.toString(numSequences));
+			parameters2.add(Boolean.toString(validEnd));
+			CMDgenerateFixedLengthSequences cmd = new CMDgenerateFixedLengthSequences();
+			cmd.run(parameters2);
+			return;
 		}
 
@@ -62,8 +79,10 @@
 		model = (IStochasticProcess) dataObject;
 		Collection<List<? extends Event<?>>> sequences = new LinkedHashSet<List<? extends Event<?>>>();
-		
-		List<List<? extends Event<?>>> seqsTmp = new ArrayList<List<? extends Event<?>>>(model.generateSequences(maxLengthall+1, true));
-		
-		Console.traceln("" + seqsTmp.size() + " of length " + maxLengthall + " possible");
+
+		List<List<? extends Event<?>>> seqsTmp = new ArrayList<List<? extends Event<?>>>(
+				model.generateSequences(maxLengthAll + 1, true));
+
+		Console.traceln("" + seqsTmp.size() + " of length " + maxLengthAll
+				+ " possible");
 		List<Double> probabilities = new ArrayList<Double>(seqsTmp.size());
 		double probSum = 0.0;
@@ -73,8 +92,8 @@
 			probSum += prob;
 		}
-		
+
 		Random r = new Random();
-		int j=0;
-		while (sequences.size() < numSequences && j<=maxIterOuter) {
+		int j = 0;
+		while (sequences.size() < numSequences && j <= numSequences * 100) {
 			j++;
 			double randVal = r.nextDouble() * probSum;
@@ -87,13 +106,14 @@
 			}
 			List<? extends Event<?>> seqTmp = seqsTmp.get(index);
-			if( seqTmp.get(seqTmp.size()-1)!=Event.ENDEVENT ) {
-				for( int i=0 ; i<maxIterInner ; i++ ) {
-					List<? extends Event<?>> sequence = finishSequence(seqTmp, model);
-					if( sequence.size() == length+2 ) {
-						j=0;
-						sequences.add(sequence);
-						break;
-					}
+			if (seqTmp.get(seqTmp.size() - 1) != Event.ENDEVENT) {
+				List<? extends Event<?>> sequence;
+				if (validEnd) {
+					sequence = finishSequence(seqTmp, model, length + 2,
+							validEnd);
+				} else {
+					sequence = finishSequence(seqTmp, model, length + 1,
+							validEnd);
 				}
+				sequences.add(sequence);
 			}
 		}
@@ -103,24 +123,30 @@
 		Console.println("" + sequences.size() + " sequences generated");
 	}
-	
-	public List<? extends Event<?>> finishSequence(List<? extends Event<?>> sequence, IStochasticProcess model) {
+
+	public List<? extends Event<?>> finishSequence(
+			List<? extends Event<?>> sequence, IStochasticProcess model,
+			int maxLength, boolean validEnd) {
 		Random r = new Random();
-		List<Event<?>> sequenceCopy =  new LinkedList<Event<?>>(sequence);
-
 		boolean endFound = false;
+		List<Event<?>> sequenceCopy = new LinkedList<Event<?>>(sequence);
 
 		while (!endFound) {
-			double randVal = r.nextDouble();
-			double probSum = 0.0;
-			for (Event<?> symbol : model.getEvents()) {
-				probSum += model.getProbability(sequenceCopy, symbol);
-				if (probSum >= randVal) {
-					endFound = (symbol == Event.ENDEVENT);
-					if (!(symbol == Event.STARTEVENT )) {
-						// only add the symbol the sequence if it is not START
-						// or END
-						sequenceCopy.add(symbol);
+			sequenceCopy = new LinkedList<Event<?>>(sequence);
+			while (!endFound && sequenceCopy.size() < maxLength) {
+				double randVal = r.nextDouble();
+				double probSum = 0.0;
+				for (Event<?> symbol : model.getEvents()) {
+					probSum += model.getProbability(sequenceCopy, symbol);
+					if (probSum >= randVal) {
+						if (!(symbol == Event.STARTEVENT || symbol == Event.ENDEVENT)) {
+							// only add the symbol the sequence if it is not
+							// START
+							// or END
+							sequenceCopy.add(symbol);
+						}
+						endFound = (symbol == Event.ENDEVENT)
+								|| (!validEnd && sequenceCopy.size() == maxLength);
+						break;
 					}
-					break;
 				}
 			}
@@ -136,5 +162,5 @@
 	@Override
 	public void help() {
-		Console.println("Usage: generateFixedLengthSequences <modelname> <sequencesName> <lenght> <maxlengthAll> <numSequences>");
+		Console.println("Usage: generateFixedLengthSequences <modelname> <sequencesName> <lenght> <maxlengthAll> <numSequences> {<validEnd>}");
 	}
 
