Class: MiniKraken::Rela::Conde
- Inherits:
-
GoalRelation
- Object
- Core::Specification
- Core::Relation
- GoalRelation
- MiniKraken::Rela::Conde
- 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
Instance Method Summary collapse
-
#cond(goals, ctx) ⇒ Object
Yields [Context, NilClass] result of the disjunction.
-
#initialize ⇒ Conde
constructor
A new instance of Conde.
-
#polyadic? ⇒ TrueClass
A relation is polyadic when it accepts an arbitrary number of arguments.
-
#solver_for(actuals, ctx) ⇒ Fiber<Context>
A Fiber that yields Outcomes objects.
Methods inherited from Core::Specification
#check_arity, #inspect, #variadic?
Constructor Details
#initialize ⇒ Conde
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
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.
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.
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 |