rdf_to_graphviz 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/lib/rdf_to_graphviz.rb +118 -40
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 7f3550df099393a3d0b3120acceeadcb8a492243
|
4
|
+
data.tar.gz: a53c02da8a8272e7c9fa3c9aaf7aadd4e8cdcb23
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 308cdaf9dfd133ee111061d7e1713b4bd9138e170d6eb37d716f2de9856afdfce29df4fcf11b97a1b0f4fb04e6f6bbb7b9fb5c7a0b9cd84338fb53391fa2a8ed
|
7
|
+
data.tar.gz: 051ca5e51b0abbe1f169276269ea582bc1ef7130eb3b4b28fa552774b55d3cafc54d06fa28b5bf0af7ef0c6e2140f449d165cc52d062748922d569f055b65ed3
|
data/lib/rdf_to_graphviz.rb
CHANGED
@@ -7,8 +7,34 @@ require 'sparql'
|
|
7
7
|
# Require 'ruby-graphviz' gem
|
8
8
|
require 'ruby-graphviz'
|
9
9
|
|
10
|
+
|
11
|
+
# @note The most advance example is show in scenrio 3. The most advance example is shown in scenario 3. The different gem (project) will address the question: how automatically generate graphviz attr file by rules.
|
12
|
+
# @example scenario 1: Generates Graphviz graph from rdf_graph and save to png files (default files name: "res_graph.png")
|
13
|
+
# require 'rdf_to_graphviz'
|
14
|
+
# konwerter = RdfToGraphviz.new
|
15
|
+
# queryable = RDF::Graph.load("http://ruby-rdf.github.com/rdf/etc/doap.nt")
|
16
|
+
# konwerter.save_rdf_graph_as(queryable, :is_literal_object_uniq => false) # literal objcects aren't unig
|
17
|
+
# @example scenario 2: Generates dot string from rdf_graph (default options). The same result like in 1 scenario.
|
18
|
+
# require 'rdf_to_graphviz'
|
19
|
+
# konwerter = RdfToGraphviz.new
|
20
|
+
# queryable = RDF::Graph.load("http://ruby-rdf.github.com/rdf/etc/doap.nt")
|
21
|
+
# str = konwerter.rdf_graph_to_dot(queryable)
|
22
|
+
# GraphViz.parse_string(str).output( :png => "res_graph.png" )
|
23
|
+
# @example scenario 3: Generate Graphviz graph from rdf graph and save to png files using attributes defining in another rdf file
|
24
|
+
# require 'rdf_to_graphviz'
|
25
|
+
# konwerter = RdfToGraphviz.new
|
26
|
+
# queryable = RDF::Graph.load("https://github.com/widu/rdf_to_graphviz/blob/master/ttl/test1.ttl")
|
27
|
+
# rdf_presentation_attr = = RDF::Graph.load("https://github.com/widu/rdf_to_graphviz/blob/master/ttl/grvz.ttl") # reads rdf which contain attr definitions from turtle file
|
28
|
+
# options = {:presentation_attr => rdf_presentation_attr}
|
29
|
+
# konwerter.save_rdf_graph_as(queryable, options)
|
30
|
+
|
10
31
|
class RdfToGraphviz
|
11
32
|
|
33
|
+
def initialize
|
34
|
+
|
35
|
+
@atr = {}
|
36
|
+
end
|
37
|
+
|
12
38
|
def term_name(statement)
|
13
39
|
if statement.literal?
|
14
40
|
statement.to_s
|
@@ -21,7 +47,7 @@ class RdfToGraphviz
|
|
21
47
|
end
|
22
48
|
|
23
49
|
##
|
24
|
-
#
|
50
|
+
# Build graph object from rdf_graph
|
25
51
|
# @param [RDF::Graph] rdf_graph
|
26
52
|
# @param [Hash{Symbol => Object}] options
|
27
53
|
# @option options [TrueClass] :digraph - default = true - if true, graph has a direction
|
@@ -34,7 +60,6 @@ class RdfToGraphviz
|
|
34
60
|
# @option options [TrueClass] :is_literal_object_uniq - default = true
|
35
61
|
# @return [String] - contain representation of RDF::Graph in Graphviz dot
|
36
62
|
#
|
37
|
-
|
38
63
|
def build_graph(rdf_graph, options = {})
|
39
64
|
options[:graph_type] ||= :digraph
|
40
65
|
options[:subject_shape] ||= "doublecircle"
|
@@ -48,7 +73,11 @@ class RdfToGraphviz
|
|
48
73
|
n=0
|
49
74
|
graph = GraphViz.new( :G, :type => options[:graph_type] )
|
50
75
|
rdf_graph.each_statement do |statement|
|
51
|
-
|
76
|
+
if @atr[term_name(statement[0])]
|
77
|
+
s = graph.add_nodes( term_name(statement[0]), @atr[term_name(statement[0])])
|
78
|
+
else
|
79
|
+
s = graph.add_nodes( term_name(statement[0]), {:shape => options[:subject_shape], :color => options[:subject_color] })
|
80
|
+
end
|
52
81
|
nodes << term_name(statement[0])
|
53
82
|
|
54
83
|
if statement[2].literal? then
|
@@ -59,65 +88,114 @@ class RdfToGraphviz
|
|
59
88
|
n += 1
|
60
89
|
end
|
61
90
|
else
|
62
|
-
|
63
|
-
|
64
|
-
|
91
|
+
if @atr[term_name(statement[2])]
|
92
|
+
o = graph.add_nodes( term_name(statement[2]), @atr[term_name(statement[2])])
|
93
|
+
else
|
94
|
+
o = graph.add_nodes( term_name(statement[2]), {:shape => options[:object_shape], :color => options[:object_color]})
|
95
|
+
end
|
65
96
|
end
|
66
97
|
# Create an edge between the two nodes
|
67
|
-
|
68
|
-
|
69
|
-
|
98
|
+
if @atr[statement[1].pname]
|
99
|
+
graph.add_edges( s, o, @atr[statement[1].pname] )
|
100
|
+
else
|
101
|
+
graph.add_edges( s, o, {:label => statement[1].pname, :color => options[:predicate_color] } )
|
102
|
+
end
|
70
103
|
end
|
71
104
|
nodes.each do |nd|
|
72
|
-
|
105
|
+
if @atr[nd]
|
106
|
+
graph.add_nodes( nd, @atr[nd])
|
107
|
+
else
|
108
|
+
graph.add_nodes( nd, {:shape => options[:subject_shape], :color => options[:subject_color] })
|
109
|
+
end
|
73
110
|
end
|
74
111
|
# graph.output( :png => "res_graph.png" )
|
75
112
|
graph
|
76
113
|
end
|
77
114
|
|
115
|
+
def build_atr(rdf_graph)
|
116
|
+
|
117
|
+
atr_lok = {}
|
118
|
+
rdf_graph.each_statement do |statement|
|
119
|
+
p = statement[1].pname
|
120
|
+
# puts p
|
121
|
+
indx = p.index("grvz#")
|
78
122
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
123
|
+
if indx
|
124
|
+
p1 = p[indx+5..-1].downcase
|
125
|
+
|
126
|
+
obj = term_name(statement[0])
|
127
|
+
# puts obj
|
128
|
+
# puts p
|
129
|
+
st= statement[2].to_s
|
130
|
+
atr_lok = @atr[obj]
|
131
|
+
if atr_lok == nil then
|
132
|
+
atr_lok = {p1 => st}
|
133
|
+
else
|
134
|
+
atr_lok[p1] = st
|
135
|
+
end
|
136
|
+
#puts atr_lok
|
137
|
+
@atr[obj] = atr_lok
|
138
|
+
end
|
139
|
+
end
|
140
|
+
@atr
|
141
|
+
|
142
|
+
end
|
143
|
+
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
# Generates string containing definition of graph in dot format from RDF::Graph
|
148
|
+
# @note This method should only be used in outer space.
|
149
|
+
# @param [RDF::Graph] rdf_graph
|
150
|
+
# @param [Hash{Symbol => Object}] options
|
151
|
+
# @option options [TrueClass] :digraph (true)- if true, graph has a direction
|
152
|
+
# @option options [String] :subject_shape ("doublecircle")
|
153
|
+
# @option options [String] :object_shape ("circle")
|
154
|
+
# @option options [String] :literal_object_shape ("rectangle")
|
155
|
+
# @option options [String] :subject_color ("red")
|
156
|
+
# @option options [String] :object_color ("blue")
|
157
|
+
# @option options [String] :predicate_color ("black")
|
158
|
+
# @option options [TrueClass] :is_literal_object_uniq (true)
|
159
|
+
# @option options [RDF::Graph] :presentation_attr
|
160
|
+
# @return [String] - contain representation of RDF::Graph in Graphviz dot
|
93
161
|
def rdf_graph_to_dot(rdf_graph, options = {})
|
162
|
+
if options[:presentation_attr]
|
163
|
+
build_atr(options[:presentation_attr])
|
164
|
+
end
|
94
165
|
build_graph(rdf_graph, options).to_s
|
95
166
|
end
|
96
167
|
|
97
168
|
|
98
|
-
|
99
|
-
#
|
100
|
-
#
|
101
|
-
#
|
102
|
-
#
|
103
|
-
#
|
104
|
-
#
|
105
|
-
#
|
106
|
-
#
|
107
|
-
#
|
108
|
-
#
|
109
|
-
#
|
110
|
-
#
|
111
|
-
#
|
112
|
-
#
|
169
|
+
|
170
|
+
# Saves RDF::Graph as png file or different format
|
171
|
+
#
|
172
|
+
# @param [RDF::Graph] rdf_graph
|
173
|
+
# @param [Hash{Symbol => Object}] options
|
174
|
+
# @option options [TrueClass] :digraph (true)- if true, graph has a direction
|
175
|
+
# @option options [String] :subject_shape ("doublecircle")
|
176
|
+
# @option options [String] :object_shape ("circle")
|
177
|
+
# @option options [String] :literal_object_shape ("rectangle")
|
178
|
+
# @option options [String] :subject_color ("red")
|
179
|
+
# @option options [String] :object_color ("blue")
|
180
|
+
# @option options [String] :predicate_color ("black")
|
181
|
+
# @option options [TrueClass] :is_literal_object_uniq (true)
|
182
|
+
# @option options [RDF::Graph] :presentation_attr
|
183
|
+
# @option options [String] :format (:png)
|
184
|
+
# @option options [String] :file_name ("res_graph.png")
|
185
|
+
# @return [nil]
|
113
186
|
#
|
114
187
|
def save_rdf_graph_as(rdf_graph, options = {})
|
115
188
|
options[:format] ||= :png
|
116
189
|
options[:file_name] ||= "res_graph.png"
|
190
|
+
if options[:presentation_attr]
|
191
|
+
build_atr(options[:presentation_attr])
|
192
|
+
end
|
117
193
|
g = build_graph(rdf_graph, options)
|
118
194
|
g.output( options[:format] => options[:file_name] )
|
119
195
|
end
|
120
196
|
|
121
|
-
|
197
|
+
|
198
|
+
|
199
|
+
private :term_name, :build_graph, :build_atr
|
122
200
|
|
123
201
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rdf_to_graphviz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- WiDu
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-04-
|
11
|
+
date: 2016-04-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: linkeddata
|
@@ -52,8 +52,7 @@ dependencies:
|
|
52
52
|
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
54
|
version: '0'
|
55
|
-
description:
|
56
|
-
& sparql gems. It's not compatible with 0.0.0.2!"
|
55
|
+
description: Gem translate RDF::Graph to Graphviz Dot format.
|
57
56
|
email: wdulek@gmail.com
|
58
57
|
executables: []
|
59
58
|
extensions: []
|
@@ -83,5 +82,6 @@ rubyforge_project:
|
|
83
82
|
rubygems_version: 2.4.5.1
|
84
83
|
signing_key:
|
85
84
|
specification_version: 4
|
86
|
-
summary:
|
85
|
+
summary: Generate simple dot graph from RDF::Graph
|
87
86
|
test_files: []
|
87
|
+
has_rdoc:
|