Class: OKF::CLI::Graph

Inherits:
Command show all
Defined in:
lib/okf/cli/graph.rb

Overview

The knowledge graph as text — nodes, edges and the rollups the browser page draws, for a reader that has no browser.

Constant Summary

Constants inherited from Command

Command::DUCK_TYPE

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Command

hidden?, #initialize

Constructor Details

This class inherits a constructor from OKF::CLI::Command

Class Method Details

.groupObject



12
13
14
# File 'lib/okf/cli/graph.rb', line 12

def self.group
  :graph
end

.help_rowsObject



16
17
18
19
20
# File 'lib/okf/cli/graph.rb', line 16

def self.help_rows
  [
    [ "graph     <dir|@slug> [--json] [--minimal] [--hubs]", "print the knowledge graph" ]
  ]
end

.idObject



8
9
10
# File 'lib/okf/cli/graph.rb', line 8

def self.id
  :graph
end

Instance Method Details

#call(argv) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/okf/cli/graph.rb', line 22

def call(argv)
  options = { json: false, minimal: false, body: true, hubs: false }
  parser = OptionParser.new do |o|
    o.banner = "Usage: okf graph <dir|@slug> [--json] [--minimal] [--no-body] [--hubs]"
    json_flags(o, options, "emit nodes and edges as JSON")
    o.on("--minimal", "leanest nodes (id + title); adds type/tag indexes") { options[:minimal] = true }
    o.on("--[no-]body", "include each concept's body (default: yes)") { |v| options[:body] = v }
    o.on("--hubs", "rank concepts by inbound links, with the source areas") { options[:hubs] = true }
    help_flag(o)
  end
  dir = positional_dir(parser, argv) or return 2

  return print_hubs(dir, options) if options[:hubs]

  folder = OKF::Bundle::Folder.load(dir)
  graph = folder.graph(minimal: options[:minimal], body: options[:body])
  report_skipped(folder)
  if options[:json]
    # The head every view carries: a payload of nodes and edges that never
    # says which bundle they came from is exactly what an agent holding
    # several bundles has to guess at.
    payload = bundle_head(dir).merge(graph.to_h)
    payload = payload.merge(types: graph.type_index, tags: graph.tag_index) if options[:minimal]
    emit_json(payload)
  else
    @out.puts "Graph — #{bundle_label(dir)} (#{graph.nodes.size} #{pluralize(graph.nodes.size, "concept")}, " \
              "#{graph.edges.size} #{pluralize(graph.edges.size, "link")})"
  end
  0
end