#!/usr/bin/env ruby require 'graphshaper' require 'optparse' start_time = Time.now 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("-s", "--sql", "Store the graph in sql format") do |sql| options[:sql] = sql 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 # check for installed components: graphviz if options[:png] and `which circo` == "" puts "graphviz is not installed, can't export to png. Please install graphviz or run without --png option." exit end # check for running component: AvocadoDB if options[:avocado] begin HTTParty.get 'http://localhost:8529' rescue Errno::ECONNREFUSED, Errno::EHOSTUNREACH puts "No AvocadoDB instance is running on port 8529. Please start AvocadoDB or run without --avocado option." exit end end adapters = [] if options[:log] generated_vertices_csv_file = File.new("generated_vertices.csv", "w") generated_edges_csv_file = File.new("generated_edges.csv", "w") adapters << Graphshaper::LoggingAdapter.new(generated_vertices_csv_file, generated_edges_csv_file) end if options[:avocado] adapters << Graphshaper::AvocadoDbAdapter.new("vertices", "edges") end if options[:dot] generated_graph_dot_file = File.new("generated_graph.dot", "w") adapters << Graphshaper::DotAdapter.new(generated_graph_dot_file) end if options[:sql] generated_graph_sql_file = File.new("generated_graph.sql", "w") generated_vertices_sql_file = File.new("generated_vertices.sql", "w") generated_edges_sql_file = File.new("generated_edges.sql", "w") adapters << Graphshaper::SqlAdapter.new(generated_graph_sql_file, generated_vertices_sql_file, generated_edges_sql_file) end inner_vertices = 20 outer_vertices = ARGV.first.to_i - inner_vertices graph = Graphshaper::UndirectedGraph.new inner_vertices, adapters: adapters graph.connect_all_vertices outer_vertices.times do graph.add_vertex { |preferential_attachment| preferential_attachment > rand } end adapters.each { |adapter| adapter.close } 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] print " and saved in sql format" if options[:sql] if options[:png] system('circo -Tpng generated_graph.dot -o generated_graph.png') end ellapsed_time = Time.now - start_time 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