コード例 #1
0
 public void AddConstraint(IFluentLayout <T> constraint)
 {
     foreach (var c in GetConstraintsFromFluentLayout(constraint))
     {
         solver.AddConstraint(c);
     }
 }
コード例 #2
0
        protected IEnumerable <ClConstraint> GetConstraintsFromFluentLayout(IFluentLayout <T> fluentLayout)
        {
            var constraints = new List <ClConstraint>();
            ClLinearExpression firstExpression = null;

            firstExpression = GetExpressionFromViewAndAttribute(fluentLayout.View, fluentLayout.Attribute);

            ClLinearExpression secondExpression = null;

            if (fluentLayout.SecondItem != null)
            {
                secondExpression = GetExpressionFromViewAndAttribute(
                    fluentLayout.SecondItem.View,
                    fluentLayout.SecondItem.Attribute
                    );

                var multiplier = !Cl.Approx(fluentLayout.Multiplier, 0) ? fluentLayout.Multiplier : 1;
                //make sure to construct the least complicated tableau possible by avoiding needless operations
                if (!Cl.Approx(multiplier, 1))
                {
                    secondExpression = Cl.Plus(
                        Cl.Times(secondExpression, multiplier),
                        new ClLinearExpression(fluentLayout.Constant)
                        );
                }
                else if (!Cl.Approx(fluentLayout.Constant, 0))
                {
                    secondExpression = Cl.Plus(
                        secondExpression,
                        new ClLinearExpression(fluentLayout.Constant)
                        );
                }
            }
            else
            {
                secondExpression = new ClLinearExpression(fluentLayout.Constant);
            }

            ClConstraint cn       = null;
            var          strength = ClStrength.Strong;
            var          priority = fluentLayout.Priority / 1000;

            switch (fluentLayout.Relation)
            {
            case LayoutRelation.Equal:
                cn = new ClLinearEquation(
                    firstExpression,
                    secondExpression, strength, priority
                    );
                break;

            case LayoutRelation.GreaterThanOrEqual:
                cn = new ClLinearInequality(
                    firstExpression,
                    Cl.Operator.GreaterThanOrEqualTo,
                    secondExpression, strength, priority
                    );
                break;

            case LayoutRelation.LessThanOrEqual:
                cn = new ClLinearInequality(
                    firstExpression,
                    Cl.Operator.LessThanOrEqualTo,
                    secondExpression, strength, priority
                    );
                break;
            }

            constraints.Add(cn);

            return(constraints);
        }
コード例 #3
0
 public void AddConstraint(IFluentLayout <T> constraint)
 {
     solver.addConstraint(GetConstraintFromFluentLayout(constraint));
 }
コード例 #4
0
        protected Constraint GetConstraintFromFluentLayout(IFluentLayout <T> fluentLayout)
        {
            var firstExpression = GetVariableFromViewAndAttribute(fluentLayout.View, fluentLayout.Attribute);

            Expression secondExpression = null;
            Variable   secondVariable   = null;

            if (fluentLayout.SecondItem != null)
            {
                secondVariable = GetVariableFromViewAndAttribute(
                    fluentLayout.SecondItem.View,
                    fluentLayout.SecondItem.Attribute
                    );

                var multiplier = !Approx(fluentLayout.Multiplier, 0) ? fluentLayout.Multiplier : 1;

                //make sure to construct the least complicated tableau possible by avoiding needless operations
                if (!Approx(multiplier, 1))
                {
                    secondExpression = kiwi.kiwi.__plus__(
                        kiwi.kiwi.__mult__(secondVariable, multiplier),
                        fluentLayout.Constant
                        );
                }
                else if (!Approx(fluentLayout.Constant, 0))
                {
                    secondExpression = kiwi.kiwi.__plus__(
                        secondVariable,
                        fluentLayout.Constant
                        );
                }
                else
                {
                    secondExpression = new Expression(new Term(secondVariable));
                }
            }
            else
            {
                secondExpression = new Expression(fluentLayout.Constant);
            }

            Constraint constraint = null;
            var        strength   = fluentLayout.Priority;

            switch (fluentLayout.Relation)
            {
            case LayoutRelation.Equal:
                constraint = new Constraint(kiwi.kiwi.__minus__(firstExpression, secondExpression),
                                            RelationalOperator.OP_EQ, strength);
                break;

            case LayoutRelation.GreaterThanOrEqual:
                constraint = new Constraint(kiwi.kiwi.__minus__(firstExpression, secondExpression),
                                            RelationalOperator.OP_GE, strength);
                break;

            case LayoutRelation.LessThanOrEqual:
                constraint = new Constraint(kiwi.kiwi.__minus__(firstExpression, secondExpression),
                                            RelationalOperator.OP_LE, strength);
                break;
            }

            return(constraint);
        }