From 1765807b8307fb89f42c964e926c58157216de97 Mon Sep 17 00:00:00 2001 From: Jakub Niechaj Date: Sun, 8 Oct 2023 17:55:36 +0200 Subject: [PATCH] Add json migration --- .../Simulation/Detectors/DetectorManager.ts | 15 +- .../Simulation/Figures/FigureManager.ts | 2 +- .../Simulation/Materials/MaterialManager.ts | 2 +- .../Simulation/Scoring/ScoringManager.ts | 15 +- .../SpecialComponentManager.ts | 2 +- .../Simulation/Zones/ZoneManager.ts | 13 +- src/ThreeEditor/examples/README.md | 4 +- src/ThreeEditor/examples/ex2.json | 6 +- src/ThreeEditor/examples/ex3.json | 6 +- src/ThreeEditor/examples/ex4.json | 6 +- src/ThreeEditor/examples/ex5.json | 4 +- .../examples/migrations/migrateEx0.10_0.11.js | 149 ++++++++++++++++++ src/ThreeEditor/js/YaptideEditor.js | 2 +- 13 files changed, 193 insertions(+), 33 deletions(-) create mode 100644 src/ThreeEditor/examples/migrations/migrateEx0.10_0.11.js diff --git a/src/ThreeEditor/Simulation/Detectors/DetectorManager.ts b/src/ThreeEditor/Simulation/Detectors/DetectorManager.ts index 30fcde547..822d756ec 100644 --- a/src/ThreeEditor/Simulation/Detectors/DetectorManager.ts +++ b/src/ThreeEditor/Simulation/Detectors/DetectorManager.ts @@ -40,7 +40,7 @@ export class DetectorManager { /****************************Private****************************/ private readonly metadata = { - version: `0.10`, //update this to current YaptideEditor version when format changes + version: `0.11`, //update this to current YaptideEditor version when format changes type: 'Manager', generator: 'DetectorManager.toJSON' } satisfies Record; @@ -133,11 +133,14 @@ export class DetectorManager return this.editor.zoneManager.getZoneByUuid(geometryData.zoneUuid) !== undefined; }) .filter(additionalPredicate || (() => true)) - .reduce((acc, geometry) => { - acc[geometry.uuid] = `${geometry.name} [${geometry.id}]`; - - return acc; - }, {} as Record); + .reduce( + (acc, geometry) => { + acc[geometry.uuid] = `${geometry.name} [${geometry.id}]`; + + return acc; + }, + {} as Record + ); return options; } diff --git a/src/ThreeEditor/Simulation/Figures/FigureManager.ts b/src/ThreeEditor/Simulation/Figures/FigureManager.ts index 1b56885e7..47378e616 100644 --- a/src/ThreeEditor/Simulation/Figures/FigureManager.ts +++ b/src/ThreeEditor/Simulation/Figures/FigureManager.ts @@ -50,7 +50,7 @@ export class FigureManager { /****************************Private****************************/ private readonly metadata = { - version: `0.10`, //update this to current YaptideEditor version when format changes + version: `0.11`, //update this to current YaptideEditor version when format changes type: 'Manager', generator: 'FigureManager.toJSON' } satisfies Record; diff --git a/src/ThreeEditor/Simulation/Materials/MaterialManager.ts b/src/ThreeEditor/Simulation/Materials/MaterialManager.ts index 14e15ab61..7059d6983 100644 --- a/src/ThreeEditor/Simulation/Materials/MaterialManager.ts +++ b/src/ThreeEditor/Simulation/Materials/MaterialManager.ts @@ -25,7 +25,7 @@ export type Icru = number; export class MaterialManager extends THREE.Object3D implements SimulationPropertiesType { /****************************Private****************************/ private readonly metadata = { - version: `0.10`, //update this to current YaptideEditor version when format changes + version: `0.11`, //update this to current YaptideEditor version when format changes type: 'Manager', generator: 'MaterialManager.toJSON' } satisfies Record; diff --git a/src/ThreeEditor/Simulation/Scoring/ScoringManager.ts b/src/ThreeEditor/Simulation/Scoring/ScoringManager.ts index 3b94e464c..f254cf9ed 100644 --- a/src/ThreeEditor/Simulation/Scoring/ScoringManager.ts +++ b/src/ThreeEditor/Simulation/Scoring/ScoringManager.ts @@ -57,7 +57,7 @@ export class ScoringManager { /****************************Private****************************/ private readonly metadata = { - version: `0.10`, //update this to current YaptideEditor version when format changes + version: `0.11`, //update this to current YaptideEditor version when format changes type: 'Manager', generator: 'ScoringManager.toJSON' } satisfies Record; @@ -110,11 +110,14 @@ export class ScoringManager .filter(filter => { return filter.rules.length; }) - .reduce((acc, filter) => { - acc[filter.uuid] = `${filter.name} [${filter.id}]`; - - return acc; - }, {} as Record); + .reduce( + (acc, filter) => { + acc[filter.uuid] = `${filter.name} [${filter.id}]`; + + return acc; + }, + {} as Record + ); return options; } diff --git a/src/ThreeEditor/Simulation/SpecialComponents/SpecialComponentManager.ts b/src/ThreeEditor/Simulation/SpecialComponents/SpecialComponentManager.ts index e7d74861d..73e782b8b 100644 --- a/src/ThreeEditor/Simulation/SpecialComponents/SpecialComponentManager.ts +++ b/src/ThreeEditor/Simulation/SpecialComponents/SpecialComponentManager.ts @@ -27,7 +27,7 @@ export class SpecialComponentManager { /****************************Private****************************/ private readonly metadata = { - version: `0.10`, //update this to current YaptideEditor version when format changes + version: `0.11`, //update this to current YaptideEditor version when format changes type: 'Manager', generator: 'SpecialComponentManager.toJSON' } satisfies Record; diff --git a/src/ThreeEditor/Simulation/Zones/ZoneManager.ts b/src/ThreeEditor/Simulation/Zones/ZoneManager.ts index 9d55fa367..c85e37f75 100644 --- a/src/ThreeEditor/Simulation/Zones/ZoneManager.ts +++ b/src/ThreeEditor/Simulation/Zones/ZoneManager.ts @@ -51,7 +51,7 @@ export class ZoneManager { /****************************Private****************************/ private readonly metadata = { - version: `0.10`, //update this to current YaptideEditor version when format changes + version: `0.11`, //update this to current YaptideEditor version when format changes type: 'Manager', generator: 'ZoneManager.toJSON' } satisfies Record; @@ -131,11 +131,14 @@ export class ZoneManager } getZoneOptions(): Record { - const zoneOptions = [this.worldZone, ...this.zones].reduce((acc, zone) => { - acc[zone.uuid] = `${zone.name} [${zone.id}]`; + const zoneOptions = [this.worldZone, ...this.zones].reduce( + (acc, zone) => { + acc[zone.uuid] = `${zone.name} [${zone.id}]`; - return acc; - }, {} as Record); + return acc; + }, + {} as Record + ); return zoneOptions; } diff --git a/src/ThreeEditor/examples/README.md b/src/ThreeEditor/examples/README.md index c35fa0d51..185a12b76 100644 --- a/src/ThreeEditor/examples/README.md +++ b/src/ThreeEditor/examples/README.md @@ -36,4 +36,6 @@ The following table shows which versions can be migrated to which other versions | From version | To version | | ------------ | ---------- | | 0.7 | 0.9 | -| 0.9 | 0.10 | +| 0.9 | 0.10 | +| 0.9 | 0.10 | +| 0.10 | 0.11 | diff --git a/src/ThreeEditor/examples/ex2.json b/src/ThreeEditor/examples/ex2.json index 5d09cf2c2..f43cbcbe0 100644 --- a/src/ThreeEditor/examples/ex2.json +++ b/src/ThreeEditor/examples/ex2.json @@ -312,7 +312,7 @@ { "uuid": "334be148-f344-4156-a7c2-80c0c83bc9ea", "name": "LEAD", - "sanitized_name": "Pb", + "sanitizedName": "Pb", "icru": 82, "density": 11.35, "color": 2369903 @@ -320,14 +320,14 @@ { "uuid": "edebc384-945d-499d-86a1-a9ba93cea36e", "name": "AIR, DRY (NEAR SEA LEVEL)", - "sanitized_name": "air_dry_near_sea_level", + "sanitizedName": "air_dry_near_sea_level", "icru": 104, "density": 1 }, { "uuid": "E0986447-8AA7-4325-80D9-CEC2178494EB", "name": "GLASS, LEAD", - "sanitized_name": "glass_lead", + "sanitizedName": "glass_lead", "icru": 170, "density": 1, "color": 4063060 diff --git a/src/ThreeEditor/examples/ex3.json b/src/ThreeEditor/examples/ex3.json index ca5670580..ae8331069 100644 --- a/src/ThreeEditor/examples/ex3.json +++ b/src/ThreeEditor/examples/ex3.json @@ -276,7 +276,7 @@ { "uuid": "334be148-f344-4156-a7c2-80c0c83bc9ea", "name": "LEAD", - "sanitized_name": "Pb", + "sanitizedName": "Pb", "icru": 82, "density": 11.35, "color": 2369903 @@ -284,7 +284,7 @@ { "uuid": "edebc384-945d-499d-86a1-a9ba93cea36e", "name": "AIR, DRY (NEAR SEA LEVEL)", - "sanitized_name": "air_dry_near_sea_level", + "sanitizedName": "air_dry_near_sea_level", "icru": 104, "density": 1, "color": 6224891, @@ -294,7 +294,7 @@ { "uuid": "05ec1d44-947a-4388-afe4-3b94ae80434b", "name": "POLYMETHYL METHACRALATE (LUCITE, PERSPEX, PMMA)", - "sanitized_name": "polymethyl_methacralate_lucite_perspex_pmma", + "sanitizedName": "polymethyl_methacralate_lucite_perspex_pmma", "icru": 223, "density": 1, "color": 0 diff --git a/src/ThreeEditor/examples/ex4.json b/src/ThreeEditor/examples/ex4.json index d5eea1f5a..a056f9114 100644 --- a/src/ThreeEditor/examples/ex4.json +++ b/src/ThreeEditor/examples/ex4.json @@ -175,7 +175,7 @@ "customMaterial": { "uuid": "0c52dad9-d24b-4f3b-8ffc-c44a944e9ef6", "name": "WATER, LIQUID", - "sanitizedName": "water_liquid", + "sanitizedName": "water_liquid", "icru": 276, "density": 1, "color": 4083199, @@ -354,7 +354,7 @@ { "uuid": "be881070-c34e-4a41-83ca-2110cb0395b7", "name": "AIR, DRY (NEAR SEA LEVEL)", - "sanitized_name": "air_dry_near_sea_level", + "sanitizedName": "air_dry_near_sea_level", "icru": 104, "density": 0.00120479, "color": 9744820, @@ -364,7 +364,7 @@ { "uuid": "2cd6724d-dfae-46db-bc6c-7ec8b457d5c9", "name": "POLYMETHYL METHACRALATE (LUCITE, PERSPEX, PMMA)", - "sanitized_name": "polymethyl_methacralate_lucite_perspex_pmma", + "sanitizedName": "polymethyl_methacralate_lucite_perspex_pmma", "icru": 223, "density": 1.19, "color": 5548888 diff --git a/src/ThreeEditor/examples/ex5.json b/src/ThreeEditor/examples/ex5.json index 4d17c1b28..c09e3a5bc 100644 --- a/src/ThreeEditor/examples/ex5.json +++ b/src/ThreeEditor/examples/ex5.json @@ -371,7 +371,7 @@ { "uuid": "be881070-c34e-4a41-83ca-2110cb0395b7", "name": "AIR, DRY (NEAR SEA LEVEL)", - "sanitized_name": "air_dry_near_sea_level", + "sanitizedName": "air_dry_near_sea_level", "icru": 104, "density": 0.00120479, "color": 4063228, @@ -381,7 +381,7 @@ { "uuid": "2cd6724d-dfae-46db-bc6c-7ec8b457d5c9", "name": "POLYMETHYL METHACRALATE (LUCITE, PERSPEX, PMMA)", - "sanitized_name": "polymethyl_methacralate_lucite_perspex_pmma", + "sanitizedName": "polymethyl_methacralate_lucite_perspex_pmma", "icru": 223, "density": 1.19, "color": 4160573 diff --git a/src/ThreeEditor/examples/migrations/migrateEx0.10_0.11.js b/src/ThreeEditor/examples/migrations/migrateEx0.10_0.11.js new file mode 100644 index 000000000..c96d86f22 --- /dev/null +++ b/src/ThreeEditor/examples/migrations/migrateEx0.10_0.11.js @@ -0,0 +1,149 @@ +// Adjust this file to next migration version remember to update the version in the file name + +// can't migrate partial data, so if some data is missing migration will be aborted +const checkEmpty = (obj) => Object.entries(obj).forEach(([key,item]) => { + if(item === undefined) + throw new Error(`Missing ${key} in ${JSON.stringify(obj)}`); +}); + +// if metadata key has property version, then it's inputJson file, otherwise it's simulation job file +const checkType = (obj) => obj.metadata && obj.metadata.version ? 'inputJson' : 'simulationJob'; + +// replace whitespace with underscore and remove all non alphanumeric and non underscore characters +const sanitizationFunc = (str) => str.replace(/\s/g, '_').replace(/\W/g, ''); + +// migrate inputJson file +const migrateInputJson = (inputJson) => { + const { + zoneManager: oldZoneManager, + materialManager: oldMaterialManager, + scoringManager: oldScoringManager, + ...restInputJson + } + = inputJson; + + checkEmpty({ + zoneManager: oldZoneManager, + materialManager: oldMaterialManager, + scoringManager: oldScoringManager, + }); + + const materialManager = { + ...oldMaterialManager, + materials: oldMaterialManager.materials.map(material => ({ + ...material, + sanitizedName: sanitizationFunc(material.name) + })), + metadata: { + version: `0.11`, + type: "Manager", + generator: "MaterialManager.toJSON" + }, + } + + const zoneManager = { + ...oldZoneManager, + zones: oldZoneManager.zones.map(zone => ({ + ...zone, + ...("customMaterial" in zone ? {customMaterial: { + ...zone.customMaterial, + sanitizedName: sanitizationFunc(zone.customMaterial.name) + }} : {}) + })), + worldZone: { + ...oldZoneManager.worldZone, + ...("customMaterial" in oldZoneManager.worldZone ? {customMaterial: { + ...oldZoneManager.worldZone.customMaterial, + sanitizedName: sanitizationFunc(oldZoneManager.worldZone.customMaterial.name) + }} : {}) + }, + metadata: { + version: `0.11`, + type: "Manager", + generator: "ZoneManager.toJSON" + }, + } + + const scoringManager = { + ...oldScoringManager, + outputs: oldScoringManager.outputs.map(output => ({ + ...output, + quantities: output.quantities.map(quantity => ({ + ...quantity, + ...("customMaterial" in quantity ? {customMaterial: { + ...quantity.customMaterial, + sanitizedName: sanitizationFunc(quantity.customMaterial.name) + }} : {}) + })) + })), + metadata: { + version: "0.11", + type: "Manager", + generator: "ScoringManager.toJSON" + } + } + + return { + ...restInputJson, + zoneManager, + materialManager, + scoringManager, + metadata: { + version: `0.11`, + type: "Editor", + generator: "YaptideEditor.toJSON" + } + } +} + +// migrate simulation job file +const migrateSimulationJob = (fileJson) => { + const { input, + ...restJson } = fileJson; + + checkEmpty({input}); + const { inputJson,...restInput } = input; + + return { + input: { + inputJson: migrateInputJson(inputJson), + ...restInput + }, + ...restJson + } +} + + +const path = require('path'); +const fs = require('fs'); + +const fileNameToMigrate = process.argv[2]; + +const pathToMigrate = path.resolve(process.cwd(), fileNameToMigrate); +const fileJson = require(pathToMigrate); + +// create temp dir if not exists +if (!fs.existsSync('./migrations/temp')) + fs.mkdirSync('./migrations/temp'); + +fs.writeFileSync(`./migrations/temp/${Date.now()}_${fileNameToMigrate}`, JSON.stringify(fileJson, null, 2)); + +const type = checkType(fileJson); +let newJson; + +switch (type) { + case 'inputJson': + newJson = migrateInputJson(fileJson); + + break; + case 'simulationJob': + newJson = migrateSimulationJob(fileJson); + + break; + default: + throw new Error(`Unknown type ${type}`); +} + +fs.writeFileSync(pathToMigrate, JSON.stringify(newJson, null, 2)); + + diff --git a/src/ThreeEditor/js/YaptideEditor.js b/src/ThreeEditor/js/YaptideEditor.js index 73eaa75c2..436f3ce38 100644 --- a/src/ThreeEditor/js/YaptideEditor.js +++ b/src/ThreeEditor/js/YaptideEditor.js @@ -22,7 +22,7 @@ _DEFAULT_CAMERA.name = 'Camera'; _DEFAULT_CAMERA.position.set(0, 5, 10); _DEFAULT_CAMERA.lookAt(new THREE.Vector3()); -export const JSON_VERSION = `0.10`; +export const JSON_VERSION = `0.11`; export function YaptideEditor(container) { this.signals = {