Class: Tes::Request::Profile

Inherits:
Object
  • Object
show all
Includes:
Comparable
Defined in:
lib/tes/request/profile.rb

Constant Summary collapse

REG_LOCK_HEADER =
/^(@|\$)\s*\|+\s*/
REG_POINT_ASK =
/^\*(\d+):/
REG_REFER_ASK =
/^&(\d+)\./
REG_POINT_ASK_GREEDY =
/^\[\*(\d+)\]:/
REG_REFER_ASK_GREEDY =
/^\[&(\d+)\]\./

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(profile_lines = []) ⇒ Profile

深度解析环境要求

Parameters:

  • profile_lines (Array<String>) (defaults to: [])

    测试环境要求语句列表



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
# File 'lib/tes/request/profile.rb', line 16

def initialize(profile_lines=[])
  @data = []

  # 不因申明顺序不一致性等其他差异而误判环境要求的一致性
  point_asks = {}
  profile_lines.each do |raw_line|
    lock_type = (raw_line =~ REG_LOCK_HEADER and raw_line =~ /^\$/) ? :share : :lock
    line = raw_line.sub(REG_LOCK_HEADER, '')

    case line
      when REG_POINT_ASK
        mt = line.match(REG_POINT_ASK)
        ask = Ask.new line.sub(REG_POINT_ASK, '')
        ask.lock_type = lock_type
        point_asks[mt[1]] = ask
      when REG_POINT_ASK_GREEDY
        mt = line.match(REG_POINT_ASK_GREEDY)
        ask = Ask.new line.sub(REG_POINT_ASK_GREEDY, '')
        ask.greedy= true
        ask.lock_type = lock_type
        point_asks[mt[1]] = ask
      when REG_REFER_ASK
        mt= line.match(REG_REFER_ASK)
        src_ask = point_asks[mt[1]]
        ref_exp = Expression.new line.sub(REG_REFER_ASK, '')
        src_ask.reference << ref_exp
      when REG_REFER_ASK_GREEDY
        mt= line.match(REG_REFER_ASK_GREEDY)
        src_ask = point_asks[mt[1]]
        ref_exp = Expression.new line.sub(REG_REFER_ASK_GREEDY, '')
        src_ask.reference << ref_exp
      else
        ask = Ask.new(line)
        ask.lock_type =lock_type
        @data << ask
    end
  end

  @data += point_asks.values
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



57
58
59
# File 'lib/tes/request/profile.rb', line 57

def data
  @data
end

Instance Method Details

#+(other) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
# File 'lib/tes/request/profile.rb', line 86

def +(other)
  ret = self.<=>(other)
  case ret
    when 0, 1
      self
    when -1
      other
    else
      merge(other)
  end
end

#<=>(other) ⇒ Object



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
# File 'lib/tes/request/profile.rb', line 59

def <=>(other)
  all_self_hash = self.data.group_by {|e| e.to_s}
  all_self_hash_keys = Set.new all_self_hash.keys
  all_other_hash = other.data.group_by {|e| e.to_s}
  all_other_hash_keys = Set.new all_other_hash.keys

  # 如果相等或者可比较则直接返回(只在相等的时候有效)
  return 0 if all_self_hash == all_other_hash

  hash1 = Hash[all_self_hash_keys.to_a.map {|e| [e, true]}]
  all_other_hash_keys.to_a.each do |k|
    hash1.include?(k)
  end

  if all_self_hash_keys == all_other_hash_keys
    compare_when_keys_same(all_self_hash, all_other_hash)
  elsif all_self_hash_keys < all_other_hash_keys
    compare_when_keys_subset(all_self_hash, all_other_hash)
  elsif all_self_hash_keys > all_other_hash_keys
    ret = compare_when_keys_subset(all_other_hash, all_self_hash)
    ret ? 0 - ret : ret
  else
    nil
  end
end

#merge_able?(other) ⇒ Boolean

Returns:

  • (Boolean)


98
99
100
101
102
103
# File 'lib/tes/request/profile.rb', line 98

def merge_able?(other)
  self.+(other)
  true
rescue RuntimeError
  false
end

#request(pool) ⇒ Object

向资源池环境中申请资源,但并不进行锁定,只是有礼貌的进行问询式申请

真需要使用资源,需要在其返回列表后向服务器申请锁定这些资源列表

Parameters:

  • pool (Hash<String,Hash>)

    所有空闲可用的资源池



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
# File 'lib/tes/request/profile.rb', line 108

def request(pool)
  get_candidates_lab = ->(ask, answer_cache) do
    pool.keys.select {|k| !answer_cache.include?(k) && ask.match?(pool[k])}
  end

  answers_flat = []
  answers = @data.map do |ask|
    candidates = get_candidates_lab.call(ask, answers_flat)
    unless candidates.size > 0
      nil
    else
      if ask.greedy
        answers_flat += candidates
        unless ask.reference.size > 0
          candidates
        else
          candidates.map do |candidate|
            refs = ask.reference.inject([]) do |t, r|
              ret = pool[candidate].get_by_chain(r.data[:left_exp])
              ret.is_a?(Array) ? (t + ret) : (t << ret)
            end
            answers_flat += refs
            [candidate, refs]
          end
        end
      else
        candidate = candidates.first
        answers_flat << candidate
        unless ask.reference.size > 0
          candidate
        else
          refs = ask.reference.inject([]) do |t, r|
            ret = pool[candidate].get_by_chain(r.data[:left_exp])
            ret.is_a?(Array) ? (t + ret) : (t << ret)
          end
          answers_flat += refs
          [candidate, refs]
        end
      end
    end
  end

  answers
end

#to_s(split = "\n") ⇒ Object



153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/tes/request/profile.rb', line 153

def to_s(split="\n")
  ret = []
  point = 0
  @data.each do |ask|
    header = (ask.lock_type == :share ? '$|' : '@|')
    if ask.reference and ask.reference.size > 0
      point_ask = ask
      refer_asks = ask.reference
      point += 1
      ret << header + ("*#{point}:" + point_ask.to_s)
      refer_asks.each do |r_ask|
        ret << ("&#{point}." + r_ask.to_s)
      end
    else
      ret << header + ask.to_s
    end
  end
  ret.join(split)
end