Class: Gamefic::Query::Base

Inherits:
Object
  • Object
show all
Includes:
Gamefic
Defined in:
lib/gamefic/query/base.rb

Direct Known Subclasses

Children, Expression, Family, Parent, Self, Siblings, Text

Constant Summary collapse

@@ignored_words =
['a', 'an', 'the', 'and', ',']
@@subquery_prepositions =
['in', 'on', 'of', 'inside', 'from']

Constants included from Gamefic

VERSION

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*arguments) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
17
18
# File 'lib/gamefic/query/base.rb', line 9

def initialize *arguments
  test_arguments arguments
  @optional = false
  if arguments.include?(:optional)
    @optional = true
    arguments.delete :optional
  end
  @arguments = arguments
  @match_hash = Hash.new
end

Instance Attribute Details

#argumentsObject

Returns the value of attribute arguments.



8
9
10
# File 'lib/gamefic/query/base.rb', line 8

def arguments
  @arguments
end

Instance Method Details

#allow_ambiguous?Boolean

Check whether the query allows ambiguous matches. If allowed, this query’s

Returns:

  • (Boolean)


21
22
23
# File 'lib/gamefic/query/base.rb', line 21

def allow_ambiguous?
  false
end

#allow_many?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/gamefic/query/base.rb', line 24

def allow_many?
  false
end

#base_specificityObject



76
77
78
# File 'lib/gamefic/query/base.rb', line 76

def base_specificity
  0
end

#context_from(subject) ⇒ Object



33
34
35
# File 'lib/gamefic/query/base.rb', line 33

def context_from(subject)
  subject
end

#execute(subject, description) ⇒ Array

Returns:



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/gamefic/query/base.rb', line 52

def execute(subject, description)
  if (allow_many? or allow_ambiguous?) and !Query.allow_plurals?
    return Matches.new([], '', description)
  end
  if !allow_ambiguous?
    if allow_many? and !description.include?(',') and !description.downcase.include?(' and ')
      return Matches.new([], '', description)
    end
  end
  array = context_from(subject)
  matches = self.match(description, array)
  objects = matches.objects
  matches = Matches.new(objects, matches.matching_text, matches.remainder)
  if objects.length == 0 and matches.remainder == "it" and subject.respond_to?(:last_object)
    if !subject.last_object.nil?
      obj = subject.last_object
      if validate(subject, obj)
        matches = Matches.new([obj], "it", "")
      end
    end
  end
  @match_hash[subject] = matches
  matches
end

#last_match_for(subject) ⇒ Object



27
28
29
# File 'lib/gamefic/query/base.rb', line 27

def last_match_for(subject)
  @match_hash[subject]
end

#match(description, array) ⇒ Object



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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'lib/gamefic/query/base.rb', line 143

def match(description, array)
  keywords = get_keywords(description)
  array.each { |e|
    if e.uid == keywords[0]
      return Matches.new([e], keywords.shift, keywords.join(' '))
    end
  }
  used = []
  skipped = []
  possibilities = array
  at_least_one_match = false
  while keywords.length > 0
    next_word = keywords.shift
    if @@subquery_prepositions.include?(next_word)
      if !at_least_one_match
        return Matches.new([], '', description)
      end
      so_far = keywords.join(' ')
      in_matched = self.match(so_far, array)
      if in_matched.objects.length > 0 and (in_matched.objects.length == 1 or in_matched.objects[0].kind_of?(Array))
        # Subset matching should only consider the intersection of the
        # original array and the matched object's children. This ensures
        # that it won't erroneously match a child that was excluded from
        # the original query.
        parent = in_matched.objects.shift
        subset = self.match(used.join(' '), (array & (parent.kind_of?(Array) ? parent[0].children : parent.children)))
        if subset.objects.length == 1
          if in_matched.objects.length == 0
            return subset
          else
            return Matches.new([subset.objects] + in_matched.objects, subset.matching_text, subset.remainder)
          end
        end
      end
    end
    used.push next_word
    new_results = []
    possibilities.each { |p|
      words = Keywords.new(used.last)
      if words.length > 0
        matches = words.found_in(p.keywords, (allow_many? or allow_ambiguous?))
        if matches > 0
          new_results.push p
        end
      end
    }
    if new_results.length > 0
      at_least_one_match = true
      intersection = possibilities & new_results
      if intersection.length == 0
        skipped.push used.pop
      else
        skipped.clear
        possibilities = intersection
      end
    elsif (next_word.downcase == 'and' or next_word == ',')
      while keywords.first == ',' or keywords.first.downcase == 'and'
        used.push keywords.shift
      end
      if allow_ambiguous?
        # Ambiguous queries filter based on all keywords instead of
        # building an array of specified entities
        next
      end
      so_far = keywords.join(' ')
      recursed = self.match(so_far, array)
      if possibilities.length == 1 and !allow_ambiguous?
        possibilities = [possibilities]
      else
        # Force lists of things to be uniquely identifying
        return Matches.new([], '', description)
      end
      objects = recursed.objects.clone
      while objects.length > 0
        obj = objects.shift
        if obj.kind_of?(Array)
          possibilities.push obj
        else
          combined = [obj] + objects
          possibilities.push combined
          break
        end
      end
      used += recursed.matching_text.split_words
      skipped = recursed.remainder.split_words
      keywords = []
    else
      # The first unignored word must have at least one match
      if at_least_one_match and !@@ignored_words.include?(used.last)
        keywords.unshift used.pop
        return Matches.new(possibilities, used.join(' '), keywords.join(' '))
      else
        if !@@ignored_words.include?(used.last)
          return Matches.new([], '', description)
        end
      end
    end
  end
  if at_least_one_match and (used - @@ignored_words).length > 0
    r = Matches.new(possibilities, used.join(' '), skipped.join(' '))
    return r
  else
    return Matches.new([], '', description)
  end
end

#optional?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/gamefic/query/base.rb', line 30

def optional?
  @optional
end

#signatureObject



106
107
108
# File 'lib/gamefic/query/base.rb', line 106

def signature
  "#{self.class}(#{@arguments.join(',')})"
end

#specificityObject



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
# File 'lib/gamefic/query/base.rb', line 79

def specificity
  if @specificity == nil
    @specificity = base_specificity
    magnitude = 1
    @arguments.each { |item|
      if item.kind_of?(Entity)
        @specificity += (magnitude * 10)
        item = item.class
      end
      if item.kind_of?(Class)
        s = item
        while s != nil
          @specificity += magnitude
          s = s.superclass
        end
      else
        @specificity += magnitude
      end
    }
    if allow_many?
      # HACK Ridiculously high magic number to force queries that return
      # arrays to take precedence over everything
      @specificity = @specificity * 10
    end
  end
  @specificity
end

#test_arguments(arguments) ⇒ Object



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
# File 'lib/gamefic/query/base.rb', line 109

def test_arguments arguments
  my_classes = [Gamefic::Entity]
  my_objects = []
  arguments.each { |a|
    if a.kind_of?(Class) or a.kind_of?(Module)
      my_classes.push a
    elsif a.kind_of?(Gamefic::Entity)
      my_objects.push a
    elsif a.kind_of?(Symbol)
      if my_classes.length == 0 and my_objects.length == 0
        raise ArgumentError.new("Query signature requires at least one class, module, or object to accept a method symbol")
      end
      if my_classes.length > 0
        responds = false
        my_classes.each { |c|
          if c.instance_methods.include?(a)
            responds = true
            break
          end
        }
        if !responds
          raise ArgumentError.new("Query signature does not have a target that responds to #{a}")
        end
      end
      my_objects.each { |o|
        if !o.respond_to?(a)
          raise ArgumentError.new("Query signature contains object '#{o}' that does not respond to '#{a}'")
        end
      }
    else
      raise ArgumentError.new("Invalid argument '#{a}' in query signature")
    end
  }
end

#validate(subject, object) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/gamefic/query/base.rb', line 36

def validate(subject, object)
  arr = context_from(subject)
  @arguments.each { |arg|
    arr = arr.that_are(arg)
  }
  if (allow_many? or allow_ambiguous?)
    if object.kind_of?(Array)
      return (object & arr) == object
    end
    return false
  elsif !object.kind_of?(Array)
    return arr.include?(object)
  end
  return false
end