Class: GEXF::Graph

Inherits:
Object
  • Object
show all
Defined in:
lib/gexf/graph.rb

Constant Summary collapse

STRING =
:string
INTEGER =
:integer
IDTYPES =
[STRING, INTEGER]
STATIC =
:static
DYNAMIC =
:dynamic
MODES =
[STATIC, DYNAMIC]
EDGETYPES =
GEXF::Edge::TYPES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Graph

Returns a new instance of Graph.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/gexf/graph.rb', line 15

def initialize(opts={})
  edgetype   = opts[:defaultedgetype] || GEXF::Edge::UNDIRECTED
  idtype     = opts[:idtype]   || STRING
  mode       = opts[:mode]     || STATIC
  id_counter = Struct.new(:nodes, :edges, :attributes)

  raise ArgumentError.new "Invalid defaultedgetype: '#{edgetype}'" unless EDGETYPES.include?(edgetype)
  raise ArgumentError.new "Invalid idtype: '#{idtype}'"     unless IDTYPES.include?(idtype)
  raise ArgumentError.new "Invalid mode: '#{mode}'"         unless MODES.include?(mode)

  @defaultedgetype = edgetype
  @idtype          = idtype
  @mode            = mode
  @id_counter      = id_counter.new(0,0,0)
  @attributes      = {}

  @nodes           = GEXF::NodeSet.new
  @edges           = GEXF::EdgeSet.new
end

Instance Attribute Details

#defaultedgetypeObject (readonly)

Returns the value of attribute defaultedgetype.



13
14
15
# File 'lib/gexf/graph.rb', line 13

def defaultedgetype
  @defaultedgetype
end

#edgesObject (readonly)

Returns the value of attribute edges.



13
14
15
# File 'lib/gexf/graph.rb', line 13

def edges
  @edges
end

#idtypeObject (readonly)

Returns the value of attribute idtype.



13
14
15
# File 'lib/gexf/graph.rb', line 13

def idtype
  @idtype
end

#modeObject (readonly)

Returns the value of attribute mode.



13
14
15
# File 'lib/gexf/graph.rb', line 13

def mode
  @mode
end

#nodesObject (readonly)

Returns the value of attribute nodes.



13
14
15
# File 'lib/gexf/graph.rb', line 13

def nodes
  @nodes
end

Instance Method Details

#attribute_definitionsObject



62
63
64
# File 'lib/gexf/graph.rb', line 62

def attribute_definitions
  @attributes.dup
end

#create_edge(source, target, opts = {}) ⇒ Object



55
56
57
58
59
60
# File 'lib/gexf/graph.rb', line 55

def create_edge(source, target, opts={})
  opts[:type]       ||= defaultedgetype
  id                = assign_id(:edges, opts.delete(:id))
  @edges << edge    = GEXF::Edge.new(id, source.id, target.id, opts.merge(:graph => self))
  edge
end

#create_node(opts = {}) ⇒ Object



49
50
51
52
53
# File 'lib/gexf/graph.rb', line 49

def create_node(opts={})
  id                = assign_id(:nodes, opts.delete(:id))
  @nodes << node    = GEXF::Node.new(id, self, opts)
  node
end

#define_edge_attribute(title, opts = {}) ⇒ Object



44
45
46
47
# File 'lib/gexf/graph.rb', line 44

def define_edge_attribute(title, opts={})
  id                = assign_id(:attributes, opts.delete(:id)).to_s
  @edges.define_attribute(id, title, opts)
end

#define_node_attribute(title, opts = {}) ⇒ Object



39
40
41
42
# File 'lib/gexf/graph.rb', line 39

def define_node_attribute(title, opts={})
  id                = assign_id(:attributes, opts.delete(:id)).to_s
  @nodes.define_attribute(id, title, opts)
end

#defined_attributesObject



35
36
37
# File 'lib/gexf/graph.rb', line 35

def defined_attributes
  nodes.defined_attributes.merge(edges.defined_attributes)
end

#to_xmlObject



66
67
68
69
# File 'lib/gexf/graph.rb', line 66

def to_xml
  serializer = GEXF::XmlSerializer.new(self)
  serializer.serialize!
end