Skip to content

Commit

Permalink
Merge pull request #31 from publicodes/fix-input-rules-shouldn't-be-m…
Browse files Browse the repository at this point in the history
…odified

Fix(optim): input rules are now deep copied [NGC-110]
  • Loading branch information
EmileRolley committed Dec 12, 2023
2 parents 18d1748 + 3c5dfbe commit 5ee4207
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 8 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,13 @@
"license": "MIT",
"dependencies": {
"@types/node": "^18.11.18",
"publicodes": "^1.0.0-beta.71"
"publicodes": "^1.0.0-beta.77"
},
"devDependencies": {
"@types/jest": "^29.2.5",
"docdash": "^2.0.1",
"jest": "^29.4.1",
"mitata": "^0.1.6",
"prettier": "^3.0.0",
"ts-jest": "^29.0.4",
"ts-node": "^10.9.1",
Expand Down
5 changes: 4 additions & 1 deletion source/optims/constantFolding.ts
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,10 @@ export function constantFolding(
toKeep?: PredicateOnRule,
params?: FoldingParams,
): ParsedRules<RuleName> {
const parsedRules: ParsedRules<RuleName> = engine.getParsedRules()
const parsedRules: ParsedRules<RuleName> =
// PERF: could it be avoided?
JSON.parse(JSON.stringify(engine.getParsedRules()))

let ctx: FoldingCtx = initFoldingCtx(engine, parsedRules, toKeep, params)

Object.entries(ctx.parsedRules).forEach(([ruleName, ruleNode]) => {
Expand Down
35 changes: 33 additions & 2 deletions test/optims/constantFolding.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { getRawNodes, RuleName, RawRules } from '../../source/commons'
import Engine from 'publicodes'
import {
getRawNodes,
RuleName,
RawRules,
disabledLogger,
} from '../../source/commons'
import { constantFolding } from '../../source/optims/'
import { callWithEngine } from '../utils.test'

Expand All @@ -14,7 +20,32 @@ function constantFoldingWith(rawRules: any, targets?: RuleName[]): RawRules {
return getRawNodes(res)
}

describe('Constant folding optim', () => {
describe('Constant folding [meta]', () => {
it('should not modify the original rules', () => {
const rawRules = {
ruleA: {
titre: 'Rule A',
formule: 'B . C * D',
},
'ruleA . B . C': {
valeur: '10',
},
'ruleA . D': {
valeur: '3',
},
}
const engine = new Engine(rawRules, { logger: disabledLogger })
const untouchedParsedRules = getRawNodes(engine.getParsedRules())

constantFolding(engine, ([ruleName, _]) => ruleName === 'ruleA')

expect(getRawNodes(engine.getParsedRules())).toStrictEqual(
untouchedParsedRules,
)
})
})

describe('Constant folding [base]', () => {
it('∅ -> ∅', () => {
expect(constantFoldingWith({})).toStrictEqual({})
})
Expand Down
13 changes: 9 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2153,6 +2153,11 @@ minimatch@^9.0.0:
dependencies:
brace-expansion "^2.0.1"

mitata@^0.1.6:
version "0.1.6"
resolved "https://registry.yarnpkg.com/mitata/-/mitata-0.1.6.tgz#0d25f0dd105d6c50ec8d9047ee21bed1552fbffd"
integrity sha512-VKQ0r3jriTOU9E2Z+mwbZrUmbg4Li4QyFfi7kfHKl6reZhGzL0AYlu3wE0VPXzIwA5xnFzmEQoBwCcNT8stUkA==

ms@2.1.2:
version "2.1.2"
resolved "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz"
Expand Down Expand Up @@ -2326,10 +2331,10 @@ prompts@^2.0.1:
kleur "^3.0.3"
sisteransi "^1.0.5"

publicodes@^1.0.0-beta.71:
version "1.0.0-beta.71"
resolved "https://registry.yarnpkg.com/publicodes/-/publicodes-1.0.0-beta.71.tgz#662478db6460bf1348183c2e012c7b35b19a55c9"
integrity sha512-m4JIdP3J1wncTmIAPFytZn2sv9y1vGL0BSJJdmsga8Un5q1GmRRZEo/BQGt8WKw2Mm8O14LuobdEV+g15h9Dcg==
publicodes@^1.0.0-beta.77:
version "1.0.0-beta.77"
resolved "https://registry.yarnpkg.com/publicodes/-/publicodes-1.0.0-beta.77.tgz#45e3c4d2a46bfadcc932e1405ea037e659c28134"
integrity sha512-F8U3WGUWMo3/rxhWYS1gWIiG20g1Yy/+PpXdHM99d6ZHKWnnyh/4txVEuyVE75glgDs+mTjwZPnmoKWsTMXluA==

punycode@^2.1.0:
version "2.3.0"
Expand Down

0 comments on commit 5ee4207

Please sign in to comment.