rdf_to_graphviz 0.0.1 → 0.0.2

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.
Files changed (3) hide show
  1. checksums.yaml +4 -4
  2. data/lib/rdf_to_graphviz.rb +71 -33
  3. metadata +16 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 801d792d71bc697bd14095d435b2a1d956de949d
4
- data.tar.gz: 7a5929bea84a35658da8c8919f2a55b857fecfde
3
+ metadata.gz: d034053d07e3c2b2ba03da99377ec812de05c0c3
4
+ data.tar.gz: 7bd2d44c16cf159e6a797e3cf1c6750b9ad89d06
5
5
  SHA512:
6
- metadata.gz: 5632a45520504376a7aa869659db7f430114a697063d8251f82da82da2da7297c4e9c2a7cb9dabb0d4cb018f215951015b1fbf51a174fb6872e7845576ff9c6c
7
- data.tar.gz: 821ef84f100434a4a3fbd542a11f2d7088081eb02fa766b24b5125ec8a17c243394e7f1a0cb5c58fe7367367c9e147d28b048a26f5c0aa7e4606c32c66d5de28
6
+ metadata.gz: 758beb446cddfbc695b70d559ea5c7b7b97fef44feb470130bd77dbe877680a0b480651890c204c79128599e3946e919d6b96c3afc1b15f6e986836140f5d709
7
+ data.tar.gz: 2f2392a5ab8dbd8523024df116c716765f7a2879f6d6b340ddc283ef0bf474f5c8b6e89b6775828410808599a5c1b961cff10644077b47a25670a290460e6113
@@ -4,6 +4,9 @@ require 'linkeddata'
4
4
  # Require 'sparql' gem
5
5
  require 'sparql'
6
6
 
7
+ # Require 'ruby-graphviz' gem
8
+ require 'ruby-graphviz'
9
+
7
10
  class RdfToGraphviz
8
11
 
9
12
  def term_name(statement)
@@ -31,11 +34,9 @@ class RdfToGraphviz
31
34
  # @option options [TrueClass] :is_literal_object_uniq - default = true
32
35
  # @return [String] - contain representation of RDF::Graph in Graphviz dot
33
36
  #
34
- def rdf_graph_to_dot(rdf_graph, options = {})
35
- txt_dot = ""
36
37
 
37
- #options[:rdf_graph] ||= @graph
38
- options[:digraph] = true if options[:digraph].nil?
38
+ def build_graph(rdf_graph, options = {})
39
+ options[:graph_type] ||= :digraph
39
40
  options[:subject_shape] ||= "doublecircle"
40
41
  options[:object_shape] ||= "circle"
41
42
  options[:literal_object_shape] ||= "rectangle"
@@ -43,43 +44,80 @@ class RdfToGraphviz
43
44
  options[:object_color] ||= "blue"
44
45
  options[:predicate_color] ||= "black"
45
46
  options[:is_literal_object_uniq] = true if options[:is_literal_object_uniq].nil?
46
-
47
-
47
+ nodes = []
48
48
  n=0
49
-
50
- if options[:digraph] == true
51
- txt_dot << "\ndigraph digraph_1 {"
52
- else
53
- txt_dot << "\ngraph graph_1 {"
54
- end
55
-
49
+ graph = GraphViz.new( :G, :type => options[:graph_type] )
56
50
  rdf_graph.each_statement do |statement|
51
+ s = graph.add_nodes( term_name(statement[0]), {:shape => options[:subject_shape], :color => options[:subject_color] })
52
+ nodes << term_name(statement[0])
57
53
 
58
- s = term_name(statement[0])
59
- o = term_name(statement[2])
54
+ if statement[2].literal? then
55
+ if options[:is_literal_object_uniq] == true
56
+ o = graph.add_nodes( statement[2].to_s, {:shape => options[:literal_object_shape]})
57
+ else
58
+ o = graph.add_nodes( "o"+n.to_s, {:label => statement[2].to_s, :shape => options[:literal_object_shape]})
59
+ n += 1
60
+ end
61
+ else
62
+ # unless nodes.include?( statement[2] )
63
+ o = graph.add_nodes( term_name(statement[2]), {:shape => options[:object_shape], :color => options[:object_color]})
64
+ # end
65
+ end
66
+ # Create an edge between the two nodes
67
+
68
+ graph.add_edges( s, o, {:label => statement[1].pname, :color => options[:predicate_color] } )
69
+
70
+ end
71
+ nodes.each do |nd|
72
+ graph.add_nodes( nd, {:shape => options[:subject_shape], :color => options[:subject_color] })
73
+ end
74
+ # graph.output( :png => "res_graph.png" )
75
+ graph
76
+ end
60
77
 
61
- txt_dot << "\n\"#{s}\"[label=\"#{s}\", color=#{options[:subject_color]}, shape=#{options[:subject_shape]}];"
62
78
 
63
- if statement[2].literal?
64
- if options[:is_literal_object_uniq] == true
65
- txt_dot << "\n\"#{o}\"[label=\"#{o}\", shape=#{options[:literal_object_shape]}];"
66
- else
79
+ ##
80
+ # rdf_graph_to_dot(rdf_graph, options = {}) generate simple dot graph from RDF::Graph
81
+ # @param [RDF::Graph] rdf_graph
82
+ # @param [Hash{Symbol => Object}] options
83
+ # @option options [TrueClass] :digraph - default = true - if true, graph has a direction
84
+ # @option options [String] :subject_shape - default = doublecircle
85
+ # @option options [String] :object_shape - default = circle
86
+ # @option options [String] :literal_object_shape - default = rectangle
87
+ # @option options [String] :subject_color - default = red
88
+ # @option options [String] :object_color - default = blue
89
+ # @option options [String] :predicate_color - default = black
90
+ # @option options [TrueClass] :is_literal_object_uniq - default = true
91
+ # @return [String] - contain representation of RDF::Graph in Graphviz dot
92
+ #
93
+ def rdf_graph_to_dot(rdf_graph, options = {})
94
+ build_graph(rdf_graph, options).to_s
95
+ end
67
96
 
68
- txt_dot << "\n#{"\"o"+n.to_s}\"[label=\"#{o}\", shape=#{options[:literal_object_shape]}];"
69
-
70
- o = "o"+n.to_s
71
- n += 1
72
- end
73
- else
74
- txt_dot << "\n\"#{o}\"[label=\"#{o}\", color=#{options[:object_color]}, shape=#{options[:object_shape]}];"
75
- end
76
97
 
77
- txt_dot << "\n\"#{s}\" -> \"#{o}\" [label=\"#{statement[1].pname}\", color=#{options[:predicate_color]}];"
78
-
79
- end
80
- txt_dot << "}"
98
+ ##
99
+ # rdf_graph_to_dot(rdf_graph, options = {}) generate simple dot graph from RDF::Graph
100
+ # @param [RDF::Graph] rdf_graph
101
+ # @param [Hash{Symbol => Object}] options
102
+ # @option options [TrueClass] :digraph - default = true - if true, graph has a direction
103
+ # @option options [String] :subject_shape - default = doublecircle
104
+ # @option options [String] :object_shape - default = circle
105
+ # @option options [String] :literal_object_shape - default = rectangle
106
+ # @option options [String] :subject_color - default = red
107
+ # @option options [String] :object_color - default = blue
108
+ # @option options [String] :predicate_color - default = black
109
+ # @option options [TrueClass] :is_literal_object_uniq - default = true
110
+ # @option options [String] :format - default = :png
111
+ # @option options [String] :file_name - default = "res_graph.png"
112
+ # @return [nil]
113
+ #
114
+ def save_rdf_graph_as(rdf_graph, options = {})
115
+ options[:format] ||= :png
116
+ options[:file_name] ||= "res_graph.png"
117
+ g = build_graph(rdf_graph, options)
118
+ g.output( options[:format] => options[:file_name] )
81
119
  end
82
120
 
83
- private :term_name
121
+ private :term_name, :build_graph
84
122
 
85
123
  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.1
4
+ version: 0.0.2
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-21 00:00:00.000000000 Z
11
+ date: 2016-04-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: linkeddata
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: ruby-graphviz
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
41
55
  description: "Gem translate RDF::Graph to Graphviz Dot format.\n\tRequire: linkeddata
42
56
  & sparql gems. It's not compatible with 0.0.0.2!"
43
57
  email: wdulek@gmail.com