Class ConstraintSolver::Variable
In: lib/Variable.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 a variable in a constraint satisfaction problem.

Methods

==   assigned?   new   reset   to_s   value=   values  

Attributes

domain  [R] 
merit  [R] 
name  [R] 
value  [R] 

Public Class methods

Initialises a new variable. The name of the variable and at least one of domain or value are required. If domain is nil and a value is given, a new Domain is constructed with only the value specified in it. The fourth argument is the merit of the variable.

[Source]

# File lib/Variable.rb, line 12
        def initialize(name, domain=nil, value=nil, merit=0)
            if not name.kind_of?(String)
                raise ArgumentError, "The name of the variable has to be specified."
            end
            if domain.nil? and value.nil?
                raise ArgumentError, "At least one of domain or value are required."
            end
            @name = name
            @value = value
            if not domain.nil? and not domain.kind_of?(Domain)
                domain = Domain.new([ domain ].to_set);
            end
            if value.nil?
                @domain = domain.nil? ? Domain.new : domain
            else
                if domain.nil?
                    @domain = Domain.new([ value ].to_set)
                else
                    if domain.include?(value)
                        @domain = domain
                    else
                        raise ArgumentError, "Value " + value.to_s + " not in domain " + domain.to_s + "!"
                    end
                end
            end
            @merit = merit
        end

Public Instance methods

[Source]

# File lib/Variable.rb, line 62
        def ==(variable)
            return false unless variable.kind_of?(Variable)
            (@name == variable.name) and (@value == variable.value)
        end

[Source]

# File lib/Variable.rb, line 67
        def assigned?
            not @value.nil?
        end

[Source]

# File lib/Variable.rb, line 71
        def reset
            @value = nil
        end

[Source]

# File lib/Variable.rb, line 58
        def to_s
            @name.to_s + " = " + @value.to_s + " \\in " + @domain.to_s
        end

Assigns a new value to the variable. The new value has to be in the domain of the variable.

[Source]

# File lib/Variable.rb, line 42
        def value=(value)
            if value.nil?
                return
            end
            if @domain.include?(value)
                @value = value
            else
                raise ArgumentError, "The domain of " + @name.to_s + " (" + @domain.to_s +
                    ") does not contain " + value.to_s + "!"
            end
        end

[Source]

# File lib/Variable.rb, line 54
        def values
            domain.values
        end

[Validate]