Skip to content

Commit

Permalink
Add json migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Derstilon committed Oct 8, 2023
1 parent 1c5bcb3 commit 1765807
Show file tree
Hide file tree
Showing 13 changed files with 193 additions and 33 deletions.
15 changes: 9 additions & 6 deletions src/ThreeEditor/Simulation/Detectors/DetectorManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string | number>;
Expand Down Expand Up @@ -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<string, string>);
.reduce(
(acc, geometry) => {
acc[geometry.uuid] = `${geometry.name} [${geometry.id}]`;

return acc;
},
{} as Record<string, string>
);

return options;
}
Expand Down
2 changes: 1 addition & 1 deletion src/ThreeEditor/Simulation/Figures/FigureManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string | number>;
Expand Down
2 changes: 1 addition & 1 deletion src/ThreeEditor/Simulation/Materials/MaterialManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string | number>;
Expand Down
15 changes: 9 additions & 6 deletions src/ThreeEditor/Simulation/Scoring/ScoringManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string | number>;
Expand Down Expand Up @@ -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<string, string>);
.reduce(
(acc, filter) => {
acc[filter.uuid] = `${filter.name} [${filter.id}]`;

return acc;
},
{} as Record<string, string>
);

return options;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string | number>;
Expand Down
13 changes: 8 additions & 5 deletions src/ThreeEditor/Simulation/Zones/ZoneManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string | number>;
Expand Down Expand Up @@ -131,11 +131,14 @@ export class ZoneManager
}

getZoneOptions(): Record<string, string> {
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<string, string>);
return acc;
},
{} as Record<string, string>
);

return zoneOptions;
}
Expand Down
4 changes: 3 additions & 1 deletion src/ThreeEditor/examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 |
6 changes: 3 additions & 3 deletions src/ThreeEditor/examples/ex2.json
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,22 @@
{
"uuid": "334be148-f344-4156-a7c2-80c0c83bc9ea",
"name": "LEAD",
"sanitized_name": "Pb",
"sanitizedName": "Pb",
"icru": 82,
"density": 11.35,
"color": 2369903
},
{
"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
Expand Down
6 changes: 3 additions & 3 deletions src/ThreeEditor/examples/ex3.json
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,15 @@
{
"uuid": "334be148-f344-4156-a7c2-80c0c83bc9ea",
"name": "LEAD",
"sanitized_name": "Pb",
"sanitizedName": "Pb",
"icru": 82,
"density": 11.35,
"color": 2369903
},
{
"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,
Expand All @@ -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
Expand Down
6 changes: 3 additions & 3 deletions src/ThreeEditor/examples/ex4.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/ThreeEditor/examples/ex5.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
149 changes: 149 additions & 0 deletions src/ThreeEditor/examples/migrations/migrateEx0.10_0.11.js
Original file line number Diff line number Diff line change
@@ -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));


2 changes: 1 addition & 1 deletion src/ThreeEditor/js/YaptideEditor.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down

0 comments on commit 1765807

Please sign in to comment.