Skip to content

Commit

Permalink
test(optim): migrate the publiopti source code inside the model repo
Browse files Browse the repository at this point in the history
  • Loading branch information
EmileRolley committed Feb 6, 2023
1 parent 9834282 commit 7fe2fe2
Show file tree
Hide file tree
Showing 6 changed files with 1,140 additions and 18 deletions.
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
},
"scripts": {
"compile:md": "node scripts/rulesToJSON.js --markdown && node scripts/personasToJSON.js --markdown && node scripts/documentationToJSON.js --markdown",
"compile": "node scripts/rulesToJSON.js && node scripts/personasToJSON.js && node scripts/documentationToJSON.js",
"compile": "npx tsup scripts/publiopti -d scripts/publiopti-dist && node scripts/rulesToJSON.js && node scripts/personasToJSON.js && node scripts/documentationToJSON.js",
"compile:personas": "node scripts/personasToJSON.js",
"compile:rules": "node scripts/rulesToJSON.js",
"compile:rules": "tsup scripts/publiopti -d scripts/publiopti-dist && node scripts/rulesToJSON.js",
"test": "echo \"Error: no test specified\" && exit 1",
"translate": "node scripts/i18n/translate-rules.js && node scripts/i18n/translate-personas.js",
"check:personas": "node scripts/i18n/check-personas.js ",
Expand Down Expand Up @@ -41,7 +41,6 @@
"deepl-node": "^1.7.0",
"dotenv": "^16.0.3",
"isomorphic-fetch": "^3.0.0",
"publiopti": "^0.1.20",
"ramda": "^0.28.0",
"yargs": "^17.6.0"
}
Expand Down
23 changes: 21 additions & 2 deletions scripts/modelOptim.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
const Engine = require('publicodes').default
const path = require('path')
const { readFileSync, writeFileSync } = require('fs')
const { constantFolding, disabledLogger, getRawNodes } = require('publiopti')
const {
constantFolding,
disabledLogger,
getRawNodes,
} = require('./publiopti-dist/index')

/**
* Applies a constant folding optimization pass to the parsed rules from the [model] path.
Expand All @@ -19,7 +23,7 @@ function constantFoldingFromJSONFile(
jsonDestPath,
ignore,
targets,
verbose = false
verbose = true
) {
const log = verbose ? console.log : function (_) {}
try {
Expand All @@ -34,11 +38,26 @@ function constantFoldingFromJSONFile(
rules = readRawRules(modelPath, ignore ?? [])
}

console.log(
'[BEFORE]: alimentation . plats . végétalien . nombre:',
rules['alimentation . plats . végétalien . nombre']
)
const engine = new Engine(rules, { logger: disabledLogger })

log('Constant folding pass...')
const foldedRules = constantFolding(engine /* , targets */)

console.log(
'[AFTER]: alimentation . plats . végétalien . nombre:',
foldedRules['alimentation . plats . végétalien . nombre']
)
// const local = JSON.parse(readFileSync('local-fr-opti.json'), 'utf8')
// Object.keys(local).forEach((key) => {
// if (!foldedRules[key]) {
// console.log('Missing key:', key)
// }
// })

log(`Writing in '${jsonDestPath}'...`)
writeFileSync(jsonDestPath, JSON.stringify(getRawNodes(foldedRules)))
} catch (error) {
Expand Down
24 changes: 24 additions & 0 deletions scripts/publiopti/commons.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import type { RuleNode } from "publicodes";
import RawPublicodes from "publicodes";

export type RuleName = string;
export type ParsedRules = Record<RuleName, RuleNode<RuleName>>;
export type RawRules = RawPublicodes<RuleName>;

export function getRawNodes(parsedRules: ParsedRules): RawRules {
return Object.fromEntries(
Object.values(parsedRules).reduce((acc: any, rule) => {
const { nom, ...rawNode } = rule.rawNode;
acc.push([nom, rawNode]);
return acc;
}, [])
) as RawRules;
}

function consumeMsg(_: string): void {}

export const disabledLogger = {
log: consumeMsg,
warn: consumeMsg,
error: consumeMsg,
};
Loading

0 comments on commit 7fe2fe2

Please sign in to comment.