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.Set;
 
 
     private static final Logger log = Logger.getLogger(AllEdgesBranchDataSetGenerator.class);
 
     private static final AllEdgesBranchDataSetGenerator instance = new AllEdgesBranchDataSetGenerator();
     private static List<DataSetdataSetCache = new LinkedList<>();
 
     private AllEdgesBranchDataSetGenerator() {
     }
 
     public static AllEdgesBranchDataSetGenerator getInstance() {
         return ;
     }
 
     @Override
     public List<DataSetgenerateDataSets(DataSpec dataSpec,
             BranchGraph branchGraph) {
         // only recompute the datasets if they aren't in the cache
         synchronized () {
             if (.isEmpty()) {
                  = internalDataSetGeneration(dataSpecbranchGraph);
             }
         }
         // 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 dataSpec,
             BranchGraph 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) {
                 .info("Adding start node "+node);
                 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);
         }
 
         // basis for all edges is all paths
         List<DiskList<BranchGraphEdge>> allPaths = AllPathsBranchDataSetGenerator.getInstance().allPaths(graphrootNode);
         // the set of edges to be covered = the union of edges covered by all paths
         Set<BranchGraphEdgeuncoveredEdges = new HashSet<>();
         for(DiskList<BranchGraphEdgepath : allPaths){
             uncoveredEdges.addAll(path);
         }
 
         // greedy approximation algorithm for mimimum set-cover
         List<DataSetgeneratedDataSets = new LinkedList<>();
         while (!uncoveredEdges.isEmpty()){
             // find the path that covers the most new edges
             int maxScore = 0;
             List<BranchGraphEdgegreedyPick = null;
             for(List<BranchGraphEdgepath : allPaths){
                 int pathScore = 0;
                 for(BranchGraphEdge edge : path){
                     if (uncoveredEdges.contains(edge)) {
                        pathScore++;
                    }
                }
                if (pathScore>maxScore) {
                    maxScore = pathScore;
                    greedyPick = path;
                }
            }
            // generate a dataset from this path if we can
            List<IBranchGraphElementpathWithNodes = insertNodes(greedyPickgraph);
            try {
                DataSet dataSet = dataSetFromPath(pathWithNodesdataSpec);
                generatedDataSets.add(dataSet);
                uncoveredEdges.removeAll(greedyPick);
                allPaths.remove(greedyPick);
            }
            catch (IllegalStateException e) {
                // the requirements of this path cannot be satisfied, so no dataset, and it gets removed from the options
                allPaths.remove(greedyPick);
                // if this path contains an edge that others do not, then it is impossible to cover that edge
                Set<BranchGraphEdgeremainingEdges = new HashSet<>();
                for(DiskList<BranchGraphEdgepath : allPaths){
                    remainingEdges.addAll(path);
                }
                for(BranchGraphEdge edge : greedyPick){
                    if (!remainingEdges.contains(edge)) {
                        uncoveredEdges.remove(edge);
                    }
                }
            }
        }
        return generatedDataSets;
    }
New to GrepCode? Check out our FAQ X