Class: SlenderT

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

Constant Summary collapse

FCSV =
CSV

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(contents = nil, opts = {}) ⇒ SlenderT

Returns a new instance of SlenderT.



20
21
22
23
# File 'lib/slender_t.rb', line 20

def initialize(contents=nil, opts={})
  @spo, @pos, @osp = {}, {}, {}
  self.load(contents, opts) if contents
end

Instance Attribute Details

#ospObject (readonly)

Returns the value of attribute osp.



18
19
20
# File 'lib/slender_t.rb', line 18

def osp
  @osp
end

#posObject (readonly)

Returns the value of attribute pos.



18
19
20
# File 'lib/slender_t.rb', line 18

def pos
  @pos
end

#spoObject (readonly)

Returns the value of attribute spo.



18
19
20
# File 'lib/slender_t.rb', line 18

def spo
  @spo
end

Class Method Details

.load(source, opts = {}) ⇒ Object



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

def load(source, opts={})
  SlenderT.new(source, opts)
end

Instance Method Details

#add(subject, predicate, object) ⇒ Object



42
43
44
45
46
47
# File 'lib/slender_t.rb', line 42

def add(subject, predicate, object)
  add_to_index(self.spo, subject, predicate, object)
  add_to_index(self.pos, predicate, object, subject)
  add_to_index(self.osp, object, subject, predicate)
  [subject, predicate, object]
end

#find(subject = nil, predicate = nil, object = nil) ⇒ Object Also known as: triples



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
# File 'lib/slender_t.rb', line 60

def find(subject=nil, predicate=nil, object=nil)
  begin
    if subject and predicate and object
      if self.spo[subject] and self.spo[subject][predicate] and self.spo[subject][predicate].include?(object)
        return [[subject, predicate, object]]
      else
        return []
      end
    elsif subject and predicate and object.nil?
      return self.spo[subject][predicate].map {|o| [subject, predicate, o]}
    elsif subject and predicate.nil? and object
      return self.osp[object][subject].map {|p| [subject, p, object]}
    elsif subject and predicate.nil? and object.nil?
      return self.spo[subject].inject([]) do |list, h|
        p, objects = h.first, h.last
        objects.each {|o| list << [subject, p, o]}
        list
      end
    elsif subject.nil? and predicate and object
      return self.pos[predicate][object].map {|s| [s, predicate, object]}
    elsif subject.nil? and predicate and object.nil?
      return self.pos[predicate].inject([]) do |list, h|
        o, subjects = h.first, h.last
        subjects.each {|s| list << [s, predicate, o]}
        list
      end
    elsif subject.nil? and predicate.nil? and object
      self.osp[object].inject([]) do |list, h|
        s, predicates = h.first, h.last
        predicates.each {|p| list << [s, p, object]}
        list
      end
    elsif subject.nil? and predicate.nil? and object.nil?
      list = []
      self.spo.each do |s, predicates|
        predicates.each do |p, objects|
          objects.each {|o| list << [s, p, o]}
        end
      end
      list
    end
  rescue
    []
  end
end

#inspectObject



107
108
109
# File 'lib/slender_t.rb', line 107

def inspect
  self.spo.keys.size > 20 ? "#{self.class}: #{self.spo.keys.size} unique subjects" : "#{self.class}: #{self.spo.keys.inspect}"
end

#load(contents, opts = {}) ⇒ Object

Expects CSV, a filename, or a URL with triples in it If there is a header in the file, use load(contents, :header => true) so that we can ignore the first line If there are special converters needed for FasterCSV to work, include them in the options as well.



30
31
32
33
34
35
36
# File 'lib/slender_t.rb', line 30

def load(contents, opts={})
  table = infer_csv_contents(contents, opts)
  return nil unless contents
  table.each do |row|
    self.add(*row)
  end
end

#query(*triples) ⇒ Object

Take a list of triples with variables in them, and resolve the constraints of the triples. Usage: query([ [‘?company’, ‘headquarters’, ‘New York’], [‘?company’, ‘industry’, ‘Investment Banking’], ])



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
# File 'lib/slender_t.rb', line 129

def query(*triples)
  bindings = nil
  triples.each do |triple|
    binding_position = {}
    query = []
    triple.each_with_index do |e, i|
      if query_variable?(e)
        binding_position[e] = i
        query << nil
      else
        query << e 
      end
    end
    rows = find(*query)
    if bindings.nil?
      bindings = rows.inject([]) do |list, row|
        binding = {}
        binding_position.each do |var, pos|
          binding[var] = row[pos]
        end
        list << binding
      end
    else
      new_binding = []
      bindings.each do |binding|
        rows.each do |row|
          valid_match = true
          temp_binding = binding.dup
          binding_position.each do |var, pos|
            if temp_binding.include?(var)
              valid_match = false if temp_binding[var] != row[pos]
            else
              temp_binding[var] = row[pos]
            end
          end
          new_binding << temp_binding if valid_match
        end
      end
      bindings = new_binding.dup
    end
    bindings
  end
  return bindings
end

#remove(subject, predicate, object) ⇒ Object



49
50
51
52
53
54
55
56
57
58
# File 'lib/slender_t.rb', line 49

def remove(subject, predicate, object)
  triples = find(subject, predicate, object)
  return true unless triples
  for s, p, o in triples do
    self.remove_from_index(self.spo, s, p, o)
    self.remove_from_index(self.pos, p, o, s)
    self.remove_from_index(self.osp, o, s, p)
  end
  [subject, predicate, object]
end

#save(filename) ⇒ Object



38
39
40
# File 'lib/slender_t.rb', line 38

def save(filename)
  File.open(filename, 'wb') {|f| f.write self.to_csv}
end

#to_csvObject

Just very basic for now



112
113
114
# File 'lib/slender_t.rb', line 112

def to_csv
  self.find.map {|row| row.join(',')}.join("\n")
end

#value(subject = nil, predicate = nil, object = nil) ⇒ Object



116
117
118
119
120
121
122
# File 'lib/slender_t.rb', line 116

def value(subject=nil, predicate=nil, object=nil)
  s, p, o = find(subject, predicate, object).first
  return s unless subject
  return p unless predicate
  return o unless object
  nil
end