Class: OntologyGraph::Graph

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/ontology_graph/graph.rb

Overview

Class for handling the graph of ontology classes. This allows quick access to each RDFS class know to the system.

If ActiveRDF is available, the graph can initialize itself from the RDF store and allows access to all the subclass and superclass relations in the system.

Instance Method Summary collapse

Constructor Details

#initializeGraph

Create a new class graph



14
15
16
# File 'lib/ontology_graph/graph.rb', line 14

def initialize
  @class_hash = {}
end

Instance Method Details

#add_relation(superclass, subclass) ⇒ Object

Add a relation between a subclass or a superclass



19
20
21
22
23
24
# File 'lib/ontology_graph/graph.rb', line 19

def add_relation(superclass, subclass)
  subclass = get_or_create_node(subclass)
  superclass = get_or_create_node(superclass)
  return if(subclass.uri == superclass.uri) # don't accept self-relations
  superclass.add_subclass(subclass)
end

#build_from_ardfObject

Build the whole graph from the RDF store. This will include all Resources that are either marked as an rdfs:class orare used as an rdf:type and all the subclass relations between them.



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/ontology_graph/graph.rb', line 49

def build_from_ardf
  return unless(N::URI.active_rdf?)
  
  # First select all classes and add them as nodes
  class_qry = ActiveRDF::Query.new(N::URI).select(:class).where(:class, N::RDF.type, N::RDFS.Class)
  types = class_qry.execute
  types.each { |t| @class_hash[t.to_s] = ClassNode.new(t.to_s) }
  
  # Now, look for all subclass relationships and add them
  subtype_qry = ActiveRDF::Query.new(N::URI).distinct.select(:class, :subclass)
  subtype_qry.where(:subclass, RDFS.subClassOf, :class)
  subtype_list = subtype_qry.execute
  subtype_list.each  { |cl, subcl| add_relation(cl, subcl) }
  
  # Flag all classes that are actually used
  used_types_qry = ActiveRDF::Query.new(N::URI).distinct.select(:type)
  used_types_qry.where(:element, RDF.type, :type)
  used_types = used_types_qry.execute
  used_types.each { |t| get_or_create_node(t).flag_used }
  
  weed_inference
  
end

#each(&block) ⇒ Object Also known as: each_node

Iterate through each node



41
42
43
# File 'lib/ontology_graph/graph.rb', line 41

def each(&block)
  @class_hash.each_value(&block)
end

#get_node(name) ⇒ Object



36
37
38
# File 'lib/ontology_graph/graph.rb', line 36

def get_node(name)
  @class_hash[name.to_s]
end

#get_or_create_node(name) ⇒ Object

Retrieve the graph node for the given name/URL. If the node does not already exist, it will be created.



28
29
30
31
32
33
34
# File 'lib/ontology_graph/graph.rb', line 28

def get_or_create_node(name)
  node = get_node(name)
  return node if(node)
  node = ClassNode.new(name)
  @class_hash[name] = node
  node
end

#reset_flagsObject

Reset the flags for tree walking on all nodes of the tree.



74
75
76
# File 'lib/ontology_graph/graph.rb', line 74

def reset_flags
  @class_hash.each_value { |n| n.flags = nil }
end

#tree_from(node_name, &block) ⇒ Object

Create a tree of subclasses, starting from the given node. This will return a ClassNode which is the root of a tree of all classes that are reachable from the current node as subclasses.



81
82
83
84
# File 'lib/ontology_graph/graph.rb', line 81

def tree_from(node_name, &block)
  reset_flags
  make_tree_from(node_name, &block)
end

#weed_inferenceObject

Weed out “inferred” subclass relationships from the graph, if they exist.

Raises:

  • (RuntimeError)


88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/ontology_graph/graph.rb', line 88

def weed_inference
  root_elements = []
  @class_hash.each_value do |node|
    if(node.superclasses.empty?)
      root_elements << node
    end
  end
  
  raise(RuntimeError, "No root elements in the graph, ontology graph cannot by cyclic") if(root_elements.empty?)

  root_elements.each { |root| root.weed_superclasses }
end