Skip to content

Commit

Permalink
improve parsing for stratified parameters (#4930)
Browse files Browse the repository at this point in the history
Co-authored-by: Yohann Paris <[email protected]>
  • Loading branch information
mwdchang and YohannParis authored Sep 27, 2024
1 parent 823cdfd commit 4c53dd9
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
43 changes: 36 additions & 7 deletions packages/client/hmi-client/src/model-representation/mira/mira.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,9 @@ export const emptyMiraModel = () => {
/**
* Collection of MMT related functions
* */
export const getContextKeys = (miraModel: MiraModel) => {
export const getContext = (miraModel: MiraModel) => {
const modifierKeys = new Set<string>();
const modifierValues = new Set<string>();

// Heuristics to avoid picking up wrong stuff
// - if the modifier value starts with 'ncit:' then it is not a user initiated stratification
Expand All @@ -45,6 +46,7 @@ export const getContextKeys = (miraModel: MiraModel) => {
const addWithGuard = (k: string, v: string) => {
if (v.startsWith('ncit:')) return;
modifierKeys.add(k);
modifierValues.add(v);
};

miraModel.templates.forEach((t) => {
Expand All @@ -65,20 +67,21 @@ export const getContextKeys = (miraModel: MiraModel) => {
}
if (t.controllers && t.controllers.length > 0) {
t.controllers.forEach((miraConcept) => {
// Object.keys(miraConcept.context).forEach((key) => modifierKeys.add(key));
Object.entries(miraConcept.context).forEach(([k, v]) => {
addWithGuard(k, v as string);
});
});
}
});

const modifiers = [...modifierKeys];
return modifiers;
return {
keys: [...modifierKeys],
values: [...modifierValues]
};
};

export const isStratifiedModel = (miraModel: MiraModel) => {
const keys = getContextKeys(miraModel);
const keys = getContext(miraModel).keys;
return keys.length > 0;
};

Expand All @@ -93,6 +96,7 @@ export const isStratifiedModel = (miraModel: MiraModel) => {
export const collapseParameters = (miraModel: MiraModel, miraTemplateParams: MiraTemplateParams) => {
const map = new Map<string, string[]>();
const keys = Object.keys(miraModel.parameters);
const contextValues = getContext(miraModel).values;

for (let i = 0; i < keys.length; i++) {
const key = keys[i];
Expand All @@ -107,14 +111,27 @@ export const collapseParameters = (miraModel: MiraModel, miraTemplateParams: Mir
continue;
}

const nameContainsContext = _.intersection(tokens, contextValues).length > 0;
const hasNumericSuffixes =
tokens.length > 1 &&
tokens.every((d, idx) => {
if (idx === 0) return true;
return !Number.isNaN(+d);
});

if (!nameContainsContext && !hasNumericSuffixes) {
map.set(name, [name]);
continue;
}

if (map.has(rootName)) {
map.get(rootName)?.push(name);
} else {
map.set(rootName, [name]);
}
}

const mapKeys = [...map.keys()];
let mapKeys = [...map.keys()];
const templateParams = Object.values(miraTemplateParams);

/**
Expand All @@ -135,6 +152,18 @@ export const collapseParameters = (miraModel: MiraModel, miraTemplateParams: Mir
});
}
}

// when a key only has one child, we will rename the key of the root to the child.
// this is to avoid renaming when a single entry key has an underscore
mapKeys = [...map.keys()];
[...map.keys()].forEach((key) => {
const newKey = map.get(key)?.[0];
if (newKey && map.get(key)?.length === 1 && newKey !== key) {
map.set(newKey, [newKey]);
map.delete(key);
}
});

return map;
};

Expand Down Expand Up @@ -203,7 +232,7 @@ export const rawTemplatesSummary = (miraModel: MiraModel) => {
export const collapseTemplates = (miraModel: MiraModel) => {
const allTemplates: TemplateSummary[] = [];
const uniqueTemplates: TemplateSummary[] = [];
const scrubbingKeys = getContextKeys(miraModel);
const scrubbingKeys = getContext(miraModel).keys;

// 1. Roll back to "original name" by trimming off modifiers
miraModel.templates.forEach((t) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { getModelType } from '@/services/model';
import { AMRSchemaNames } from '@/types/common';
import { parseCurie } from '@/services/concept';
import { NestedPetrinetRenderer } from './petrinet/nested-petrinet-renderer';
import { isStratifiedModel, getContextKeys, collapseTemplates } from './mira/mira';
import { isStratifiedModel, getContext, collapseTemplates } from './mira/mira';
import { extractTemplateMatrix } from './mira/mira-util';

export const getVariable = (miraModel: MiraModel, variableName: string) => {
Expand Down Expand Up @@ -63,7 +63,7 @@ export const getModelRenderer = (
processedSet.add(conceptName);
});
});
const dims = getContextKeys(miraModel);
const dims = getContext(miraModel).keys;
dims.unshift('base');

const { matrixMap } = collapseTemplates(miraModel);
Expand Down

0 comments on commit 4c53dd9

Please sign in to comment.