constraint.dart 2.9 KB
Newer Older
1
// Copyright 2016 The Chromium Authors. All rights reserved.
Chinmay Garde's avatar
Chinmay Garde committed
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
import 'priority.dart';
import 'expression.dart';
Chinmay Garde's avatar
Chinmay Garde committed
7

8 9 10 11 12
/// Relationships between [Constraint] expressions.
///
/// A [Constraint] is created by specifying a relationship between two
/// expressions. The [Solver] tries to satisfy this relationship after the
/// [Constraint] has been added to it at a set priority.
13
enum Relation {
14 15
  /// The relationship between the left and right hand sides of the expression
  /// is `==`, (lhs == rhs).
16
  equalTo,
17 18
  /// The relationship between the left and right hand sides of the expression
  /// is `<=`, (lhs <= rhs).
19
  lessThanOrEqualTo,
20 21
  /// The relationship between the left and right hand sides of the expression
  /// is `>=`, (lhs => rhs).
22 23
  greaterThanOrEqualTo,
}
Chinmay Garde's avatar
Chinmay Garde committed
24

25 26 27 28
/// A relationship between two expressions (represented by [Expression]) that
/// the [Solver] tries to hold true. In case of ambiguities, the [Solver] will
/// use priorities to determine [Constraint] precedence. Once a [Constraint] is
/// added to the [Solver], this [Priority] cannot be changed.
Chinmay Garde's avatar
Chinmay Garde committed
29
class Constraint {
30 31 32
  /// Creates a new [Constraint] by specifying a single [Expression]. This
  /// assumes that the right hand side [Expression] is the constant zero.
  /// (`<expression> <relation> <0>`)
33 34
  Constraint(this.expression, this.relation);

35
  /// The [Relation] between a [Constraint] [Expression] and zero.
Chinmay Garde's avatar
Chinmay Garde committed
36
  final Relation relation;
37

38 39 40 41
  /// The [Constraint] [Expression]. The [Expression] on the right hand side of
  /// constraint must be zero. If the [Expression] on the right is not zero,
  /// it must be negated from the left hand [Expression] before a [Constraint]
  /// can be created.
Chinmay Garde's avatar
Chinmay Garde committed
42 43
  final Expression expression;

44 45 46 47
  /// The [Constraint] [Priority]. The [Priority] can only be modified when the
  /// [Constraint] is being created. Once it is added to the solver,
  /// modifications to the [Constraint] [Priority] will have no effect on the
  /// how the solver evaluates the constraint.
48
  double priority = Priority.required;
49

50 51 52 53
  /// The operator `|` is overloaded as a convenience so that constraint
  /// priorities can be specifed along with the [Constraint] expression.
  ///
  /// For example: `ax + by + cx <= 0 | Priority.weak`. See [Priority].
54
  Constraint operator |(double p) => this..priority = p;
55

pq's avatar
pq committed
56
  @override
57 58 59 60 61 62
  String toString() {
    StringBuffer buffer = new StringBuffer();
    buffer.write(expression.toString());

    switch (relation) {
      case Relation.equalTo:
Hixie's avatar
Hixie committed
63
        buffer.write(' == 0 ');
64 65
        break;
      case Relation.greaterThanOrEqualTo:
Hixie's avatar
Hixie committed
66
        buffer.write(' >= 0 ');
67 68
        break;
      case Relation.lessThanOrEqualTo:
Hixie's avatar
Hixie committed
69
        buffer.write(' <= 0 ');
70 71 72
        break;
    }

Hixie's avatar
Hixie committed
73
    buffer.write(' | priority = $priority');
74

75
    if (priority == Priority.required)
Hixie's avatar
Hixie committed
76
      buffer.write(' (required)');
77 78 79

    return buffer.toString();
  }
Chinmay Garde's avatar
Chinmay Garde committed
80
}