Class: FactDb::Transformers::CypherTransformer

Inherits:
Base
  • Object
show all
Defined in:
lib/fact_db/transformers/cypher_transformer.rb

Overview

Transforms results into Cypher-like graph notation. This format is readable by both humans and LLMs, and encodes nodes, relationships, and properties explicitly.

Examples:

Output format

(paula:Person {name: "Paula Chen"})
(microsoft:Organization {name: "Microsoft"})
(paula)-[:WORKS_AT {since: "2024-01-10", role: "Principal Engineer"}]->(microsoft)

Instance Method Summary collapse

Instance Method Details

#transform(results) ⇒ String

Transform results to Cypher format.

Parameters:

Returns:

  • (String)

    Cypher-like graph notation



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/fact_db/transformers/cypher_transformer.rb', line 18

def transform(results)
  lines = []
  defined_nodes = Set.new

  # Define entity nodes
  results.each_entity do |entity|
    node_def = entity_to_cypher(entity)
    if node_def && !defined_nodes.include?(node_def)
      lines << node_def
      defined_nodes << node_def
    end
  end

  # Define relationships from facts
  results.each_fact do |fact|
    relationship = fact_to_cypher(fact, results.entities, defined_nodes, lines)
    lines << relationship if relationship
  end

  lines.join("\n")
end