graphs 0.1.5-x86-linux → 0.1.6-x86-linux
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/lib/graphs/json.rb +67 -0
- data/tests/gdf_tests.rb +8 -8
- data/tests/json_tests.rb +133 -0
- data/tests/tests.rb +11 -0
- metadata +11 -6
data/lib/graphs/json.rb
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
#! /usr/bin/ruby1.9.1
|
2
|
+
# -*- coding: UTF-8 -*-
|
3
|
+
|
4
|
+
require 'json'
|
5
|
+
require_relative '../graph'
|
6
|
+
|
7
|
+
class Graph
|
8
|
+
# Returns a JSON version of the current graph
|
9
|
+
# @param opts [Hash] A customizable set of options
|
10
|
+
# @see JSONGraph::unparse
|
11
|
+
def to_json(opts=nil)
|
12
|
+
JSONGraph::unparse(self, opts)
|
13
|
+
end
|
14
|
+
|
15
|
+
# Write the current graph into a JSON file. This method is used internally,
|
16
|
+
# use Graph#write instead.
|
17
|
+
# @param filename [String] a valid filename
|
18
|
+
# @see JSON::unparse
|
19
|
+
def write_json(filename, opts=nil)
|
20
|
+
json = JSONGraph::unparse(self, opts)
|
21
|
+
f = File.open(filename, 'w')
|
22
|
+
f.write(json)
|
23
|
+
f.close
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
# JSON-related functions
|
28
|
+
module JSONGraph
|
29
|
+
|
30
|
+
# Loads a JSON file and return a new Graph object
|
31
|
+
# @param filename [String] a valid filename
|
32
|
+
# @see JSONGraph::parse
|
33
|
+
def self.load(filename)
|
34
|
+
self.parse(File.read(filename))
|
35
|
+
end
|
36
|
+
|
37
|
+
# Parse some JSON text and return a new Graph object
|
38
|
+
# @param content [String] a valid GDF String
|
39
|
+
# @see JSONGraph::load
|
40
|
+
# @see JSONGraph::unparse
|
41
|
+
def self.parse(content)
|
42
|
+
|
43
|
+
if (content.nil? || content.length == 0)
|
44
|
+
return Graph.new([],[])
|
45
|
+
end
|
46
|
+
|
47
|
+
content = JSON.parse content
|
48
|
+
|
49
|
+
nodes = content['nodes']
|
50
|
+
edges = content['edges']
|
51
|
+
|
52
|
+
Graph.new(nodes, edges)
|
53
|
+
end
|
54
|
+
|
55
|
+
# Return a JSON String which describe the given Graph
|
56
|
+
# @param graph [Graph]
|
57
|
+
# @param opts [Hash] A customizable set of options
|
58
|
+
# @see Graph#write
|
59
|
+
def self.unparse(graph, opts=nil)
|
60
|
+
|
61
|
+
nodes = graph.nodes.map { |n| n.to_hash }
|
62
|
+
edges = graph.edges.map { |e| e.to_hash }
|
63
|
+
|
64
|
+
JSON.dump({ 'nodes' => nodes, 'edges' => edges })
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
data/tests/gdf_tests.rb
CHANGED
@@ -35,15 +35,15 @@ class GDF_Graph_test < Test::Unit::TestCase
|
|
35
35
|
|
36
36
|
def test_empty_graph_write_gdf
|
37
37
|
g = Graph.new
|
38
|
-
g.write('/tmp/_graph_test.gdf')
|
39
|
-
g2 = GDF.load('/tmp/_graph_test.gdf')
|
40
|
-
assert_equal(g, g2)
|
41
|
-
end
|
42
38
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
39
|
+
f = Tempfile.new([ 'foo', '.gdf' ])
|
40
|
+
f.close
|
41
|
+
|
42
|
+
g.write(f.path)
|
43
|
+
g2 = GDF.load(f.path)
|
44
|
+
|
45
|
+
assert_equal(g, g2)
|
46
|
+
f.unlink
|
47
47
|
end
|
48
48
|
end
|
49
49
|
|
data/tests/json_tests.rb
ADDED
@@ -0,0 +1,133 @@
|
|
1
|
+
#! /usr/bin/ruby1.9.1
|
2
|
+
# -*- coding: UTF-8 -*-
|
3
|
+
|
4
|
+
require_relative '../lib/graphs/json'
|
5
|
+
|
6
|
+
module JSONUtils
|
7
|
+
def self.get_sample_graph
|
8
|
+
@@json
|
9
|
+
end
|
10
|
+
|
11
|
+
@@json = <<EOJSON
|
12
|
+
{
|
13
|
+
"nodes" : [
|
14
|
+
{ "label": "foo" }, { "label": "bar" }
|
15
|
+
],
|
16
|
+
"edges" : [
|
17
|
+
{ "node1": "bar", "node2": "foo" }
|
18
|
+
]
|
19
|
+
}
|
20
|
+
EOJSON
|
21
|
+
|
22
|
+
@@json.gsub!(/\s+/, '')
|
23
|
+
end
|
24
|
+
|
25
|
+
class JSON_Graph_test < Test::Unit::TestCase
|
26
|
+
|
27
|
+
# == Graph#to_json == #
|
28
|
+
|
29
|
+
def test_empty_graph_to_json
|
30
|
+
g = Graph.new
|
31
|
+
empty_json = '{"nodes":[],"edges":[]}'
|
32
|
+
assert_equal(empty_json, g.to_json)
|
33
|
+
end
|
34
|
+
|
35
|
+
def test_sample_graph_to_json
|
36
|
+
json = JSONUtils::get_sample_graph
|
37
|
+
g = JSONGraph::parse(json)
|
38
|
+
assert_equal(json, g.to_json)
|
39
|
+
end
|
40
|
+
|
41
|
+
# == Graph#write('….json') == #
|
42
|
+
|
43
|
+
def test_empty_graph_write_json
|
44
|
+
g = Graph.new
|
45
|
+
|
46
|
+
f = Tempfile.new([ 'foo', '.json' ])
|
47
|
+
f.close
|
48
|
+
|
49
|
+
g.write(f.path)
|
50
|
+
g2 = JSONGraph.load(f.path)
|
51
|
+
|
52
|
+
assert_equal(g, g2)
|
53
|
+
f.unlink
|
54
|
+
end
|
55
|
+
|
56
|
+
def setup
|
57
|
+
if File.exists? '/tmp/_graph_test.json'
|
58
|
+
File.delete '/tmp/_graph_test.json'
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
|
64
|
+
class JSON_test < Test::Unit::TestCase
|
65
|
+
|
66
|
+
# == JSON::parse == #
|
67
|
+
|
68
|
+
def test_parse_empty_graph
|
69
|
+
g = JSONGraph::parse('')
|
70
|
+
|
71
|
+
assert_equal([], g.nodes)
|
72
|
+
assert_equal([], g.edges)
|
73
|
+
end
|
74
|
+
|
75
|
+
def test_parse_empty_graph_with_nodes_list
|
76
|
+
|
77
|
+
s = '{"nodes":[]}'
|
78
|
+
|
79
|
+
g = JSONGraph::parse(s)
|
80
|
+
|
81
|
+
assert_equal([], g.nodes)
|
82
|
+
assert_equal([], g.edges)
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_parse_empty_graph_with_nodes_and_edges_lists
|
86
|
+
s = '{"nodes":[],"edges":[]}'
|
87
|
+
g = JSONGraph::parse(s)
|
88
|
+
|
89
|
+
assert_equal([], g.nodes)
|
90
|
+
assert_equal([], g.edges)
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_parse_one_node_no_edge
|
94
|
+
s = '{"nodes":[{"label":"foo"}]}'
|
95
|
+
g = JSONGraph::parse(s)
|
96
|
+
|
97
|
+
assert_equal(1, g.nodes.length)
|
98
|
+
assert_equal('foo', g.nodes[0]['label'])
|
99
|
+
assert_equal([], g.edges)
|
100
|
+
end
|
101
|
+
|
102
|
+
def test_parse_sample_graph
|
103
|
+
g = JSONGraph::parse(JSONUtils::get_sample_graph)
|
104
|
+
|
105
|
+
assert_equal(2, g.nodes.length)
|
106
|
+
assert_equal(1, g.edges.length)
|
107
|
+
|
108
|
+
assert_equal('foo', g.nodes[0]['label'])
|
109
|
+
assert_equal('bar', g.nodes[1]['label'])
|
110
|
+
|
111
|
+
assert_equal('bar', g.edges[0]['node1'])
|
112
|
+
assert_equal('foo', g.edges[0]['node2'])
|
113
|
+
|
114
|
+
end
|
115
|
+
|
116
|
+
# == JSON::unparse == #
|
117
|
+
|
118
|
+
def test_unparse_empty_graph
|
119
|
+
g = Graph.new
|
120
|
+
|
121
|
+
s = JSONGraph::unparse(g)
|
122
|
+
|
123
|
+
assert_equal('{"nodes":[],"edges":[]}', s)
|
124
|
+
end
|
125
|
+
|
126
|
+
def test_unparse_sample_graph
|
127
|
+
g1 = JSONGraph::parse(JSONUtils::get_sample_graph)
|
128
|
+
g2 = JSONGraph::parse(JSONGraph::unparse(g1))
|
129
|
+
|
130
|
+
assert_equal(g1, g2)
|
131
|
+
end
|
132
|
+
|
133
|
+
end
|
data/tests/tests.rb
ADDED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: graphs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.6
|
5
5
|
prerelease:
|
6
6
|
platform: x86-linux
|
7
7
|
authors:
|
@@ -9,9 +9,9 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2013-01-15 00:00:00.000000000 Z
|
13
13
|
dependencies: []
|
14
|
-
description: Provide functions to (un)parse GDF files and generate graphs
|
14
|
+
description: Provide functions to (un)parse GDF/JSON files and generate graphs
|
15
15
|
email: batifon@yahoo.fr
|
16
16
|
executables: []
|
17
17
|
extensions: []
|
@@ -19,10 +19,13 @@ extra_rdoc_files: []
|
|
19
19
|
files:
|
20
20
|
- lib/graph.rb
|
21
21
|
- lib/graphs/gdf.rb
|
22
|
+
- lib/graphs/json.rb
|
22
23
|
- tests/graph_tests.rb
|
23
|
-
- tests/
|
24
|
+
- tests/tests.rb
|
24
25
|
- tests/node_tests.rb
|
26
|
+
- tests/gdf_tests.rb
|
25
27
|
- tests/gexf_tests.rb
|
28
|
+
- tests/json_tests.rb
|
26
29
|
homepage: https://github.com/bfontaine/Graphs.rb
|
27
30
|
licenses:
|
28
31
|
- MIT
|
@@ -44,12 +47,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
44
47
|
version: '0'
|
45
48
|
requirements: []
|
46
49
|
rubyforge_project:
|
47
|
-
rubygems_version: 1.8.
|
50
|
+
rubygems_version: 1.8.23
|
48
51
|
signing_key:
|
49
52
|
specification_version: 3
|
50
53
|
summary: Utilities to manipulate graph files
|
51
54
|
test_files:
|
52
55
|
- tests/graph_tests.rb
|
53
|
-
- tests/
|
56
|
+
- tests/tests.rb
|
54
57
|
- tests/node_tests.rb
|
58
|
+
- tests/gdf_tests.rb
|
55
59
|
- tests/gexf_tests.rb
|
60
|
+
- tests/json_tests.rb
|