Skip to content

Commit

Permalink
Sanitize Data Model
Browse files Browse the repository at this point in the history
Closes #1
  • Loading branch information
LeonBein committed Aug 27, 2024
1 parent 7e30b89 commit 2ea164c
Show file tree
Hide file tree
Showing 12 changed files with 1,286 additions and 199 deletions.
43 changes: 5 additions & 38 deletions dataModel/SimulationModelDescriptor.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ export const SimulationModelDescriptor = {
{ "name": "resources", "type": "Resource", isMany : true },
{ "name": "timeTables", "type": "Timetable", isMany : true },
{ "name": "costDrivers", "type": "AbstractCostDriver", isMany : true },
{ "name": "environmentMappingConfig", "type": "EnvironmentMappingConfig" }
{ "name": "variants", "type": "Variant", isMany : true }
]
},

Expand Down Expand Up @@ -119,54 +119,22 @@ export const SimulationModelDescriptor = {
{ "name": "cost", "type": "Real"},
]
},

{
"name" : "EnvironmentMappingConfig",
"properties": [
{ "name": "variants", "type": "VariantExtended", isMany : true },
]
},
{
"name" : "VariantExtended",
"name" : "Variant",
"properties": [
{ "name": "id", "type": "String"},
{ "name": "name", "type": "String"},
{ "name": "frequency", "type": "Real"},
{ "name": "mappings", "type": "DriversMapping", isMany : true }
{ "name": "mappings", "type": "DriverConcretization", isMany : true }
]
},
{
"name" : "DriversMapping",
"name" : "DriverConcretization",
"properties": [
{ "name": "abstractDriver", "type": "String"},
{ "name": "concreteDriver", "type": "String"},
]
},

{
"name" : "CostVariantConfig",
"properties": [
{ "name": "count", "type": "String"},
{ "name": "variants", "type": "Variant", isMany : true }
]
},
{
"name" : "Variant",
"properties": [
{ "name": "id", "type": "String"},
{ "name": "name", "type": "String"},
{ "name": "frequency", "type": "String"},
{ "name": "drivers", "type": "DriverConcretization", isMany : true }
]
},
{
"name" : "DriverConcretization",
"properties": [
{ "name": "abstractId", "type": "String"},
{ "name": "concreteId", "type": "String"}
]
},

{
"name": "Model",
"properties": [
Expand All @@ -181,8 +149,7 @@ export const SimulationModelDescriptor = {
"properties": [
{ "name": "activities", "type": "Activity", "isMany": true },
{ "name": "events", "type": "Event", "isMany": true },
{ "name": "gateways", "type": "Gateway", "isMany": true },
{ "name": "costVariantConfig", "type": "CostVariantConfig" }
{ "name": "gateways", "type": "Gateway", "isMany": true }
]
},

Expand Down
1,205 changes: 1,204 additions & 1 deletion demo/tum_shop_simubridge.json

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions frontend/cypress/e2e/application.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,10 @@ describe('LCA Configuration Tests', () => {
cy.findByText('Variant saved').should('exist');
cy.findByRole('textbox', { placeholder: /Variant Name/i }).should('have.value', '');
cy.get('input[id="variantFrequencyInput"').should('have.value', '15%');
cy.visit('http://localhost:3000/'); // Click away to ensure data is actually saved and not just displayed
cy.visit('http://localhost:3000/lcavariants');
cy.get('button.chakra-accordion__button').should('have.length', 1);
cy.get('button.chakra-accordion__button').findByText(newVariantName).should('exist');
});
});
});
104 changes: 50 additions & 54 deletions frontend/cypress/e2e/configFromScenarioTest.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -954,27 +954,25 @@ const scenarioDataWithCostVariants =
]
}
],
"costVariantConfig": {
"count": "100",
"variants": [
{
"id": "Shipment and delivery over distance A",
"frequency": "0.2",
"drivers": [
{
"id": "Delivery",
"cost": "0.00002843"
},
{
"id": "Filling Material",
"cost": "0.00001468"
"variants": [
{
"id" : "1",
"name": "Shipment and delivery over distance A",
"frequency": "0.2",
"mappings": [ // TODO mappings are not done cost based anymore
{
"id": "Delivery",
"cost": "0.00002843"
},
{
"id": "Filling Material",
"cost": "0.00001468"

}
}

]
}
]
}
]
}
]
},
"models": [
{
Expand Down Expand Up @@ -1537,43 +1535,41 @@ const logisticsScenario = {
},

],
"costVariantConfig": {
"count": "100",
"variants": [
{
"id": "Shipment and delivery over distance A",
"frequency": "0.2",
"drivers": [
{
"id": "Delivery",
"cost": "0.00002843"
},
{
"id": "Filling Material",
"cost": "0.00001468"
},
{
"id": "Packaging Material",
"cost": "0.00003806"
},
{
"id": "Re-Routing",
"cost": "0.000008529"
},
{
"id": "Receipt",
"cost": "0.00001153"
},
{
"id": "Shipment",
"cost": "0.00007839"
},
"variants": [
{
"id" : "2",
"name": "Shipment and delivery over distance A",
"frequency": "0.2",
"mappings": [ // TODO mappings are not done cost based anymore
{
"id": "Delivery",
"cost": "0.00002843"
},
{
"id": "Filling Material",
"cost": "0.00001468"
},
{
"id": "Packaging Material",
"cost": "0.00003806"
},
{
"id": "Re-Routing",
"cost": "0.000008529"
},
{
"id": "Receipt",
"cost": "0.00001153"
},
{
"id": "Shipment",
"cost": "0.00007839"
},


]
}
]
}
]
}
]
},
"models": [
{
Expand Down
15 changes: 4 additions & 11 deletions frontend/cypress/fixtures/lca/lcaTestScenario.json
Original file line number Diff line number Diff line change
Expand Up @@ -1097,11 +1097,9 @@
]
}
],
"environmentMappingConfig":{
"variants":[

]
}
"variants":[

]
},
"models":[
{
Expand Down Expand Up @@ -1638,12 +1636,7 @@
]
}
}
],
"costVariantConfig":{
"variants":[

]
}
]
}
}
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,10 @@ import DistributionEditor from '../../DistributionEditor';
import AbstractModelElementEditor from './AbstractModelElementEditor';

import { distributionToState, stateToDistribution } from '../../../util/Distributions';
import { saveCostVariantConfig } from '../../Lca/Logic/LcaDataManager';


const Activity = ({ getData, currentElement, setCurrentRightSideBar }) => {
const [allAbstractCostDrivers, setAllAbstractCostDrivers] = useState([]);
const [allAbstractCostDrivers, setAllAbstractCostDrivers] = useState([]); //TODO this shouldn't be a state
const reactDomNavigator = useNavigate();

//init
Expand Down Expand Up @@ -83,13 +82,11 @@ const Activity = ({ getData, currentElement, setCurrentRightSideBar }) => {

const removeAbstractCostDriver = (index) => {
setAbstractCostDrivers(activityConfiguration.costDrivers.filter((value, localIndex) => localIndex !== index));
saveCostVariantConfig(getData, allAbstractCostDrivers);
}

const handleAbstractCostDrivers = (index, value) => {
activityConfiguration.costDrivers[index] = value;
setAbstractCostDrivers(activityConfiguration.costDrivers.filter(abstractCostDriver => abstractCostDriver));
saveCostVariantConfig(getData, allAbstractCostDrivers);
}

return <AbstractModelElementEditor {...{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ function LcaVariantsConfiguration({ getData, toasting }) {

setVariants(updatedVariants);

await lcaDm.saveCostVariant(allCostDrivers, variant, updatedVariants, getData, toasting);
await lcaDm.saveCostVariantConfig(getData, allCostDrivers);
await lcaDm.saveCostVariant(variant, updatedVariants, getData, toasting);

setCurrentVariant({ name: '', mappings: [], frequency: 15 });
toasting("success", "Variant saved", "Cost variant saved successfully");
Expand All @@ -66,7 +65,6 @@ function LcaVariantsConfiguration({ getData, toasting }) {
}

await lcaDm.deleteVariantFromConfiguration(variantId, getData);
await lcaDm.deleteVariantFromCostVariantConfig(variantId, getData);

toasting("info", "Variant deleted", "Cost variant deleted successfully");
};
Expand Down
82 changes: 9 additions & 73 deletions frontend/src/components/Lca/Logic/LcaDataManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,8 @@ export const getCostDriversFromScenario = (getData) => {
export const getVariants = (getData) => {
const scenario = getData().getCurrentScenario();

if (scenario) {
if (scenario.resourceParameters.environmentMappingConfig && scenario.resourceParameters.environmentMappingConfig.variants) {
return scenario.resourceParameters.environmentMappingConfig.variants;
}
if (scenario && scenario.resourceParameters.variants) {
return scenario.resourceParameters.variants;
}
return [];
};
Expand Down Expand Up @@ -53,90 +51,28 @@ export const saveAllCostDrivers = async (abstractCostDrivers, getData) => {
await getData().saveCurrentScenario();
};

export const saveCostVariant = async (allCostDrivers, variant,
updatedVariants, getData) => {
export const saveCostVariant = async (variant, updatedVariants, getData) => {
//save variants and its mappings
let driversMappings = variant.mappings.map(mapping => {
return SimulationModelModdle.getInstance().create("simulationmodel:DriversMapping", {
return SimulationModelModdle.getInstance().create("simulationmodel:DriverConcretization", {
abstractDriver: mapping.abstractDriver,
concreteDriver: mapping.concreteDriver,
});
});

let variantExtended = SimulationModelModdle.getInstance().create("simulationmodel:VariantExtended", {
let updatedVariant = SimulationModelModdle.getInstance().create("simulationmodel:Variant", {
id: variant.id,
name: variant.name,
frequency: variant.frequency,
mappings: driversMappings,
});
let updatedVariantsObject = [...updatedVariants.filter(v => v.id !== variant.id), variantExtended];
const environmentMappingConfig =
SimulationModelModdle.getInstance().create("simulationmodel:EnvironmentMappingConfig", {
variants: updatedVariantsObject,
});
getData().getCurrentScenario().resourceParameters.environmentMappingConfig = environmentMappingConfig;
await getData().saveCurrentScenario();

saveCostVariantConfig(getData, allCostDrivers);
let updatedVariantsObject = [...updatedVariants.filter(v => v.id !== variant.id), updatedVariant];
getData().getCurrentScenario().resourceParameters.variants = updatedVariantsObject;
await getData().saveCurrentScenario();
};

export const saveCostVariantConfig = async (getData, allCostDrivers) => {
const scenario = getData().getCurrentScenario();

if (!scenario) {
return;
}

let formattedVariants = [];
const variants = scenario.resourceParameters.environmentMappingConfig.variants;
variants.forEach(v => {
let drivers = [];
v.mappings.forEach(m => {
const concreteDriver = allCostDrivers
.flatMap(driver => driver.concreteCostDrivers)
.find(driver => driver.id === m.concreteDriver);
const driver = SimulationModelModdle.getInstance().create("simulationmodel:DriverConcretization", {
abstractId: m.abstractDriver,
concreteId: concreteDriver.name //TODO recheck whole usage of names vs. ids
});
drivers.push(driver);
});

console.log('Drivers:', drivers);
let costVariant = SimulationModelModdle.getInstance().create("simulationmodel:Variant", {
id: v.name,
frequency: v.frequency,
drivers: drivers,
});
formattedVariants.push(costVariant);
});

let costVariantConfig = SimulationModelModdle.getInstance().create("simulationmodel:CostVariantConfig", {
count: formattedVariants.length,
variants: formattedVariants,
});

getData().getCurrentScenario().models[0].modelParameter.costVariantConfig = costVariantConfig;
await getData().saveCurrentScenario();
}

export const deleteVariantFromConfiguration = async (variantId, getData) => {
const environmentMappingConfig = SimulationModelModdle.getInstance().create("simulationmodel:EnvironmentMappingConfig", {
variants: getData().getCurrentScenario()
.resourceParameters.environmentMappingConfig.variants.filter(v => v.id !== variantId),
});
getData().getCurrentScenario().resourceParameters.environmentMappingConfig = environmentMappingConfig;
await getData().saveCurrentScenario();
};

export const deleteVariantFromCostVariantConfig = async (variantId, getData) => {
let costVariantConfig = getData().getCurrentScenario().models[0].modelParameter.costVariantConfig;
const updatedCostVariantConfig = { ...costVariantConfig };

updatedCostVariantConfig.variants = updatedCostVariantConfig.variants.filter(v => v.id !== variantId);
updatedCostVariantConfig.count = updatedCostVariantConfig.variants.length;

getData().getCurrentScenario().models[0].modelParameter.costVariantConfig = updatedCostVariantConfig;
getData().getCurrentScenario().resourceParameters.variants = getData().getCurrentScenario().resourceParameters.variants.filter(v => v.id !== variantId);
await getData().saveCurrentScenario();
}
};
Loading

0 comments on commit 2ea164c

Please sign in to comment.