From 1cf63069167759c4040b2ea3cacc4bed85333a71 Mon Sep 17 00:00:00 2001 From: Adam Sherwood Date: Tue, 23 Jan 2024 21:39:44 +0000 Subject: [PATCH] add getConstraints to solver --- dist/constraint.d.ts | 4 ++-- dist/constraint.d.ts.map | 2 +- dist/solver.d.ts | 6 ++++++ dist/solver.d.ts.map | 2 +- dist/solver.js | 8 ++++++++ docs/Kiwi.md | 9 +++++++++ src/solver.ts | 9 +++++++++ test/main.js | 25 +++++++++++++++++++++++++ 8 files changed, 61 insertions(+), 4 deletions(-) diff --git a/dist/constraint.d.ts b/dist/constraint.d.ts index c1d5bd8..9658dee 100644 --- a/dist/constraint.d.ts +++ b/dist/constraint.d.ts @@ -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 } /** diff --git a/dist/constraint.d.ts.map b/dist/constraint.d.ts.map index 7b000d9..cc27994 100644 --- a/dist/constraint.d.ts.map +++ b/dist/constraint.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"constraint.d.ts","sourceRoot":"","sources":["../src/constraint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,iBAAiB,CAAA;AAE1C,OAAO,EAAC,QAAQ,EAAC,MAAM,eAAe,CAAA;AAEtC;;;;;;;;;;GAUG;AACH,oBAAY,QAAQ;IACnB,EAAE,IAAA;IACF,EAAE,IAAA;IACF,EAAE,IAAA;CACF;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,UAAU;gBAErB,UAAU,EAAE,UAAU,GAAG,QAAQ,EACjC,QAAQ,EAAE,QAAQ,EAClB,GAAG,CAAC,EAAE,UAAU,GAAG,QAAQ,GAAG,MAAM,EACpC,QAAQ,GAAE,MAA0B;IAWrC;;;OAGG;IACI,EAAE,IAAI,MAAM;IAInB;;;;OAIG;IACI,UAAU,IAAI,UAAU;IAI/B;;;;OAIG;IACI,EAAE,IAAI,QAAQ;IAIrB;;;;OAIG;IACI,QAAQ,IAAI,MAAM;IAIlB,QAAQ,IAAI,MAAM;IAMzB,OAAO,CAAC,WAAW,CAAY;IAC/B,OAAO,CAAC,SAAS,CAAU;IAC3B,OAAO,CAAC,SAAS,CAAQ;IACzB,OAAO,CAAC,GAAG,CAAiB;CAC5B"} \ No newline at end of file +{"version":3,"file":"constraint.d.ts","sourceRoot":"","sources":["../src/constraint.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAC,MAAM,iBAAiB,CAAA;AAE1C,OAAO,EAAC,QAAQ,EAAC,MAAM,eAAe,CAAA;AAEtC;;;;;;;;;;GAUG;AACH,oBAAY,QAAQ;IACnB,EAAE,IAAA,CAAE,KAAK;IACT,EAAE,IAAA,CAAE,KAAK;IACT,EAAE,IAAA;CACF;AAED;;;;;;;;;;;GAWG;AACH,qBAAa,UAAU;gBAErB,UAAU,EAAE,UAAU,GAAG,QAAQ,EACjC,QAAQ,EAAE,QAAQ,EAClB,GAAG,CAAC,EAAE,UAAU,GAAG,QAAQ,GAAG,MAAM,EACpC,QAAQ,GAAE,MAA0B;IAWrC;;;OAGG;IACI,EAAE,IAAI,MAAM;IAInB;;;;OAIG;IACI,UAAU,IAAI,UAAU;IAI/B;;;;OAIG;IACI,EAAE,IAAI,QAAQ;IAIrB;;;;OAIG;IACI,QAAQ,IAAI,MAAM;IAIlB,QAAQ,IAAI,MAAM;IAMzB,OAAO,CAAC,WAAW,CAAY;IAC/B,OAAO,CAAC,SAAS,CAAU;IAC3B,OAAO,CAAC,SAAS,CAAQ;IACzB,OAAO,CAAC,GAAG,CAAiB;CAC5B"} \ No newline at end of file diff --git a/dist/solver.d.ts b/dist/solver.d.ts index 052e9f6..2ef2d21 100644 --- a/dist/solver.d.ts +++ b/dist/solver.d.ts @@ -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. * diff --git a/dist/solver.d.ts.map b/dist/solver.d.ts.map index 4cb0d0f..39ef7f6 100644 --- a/dist/solver.d.ts.map +++ b/dist/solver.d.ts.map @@ -1 +1 @@ -{"version":3,"file":"solver.d.ts","sourceRoot":"","sources":["../src/solver.ts"],"names":[],"mappings":"AAAA,OAAO,EAAC,UAAU,EAAE,QAAQ,EAAC,MAAM,iBAAiB,CAAA;AACpD,OAAO,EAAC,UAAU,EAAC,MAAM,iBAAiB,CAAA;AAG1C,OAAO,EAAC,QAAQ,EAAC,MAAM,eAAe,CAAA;AAEtC;;;;GAIG;AACH,qBAAa,MAAM;IAClB;;;OAGG;IACI,aAAa,SAAO;IAE3B;;OAEG;;IAGH;;;;;;;OAOG;IACI,gBAAgB,CACtB,GAAG,EAAE,UAAU,GAAG,QAAQ,EAC1B,QAAQ,EAAE,QAAQ,EAClB,GAAG,EAAE,UAAU,GAAG,QAAQ,GAAG,MAAM,EACnC,QAAQ,GAAE,MAA0B,GAClC,UAAU;IAMb;;;;OAIG;IACI,aAAa,CAAC,UAAU,EAAE,UAAU,GAAG,IAAI;IAoDlD;;;;OAIG;IACI,gBAAgB,CAAC,UAAU,EAAE,UAAU,GAAG,IAAI;IA+BrD;;;;;OAKG;IACI,aAAa,CAAC,UAAU,EAAE,UAAU,GAAG,OAAO;IAIrD;;;;;OAKG;IACI,eAAe,CAAC,QAAQ,EAAE,QAAQ,EAAE,QAAQ,EAAE,MAAM,GAAG,IAAI;IAiBlE;;;;OAIG;IACI,kBAAkB,CAAC,QAAQ,EAAE,QAAQ,GAAG,IAAI;IAQnD;;;;;OAKG;IACI,eAAe,CAAC,QAAQ,EAAE,QAAQ,GAAG,OAAO;IAInD;;;;;OAKG;IACI,YAAY,CAAC,QAAQ,EAAE,QAAQ,EAAE,KAAK,EAAE,MAAM,GAAG,IAAI;IA6C5D;;OAEG;IACI,eAAe,IAAI,IAAI;IAc9B;;;;;OAKG;IACH,OAAO,CAAC,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B9B;;;;;;;;;OASG;IACH,OAAO,CAAC,iBAAiB;IAsBzB;;;;;;;;;;;;;;;;;;;;OAoBG;IACH,OAAO,CAAC,uBAAuB;IA0C/B;;;;OAIG;IACH,OAAO,CAAC,wBAAwB;IAShC;;;;OAIG;IACH,OAAO,CAAC,oBAAoB;IAS5B;;;;;;OAMG;IACH,OAAO,CAAC,mBAAmB;IAY3B;;;;OAIG;IACH,OAAO,CAAC,WAAW;IAInB,OAAO,CAAC,MAAM,CAAgB;IAC9B,OAAO,CAAC,OAAO,CAAiB;IAChC,OAAO,CAAC,OAAO,CAAiB;IAChC,OAAO,CAAC,QAAQ,CAAkB;IAClC,OAAO,CAAC,eAAe,CAAe;IACtC,OAAO,CAAC,UAAU,CAAiB;IACnC,OAAO,CAAC,WAAW,CAAY;IAC/B,OAAO,CAAC,OAAO,CAAY;CAC3B"} \ No newline at end of file diff --git a/dist/solver.js b/dist/solver.js index 02419da..1de1a71 100644 --- a/dist/solver.js +++ b/dist/solver.js @@ -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. * diff --git a/docs/Kiwi.md b/docs/Kiwi.md index 5add837..4d3bae3 100644 --- a/docs/Kiwi.md +++ b/docs/Kiwi.md @@ -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) ⇒ Bool + - [.getConstraints()](#module_@lume/kiwi..Solver+getConstraints) ⇒ [ 'Array' ].<Constraint> - [.addEditVariable(variable, strength)](#module_@lume/kiwi..Solver+addEditVariable) - [.removeEditVariable(variable)](#module_@lume/kiwi..Solver+removeEditVariable) - [.hasEditVariable(variable)](#module_@lume/kiwi..Solver+hasEditVariable) ⇒ Bool @@ -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) ⇒ Bool + - [.getConstraints()](#module_@lume/kiwi..Solver+getConstraints) ⇒ [ 'Array' ].<Constraint> - [.addEditVariable(variable, strength)](#module_@lume/kiwi..Solver+addEditVariable) - [.removeEditVariable(variable)](#module_@lume/kiwi..Solver+removeEditVariable) - [.hasEditVariable(variable)](#module_@lume/kiwi..Solver+hasEditVariable) ⇒ Bool @@ -477,6 +479,13 @@ Test whether the solver contains the constraint. | ---------- | ----------------------- | ---------------------- | | constraint | Constraint | Constraint to test for | + + +### solver.getConstraints() ⇒ [ 'Array' ].<Constraint> + +Get an array of the current constraints. + +**Kind**: instance method of [Solver](#module_@lume/kiwi..Solver) ### solver.addEditVariable(variable, strength) diff --git a/src/solver.ts b/src/solver.ts index 43d003d..56f57dc 100644 --- a/src/solver.ts +++ b/src/solver.ts @@ -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. * diff --git a/test/main.js b/test/main.js index 47e428d..9207d10 100644 --- a/test/main.js +++ b/test/main.js @@ -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 () {