示例#1
0
        /// <summary>
        /// Build one or more arcs from the ternary constraint expression.
        /// </summary>
        /// <remarks>
        /// There will be more than one arc because binarization of the expression requires introducing an
        /// encapsulated variable between the two variables involved in the expression.
        /// </remarks>
        /// <param name="expressionConstraintNode">Expression constraint abstract syntax tree.</param>
        /// <returns>One or more arcs.</returns>
        private IReadOnlyCollection <Arc> BuildTernaryExpression(ConstraintExpressionNode expressionConstraintNode)
        {
            var arcAccumulator = new List <Arc>();

            if (!expressionConstraintNode.InnerExpression.RightExpression.IsLiteral)
            {
                var left  = CreateNodeFrom(expressionConstraintNode.InnerExpression.LeftExpression);
                var right = CreateNodeFrom(expressionConstraintNode.InnerExpression.RightExpression);
                var encapsulatedVariable     = new EncapsulatedVariable("U");
                var encapsulatedVariableNode = new EncapsulatedVariableNode(encapsulatedVariable);
                var arc1 = new Arc(left, encapsulatedVariableNode, new EncapsulatedVariableConnector(left, encapsulatedVariableNode, new EncapsulatedSelector(1)));
                arcAccumulator.Add(arc1);
                var arc2 = new Arc(right, encapsulatedVariableNode, new EncapsulatedVariableConnector(encapsulatedVariableNode, right, new EncapsulatedSelector(2)));
                arcAccumulator.Add(arc2);
                var ternaryConstraintExpression     = new TernaryConstraintExpression(expressionConstraintNode, encapsulatedVariableNode, arc1, arc2);
                var encapsulatedVariableDomainValue = ComputeEncapsulatedDomain(ternaryConstraintExpression);
                encapsulatedVariable.DomainValue = encapsulatedVariableDomainValue;
                var expressionSolution = new TernaryConstraintExpressionSolution(ternaryConstraintExpression, encapsulatedVariableDomainValue);
                _modelSolverMap.AddTernaryExpressionSolution(expressionSolution);
            }
            else
            {
                var left = CreateNodeFrom(expressionConstraintNode.InnerExpression.LeftExpression);
                var encapsulatedVariable     = new EncapsulatedVariable("U");
                var encapsulatedVariableNode = new EncapsulatedVariableNode(encapsulatedVariable);
                var arc1 = new Arc(left, encapsulatedVariableNode, new EncapsulatedVariableConnector(left, encapsulatedVariableNode, new EncapsulatedSelector(1)));
                arcAccumulator.Add(arc1);
                var ternaryConstraintExpression     = new TernaryConstraintExpression(expressionConstraintNode, encapsulatedVariableNode, arc1, arc1);
                var encapsulatedVariableDomainValue = ComputeEncapsulatedDomain(ternaryConstraintExpression);
                encapsulatedVariable.DomainValue = encapsulatedVariableDomainValue;
                var expressionSolution = new TernaryConstraintExpressionSolution(ternaryConstraintExpression, encapsulatedVariableDomainValue);
                _modelSolverMap.AddTernaryExpressionSolution(expressionSolution);
            }

            return(new ReadOnlyCollection <Arc>(arcAccumulator));
        }
 /// <summary>
 /// Initialize an encapsulated variable domain value with an encapsulated variable and value sets.
 /// </summary>
 internal EncapsulatedVariableDomainValue(EncapsulatedVariable encapsulatedVariable, IEnumerable <ValueSet> valueSets)
 {
     _encapsulatedVariable = encapsulatedVariable;
     _sets = new List <ValueSet>(valueSets);
 }