Class: Openphar::Exporters::Neo4jExporter

Inherits:
Object
  • Object
show all
Defined in:
lib/openphar/exporters/neo4j_exporter.rb

Overview

Main Neo4j exporter - orchestrates the export process

Uses model-driven architecture with separate concerns:

  • Neo4j::NodeBuilder: Converts models to Cypher nodes
  • Neo4j::RelationshipBuilder: Builds relationships
  • Neo4j::PropertyMapper: Maps model attributes to Neo4j properties
  • Neo4j::ModelRegistry: Registry of exportable models

Usage:

exporter = Openphar::Exporters::Neo4jExporter.new
exporter.export_monographs("data/jp/crude-drugs/", "output.cypher")
exporter.export_relationships("output.cypher")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_path: nil) ⇒ Neo4jExporter

Returns a new instance of Neo4jExporter.



23
24
25
26
27
28
29
30
31
32
# File 'lib/openphar/exporters/neo4j_exporter.rb', line 23

def initialize(output_path: nil)
  @output_path = output_path
  @stats = { nodes: 0, relationships: 0, properties: 0 }

  # Initialize model-driven components (using Neo4j sub-module)
  registry = Neo4j::ModelRegistry.new
  mapper = Neo4j::PropertyMapper.new(registry)
  @node_builder = Neo4j::NodeBuilder.new(mapper)
  @relationship_builder = Neo4j::RelationshipBuilder.new
end

Instance Attribute Details

#node_builderObject (readonly)

Returns the value of attribute node_builder.



21
22
23
# File 'lib/openphar/exporters/neo4j_exporter.rb', line 21

def node_builder
  @node_builder
end

#output_pathObject (readonly)

Returns the value of attribute output_path.



21
22
23
# File 'lib/openphar/exporters/neo4j_exporter.rb', line 21

def output_path
  @output_path
end

#relationship_builderObject (readonly)

Returns the value of attribute relationship_builder.



21
22
23
# File 'lib/openphar/exporters/neo4j_exporter.rb', line 21

def relationship_builder
  @relationship_builder
end

#statsObject (readonly)

Returns the value of attribute stats.



21
22
23
# File 'lib/openphar/exporters/neo4j_exporter.rb', line 21

def stats
  @stats
end

Instance Method Details

#export_csv(directory, nodes_path, rels_path) ⇒ Object

Export to CSV for Neo4j Admin Import

Parameters:

  • directory (String)

    Directory with JSON-LD files

  • nodes_path (String)

    Output path for nodes CSV

  • rels_path (String)

    Output path for relationships CSV



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/openphar/exporters/neo4j_exporter.rb', line 127

def export_csv(directory, nodes_path, rels_path)
  nodes_csv = CSV.open(nodes_path, "w")
  rels_csv = CSV.open(rels_path, "w")

  # Node header
  nodes_csv << [:id, :type, :monograph_id, :publisher, :status, :label_en, :label_ja]

  # Relationship header
  rels_csv << [:start_id, :end_id, :type, :properties]

  Dir.glob(File.join(directory, "**", "*.jsonld")).sort.each do |file|
    data = JSON.parse(File.read(file))
    next unless data["@id"]

    id = data["@id"]
    type = data["@type"]
    node_type = node_builder.registry.node_type_for(type)

    pref_label = data["prefLabel"] || {}
    label_en = pref_label["en"] || ""
    label_ja = pref_label["ja"] || ""

    nodes_csv << [
      id,
      node_type,
      data["monographId"] || "",
      data["publisher"] || "",
      data["status"] || "",
      label_en,
      label_ja
    ]

    @stats[:nodes] += 1
  end

  nodes_csv.close
  rels_csv.close

  puts "\nCSV Export complete:"
  puts "  Nodes: #{@stats[:nodes]}"
  puts "  Nodes CSV: #{nodes_path}"
  puts "  Rels CSV: #{rels_path}"

  { nodes: nodes_path, rels: rels_path }
end

#export_monographs(directory, output_path = nil, use_merge: false) ⇒ Object

Export all monograph files in a directory

Parameters:

  • directory (String)

    Path to directory containing JSON-LD files

  • output_path (String) (defaults to: nil)

    Path for output Cypher file

  • use_merge (Boolean) (defaults to: false)

    Use MERGE instead of CREATE (default: false)



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/openphar/exporters/neo4j_exporter.rb', line 39

def export_monographs(directory, output_path = nil, use_merge: false)
  @output_path = output_path || @output_path
  @use_merge = use_merge
  reset_stats

  File.open(@output_path, "w") do |f|
    f.puts "// Neo4j Cypher Export - Open Pharmacopoeia"
    f.puts "// Generated: #{Time.now.iso8601}"
    f.puts "// Import method: #{use_merge ? 'MERGE (idempotent)' : 'CREATE'}"
    f.puts ""
    f.puts "// BEGIN TRANSACTION"
    f.puts "BEGIN;"

    Dir.glob(File.join(directory, "**", "*.jsonld")).sort.each do |file|
      puts "Processing: #{file}"
      process_file(file, f)
    end

    f.puts "COMMIT;"
  end

  puts "\nExport complete:"
  puts "  Nodes: #{@stats[:nodes]}"
  puts "  Relationships: #{@stats[:relationships]}"
  puts "  Properties: #{@stats[:properties]}"

  @output_path
end

#export_relationships(output_path = nil) ⇒ Object

Export relationships between entities

Parameters:

  • output_path (String) (defaults to: nil)

    Path for relationship Cypher



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/openphar/exporters/neo4j_exporter.rb', line 71

def export_relationships(output_path = nil)
  output_path ||= @output_path.sub(".cypher", "_rels.cypher")

  File.open(output_path, "w") do |f|
    f.puts "// Neo4j Relationships - Open Pharmacopoeia"
    f.puts "// Generated: #{Time.now.iso8601}"
    f.puts ""
    f.puts "// BEGIN TRANSACTION"
    f.puts "BEGIN;"

    # Belongs to Edition
    f.puts "\n// Edition relationships"
    Dir.glob("data/jp/**/*.jsonld").sort.each do |file|
      data = JSON.parse(File.read(file))
      edition_id = data["belongsToEdition"]
      next unless edition_id

      monograph_id = data["@id"] || data["id"]
      next unless monograph_id

      f.puts @relationship_builder.build_edition_relationship(monograph_id, edition_id)
      @stats[:relationships] += 1
    end

    # Cross-publisher links
    f.puts "\n// Cross-publisher relationships"
    Dir.glob("data/cross-publisher/*.jsonld").sort.each do |file|
      data = JSON.parse(File.read(file))
      (data["@graph"] || [data]).each do |link|
        next unless link["linkType"]

        source = link.dig("sourceMonograph", "@id")
        target = link.dig("targetMonograph", "@id")
        next unless source && target

        rel_type = @relationship_builder.map_link_type(link["linkType"])
        f.puts @relationship_builder.build_cross_publisher_relationship(source, target, rel_type)
        @stats[:relationships] += 1
      end
    end

    f.puts "COMMIT;"
  end

  puts "\nRelationships export complete:"
  puts "  Relationships: #{@stats[:relationships]}"
  puts "  Output: #{output_path}"

  output_path
end