Class: Lernen::Equiv::RandomWordOracle

Inherits:
Oracle
  • Object
show all
Defined in:
lib/lernen/equiv/random_word_oracle.rb

Overview

RandomWordOracle provides an implementation of equivalence query that finds a counterexample by using random word generation.

This takes three important parameters:

  • ‘max_words` (default: `100`): It is a limit of number of random words.

    If random words is generated and tried up to this value and no counterexample
    is found, it returns `nil` finally.
    
  • ‘min_word_size` (default: `1`): It is the minimal size of randomly generated word.

    It should be greater than `0`.
    
  • ‘max_word_size` (default: `30`): It is the maximal size of randomly generated word.

    It should be greater than or equal to `min_word_size`.
    

Instance Attribute Summary

Attributes inherited from Oracle

#sul

Instance Method Summary collapse

Methods inherited from Oracle

#+, #stats

Constructor Details

#initialize(alphabet, sul, max_words: 100, min_word_size: 1, max_word_size: 30, random: Random) ⇒ RandomWordOracle

: (

  Array[In] alphabet,
  System::SUL[In, Out] sul,
  ?max_words: Integer,
  ?min_word_size: Integer,
  ?max_word_size: Integer,
  ?random: Random,
) -> void


36
37
38
39
40
41
42
43
44
# File 'lib/lernen/equiv/random_word_oracle.rb', line 36

def initialize(alphabet, sul, max_words: 100, min_word_size: 1, max_word_size: 30, random: Random)
  super(sul)

  @alphabet = alphabet
  @max_words = max_words
  @min_word_size = min_word_size
  @max_word_size = max_word_size
  @random = random
end

Instance Method Details

#find_cex(hypothesis) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/lernen/equiv/random_word_oracle.rb', line 47

def find_cex(hypothesis)
  super

  @max_words.times do
    reset_internal(hypothesis)
    word = []

    word_size = @random.rand(@min_word_size..@max_word_size)
    word_size.times do
      word << @alphabet.sample(random: @random)

      hypothesis_output, sul_output = step_internal(hypothesis, word.last)

      if hypothesis_output != sul_output # steep:ignore
        @sul.shutdown
        return word
      end
    end
  end

  nil
end