package org.graphwalker.core.generator;

/*
* #%L
 * * GraphWalker Core
 * *
 * %%
 * Copyright (C) 2005 - 2014 GraphWalker
 * *
 * %%
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 * #L%
*/

import org.graphwalker.core.algorithm.AStar;
import org.graphwalker.core.condition.StopCondition;
import org.graphwalker.core.machine.Context;
import org.graphwalker.core.model.Element;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author Kristian Karl
 *         Generates a random path, but tries to reach unvisited elements first.
 *         This is quite effective for FSM, but for a EFSM this may not work since edges
 *         can be inaccessable.
 */
public final class QuickRandomPath extends PathGeneratorBase<StopCondition> {

    private final Random random = new Random(System.nanoTime());
    private final List<Element> unvisitedElements = new ArrayList<>();
    private Element target = null;

    public QuickRandomPath(StopCondition stopCondition) {
        setStopCondition(stopCondition);
    }

    @Override
    public Context getNextStep() {
        Context context = getContext();
        if (unvisitedElements.isEmpty()) {
            unvisitedElements.addAll(context.getModel().getElements());
            unvisitedElements.remove(context.getCurrentElement());
        }
        if (null == target || target.equals(context.getCurrentElement())) {
            if (unvisitedElements.isEmpty()) {
                throw new NoPathFoundException();
            } else {
                target = unvisitedElements.get(random.nextInt(unvisitedElements.size()));
            }
        }
        Element nextElement = context.getAlgorithm(AStar.class).getNextElement(context.getCurrentElement(), target);
        unvisitedElements.remove(nextElement);
        return context.setCurrentElement(nextElement);
    }

    @Override
    public boolean hasNextStep() {
        return !getStopCondition().isFulfilled();
    }
}


@@ -581,17 +581,16 @@
 ions:%0A *
- 
 %0A * The 
@@ -717,17 +717,16 @@
 ware.%0A *
- 
 %0A * THE 
@@ -1396,16 +1396,65 @@
 Element;
+%0Aimport org.graphwalker.core.statistics.Profiler;
 %0A%0Aimport
@@ -1492,20 +1492,27 @@
 va.util.
-List
+Collections
 ;%0Aimport
@@ -1522,22 +1522,49 @@
 va.util.
-Random
+Comparator;%0Aimport java.util.List
 ;%0A%0A/**%0A 
@@ -1876,73 +1876,8 @@
  %7B%0A%0A
-    private final Random random = new Random(System.nanoTime());%0A
     
@@ -1904,26 +1904,17 @@
 lement%3E 
-unvisitedE
+e
 lements 
@@ -2173,34 +2173,25 @@
         if (
-unvisitedE
+e
 lements.isEm
@@ -2207,34 +2207,25 @@
             
-unvisitedE
+e
 lements.addA
@@ -2270,34 +2270,25 @@
             
-unvisitedE
+e
 lements.remo
@@ -2316,24 +2316,67 @@
 Element());%0A
+            Collections.shuffle(elements);%0A
         %7D%0A  
@@ -2465,26 +2465,17 @@
     if (
-unvisitedE
+e
 lements.
@@ -2578,72 +2578,81 @@
     
-target = unvisitedElements.get(random.nextInt(unvisitedE
+orderElementsUnvisitedFirst(elements);%0A                target = e
 lements.
 size
@@ -2651,15 +2651,13 @@
 nts.
-size())
+get(0
 );%0A 
@@ -2808,18 +2808,9 @@
     
-unvisitedE
+e
 leme
@@ -2888,32 +2888,490 @@
 lement);%0A    %7D%0A%0A
+    private void orderElementsUnvisitedFirst(List%3CElement%3E elements) %7B%0A        final Profiler profiler = getContext().getProfiler();%0A        if (profiler != null) %7B%0A            Collections.sort(elements, new Comparator%3CElement%3E() %7B%0A                @Override%0A                public int compare(Element a, Element b) %7B%0A                    return Boolean.compare(profiler.isVisited(a), profiler.isVisited(b));%0A                %7D%0A            %7D);%0A        %7D%0A    %7D%0A%0A
     @Override%0A  

Make QuickRandomPath start with currently unvisited elements