Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Output cluster attributes in CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Apr 13, 2024
1 parent 1fe2631 commit 7d2c9f7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
19 changes: 15 additions & 4 deletions lib/mosaik/graph/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,7 @@ def to_csv
# Set of visited edges (to avoid duplicates in undirected graphs)
visited = Set.new

# rubocop:disable Metrics/BlockLength
CSV.generate do |csv|
# VERTICES
# Collect all vertex attributes
Expand Down Expand Up @@ -208,19 +209,29 @@ def to_csv

next unless clusters.any?

# CLUSTERS
# Separator
csv << ["--"]

# VERTICES
# Collect all vertex attributes
attributes = clusters
.values
.flat_map(&:attributes)
.flat_map(&:keys)
.uniq

# Header
csv << ["vertex", "cluster"]
csv << ["vertex", "cluster", *attributes]

# Add clusters using a cluster mapping
clusters.each do |cluster_id, cluster|
cluster.vertices.each do |vertex|
csv << [vertex.id, cluster_id]
csv << [vertex.id, cluster_id, *attributes.map { |attr| cluster.attributes[attr] }]
end
end
end
# rubocop:enable Metrics/BlockLength
end

sig { returns(String) }
Expand Down Expand Up @@ -255,10 +266,10 @@ def self.from_csv(csv, directed: true)

# Add clusters from the cluster mapping
CSV.new(clusters, headers: true, header_converters: :symbol, converters: :numeric).each do |row|
row => { vertex:, cluster: }
row => { vertex:, cluster:, **attributes }

graph
.find_or_add_cluster(cluster)
.find_or_add_cluster(cluster, attributes.symbolize_keys)
.add_vertex(graph.find_or_add_vertex(vertex))
end

Expand Down
23 changes: 13 additions & 10 deletions spec/mosaik/graph/graph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -478,11 +478,11 @@
graph.add_edge("vertex1", "vertex2")
graph.add_edge("vertex2", "vertex3", method: "method", weight: 1.0)

c1 = graph.add_cluster("cluster1")
c1 = graph.add_cluster("cluster1", type: "database")
c1.add_vertex(v1)
c1.add_vertex(v2)

c2 = graph.add_cluster("cluster2")
c2 = graph.add_cluster("cluster2", type: "microservice")
c2.add_vertex(v3)

graph.add_cluster("cluster3")
Expand All @@ -497,10 +497,10 @@
vertex1,vertex2,,
vertex2,vertex3,method,1.0
--
vertex,cluster
vertex1,cluster1
vertex2,cluster1
vertex3,cluster2
vertex,cluster,type
vertex1,cluster1,database
vertex2,cluster1,database
vertex3,cluster2,microservice
CSV
end
end
Expand Down Expand Up @@ -582,10 +582,10 @@
vertex1,vertex2,,
vertex2,vertex3,method,1.0
--
vertex,cluster
vertex1,cluster1
vertex2,cluster1
vertex3,cluster2
vertex,cluster,type
vertex1,cluster1,database
vertex2,cluster1,database
vertex3,cluster2,microservice
CSV

graph = described_class.from_csv(csv, directed: true)
Expand All @@ -605,7 +605,10 @@

expect(graph.clusters.keys).to eq ["cluster1", "cluster2"]
expect(graph.find_cluster("cluster1").vertices.map(&:id)).to eq ["vertex1", "vertex2"]
expect(graph.find_cluster("cluster1").attributes).to eq type: "database"

expect(graph.find_cluster("cluster2").vertices.map(&:id)).to eq ["vertex3"]
expect(graph.find_cluster("cluster2").attributes).to eq type: "microservice"
end
end
end
Expand Down

0 comments on commit 7d2c9f7

Please sign in to comment.