Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * (C) Copyright 2013 DataGenerator Contributors.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  */
 package org.finra.datagenerator.generation;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
     private static final Logger log = Logger.getLogger(AllPathsBranchDataSetGenerator.class);
 
     private static int LOOP_DEPTH = 1; //max number of visits to any node
     private static List<DataSetdataSetCache = new LinkedList<>();
 
     private AllPathsBranchDataSetGenerator() {
     }
 
     public static AllPathsBranchDataSetGenerator getInstance() {
         return ;
     }
 
     public List<DataSetgenerateDataSets(DataSpec dataSpecBranchGraph branchGraph) {
         // only recompute the datasets if they aren't in the cache
         // Who would have put them into the cache? If allPaths had been run before now?
         synchronized () {
             if (.isEmpty()) {
                  = internalDataSetGeneration(dataSpecbranchGraph);
             }
         }
 
         //Why is a defensive copy necessary? Is someone changing the datasets after generation?
         // return defensive copy of the cached dataset
         List<DataSetret = new LinkedList<>();
         for(DataSet cachedDs : ){
             ret.add(new DataSet(cachedDs));
         }
         return ret;
     }
 
     private List<DataSetinternalDataSetGeneration(DataSpec dataSpecBranchGraph graph) {
         // add a root state as the parent of all possible start states
         List<BranchGraphNodestartNodes = new LinkedList<>();
         for(BranchGraphNode node : graph.vertexSet()){
             if (graph.inDegreeOf(node)==0) {
                 startNodes.add(node);
             }
         }
         Preconditions.checkArgument(!startNodes.isEmpty(), "No nodes with indegree 0 (aka start nodes) were found in the graph.");
         BranchGraphNode rootNode = new BranchGraphNode(.);
         graph.addVertex(rootNode);
         for(BranchGraphNode startNode : startNodes){
             graph.addEdge(rootNodestartNode);
         }
 
         List<DiskList<BranchGraphEdge>> pathsList = allPaths(graphrootNode);
         .info("There are "+pathsList.size()+" paths.");
 
         // now that we have all the paths, generate datasets from them
         List<DataSetgeneratedDataSets = new LinkedList<>();
         int i = 0;
         for(List<BranchGraphEdgepath : pathsList){
 
             if (i%1000==0) {
                 .info("Processed path "+i);
             }
 
             i++;
 
             List<IBranchGraphElementpathWithNodes = insertNodes(pathgraph);
             try {
                 DataSet dataSet = dataSetFromPath(pathWithNodesdataSpec);
                 generatedDataSets.add(dataSet);
            }
            catch (IllegalStateException e) {
                .warn("Skipping dataset due to IllegalStateException: "+e.getMessage());
                continue// the requirements of this path cannot be satisfied, so no dataset
            }
        }
        return generatedDataSets;
    }
    protected List<DiskList<BranchGraphEdge>> allPaths(
            BranchGraph graphBranchGraphNode rootNode) {
        List<DiskList<BranchGraphEdge>> pathsList = new LinkedList<>();
        // for each starting edge
        for(BranchGraphEdge startEdge : graph.outgoingEdgesOf(rootNode)){
            // keep a count of how many visits to each node
            Map<BranchGraphNodeIntegernodeVisits = new HashMap<>();
            for(BranchGraphNode node : graph.vertexSet()){
                nodeVisits.put(node, 0);
            }
            // we start off visiting this edge and node
            LinkedList<BranchGraphEdgecurPath = new LinkedList<>();
            curPath.add(startEdge);
            nodeVisits.put(graph.getEdgeTarget(startEdge), 1);
            findPaths(graphcurPathnodeVisitspathsList);
        }
        return pathsList;
    }
    private void findPaths(
            BranchGraph graph,
            LinkedList<BranchGraphEdgecurPath,
            Map<BranchGraphNodeIntegernodeVisits,
            List<DiskList<BranchGraphEdge>> pathsList) {
        Set<BranchGraphEdgeneighborEdges = graph.outgoingEdgesOf(graph.getEdgeTarget(curPath.getLast()));
        if (neighborEdges.isEmpty()) // special case where we start on an isolated node
        {
            pathsList.add(new DiskList<>(curPath));
        }
        // examine adjacent edges for path termination condition
        for(BranchGraphEdge neighbor : neighborEdges){
            if (nodeVisits.get(graph.getEdgeTarget(neighbor))>=
                    ||graph.outDegreeOf(graph.getEdgeTarget(neighbor))==0) {
                curPath.add(neighbor);
                pathsList.add(new DiskList<>(curPath));
                curPath.removeLast();
                // don't break here because multiple edges may go to end state
            }
        }
        //recursive search on all neighbor edges that aren't termination edges
        for(BranchGraphEdge neighbor : neighborEdges){
            BranchGraphNode neighborNode = graph.getEdgeTarget(neighbor);
            if (nodeVisits.get(neighborNode)>=
                    ||graph.outDegreeOf(neighborNode)==0) {
                continue;
            }
            curPath.add(neighbor);
            nodeVisits.put(neighborNodenodeVisits.get(neighborNode)+1); //increment the visit count
            findPaths(graphcurPathnodeVisitspathsList);
            nodeVisits.put(neighborNodenodeVisits.get(neighborNode)-1); //decrement as we back up
            curPath.removeLast();
        }
    }
New to GrepCode? Check out our FAQ X