Class: Chronicle::Schema::SchemaGraph

Inherits:
Object
  • Object
show all
Includes:
TSort
Defined in:
lib/chronicle/schema/schema_graph.rb

Overview

Represents a RDF graph as a DAG of types and their properties, built from a TTL string

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(default_namespace: 'https://schema.chronicle.app/') ⇒ SchemaGraph

Returns a new instance of SchemaGraph.



12
13
14
15
16
17
# File 'lib/chronicle/schema/schema_graph.rb', line 12

def initialize(default_namespace: 'https://schema.chronicle.app/')
  @default_namespace = default_namespace
  @types = []
  @properties = []
  @datatypes = []
end

Instance Attribute Details

#datatypesObject

Returns the value of attribute datatypes.



10
11
12
# File 'lib/chronicle/schema/schema_graph.rb', line 10

def datatypes
  @datatypes
end

#default_namespaceObject

Returns the value of attribute default_namespace.



10
11
12
# File 'lib/chronicle/schema/schema_graph.rb', line 10

def default_namespace
  @default_namespace
end

#propertiesObject

Returns the value of attribute properties.



10
11
12
# File 'lib/chronicle/schema/schema_graph.rb', line 10

def properties
  @properties
end

#typesObject

Returns the value of attribute types.



10
11
12
# File 'lib/chronicle/schema/schema_graph.rb', line 10

def types
  @types
end

#versionObject

Returns the value of attribute version.



10
11
12
# File 'lib/chronicle/schema/schema_graph.rb', line 10

def version
  @version
end

Class Method Details

.build_from_json(json) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/chronicle/schema/schema_graph.rb', line 19

def self.build_from_json(json)
  graph = new
  graph.version = json['version']
  json['types'].each do |type_data|
    id = graph.id_to_identifier(type_data['id'])
    graph.add_type(id).tap do |klass|
      klass.comment = type_data['comment']
      klass.subtype_ids = type_data['subtype_ids']
    end
  end
  json['properties'].each do |property_data|
    id = graph.id_to_identifier(property_data['id'])
    graph.add_property(id).tap do |property|
      property.comment = property_data['comment']
      property.domain = property_data['domain']
      property.range = property_data['range']
      property.many = property_data['many']
      property.required = property_data['required']
    end
  end
  graph.build_references!
  graph
end

Instance Method Details

#add_property(identifier) ⇒ Object



104
105
106
# File 'lib/chronicle/schema/schema_graph.rb', line 104

def add_property(identifier)
  find_property(identifier) || add_new_property(identifier)
end

#add_type(identifier) ⇒ Object



100
101
102
# File 'lib/chronicle/schema/schema_graph.rb', line 100

def add_type(identifier)
  find_type(identifier) || add_new_type(identifier)
end

#build_references!Object



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chronicle/schema/schema_graph.rb', line 63

def build_references!
  @types.each do |klass|
    klass.subtypes = klass.subtype_ids.map { |id| find_type_by_id(id) }
    klass.supertypes = @types.select { |c| c.subtype_ids.include?(klass.id) }
  end

  @properties.each do |property|
    property.domain.each do |type_id|
      klass = find_type_by_id(type_id)
      klass.properties << property if klass
    end

    # prune unknown range values from property
    property.range = property.range.select do |range|
      find_type_by_id(range)
    end

    property.range_types = property.range.map { |id| find_type_by_id(id) }
  end
end

#find_property(identifier) ⇒ Object



92
93
94
# File 'lib/chronicle/schema/schema_graph.rb', line 92

def find_property(identifier)
  @properties.find { |p| p.id == identifier_to_uri(identifier) }
end

#find_property_by_id(id) ⇒ Object



96
97
98
# File 'lib/chronicle/schema/schema_graph.rb', line 96

def find_property_by_id(id)
  @properties.find { |p| p.id == id }
end

#find_type(identifier) ⇒ Object



88
89
90
# File 'lib/chronicle/schema/schema_graph.rb', line 88

def find_type(identifier)
  @types.find { |c| c.id == identifier_to_uri(identifier) }
end

#find_type_by_id(id) ⇒ Object



84
85
86
# File 'lib/chronicle/schema/schema_graph.rb', line 84

def find_type_by_id(id)
  @types.find { |c| c.id == id }
end

#id_to_identifier(id) ⇒ Object



108
109
110
# File 'lib/chronicle/schema/schema_graph.rb', line 108

def id_to_identifier(id)
  id.gsub(@default_namespace, '')
end

#identifier_to_uri(identifier) ⇒ Object



112
113
114
# File 'lib/chronicle/schema/schema_graph.rb', line 112

def identifier_to_uri(identifier)
  "#{@default_namespace}#{identifier}"
end

#inspectObject



51
52
53
# File 'lib/chronicle/schema/schema_graph.rb', line 51

def inspect
  "#<SchemaGraph:#{object_id}>"
end

#pretty_print(pp) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/chronicle/schema/schema_graph.rb', line 43

def pretty_print(pp)
  pp.text('SchemaGraph')
  pp.nest(2) do
    pp.breakable
    pp.text("Num types: #{types.size}")
  end
end

#to_hObject



55
56
57
58
59
60
61
# File 'lib/chronicle/schema/schema_graph.rb', line 55

def to_h
  {
    version: @version,
    types: types.map(&:to_h),
    properties: properties.map(&:to_h)
  }
end