Module: ConceptQL::Behaviors::Dottable

Included in:
Debuggable, GraphNodifier::DotOperator
Defined in:
lib/conceptql/behaviors/dottable.rb

Constant Summary collapse

TYPE_COLORS =
{
  person: 'blue',
  visit_occurrence: 'orange',
  condition_occurrence: 'red',
  procedure_occurrence: 'green3',
  procedure_cost: 'gold',
  death: 'brown',
  payer_plan_period: 'blue',
  drug_exposure: 'purple',
  observation: 'magenta',
  misc: 'black'
}
@@counter =
0

Instance Method Summary collapse

Instance Method Details

#display_nameObject



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/conceptql/behaviors/dottable.rb', line 33

def display_name
  @__display_name ||= begin
    output = self.class.name.split('::').last.snakecase.titlecase
    #output += " #{operator_number}"
    output += ": #{arguments.join(', ')}" unless arguments.empty?
    if output.length > 100
      parts = output.split
      output = parts.each_slice(output.length / parts.count).map do |subparts|
        subparts.join(' ')
      end.join ('\n')
    end
    output += "\n#{options.map{|k,v| "#{k}: #{v}"}.join("\n")}" unless options.nil? || options.empty?
    output
  end
end

#graph_it(g, db) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
# File 'lib/conceptql/behaviors/dottable.rb', line 80

def graph_it(g, db)
  graph_prep(db) if respond_to?(:graph_prep)
  upstreams.each do |upstream|
    upstream.graph_it(g, db)
  end
  operator = graph_operator(g)
  upstreams.each do |upstream|
    upstream.link_to(g, graph_operator(g), db)
  end
  operator
end

#graph_operator(g) ⇒ Object



54
55
56
57
58
59
60
61
62
# File 'lib/conceptql/behaviors/dottable.rb', line 54

def graph_operator(g)
  @__graph_operator ||= begin
    me = g.add_nodes(operator_name)
    me[:label] = display_name
    me[:color] = type_color(types)
    me[:shape] = shape if respond_to?(:shape)
    me
  end
end


64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/conceptql/behaviors/dottable.rb', line 64

def link_to(g, dest_operator, db = nil)
  edge_options = {}

  types.each do |type|
    if db
      my_n = my_n(db, type)
      label = [' rows=' + my_count(db, type).to_s + ' ']
      label << ' n=' + my_n.to_s + ' '
      edge_options[:label] = label.join("\n")
      edge_options[:style] = 'dashed' if my_n.zero?
    end
    e = g.add_edges(graph_operator(g), dest_operator, edge_options)
    e[:color] = type_color(type)
  end
end

#my_count(db, type) ⇒ Object



92
93
94
95
# File 'lib/conceptql/behaviors/dottable.rb', line 92

def my_count(db, type)
  puts "counting #{operator_name} #{type}"
  evaluate(db).from_self.where(criterion_type: type.to_s).count
end

#my_n(db, type) ⇒ Object



97
98
99
# File 'lib/conceptql/behaviors/dottable.rb', line 97

def my_n(db, type)
  evaluate(db).from_self.where(criterion_type: type.to_s).select_group(:person_id).count
end

#operator_nameObject



29
30
31
# File 'lib/conceptql/behaviors/dottable.rb', line 29

def operator_name
  @__operator_name ||= self.class.name.split('::').last.snakecase.gsub(/\W/, '_').downcase + "_#{operator_number}"
end

#operator_numberObject



21
22
23
# File 'lib/conceptql/behaviors/dottable.rb', line 21

def operator_number
  @__operator_number ||= (@@counter += 1)
end

#reset_operator_numberObject



25
26
27
# File 'lib/conceptql/behaviors/dottable.rb', line 25

def reset_operator_number
  @@counter = 0
end

#type_color(*types) ⇒ Object



49
50
51
52
# File 'lib/conceptql/behaviors/dottable.rb', line 49

def type_color(*types)
  types.flatten!
  types.length == 1 ? TYPE_COLORS[types.first] || 'black' : 'black'
end