#!/usr/bin/env ruby require 'graphshaper' require 'optparse' options = {} optparse = OptionParser.new do |opts| opts.banner = "Usage: graphshaper [options] size" opts.on("-a", "--avocado", "Store the graph in a local AvocadoDB instance") do |avocado| options[:avocado] = avocado end opts.on("-l", "--log", "Store the graph in two CSV files for nodes and edges") do |log| options[:log] = log end opts.on("-d", "--dot", "Store the graph in the dot format") do |dot| options[:dot] = dot end opts.on("-p", "--png", "Export the graph as a PNG (graphviz must be installed)") do |png| options[:dot] = png options[:png] = png end opts.on_tail("--version", "Show version") do puts Graphshaper::VERSION exit end opts.parse! end if ARGV.length == 0 puts "Please enter a size for your graph." exit end number_of_vertices = ARGV.first.to_i inner_vertices = 20 adapters = [] open_files = [] if options[:log] vertex_output_file = File.new "vertices.csv", "w" open_files << vertex_output_file edge_output_file = File.new "edges.csv", "w" open_files << edge_output_file adapters << Graphshaper::LoggingAdapter.new(vertex_output_file, edge_output_file) end if options[:avocado] adapters << Graphshaper::AvocadoDbAdapter.new("vertices", "edges") end if options[:dot] dot_output_file = File.new "generated_graph.dot", "w" open_files << dot_output_file dot_adapter = Graphshaper::DotAdapter.new(dot_output_file) adapters << dot_adapter end start_time = Time.now graph = Graphshaper::UndirectedGraph.without_orphans_with_order_of inner_vertices, adapters: adapters (number_of_vertices - inner_vertices).times do graph.add_vertex { |preferential_attachment| preferential_attachment > rand } end dot_adapter.close if options[:dot] ellapsed_time = Time.now - start_time print "#{graph.order} vertices and #{graph.size} edges generated" print " and saved into AvocadoDB" if options[:avocado] print " and logged" if options[:log] print " and generated as a dot" if options[:dot] open_files.each { |file| file.close } if options[:png] system('circo -Tpng generated_graph.dot -o generated_graph.png') end if ellapsed_time < 2 puts " in about one second" elsif ellapsed_time < 60 puts " in about #{ellapsed_time.round} seconds" else puts " in about #{ellapsed_time.round / 60} minutes and #{ellapsed_time.round % 60} seconds" end