Module: Sinatra::RbbtRESTKnowledgeBase

Defined in:
lib/rbbt/rest/knowledge_base.rb

Class Method Summary collapse

Class Method Details

.registered(base) ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/rbbt/rest/knowledge_base.rb', line 10

def self.registered(base)
  base.module_eval do
    helpers KnowledgeBaseRESTHelpers

    #{{{ Children

    get '/knowledge_base/:name/:database/entity_children/:entity' do
      name = consume_parameter :name
      database = consume_parameter :database
      entity = consume_parameter :entity

      kb = get_knowledge_base name
      found = kb.identify database, entity
      raise ParameterException, "Entity #{entity} was not found" unless found

      list = kb.children(database, found).target_entity

      case @format
      when :json
        content_type "application/json"
        halt 200, prepare_entities_for_json(list).to_json
      when :html
      end
    end

    # List children
    post '/knowledge_base/:name/:database/entity_list_children/' do
      name = consume_parameter :name
      database = consume_parameter :database
      entities = consume_parameter :entities

      raise ParameterException, "No 'entities' provided" if entities.nil?

      entities = entities.split("|")

      kb = get_knowledge_base name

      children = {}
      entities.each do |entity|
        found = kb.identify database, entity
        next if found.nil?
        children[entity] = kb.children(database, found).target_entity
      end
      case @format
      when :json
        content_type "application/json"
        halt 200, prepare_entities_for_json(children).to_json
      when :html
      end
    end

    # Collection children
    post '/knowledge_base/:name/:database/entity_collection_children' do
      name = consume_parameter :name
      database = consume_parameter :database
      entities = consume_parameter :entities

      raise ParameterException, "No 'entities' provided" if entities.nil?

      entities = JSON.parse(entities)

      kb = get_knowledge_base name

      entities.each do |type,list|
        list.each do |entity|
          found = kb.identify database, entity
          next if found.nil?
          targets = kb.children(database, found).target_entity
          next if targets.nil? or targets.empty?
          target_type = kb.target database
          children[target_type] ||= []
          children[target_type].concat targets
        end
      end

      case @format
      when :json
        content_type "application/json"
        halt 200, prepare_entities_for_json(children).to_json
      when :html
      end
    end

    #{{{ Neighbours

    get '/knowledge_base/:name/:database/entity_neighbours/:entity' do
      name = consume_parameter :name
      database = consume_parameter :database
      entity = consume_parameter :entity

      kb = get_knowledge_base name
      found = kb.identify database, entity
      raise ParameterException, "Entity #{entity} was not found" unless found

      list = kb.neighbours(database, found).values.select{|list| list and list.any?}.first
      list = list.target_entity if list.respond_to? :target_entity
      list ||= []

      case @format
      when :json
        content_type "application/json"
        halt 200, prepare_entities_for_json(list).to_json
      when :html
      end
    end

    post '/knowledge_base/:name/:database/entity_list_neighbours/' do
      name = consume_parameter :name
      database = consume_parameter :database
      entities = consume_parameter :entities

      raise ParameterException, "No 'entities' provided" if entities.nil?

      entities = entities.split("|")

      kb = get_knowledge_base name

      children = {}
      entities.each do |entity|
        found = kb.identify database, entity
        next if found.nil?
        matches = kb.neighbours(database, found).values.select{|list| list and list.any?}.first
        next if matches.nil? or matches.empty?
        children[entity] = matches.target_entity
      end
      case @format
      when :json
        content_type "application/json"
        halt 200, prepare_entities_for_json(children).to_json
      when :html
      end
    end

    post '/knowledge_base/:name/:database/entity_collection_neighbours' do
      name = consume_parameter :name
      database = consume_parameter :database
      entities = consume_parameter :entities

      raise ParameterException, "No 'entities' provided" if entities.nil?

      entities = JSON.parse(entities)

      kb = get_knowledge_base name

      neighbours = {}
      entities.each do |type,list|
        list.each do |entity|

          found = kb.identify_source database, entity
          if found.nil?
            reverse = true
            found = kb.identify_target database, entity
          else
            reverse = false
          end
          next if found.nil?

          matches = kb.neighbours(database, found)[reverse ? :parents : :children]
          next if matches.nil? or matches.empty?
          targets = matches.target

          entity_type = reverse ? kb.source_type(database) : kb.target_type(database)
          neighbours[entity_type] ||= []
          neighbours[entity_type].concat targets
        end
      end

      neighbours.each{|type, list| list.uniq!}

      case @format
      when :json
        content_type "application/json"
        halt 200, prepare_entities_for_json(neighbours).to_json
      when :html
      end
    end
  end
end