Class ConstraintSolver::Domain
In: lib/Domain.rb
Parent: Object
Test::Unit::TestCase AllDifferentConstraintTest SolutionTest ConstraintSolverTest ConstraintListTest VariableTest DomainTest BinaryConstraintTest ProblemTest BinaryRelationTest Exception DomainWipeoutException UndoStackEmptyException AbstractConstraint BinaryConstraint AllDifferentConstraint Array ConstraintList BinaryRelation Variable Solution ConstraintSolver Problem Domain test/DomainTest.rb test/SolutionTest.rb lib/BinaryConstraint.rb lib/Variable.rb test/ConstraintListTest.rb lib/ConstraintList.rb test/ProblemTest.rb lib/Solution.rb test/BinaryConstraintTest.rb lib/ConstraintSolver.rb test/VariableTest.rb test/AllDifferentConstraintTest.rb lib/AllDifferentConstraint.rb lib/Problem.rb test/ConstraintSolverTest.rb lib/Domain.rb lib/AbstractConstraint.rb ConstraintSolver dot/m_19_0.png

This class represents the domain of a variable, i.e. a set of values that can be assigned to the variable.

Methods

<<   ==   collect   delete   each   empty?   include?   new   prune   sort   to_s   undoPruning  

Attributes

undoStack  [R] 
values  [R] 

Public Class methods

Initialises a new domain. Optionally, a set of initial values can be given.

[Source]

# File lib/Domain.rb, line 12
        def initialize(values=nil)
            if not (values.nil? or values.kind_of?(Set))
                raise ArgumentError, "Values must be a set!"
            end
            @values = values.nil? ? Set.new : values
            @undoStack = Array.new
        end

Public Instance methods

Adds value to the domain.

[Source]

# File lib/Domain.rb, line 21
        def <<(value)
            @values << value
        end

[Source]

# File lib/Domain.rb, line 81
        def ==(domain)
            return false unless domain.kind_of?(Domain)
            (@values == domain.values) and (@undoStack == domain.undoStack)
        end

[Source]

# File lib/Domain.rb, line 40
        def collect(&block)
            values.collect(&block)
        end

Deletes value from the domain.

[Source]

# File lib/Domain.rb, line 26
        def delete(value)
            @values.delete(value)
        end

[Source]

# File lib/Domain.rb, line 30
        def each
            @values.each { |value|
                yield value
            }
        end

[Source]

# File lib/Domain.rb, line 44
        def empty?
            @values.empty?
        end

[Source]

# File lib/Domain.rb, line 73
        def include?(value)
            @values.include?(value)
        end

Prunes the values from the domain.

[Source]

# File lib/Domain.rb, line 49
        def prune(values)
            if not values.kind_of?(Set)
                values = Set.new([ values ])
            end
            if values.empty?
                return
            end
            @undoStack.push(@values)
            @values -= values
            if @values.empty?
                self.undoPruning
                raise DomainWipeoutException
            end
        end

[Source]

# File lib/Domain.rb, line 36
        def sort(&block)
            @values.sort(&block)
        end

[Source]

# File lib/Domain.rb, line 77
        def to_s
            "{" + @values.entries.join(", ") + "}"
        end

Undoes pruning by replacing the current list of values with the one before the last time prune was called.

[Source]

# File lib/Domain.rb, line 66
        def undoPruning
            if @undoStack.empty?
                raise UndoStackEmptyException, "No more prunes to undo!"
            end
            @values = @undoStack.pop
        end

[Validate]