Class: Openphar::Exporters::Neo4j::RelationshipBuilder

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

Overview

Builds Neo4j Cypher relationships between nodes

Handles:

  • Edition relationships (monograph belongs to edition)
  • Cross-publisher links (sameSubstanceAs, hasEquivalentIn, similarTo)
  • Test specification relationships

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(registry = nil) ⇒ RelationshipBuilder

Returns a new instance of RelationshipBuilder.



15
16
17
# File 'lib/openphar/exporters/neo4j/relationship_builder.rb', line 15

def initialize(registry = nil)
  @registry = registry || ModelRegistry.new
end

Instance Attribute Details

#registryObject (readonly)

Returns the value of attribute registry.



13
14
15
# File 'lib/openphar/exporters/neo4j/relationship_builder.rb', line 13

def registry
  @registry
end

Instance Method Details

#build_cross_publisher_relationship(source_id, target_id, rel_type) ⇒ String

Builds a Cypher relationship for cross-publisher links

Parameters:

  • source_id (String)

    Source monograph @id

  • target_id (String)

    Target monograph @id

  • rel_type (String)

    Relationship type

Returns:

  • (String)

    Cypher MATCH-MERGE statement



38
39
40
41
42
43
44
# File 'lib/openphar/exporters/neo4j/relationship_builder.rb', line 38

def build_cross_publisher_relationship(source_id, target_id, rel_type)
  <<~CYPHER
    MATCH (a:Monograph {id: #{escape(source_id)}})
    MATCH (b:Monograph {id: #{escape(target_id)}})
    MERGE (a)-[:#{rel_type}]->(b);
  CYPHER
end

#build_edition_relationship(monograph_id, edition_id) ⇒ String

Builds a Cypher relationship for edition membership

Parameters:

  • monograph_id (String)

    The monograph's @id

  • edition_id (String)

    The edition's @id

Returns:

  • (String)

    Cypher MATCH-MERGE statement



24
25
26
27
28
29
30
# File 'lib/openphar/exporters/neo4j/relationship_builder.rb', line 24

def build_edition_relationship(monograph_id, edition_id)
  <<~CYPHER
    MATCH (m:Monograph {id: #{escape(monograph_id)}})
    MATCH (e:Edition {id: #{escape(edition_id)}})
    MERGE (m)-[:BELONGS_TO_EDITION]->(e);
  CYPHER
end

#build_test_spec_relationship(monograph_id, test_spec_id) ⇒ String

Builds a test specification relationship

Parameters:

  • monograph_id (String)

    The monograph's @id

  • test_spec_id (String)

    The test specification's @id

Returns:

  • (String)

    Cypher MATCH-MERGE statement



64
65
66
67
68
69
70
# File 'lib/openphar/exporters/neo4j/relationship_builder.rb', line 64

def build_test_spec_relationship(monograph_id, test_spec_id)
  <<~CYPHER
    MATCH (m:Monograph {id: #{escape(monograph_id)}})
    MATCH (t:TestSpec {id: #{escape(test_spec_id)}})
    MERGE (m)-[:HAS_TEST_SPEC]->(t);
  CYPHER
end

Maps a JSON-LD linkType to a Neo4j relationship type

Parameters:

  • link_type (String)

    The link type from JSON-LD

Returns:

  • (String)

    Neo4j relationship type



50
51
52
53
54
55
56
57
# File 'lib/openphar/exporters/neo4j/relationship_builder.rb', line 50

def map_link_type(link_type)
  case link_type
  when 'sameSubstanceAs' then 'SAME_SUBSTANCE_AS'
  when 'hasEquivalentIn' then 'HAS_EQUIVALENT_IN'
  when 'similarTo' then 'SIMILAR_TO'
  else 'RELATED_TO'
  end
end