Class: Stamina::InputString

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/stamina-core/stamina/input_string.rb

Overview

An input string is a sequence of input symbols (symbols being letters appearing on automaton edges) labeled as positive, negative or unlabeled (provided for test samples and query strings).

This class include the Enumerable module, that allows reasoning about ordered symbols.

Detailed API

Instance Method Summary collapse

Constructor Details

#initialize(symbols, positive, dup = true) ⇒ InputString

Creates an input string from symbols and positive or negative labeling.

Arguments:

  • symbols: When an array is provided, it is duplicated by default to be kept internally. Set dup to false to avoid duplicating it (in both cases, the internal array will be freezed). When a String is provided, symbols array is created using symbols.split(' ') and then freezed. dup is ignored in the case.

  • The positive argument may be true (positive string), false (negative one) or nil (unlabeled).

Raises:

  • ArgumentError if symbols is not an Array nor a String.

Raises:

  • (ArgumentError)


29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/stamina-core/stamina/input_string.rb', line 29

def initialize(symbols, positive, dup=true)
  raise(ArgumentError,
        "Input string expects an Array or a String: #{symbols} received",
        caller) unless Array===symbols or String===symbols
  @symbols = case symbols
               when String
                 symbols.split(' ').freeze
               when Array
                 (dup ? symbols.dup : symbols).freeze
             end
  @positive = positive
end

Instance Method Details

#==(o) ⇒ Object Also known as: eql?

Checks equality with another InputString. Returns true if strings have same sequence of symbols and same labeling, false otherwise. Returns nil if o is not an InputString.



98
99
100
101
# File 'lib/stamina-core/stamina/input_string.rb', line 98

def ==(o)
  return nil unless InputString===o
  label == o.label and @symbols == o.symbols
end

#eachObject

Yields the block with each string symbol, in order. Has no effect without block.



91
# File 'lib/stamina-core/stamina/input_string.rb', line 91

def each() @symbols.each {|s| yield s if block_given? } end

#empty?Boolean Also known as: lambda?

Checks if this input string is empty (aka lambda, i.e. contains no symbol).

Returns:

  • (Boolean)


45
# File 'lib/stamina-core/stamina/input_string.rb', line 45

def empty?() @symbols.empty? end

#hashObject

Computes a hash code for this string.



107
108
109
# File 'lib/stamina-core/stamina/input_string.rb', line 107

def hash
  @symbols.hash + 37*positive?.hash
end

#labelObject

Returns the exact label of this string, being true (positive string) false (negative string) or nil (unlabeled)



57
# File 'lib/stamina-core/stamina/input_string.rb', line 57

def label() @positive end

#negateObject

Copies and returns the same string, but switch the positive flag. This method returns self if it is unlabeled.



76
77
78
79
# File 'lib/stamina-core/stamina/input_string.rb', line 76

def negate
  return self if unlabeled?
  InputString.new(@symbols, !@positive, false)
end

#negative?Boolean

Returns true if this input string is negatively labeled, false otherwise.

Returns:

  • (Boolean)


67
# File 'lib/stamina-core/stamina/input_string.rb', line 67

def negative?() @positive==false end

#positive?Boolean

Returns true if this input string is positively labeled, false otherwise.

Returns:

  • (Boolean)


62
# File 'lib/stamina-core/stamina/input_string.rb', line 62

def positive?() @positive==true end

#sizeObject

Returns the string size, i.e. number of its symbols.



51
# File 'lib/stamina-core/stamina/input_string.rb', line 51

def size() @symbols.size end

#symbolsObject

Returns an array with symbols of this string. Returned array may not be modified (it is freezed).



85
# File 'lib/stamina-core/stamina/input_string.rb', line 85

def symbols() @symbols end

#to_adlObject Also known as: to_s, inspect

Prints this string in ADL.



114
115
116
117
118
# File 'lib/stamina-core/stamina/input_string.rb', line 114

def to_adl
  str = (unlabeled? ? '?' : (positive? ? '+ ' : '- '))
  str << @symbols.join(' ')
  str
end

#unlabeled?Boolean

Returns true if this input string unlabeled.

Returns:

  • (Boolean)


72
# File 'lib/stamina-core/stamina/input_string.rb', line 72

def unlabeled?() @positive.nil? end