Sha256: 9e5678a4516c54db46805fbd75a1cf745230237368107307fe4fc1826aa9aa4a

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

require_relative 'environment'

module MiniKraken
  module Core
    class Goal
      # @return [Relation] The relation corresponding to this goal
      attr_reader :relation

      # @return [Array<Term>] The actual aguments of the goal
      attr_reader :actuals

      # @param aRelation [Relation] The relation corresponding to this goal
      # @param args [Array<Term>] The actual aguments of the goal
      def initialize(aRelation, args)
        @relation = aRelation
        @actuals = validated_actuals(args)
      end

      # Attempt to achieve the goal for a given context (environment)
      # @param anEnv [Environment] The context in which the goal take place.
      # @return [Fiber<Outcome>] A Fiber object that will generate the results.
      def attain(anEnv)
        relation.solver_for(actuals, anEnv)
      end

      private

      def validated_actuals(args)
        if args.size != relation.arity
          err_msg = "Goal has #{args.size} arguments, expected #{relation.arity}"
          raise StandardError, err_msg
        end

        prefix = "Invalid goal argument "
        args.each do |actl|
          raise StandardError, prefix + actl.to_s unless actl.kind_of?(Term)
        end

        args.dup
      end
    end # class
  end # module
end # module

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mini_kraken-0.1.04 lib/mini_kraken/core/goal.rb
mini_kraken-0.1.03 lib/mini_kraken/core/goal.rb
mini_kraken-0.1.02 lib/mini_kraken/core/goal.rb