Class: MiniKraken::Core::Specification

Inherits:
Object
  • Object
show all
Defined in:
lib/mini_kraken/core/specification.rb

Direct Known Subclasses

Relation

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(aName, anArity) ⇒ Specification

Returns a new instance of Specification.

Parameters:

  • aName (String)

    Name of the relation.

  • anArity (Arity, Integer)

    Arity of the relation.



16
17
18
19
# File 'lib/mini_kraken/core/specification.rb', line 16

def initialize(aName, anArity)
  @name = aName
  @arity = anArity.is_a?(Integer) ? Arity.new(anArity, anArity) : anArity
end

Instance Attribute Details

#arityArity (readonly)

Returns arity = allowed number of arguments.

Returns:

  • (Arity)

    arity = allowed number of arguments



12
13
14
# File 'lib/mini_kraken/core/specification.rb', line 12

def arity
  @arity
end

#nameString (readonly)

Returns Name of the specification object.

Returns:

  • (String)

    Name of the specification object.



9
10
11
# File 'lib/mini_kraken/core/specification.rb', line 9

def name
  @name
end

Instance Method Details

#check_arity(actuals) ⇒ Array<Term>

Control that the number of actual arguments matches the relation’s arity. Raise an exception if the check fails

Parameters:

  • actuals (Array<Term>)

    Actuals from a goal.

Returns:

  • (Array<Term>)

    Input array if compatible with arity.



37
38
39
40
41
42
43
44
45
# File 'lib/mini_kraken/core/specification.rb', line 37

def check_arity(actuals)
  unless arity.match?(actuals.size)
    msg1 = "Count of arguments (#{actuals.size})"
    msg2 = " is out of allowed range (#{arity.low}, #{arity.high})."
    raise StandardError, msg1 + msg2
  end

  actuals
end

#inspectObject



28
29
30
# File 'lib/mini_kraken/core/specification.rb', line 28

def inspect
  name + "[#{arity.low}]"
end

#variadic?Boolean

A relation is variadic when it accepts an arbitrary number of arguments. Most built-in relation takes a fixed number of arguments (= arity).

Returns:

  • (Boolean)


24
25
26
# File 'lib/mini_kraken/core/specification.rb', line 24

def variadic?
  arity.variadic?
end