Sha256: 40ab475446613b5368831b8e51ebb22b280c5228fa4138789ee7a73b3ef62633

Contents?: true

Size: 1.8 KB

Versions: 3

Compression:

Stored size: 1.8 KB

Contents

# frozen_string_literal: true

require_relative 'term'
require_relative 'specification'

module MiniKraken
  module Core
    # A specialization of Term class for objects that take arguments.
    class ParametrizedTerm < Term
      # @return [Specification] The specification that must be invoked with arguments.
      attr_reader :specification

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

      # Constructor.
      # @param theSpecification [Specification] The callable object.
      # @param theArgs [Array<Term>] The actual aguments
      def initialize(theSpecification, theArgs)
        super()
        @specification = validated_specification(theSpecification)
        args = specification.check_arity(theArgs)
        @actuals = validated_actuals(args)
      end

      def initialize_copy(orig)
        @specification = orig.specification
        @actuals = []
      end

      # Make a copy of self with all the variable reference being
      # replaced by the corresponding value in the Hash.
      # @param substitutions [Hash {String => Term}]
      # @return [Term]
      def dup_cond(substitutions)
        duplicate = dup
        updated_actuals = actuals.map { |e| e.dup_cond(substitutions) }
        duplicate.actuals.concat(updated_actuals)

        duplicate
      end

      protected

      def validated_specification(theSpecification)
        unless theSpecification.kind_of?(Specification)
          msg_part1 = 'Expected kind_of Specification,'
          msg_part2 = "instead of #{theSpecification.class}."
          raise StandardError, "#{msg_part1} #{msg_part2}"
        end

        theSpecification
      end

      # This method should be overridden in subclasses
      def validated_actuals(args)
        args
      end
    end # class
  end # module
end # module

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
mini_kraken-0.3.02 lib/mini_kraken/core/parametrized_term.rb
mini_kraken-0.3.01 lib/mini_kraken/core/parametrized_term.rb
mini_kraken-0.3.00 lib/mini_kraken/core/parametrized_term.rb