generated from publicodes/model-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor!: redesign the package API and internal design
- Loading branch information
1 parent
50b6a36
commit 92131b3
Showing
10 changed files
with
2,177 additions
and
110 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { writeFileSync } from "fs" | ||
import { join, basename } from "path" | ||
import { stringify } from "yaml" | ||
import { getModelFromSource } from "@publicodes/tools/compilation" | ||
import Engine from "publicodes" | ||
|
||
import getPersonas from "./compile-personas.js" | ||
import generateAlternatives from "./generate-alternatives.js" | ||
|
||
const ROOT_PATH = new URL(".", import.meta.url).pathname | ||
const SRC_FILES = join(ROOT_PATH, "../src/rules/") | ||
const ALTERNATIVES_DEST_PATH = join( | ||
ROOT_PATH, | ||
"../src/rules/alternatives.publicodes", | ||
) | ||
const PERSONAS_DEST_PATH = join(ROOT_PATH, "../src/personas/personas.json") | ||
|
||
const model = getModelFromSource(SRC_FILES) | ||
const engine = check(model, "base") | ||
|
||
const resolvedRules = Object.fromEntries( | ||
Object.entries(engine.getParsedRules()).map(([dottedName, rule]) => { | ||
delete rule.rawNode["avec"] | ||
return [dottedName, rule.rawNode] | ||
}), | ||
) | ||
|
||
const alternatives = generateAlternatives(resolvedRules) | ||
console.log(`✅ './src/rules/alternatives.publicodes' generated`) | ||
writeFileSync( | ||
ALTERNATIVES_DEST_PATH, | ||
`# GENERATED FILE - DO NOT EDIT\n\n${stringify(alternatives, { | ||
aliasDuplicateObjects: false, | ||
})}`, | ||
) | ||
|
||
const personas = getPersonas(model) | ||
writeFileSync(PERSONAS_DEST_PATH, JSON.stringify(personas)) | ||
console.log(`✅ './src/personas/personas.json' generated`) | ||
|
||
function check(rules, step) { | ||
try { | ||
const engine = new Engine(rules, { logger: { warn: () => {} } }) | ||
engine.evaluate("empreinte . voiture") | ||
engine.evaluate("coûts . voiture") | ||
|
||
return engine | ||
} catch (e) { | ||
console.error(`❌ Error at (${step}):\n${e.message}\n`) | ||
process.exit(-1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Exports the personas object. | ||
* | ||
* It's defined in the `personas.yaml` file and allows to define named | ||
* situations that can be feed into the engine. | ||
*/ | ||
|
||
import { Situation } from "../../publicodes-build" | ||
import _rawPersonas from "./personas.json" | ||
|
||
export type Persona = { | ||
titre: string | ||
description?: string | ||
situation: Situation | ||
} | ||
|
||
/** | ||
* A set of named situations that can be feed into the engine and used for | ||
* testing purposes or to provide examples. | ||
*/ | ||
export default _rawPersonas as Record<string, Persona> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"aller-retours travail électrique ville":{"titre":"Aller-retours travail en ville (électrique)","description":"Personne qui va travailler avec une petite voiture électrique en ville\n","situation":{"voiture . motorisation":"électrique","voiture . gabarit":"petite","usage . km annuels . connus":"non","usage . km annuels . calculés . quotidien":20,"usage . km annuels . calculés . vacances":0}},"grandes vacances familiales":{"titre":"Grandes vacances familiales (essence)","description":"Personne qui utilise une voiture essence pour partir en vacances en\nfamille.\n","situation":{"voiture . motorisation":"thermique","voiture . thermique . carburant":"essence E5 ou E10","voiture . gabarit":"berline","usage . km annuels . connus":"non","usage . km annuels . calculés . quotidien":5,"usage . km annuels . calculés . vacances":2000}},"famille usage régulier diesel":{"titre":"Usage régulier en famille (diesel)","description":"Personne qui utilise une voiture diesel pour les trajets quotidiens et\nles vacances en famille.\n\nElle a besoin d'une voiture spacieuse pour transporter ses enfants à l'école\net aller au travail.\n","situation":{"voiture . motorisation":"thermique","voiture . thermique . carburant":"gazole B7 ou B10","voiture . gabarit":"berline","usage . km annuels . connus":"non","usage . km annuels . calculés . quotidien":33,"usage . km annuels . calculés . vacances":2000}},"famille usage régulier biocarburant":{"titre":"Usage régulier en famille (biocarburant)","description":"Personne qui utilise une voiture diesel pour les trajets quotidiens et\nles vacances en famille.\n\nElle a besoin d'une voiture spacieuse pour transporter ses enfants à l'école\net aller au travail.\n","situation":{"voiture . motorisation":"thermique","voiture . thermique . carburant":"essence E85","voiture . gabarit":"berline","usage . km annuels . connus":"non","usage . km annuels . calculés . quotidien":33,"usage . km annuels . calculés . vacances":2000}},"famille usage régulier moyenne biocarburant":{"titre":"Usage régulier en famille (moyenne/biocarburant)","description":"Personne qui utilise une voiture diesel pour les trajets quotidiens et\nles vacances en famille.\n\nElle a besoin d'une voiture spacieuse pour transporter ses enfants à l'école\net aller au travail.\n","situation":{"voiture . motorisation":"thermique","voiture . thermique . carburant":"essence E85","voiture . gabarit":"moyenne","usage . km annuels . connus":"non","usage . km annuels . calculés . quotidien":33,"usage . km annuels . calculés . vacances":2000}}} |
Oops, something went wrong.