Class: MiniKraken::Rela::Conde

Inherits:
GoalRelation show all
Includes:
Singleton
Defined in:
lib/mini_kraken/rela/conde.rb

Overview

A polyadic relation (i.e. it can takes an arbitrary number of argumentt) that behaves as the disjunction of its arguments. It succeeds if at least one of its goal arguments succeeds.

Instance Attribute Summary

Attributes inherited from Core::Specification

#arity, #name

Instance Method Summary collapse

Methods inherited from Core::Specification

#check_arity, #inspect, #variadic?

Constructor Details

#initializeConde

Returns a new instance of Conde.



20
21
22
# File 'lib/mini_kraken/rela/conde.rb', line 20

def initialize
  super('conde', Core::Arity.new(1, '*'))
end

Instance Method Details

#cond(goals, ctx) ⇒ Object

Yields [Context, NilClass] result of the disjunction

Parameters:

  • goals (Array<Goal>)

    Array of goals

  • ctx (Context)

    A ctxabulary object



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mini_kraken/rela/conde.rb', line 41

def cond(goals, ctx)
  # require 'debug'
  success = false
  ctx.place_bt_point

  goals.each do |g|
    fiber = nil

    case g
      when Core::Goal
        fiber = g.achieve(ctx)
      when Array
        conjunct = conjunction(g)
        fiber = conjunct.achieve(ctx)
      # when Core::ConsCell
        # goal_array = to_goal_array(g)
        # conjunct = conjunction(goal_array)
        # fiber = conjunct.achieve(ctx)
      else
        raise NotImplementedError
    end

    loop do
      outcome = fiber.resume(ctx)
      break if outcome.nil?

      if outcome.success?
        success = true
        Fiber.yield outcome
      end
    end

    ctx.next_alternative
  end

  Fiber.yield ctx.failed! unless success
  ctx.retract_bt_point
  Fiber.yield nil
end

#polyadic?TrueClass

A relation is polyadic when it accepts an arbitrary number of arguments.

Returns:

  • (TrueClass)


26
27
28
# File 'lib/mini_kraken/rela/conde.rb', line 26

def polyadic?
  true
end

#solver_for(actuals, ctx) ⇒ Fiber<Context>

Returns A Fiber that yields Outcomes objects.

Parameters:

  • actuals (Array<Term>)

    A two-elements array

  • ctx (Core::Context)

    A vocabulary object

Returns:

  • (Fiber<Context>)

    A Fiber that yields Outcomes objects



33
34
35
36
# File 'lib/mini_kraken/rela/conde.rb', line 33

def solver_for(actuals, ctx)
  args = *validated_args(actuals)
  Fiber.new { cond(args, ctx) }
end