Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

model function for getting and updating values #3314

Merged
merged 3 commits into from
Apr 15, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
<InputText
v-model.lazy="data.name"
:disabled="configView || readonly || data.type === ParamType.MATRIX"
@update:model-value="updateMetadata(data.value.target, 'name', $event)"
@update:model-value="updateMetadataFromInput(data.value.target, 'name', $event)"
/>
</template>
</Column>
Expand All @@ -37,7 +37,9 @@
<InputText
v-model.lazy="slotProps.data.description"
:disabled="configView || readonly || slotProps.data.type === ParamType.MATRIX"
@update:model-value="updateMetadata(slotProps.data.value.target, 'description', $event)"
@update:model-value="
updateMetadataFromInput(slotProps.data.value.target, 'description', $event)
"
/>
</template>
</Column>
Expand Down Expand Up @@ -70,7 +72,7 @@
:suggestions="curies"
@complete="onSearch"
@item-select="
updateMetadata(data.value.target, 'concept', {
updateMetadataFromInput(data.value.target, 'concept', {
grounding: { identifiers: parseCurie($event.value.curie) }
})
"
Expand All @@ -88,7 +90,9 @@
class="w-full"
:disabled="readonly"
v-model.lazy="slotProps.data.unit"
@update:model-value="(val) => updateMetadata(slotProps.data.value.target, 'unit', val)"
@update:model-value="
(val) => updateMetadataFromInput(slotProps.data.value.target, 'unit', val)
"
/>
<template v-else>--</template>
</template>
Expand Down Expand Up @@ -170,7 +174,7 @@
class="w-full"
v-model.lazy="data.source"
:disabled="readonly"
@update:model-value="(val) => updateMetadata(data.value.target, 'source', val)"
@update:model-value="(val) => updateMetadataFromInput(data.value.target, 'source', val)"
/>
</template>
</Column>
Expand Down Expand Up @@ -234,7 +238,12 @@ import { getUnstratifiedInitials } from '@/model-representation/petrinet/mira-pe
import { MiraModel, MiraTemplateParams } from '@/model-representation/mira/mira-common';
import { isStratifiedModel } from '@/model-representation/mira/mira';
import { matrixEffect } from '@/utils/easter-eggs';
import { updateVariable } from '@/model-representation/service';
import {
getMetadata,
getModelInitials,
updateVariable,
updateMetadata
} from '@/model-representation/service';
import AutoComplete, { AutoCompleteCompleteEvent } from 'primevue/autocomplete';

const typeOptions = [
Expand Down Expand Up @@ -289,9 +298,8 @@ const tableFormattedInitials = computed<ModelConfigTableData[]>(() => {
if (isStratified.value) {
initials.value.forEach((vals, init) => {
const tableFormattedMatrix: ModelConfigTableData[] = vals.map((v) => {
const initial = model.semantics?.ode.initials?.find((i) => i.target === v);

const initialsMetadata = model.metadata?.initials?.[initial!.target];
const initial = getModelInitials(model).find((i) => i.target === v);
const initialsMetadata = getMetadata(model, 'initials', initial!.target);
const sourceValue = initialsMetadata?.source;
const unitValue = initialsMetadata?.unit;
const nameValue = initialsMetadata?.name;
Expand Down Expand Up @@ -324,8 +332,8 @@ const tableFormattedInitials = computed<ModelConfigTableData[]>(() => {
});
} else {
initials.value.forEach((vals, init) => {
const initial = model.semantics?.ode.initials?.find((i) => i.target === vals[0]);
const initialsMetadata = model.metadata?.initials?.[initial!.target];
const initial = getModelInitials(model).find((i) => i.target === vals[0]);
const initialsMetadata = getMetadata(model, 'initials', initial!.target);
const sourceValue = initialsMetadata?.source;
const unitValue = initialsMetadata?.unit;
const nameValue = initialsMetadata?.name;
Expand Down Expand Up @@ -368,15 +376,10 @@ const updateCellValue = (v: any) => {
emit('update-model', clone);
};

const updateMetadata = (id: string, key: string, value: any) => {
const clonedModel = cloneDeep(props.model);
if (!clonedModel.metadata?.initials?.[id]) {
clonedModel.metadata ??= {};
clonedModel.metadata.initials ??= {};
clonedModel.metadata.initials[id] ??= {};
}
clonedModel.metadata.initials[id][key] = value;
emit('update-model', clonedModel);
const updateMetadataFromInput = (id: string, key: string, value: any) => {
const clone = cloneDeep(props.model);
updateMetadata(clone, id, 'initials', key, value);
emit('update-model', clone);
};

const isStratified = computed(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@
class="w-full"
v-model.lazy="data.source"
:disabled="readonly"
@update:model-value="(val) => updateSource(data.value.id ?? data.value.target, val)"
@update:model-value="(val) => updateMetadataFromInput(data.value.id, 'source', val)"
/>
</template>
</Column>
Expand Down Expand Up @@ -402,7 +402,14 @@ import { getModelType } from '@/services/model';
import { matrixEffect } from '@/utils/easter-eggs';
import { MiraModel, MiraTemplateParams } from '@/model-representation/mira/mira-common';
import { isStratifiedModel, collapseParameters } from '@/model-representation/mira/mira';
import { updateVariable } from '@/model-representation/service';
import {
updateVariable,
updateMetadata,
validateTimeSeries,
getTimeseries,
getMetadata,
getModelParameters
} from '@/model-representation/service';
import TeraModal from '@/components/widgets/tera-modal.vue';
import TeraInputNumber from '@/components/widgets/tera-input-number.vue';

Expand Down Expand Up @@ -436,27 +443,15 @@ const selectedValue = ref<SuggestedValue | null>(null);
const suggestedValues = computed(() => {
const matchingParameters: SuggestedValue[] = [];
props.modelConfigurations?.forEach((configuration, i) => {
if (modelType.value !== AMRSchemaNames.REGNET) {
configuration.configuration?.semantics?.ode.parameters?.forEach((parameter) => {
if (parameter.id === suggestedValuesModalContext.value.id) {
matchingParameters.push({
parameter,
configuration,
index: i
});
}
});
} else {
configuration.configuration?.model?.parameters?.forEach((parameter) => {
if (parameter.id === suggestedValuesModalContext.value.id) {
matchingParameters.push({
parameter,
configuration,
index: i
});
}
});
}
getModelParameters(configuration.configuration).forEach((parameter) => {
if (parameter.id === suggestedValuesModalContext.value.id) {
matchingParameters.push({
parameter,
configuration,
index: i
});
}
});
});

return matchingParameters;
Expand Down Expand Up @@ -510,23 +505,18 @@ const buildParameterTable = () => {
if (isStratified.value) {
parameters.value.forEach((vals, init) => {
const tableFormattedMatrix: ModelConfigTableData[] = vals.map((v) => {
let param;
if (modelType.value === AMRSchemaNames.REGNET) {
param = model.model.parameters.find((i) => i.id === v);
} else {
param = model.semantics?.ode?.parameters?.find((i) => i.id === v);
}
const param = getModelParameters(model).find((i) => i.id === v);
const paramType = getParamType(param);
const timeseriesValue = model.metadata?.timeseries?.[param!.id];
const parametersMetadata = model.metadata?.parameters?.[param!.id];
const timeseriesValue = getTimeseries(props.model, param!.id);
const parametersMetadata = getMetadata(props.model, 'parameters', param!.id);
const sourceValue = parametersMetadata?.source;
return {
id: v,
name: param.name,
name: param?.name ?? '',
type: paramType,
description: param.description,
concept: param.grounding,
unit: param.unit?.expression,
description: param?.description ?? '',
concept: param?.grounding ?? { identifiers: {} },
unit: param?.unit?.expression ?? '',
value: param,
source: sourceValue,
visibility: false,
Expand All @@ -547,24 +537,19 @@ const buildParameterTable = () => {
});
} else {
parameters.value.forEach((vals, init) => {
let param;
if (modelType.value === AMRSchemaNames.REGNET) {
param = model.model.parameters.find((i) => i.id === vals[0]);
} else {
param = model.semantics?.ode.parameters?.find((i) => i.id === vals[0]);
}

const param = getModelParameters(model).find((i) => i.id === vals[0]);
if (!param) return;
const paramType = getParamType(param);

const timeseriesValue = model.metadata?.timeseries?.[param!.id];
const parametersMetadata = model.metadata?.parameters?.[param!.id];
const timeseriesValue = getTimeseries(props.model, param.id);
const parametersMetadata = getMetadata(props.model, 'parameters', param.id);
const sourceValue = parametersMetadata?.source;
formattedParams.push({
id: init,
name: param.name,
name: param.name ?? '',
type: paramType,
description: param.description,
concept: param.grounding,
description: param.description ?? '',
concept: param.grounding ?? { identifiers: {} },
unit: param.unit?.expression,
value: param,
source: sourceValue,
Expand Down Expand Up @@ -658,30 +643,12 @@ const updateTimeseries = (id: string, value: string) => {
emit('update-model', clonedModel);
};

const updateSource = (id: string, value: string) => {
const updateMetadataFromInput = (id: string, key: string, value: any) => {
const clonedModel = cloneDeep(props.model);
if (!clonedModel.metadata?.parameters?.[id]) {
clonedModel.metadata ??= {};
clonedModel.metadata.parameters ??= {};
clonedModel.metadata.parameters[id] = {};
}
clonedModel.metadata.parameters[id].source = value;
updateMetadata(clonedModel, id, 'parameters', key, value);
emit('update-model', clonedModel);
};

const validateTimeSeries = (values: string) => {
const message = 'Incorrect format (e.g., 0:500)';
if (typeof values !== 'string') {
errorMessage.value = message;
return false;
}

const isPairValid = (pair: string): boolean => /^\d+:\d+(\.\d+)?$/.test(pair.trim());
const isValid = values.split(',').every(isPairValid);
errorMessage.value = isValid ? '' : message;
return isValid;
};

async function onSearch(event: AutoCompleteCompleteEvent) {
const query = event.query;
if (query.length > 2) {
Expand Down Expand Up @@ -744,13 +711,9 @@ const applySelectedValue = () => {
};

const countSuggestions = (id): number =>
props.modelConfigurations?.filter((configuration) => {
if (modelType.value !== AMRSchemaNames.REGNET) {
return configuration.configuration?.semantics?.ode.parameters?.find((p) => p.id === id);
}

return configuration.configuration?.model?.parameters?.find((p) => p.id === id);
}).length ?? 0;
props.modelConfigurations?.filter((configuration) =>
getModelParameters(configuration.configuration).find((p) => p.id === id)
).length ?? 0;

watch(
() => parameters.value,
Expand Down
Loading
Loading