Class: MiniKraken::Rela::Fresh

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

Overview

A specialized relation that accepts a variable names(s) and a subgoal as its arguments.

Instance Attribute Summary

Attributes inherited from Core::Specification

#arity, #name

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Core::Specification

#check_arity, #inspect, #variadic?

Constructor Details

#initializeFresh

Default initialization



16
17
18
# File 'lib/mini_kraken/rela/fresh.rb', line 16

def initialize
  super('fresh', 2)
end

Class Method Details

.build_goal(names, subgoals) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mini_kraken/rela/fresh.rb', line 20

def self.build_goal(names, subgoals)
  var_names = nil

  case names
    when String
      var_names = Atomic::KString.new(names)

    when Array
      var_names = names.map do |nm|
        nm.is_a?(String) ? Atomic::KString.new(nm) : nm
      end
  end

  nested_goal = compose_goals(subgoals)
  Core::Goal.new(instance, [var_names, nested_goal])
end

.compose_goals(subgoals) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mini_kraken/rela/fresh.rb', line 37

def self.compose_goals(subgoals)
  nested_goal = nil

  case subgoals
    when Core::Goal
      nested_goal = subgoals

    when Array
      goal_array = subgoals
      loop do
        conjunctions = []
        goal_array.each_slice(2) do |uno_duo|
          if uno_duo.size == 2
            conjunctions << Core::Goal.new(Conj2.instance, uno_duo)
          else
            conjunctions << uno_duo[0]
          end
        end
        if conjunctions.size == 1
          nested_goal = conjunctions[0]
          break
        end
        goal_array = conjunctions
      end
    end

  nested_goal
end

Instance Method Details

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

First element is an array of variable names to create. Second is a sub-goal object

Parameters:

Returns:



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/mini_kraken/rela/fresh.rb', line 71

def solver_for(actuals, ctx)
  k_names = actuals.shift
  # require 'debug'
  subgoal = validated_args(actuals).first

  ctx.enter_scope(Core::Scope.new)
  if k_names.kind_of?(Atomic::KString)
    ctx.add_vars(k_names.value)
  else
    # ... Array of KString
    names = k_names.map(&:value)
    ctx.add_vars(names)
  end

  # Wrap the subgoal's solver by an adapter
  orig_solver = subgoal.achieve(ctx)
  Core::SolverAdapter.new(orig_solver) do |adp, context|
    # puts "Adaptee #{adp.adaptee}"
    result = adp.adaptee.resume(context)
    context.leave_scope if result.nil?
    result
  end
end