graphs 0.1.6-x86-linux → 0.1.7-x86-linux

Sign up to get free protection for your applications and to get access to all the features.
data/lib/graph.rb CHANGED
@@ -65,7 +65,15 @@ class Graph
65
65
  @attrs == other.attrs
66
66
  end
67
67
 
68
+ def update(h)
69
+ Node.new super(h)
70
+ end
71
+
68
72
  def method_missing(method, *args, &block)
73
+
74
+ return @attrs[method.to_sym] if @attrs.has_key? method.to_sym
75
+ return @attrs[method.to_s] if @attrs.has_key? method.to_s
76
+
69
77
  @attrs.send(method, *args, &block)
70
78
  end
71
79
 
@@ -89,7 +97,15 @@ class Graph
89
97
  @attrs == other.attrs
90
98
  end
91
99
 
100
+ def update(h)
101
+ Edge.new super(h)
102
+ end
103
+
92
104
  def method_missing(method, *args, &block)
105
+
106
+ return @attrs[method.to_sym] if @attrs.has_key? method.to_sym
107
+ return @attrs[method.to_s] if @attrs.has_key? method.to_s
108
+
93
109
  @attrs.send(method, *args, &block)
94
110
  end
95
111
 
@@ -117,11 +133,14 @@ class Graph
117
133
  # @param n [Node]
118
134
  def push(n)
119
135
  if (!n.is_a?(Hash) && !n.is_a?(Node))
120
- raise TypeError.new "#{n.inspect} is not an Hash or a Node!"
136
+ raise TypeError.new "#{n.inspect} is not an Hash nor a Node!"
121
137
  end
122
138
 
139
+ n = Node.new(n) if (n.is_a?(Hash))
140
+
123
141
  super(n.clone.update(@defaults))
124
142
  end
143
+
125
144
  end
126
145
 
127
146
  # An array of Edge objects
@@ -146,11 +165,14 @@ class Graph
146
165
  # @param e [Edge]
147
166
  def push(e)
148
167
  if (!e.is_a?(Hash) && !e.is_a?(Edge))
149
- raise TypeError.new "#{e.inspect} is not an Hash or an Edge!"
168
+ raise TypeError.new "#{e.inspect} is not an Hash nor an Edge!"
150
169
  end
151
170
 
171
+ e = Edge.new(e) if (e.is_a?(Hash))
172
+
152
173
  super(e.clone.update(@defaults))
153
174
  end
175
+
154
176
  end
155
177
 
156
178
  attr_accessor :nodes, :edges, :attrs
@@ -313,7 +335,7 @@ class Graph
313
335
  # @see Graph#in_degree_of
314
336
  # @see Graph#out_degree_of
315
337
  def degree_of(n)
316
- label = Graph::get_label_from_node(n)
338
+ label = Graph::get_label(n)
317
339
 
318
340
  degree = 0
319
341
 
@@ -337,7 +359,7 @@ class Graph
337
359
  # @see Graph#degree_of
338
360
  # @see Graph#out_degree_of
339
361
  def in_degree_of(n)
340
- label = Graph::get_label_from_node(n)
362
+ label = Graph::get_label(n)
341
363
 
342
364
  degree = 0
343
365
 
@@ -358,7 +380,7 @@ class Graph
358
380
  # @see Graph#degree_of
359
381
  # @see Graph#out_degree_of
360
382
  def out_degree_of(n)
361
- label = Graph::get_label_from_node(n)
383
+ label = Graph::get_label(n)
362
384
 
363
385
  degree = 0
364
386
 
@@ -369,36 +391,68 @@ class Graph
369
391
  degree
370
392
  end
371
393
 
372
- # Return the “in degree” of the node n in the current graph, i.e. the number
373
- # of edges which are directed to this node. Note that the graph must be oriented.
374
- #
375
- # Edges must have the 'node1' and 'node2' attributes, which must contain
376
- # the 'label' attributes of nodes.
377
- #
378
- # @param n [Node,String] A node or a label of one
379
- # @see Graph#degree_of
380
- # @see Graph#out_degree_of
381
- def in_degree_of(n)
382
- label = Graph::get_label_from_node(n)
394
+ # return the first node which mach the given label in the current graph
395
+ # @param label [String] A node's label
396
+ def get_node(label)
383
397
 
384
- degree = 0
398
+ label = Graph::get_label(label)
399
+
400
+ self.nodes.find { |n| n.label == label }
401
+
402
+ end
403
+
404
+ # return an array of the neighbours of a node in the current graph.
405
+ # @param n [Node,String] A node with a 'label' or :label attribute, or a string
406
+ def get_neighbours(n)
407
+
408
+ label = Graph::get_label(n)
409
+ neighbours = NodeArray.new []
385
410
 
386
- # This is more efficient than in_degree_of(n)+out_degree_of(n)
387
- # since it goes only once through the edges array
388
411
  self.edges.each do |e|
389
- degree += 1 if (e['node2'] || e[:node2]).to_s == label
412
+
413
+ begin
414
+
415
+ l1 = e.node1
416
+ l2 = e.node2
417
+
418
+ rescue NoMethodError; next; end
419
+
420
+ if l2 && l1 == label
421
+
422
+ n2 = self.get_node l2
423
+
424
+ unless neighbours.include?(l2)
425
+
426
+ neighbours.push(n2)
427
+
428
+ end
429
+
430
+ end
431
+
432
+ if l1 && l2 == label && self.directed?
433
+
434
+ n1 = self.get_node l1
435
+
436
+ unless neighbours.include?(n1)
437
+
438
+ neighbours.push(n1)
439
+
440
+ end
441
+
442
+ end
443
+
390
444
  end
391
445
 
392
- degree
393
- end
446
+ neighbours
394
447
 
448
+ end
395
449
 
396
450
  # return the label of a node. Raise a TypeError exception if the argument
397
451
  # is not a Node nor a String object.
398
452
  # @param n [Node,String] A node with a 'label' or :label attribute, or a string
399
- def Graph::get_label_from_node(n)
453
+ def Graph::get_label(n)
400
454
  label = n.is_a?(Node) \
401
- ? (n['label'] || n[:label]).to_s \
455
+ ? n.label.to_s \
402
456
  : n.is_a?(String) ? n : nil
403
457
 
404
458
  raise TypeError.new("#{n.inspect} must be a Node or String object.") if label.nil?
@@ -0,0 +1,46 @@
1
+ #! /usr/bin/ruby1.9.1
2
+ # -*- coding: UTF-8 -*-
3
+
4
+ class Edge_test < Test::Unit::TestCase
5
+
6
+ def setup
7
+ @@empty = Graph::Node.new
8
+ @@alice = Graph::Node.new('label' => 'Alice')
9
+
10
+ # Alice ----> Bob
11
+ # ↑ ↑
12
+ # | |
13
+ # Oscar -------'
14
+ @@sample_graph = Graph.new(
15
+ [
16
+ { 'label' => 'Alice' },
17
+ { 'label' => 'Bob' },
18
+ { 'label' => 'Oscar' }
19
+ ],
20
+ [
21
+ { 'node1' => 'Alice', 'node2' => 'Bob' },
22
+ { :node1 => 'Oscar', 'node2' => 'Alice'},
23
+ { 'node1' => 'Oscar', :node2 => 'Bob'}
24
+ ]
25
+ )
26
+
27
+ end
28
+
29
+ def test_edge_node1_attr
30
+ assert_equal('Alice', @@sample_graph.edges[0].node1)
31
+ assert_equal('Oscar', @@sample_graph.edges[1].node1)
32
+ end
33
+
34
+ def test_edge_node2_attr
35
+ assert_equal('Alice', @@sample_graph.edges[1].node2)
36
+ assert_equal('Bob', @@sample_graph.edges[2].node2)
37
+ end
38
+
39
+ def test_edge_update
40
+
41
+ e = Graph::Edge.new
42
+
43
+ assert_equal(true, e.update({}).is_a?(Graph::Edge))
44
+ end
45
+
46
+ end
data/tests/graph_tests.rb CHANGED
@@ -551,4 +551,51 @@ class Graph_test < Test::Unit::TestCase
551
551
  end
552
552
  assert_equal(false, File.exists?(f))
553
553
  end
554
+
555
+ # == Graph#get_node == #
556
+
557
+ def test_graph_get_node_unexisting_label
558
+
559
+ n = @@sample_graph.get_node 'foobar'
560
+
561
+ assert_equal(nil, n)
562
+ end
563
+
564
+ def test_graph_get_node_existing_label
565
+
566
+ g = @@sample_graph;
567
+
568
+ n = g.get_node 'foo'
569
+
570
+ assert_equal(g.nodes[0], n)
571
+ end
572
+
573
+ # == Graph#get_neighbours == #
574
+
575
+ def test_graph_get_neighbours_unexisting_node_label
576
+
577
+ n = @@sample_graph.get_neighbours 'moo'
578
+
579
+ assert_equal([], n)
580
+
581
+ end
582
+
583
+ def test_graph_get_neighbours_unexisting_node_object
584
+
585
+ n = @@sample_graph.get_neighbours Graph::Node.new( :label => 'moo' )
586
+
587
+ assert_equal([], n)
588
+
589
+ end
590
+
591
+ def test_graph_get_neighbours
592
+
593
+ g = @@sample_graph
594
+
595
+ n = g.get_neighbours 'foo'
596
+
597
+ assert_equal([ 'bar', 'chuck' ], n.map { |m| m.label })
598
+
599
+ end
600
+
554
601
  end
data/tests/node_tests.rb CHANGED
@@ -81,4 +81,15 @@ class Node_test < Test::Unit::TestCase
81
81
  assert_equal(1, @@sample_graph.out_degree_of(@@alice))
82
82
  end
83
83
 
84
+ def test_node_label_attr
85
+ assert_equal('Alice', @@alice.label)
86
+ end
87
+
88
+ def test_node_update
89
+
90
+ n = Graph::Node.new
91
+
92
+ assert_equal(true, n.update({}).is_a?(Graph::Node))
93
+ end
94
+
84
95
  end
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.6
4
+ version: 0.1.7
5
5
  prerelease:
6
6
  platform: x86-linux
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-01-15 00:00:00.000000000 Z
12
+ date: 2013-02-12 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Provide functions to (un)parse GDF/JSON files and generate graphs
15
15
  email: batifon@yahoo.fr
@@ -20,10 +20,11 @@ files:
20
20
  - lib/graph.rb
21
21
  - lib/graphs/gdf.rb
22
22
  - lib/graphs/json.rb
23
- - tests/graph_tests.rb
24
23
  - tests/tests.rb
25
- - tests/node_tests.rb
24
+ - tests/graph_tests.rb
25
+ - tests/edge_tests.rb
26
26
  - tests/gdf_tests.rb
27
+ - tests/node_tests.rb
27
28
  - tests/gexf_tests.rb
28
29
  - tests/json_tests.rb
29
30
  homepage: https://github.com/bfontaine/Graphs.rb
@@ -47,14 +48,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
47
48
  version: '0'
48
49
  requirements: []
49
50
  rubyforge_project:
50
- rubygems_version: 1.8.23
51
+ rubygems_version: 1.8.24
51
52
  signing_key:
52
53
  specification_version: 3
53
54
  summary: Utilities to manipulate graph files
54
55
  test_files:
55
- - tests/graph_tests.rb
56
56
  - tests/tests.rb
57
- - tests/node_tests.rb
57
+ - tests/graph_tests.rb
58
+ - tests/edge_tests.rb
58
59
  - tests/gdf_tests.rb
60
+ - tests/node_tests.rb
59
61
  - tests/gexf_tests.rb
60
62
  - tests/json_tests.rb