Skip to content
This repository has been archived by the owner on Nov 1, 2024. It is now read-only.

Commit

Permalink
fix: migrate to modern dependencies, prepare to release 1.0.5 (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
kevmoo authored Jul 26, 2018
1 parent 5a4def1 commit e517a37
Show file tree
Hide file tree
Showing 24 changed files with 274 additions and 399 deletions.
9 changes: 3 additions & 6 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
.children
.project
.DS_Store
packages
.dart_tool
.packages
.pub
pubspec.lock
*.sublime-workspace
*.sublime-project
3 changes: 3 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### 1.0.5

* Updates to support Dart 2.
85 changes: 33 additions & 52 deletions example/DeltaBlue.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,8 @@ main() {
new DeltaBlue().report();
}


/// Benchmark class required to report results.
class DeltaBlue extends BenchmarkBase {

const DeltaBlue() : super("DeltaBlue");

void run() {
Expand All @@ -52,23 +50,26 @@ class DeltaBlue extends BenchmarkBase {
}
}


/**
* Strengths are used to measure the relative importance of constraints.
* New strengths may be inserted in the strength hierarchy without
* disrupting current constraints. Strengths cannot be created outside
* this class, so == can be used for value comparison.
*/
class Strength {

final int value;
final String name;

const Strength(this.value, this.name);

Strength nextWeaker() =>
const <Strength>[WEAKEST, WEAK_DEFAULT, NORMAL, STRONG_DEFAULT,
PREFERRED, STRONG_REFERRED][value];
Strength nextWeaker() => const <Strength>[
WEAKEST,
WEAK_DEFAULT,
NORMAL,
STRONG_DEFAULT,
PREFERRED,
STRONG_REFERRED
][value];

static bool stronger(Strength s1, Strength s2) {
return s1.value < s2.value;
Expand All @@ -87,19 +88,16 @@ class Strength {
}
}


// Compile time computed constants.
const REQUIRED = const Strength(0, "required");
const REQUIRED = const Strength(0, "required");
const STRONG_REFERRED = const Strength(1, "strongPreferred");
const PREFERRED = const Strength(2, "preferred");
const STRONG_DEFAULT = const Strength(3, "strongDefault");
const NORMAL = const Strength(4, "normal");
const WEAK_DEFAULT = const Strength(5, "weakDefault");
const WEAKEST = const Strength(6, "weakest");

const PREFERRED = const Strength(2, "preferred");
const STRONG_DEFAULT = const Strength(3, "strongDefault");
const NORMAL = const Strength(4, "normal");
const WEAK_DEFAULT = const Strength(5, "weakDefault");
const WEAKEST = const Strength(6, "weakest");

abstract class Constraint {

final Strength strength;

const Constraint(this.strength);
Expand Down Expand Up @@ -163,7 +161,6 @@ abstract class Constraint {
* Abstract superclass for constraints having a single possible output variable.
*/
abstract class UnaryConstraint extends Constraint {

final Variable myOutput;
bool satisfied = false;

Expand All @@ -179,8 +176,8 @@ abstract class UnaryConstraint extends Constraint {

/// Decides if this constraint can be satisfied and records that decision.
void chooseMethod(int mark) {
satisfied = (myOutput.mark != mark)
&& Strength.stronger(strength, myOutput.walkStrength);
satisfied = (myOutput.mark != mark) &&
Strength.stronger(strength, myOutput.walkStrength);
}

/// Returns true if this constraint is satisfied in the current solution.
Expand Down Expand Up @@ -217,29 +214,25 @@ abstract class UnaryConstraint extends Constraint {
}
}


/**
* Variables that should, with some level of preference, stay the same.
* Planners may exploit the fact that instances, if satisfied, will not
* change their output during plan execution. This is called "stay
* optimization".
*/
class StayConstraint extends UnaryConstraint {

StayConstraint(Variable v, Strength str) : super(v, str);

void execute() {
// Stay constraints do nothing.
}
}


/**
* A unary input constraint used to mark a variable that the client
* wishes to change.
*/
class EditConstraint extends UnaryConstraint {

EditConstraint(Variable v, Strength str) : super(v, str);

/// Edits indicate that a variable is to be changed by imperative code.
Expand All @@ -250,19 +243,16 @@ class EditConstraint extends UnaryConstraint {
}
}


// Directions.
const int NONE = 1;
const int FORWARD = 2;
const int BACKWARD = 0;


/**
* Abstract superclass for constraints having two possible output
* variables.
*/
abstract class BinaryConstraint extends Constraint {

Variable v1;
Variable v2;
int direction = NONE;
Expand All @@ -278,21 +268,23 @@ abstract class BinaryConstraint extends Constraint {
*/
void chooseMethod(int mark) {
if (v1.mark == mark) {
direction = (v2.mark != mark &&
Strength.stronger(strength, v2.walkStrength))
? FORWARD : NONE;
direction =
(v2.mark != mark && Strength.stronger(strength, v2.walkStrength))
? FORWARD
: NONE;
}
if (v2.mark == mark) {
direction = (v1.mark != mark &&
Strength.stronger(strength, v1.walkStrength))
? BACKWARD : NONE;
direction =
(v1.mark != mark && Strength.stronger(strength, v1.walkStrength))
? BACKWARD
: NONE;
}
if (Strength.weaker(v1.walkStrength, v2.walkStrength)) {
direction = Strength.stronger(strength, v1.walkStrength)
? BACKWARD : NONE;
direction =
Strength.stronger(strength, v1.walkStrength) ? BACKWARD : NONE;
} else {
direction = Strength.stronger(strength, v2.walkStrength)
? FORWARD : BACKWARD;
direction =
Strength.stronger(strength, v2.walkStrength) ? FORWARD : BACKWARD;
}
}

Expand Down Expand Up @@ -346,7 +338,6 @@ abstract class BinaryConstraint extends Constraint {
}
}


/**
* Relates two variables by the linear scaling relationship: "v2 =
* (v1 * scale) + offset". Either v1 or v2 may be changed to maintain
Expand All @@ -355,13 +346,12 @@ abstract class BinaryConstraint extends Constraint {
*/

class ScaleConstraint extends BinaryConstraint {

final Variable scale;
final Variable offset;

ScaleConstraint(Variable src, this.scale, this.offset,
Variable dest, Strength strength)
: super(src, dest, strength);
ScaleConstraint(
Variable src, this.scale, this.offset, Variable dest, Strength strength)
: super(src, dest, strength);

/// Adds this constraint to the constraint graph.
void addToGraph() {
Expand Down Expand Up @@ -401,33 +391,28 @@ class ScaleConstraint extends BinaryConstraint {
out.stay = ihn.stay && scale.stay && offset.stay;
if (out.stay) execute();
}

}


/**
* Constrains two variables to have the same value.
*/
class EqualityConstraint extends BinaryConstraint {

EqualityConstraint(Variable v1, Variable v2, Strength strength)
: super(v1, v2, strength);
: super(v1, v2, strength);

/// Enforce this constraint. Assume that it is satisfied.
void execute() {
output().value = input().value;
}
}


/**
* A constrained variable. In addition to its value, it maintain the
* structure of the constraint graph, the current dataflow graph, and
* various parameters of interest to the DeltaBlue incremental
* constraint solver.
**/
class Variable {

List<Constraint> constraints = <Constraint>[];
Constraint determinedBy;
int mark = 0;
Expand All @@ -453,9 +438,7 @@ class Variable {
}
}


class Planner {

int currentMark = 0;

/**
Expand All @@ -474,7 +457,7 @@ class Planner {
*/
void incrementalAdd(Constraint c) {
int mark = newMark();
for(Constraint overridden = c.satisfy(mark);
for (Constraint overridden = c.satisfy(mark);
overridden != null;
overridden = overridden.satisfy(mark));
}
Expand Down Expand Up @@ -621,7 +604,6 @@ class Planner {
}
}


/**
* A Plan is an ordered list of constraints to be executed in sequence
* to resatisfy all currently satisfiable constraints in the face of
Expand All @@ -643,7 +625,6 @@ class Plan {
}
}


/**
* This is the standard DeltaBlue benchmark. A long chain of equality
* constraints is constructed with a stay constraint on one end. An
Expand Down
Loading

0 comments on commit e517a37

Please sign in to comment.