Skip to content

Commit

Permalink
Merge pull request #7 from theSherwood/get_constraints
Browse files Browse the repository at this point in the history
Add getConstraints to solver
  • Loading branch information
trusktr authored Jan 25, 2024
2 parents 1e08cc8 + 1cf6306 commit ee3d349
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 4 deletions.
4 changes: 2 additions & 2 deletions dist/constraint.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { Variable } from './variable.js';
* @enum {Number}
*/
export declare enum Operator {
Le = 0,
Ge = 1,
Le = 0,// <=
Ge = 1,// >=
Eq = 2
}
/**
Expand Down
2 changes: 1 addition & 1 deletion dist/constraint.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions dist/solver.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ export declare class Solver {
* @return {Bool} true or false
*/
hasConstraint(constraint: Constraint): boolean;
/**
* Get an array of the current constraints.
*
* @return {Constraint[]}
*/
getConstraints(): Constraint[];
/**
* Add an edit variable to the solver.
*
Expand Down
2 changes: 1 addition & 1 deletion dist/solver.d.ts.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions dist/solver.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,14 @@ export class Solver {
hasConstraint(constraint) {
return this._cnMap.contains(constraint);
}
/**
* Get an array of the current constraints.
*
* @return {Constraint[]}
*/
getConstraints() {
return this._cnMap.array.map(({ first }) => first);
}
/**
* Add an edit variable to the solver.
*
Expand Down
9 changes: 9 additions & 0 deletions docs/Kiwi.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ console.assert(centerX.value() === 250)
- [.addConstraint(constraint)](#module_@lume/kiwi..Solver+addConstraint)
- [.removeConstraint(constraint)](#module_@lume/kiwi..Solver+removeConstraint)
- [.hasConstraint(constraint)](#module_@lume/kiwi..Solver+hasConstraint) ⇒ <code>Bool</code>
- [.getConstraints()](#module_@lume/kiwi..Solver+getConstraints) ⇒ <code>[ &#x27;Array&#x27; ].&lt;Constraint&gt;</code>
- [.addEditVariable(variable, strength)](#module_@lume/kiwi..Solver+addEditVariable)
- [.removeEditVariable(variable)](#module_@lume/kiwi..Solver+removeEditVariable)
- [.hasEditVariable(variable)](#module_@lume/kiwi..Solver+hasEditVariable) ⇒ <code>Bool</code>
Expand Down Expand Up @@ -406,6 +407,7 @@ The constraint solver class.
- [.addConstraint(constraint)](#module_@lume/kiwi..Solver+addConstraint)
- [.removeConstraint(constraint)](#module_@lume/kiwi..Solver+removeConstraint)
- [.hasConstraint(constraint)](#module_@lume/kiwi..Solver+hasConstraint) ⇒ <code>Bool</code>
- [.getConstraints()](#module_@lume/kiwi..Solver+getConstraints) ⇒ <code>[ &#x27;Array&#x27; ].&lt;Constraint&gt;</code>
- [.addEditVariable(variable, strength)](#module_@lume/kiwi..Solver+addEditVariable)
- [.removeEditVariable(variable)](#module_@lume/kiwi..Solver+removeEditVariable)
- [.hasEditVariable(variable)](#module_@lume/kiwi..Solver+hasEditVariable) ⇒ <code>Bool</code>
Expand Down Expand Up @@ -477,6 +479,13 @@ Test whether the solver contains the constraint.
| ---------- | ----------------------- | ---------------------- |
| constraint | <code>Constraint</code> | Constraint to test for |

<a name="module_@lume/kiwi..Solver+getConstraints"></a>

### solver.getConstraints() ⇒ <code>[ &#x27;Array&#x27; ].&lt;Constraint&gt;</code>

Get an array of the current constraints.

**Kind**: instance method of [<code>Solver</code>](#module_@lume/kiwi..Solver)
<a name="module_@lume/kiwi..Solver+addEditVariable"></a>

### solver.addEditVariable(variable, strength)
Expand Down
9 changes: 9 additions & 0 deletions src/solver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,15 @@ export class Solver {
return this._cnMap.contains(constraint)
}

/**
* Get an array of the current constraints.
*
* @return {Constraint[]}
*/
public getConstraints(): Constraint[] {
return this._cnMap.array.map(({first}) => first)
}

/**
* Add an edit variable to the solver.
*
Expand Down
25 changes: 25 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,31 @@ describe('import kiwi', function () {
assert.equal(err.message, 'unsatisfiable constraint')
}
})
it('solver.addConstraint() => solver.getConstraints()', function () {
solver = new kiwi.Solver()
var width = new kiwi.Variable()
var width2 = new kiwi.Variable()
var cn_1 = new kiwi.Constraint(new kiwi.Expression(width, 100), kiwi.Operator.Eq)
solver.addConstraint(cn_1)
var cn_2 = new kiwi.Constraint(new kiwi.Expression(width2, 100), kiwi.Operator.Eq)
solver.addConstraint(cn_2)
var cns = solver.getConstraints()
assert(cns.indexOf(cn_1) > -1)
assert(cns.indexOf(cn_2) > -1)
})
it('solver.removeConstraint() => solver.getConstraints()', function () {
solver = new kiwi.Solver()
var width = new kiwi.Variable()
var width2 = new kiwi.Variable()
var cn_1 = new kiwi.Constraint(new kiwi.Expression(width, 100), kiwi.Operator.Eq)
solver.addConstraint(cn_1)
var cn_2 = new kiwi.Constraint(new kiwi.Expression(width2, 100), kiwi.Operator.Eq)
solver.addConstraint(cn_2)
solver.removeConstraint(cn_1)
var cns = solver.getConstraints()
assert(cns.indexOf(cn_1) === -1)
assert(cns.indexOf(cn_2) > -1)
})
})

describe('Constraint raw syntax: (expr, operator, undefined, strength)', function () {
Expand Down

0 comments on commit ee3d349

Please sign in to comment.