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

feat: pivot/matrix improvement for Catlab stratify #1601

Merged
merged 9 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -79,7 +79,7 @@ import {
extractStateMatrixData,
extractTransitionMatrixData
} from '@/model-representation/petrinet/petrinet-service';
import { createStateMatrix, createTransitionMatrix } from '@/utils/pivot';
import { createMatrix1D, createMatrix2D } from '@/utils/pivot';
import Dropdown from 'primevue/dropdown';
import { Initial, ModelConfiguration, ModelParameter, Rate } from '@/types/Types';
import { NodeType } from '@/model-representation/petrinet/petrinet-renderer';
Expand Down Expand Up @@ -198,8 +198,8 @@ function configureMatrix() {

const matrixAttributes =
props.nodeType === NodeType.State
? createStateMatrix(matrixData)
: createTransitionMatrix(matrixData, colDimensions, rowDimensions);
? createMatrix1D(matrixData)
: createMatrix2D(matrixData, colDimensions, rowDimensions);

matrix.value = matrixAttributes.matrix;
chosenCol.value = colDimensions[0];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,6 @@ export const isStratifiedAMR = (amr: Model) => {
return false;
};

// Returns a 1xN matrix describing state's initials
export const extractMapping = (amr: Model, id: string) => {
const typeMapList = amr.semantics?.typing?.map as [string, string][];
const item = typeMapList.find((d) => d[0] === id);
Expand Down Expand Up @@ -664,6 +663,88 @@ export const extractStateMatrixData = (amr: Model, stateIds: string[], dimension
return results;
};

const MAX_DEPTH = 10;

/**
* Given an identifier string id, recursiviely lookup mapping information
* to find out where the id originated. At eash iteration record the term
* and type/model that was added.
*/
export const getCatlabStratasDataPoint = (amr: Model, id: string) => {
let span: any = amr.semantics?.span;
let key = id;
let c = 0;
const result: any = {};

// Recursively crawl up the strata provenance chain
while (c < MAX_DEPTH) {
c++;
// Name of strata dimension, Catlab doesn't actually track it
// so the cloest thing is the strata model name
const dimension = span[1].system.name;

// The strata
// eslint-disable-next-line
const term = span[1].map.find((d: any) => d[0] === key)[1];
result[dimension] = term;

// eslint-disable-next-line
key = span[0].map.find((d: any) => d[0] === key)[1];
span = span[0].system.semantics.span;
if (!span) {
result.id = id;
result['@base'] = key;
break;
}
}
return result;
};

export const getCatlabStatesMatrixData = (amr: Model) => {
const stateIds = amr.model.states.map((d: any) => d.id);
const results: any[] = [];
for (let i = 0; i < stateIds.length; i++) {
const result = getCatlabStratasDataPoint(amr as any, stateIds[i]);
results.push(result);
}
return results;
};

export const getCatlabTransitionsMatrixData = (amr: Model) => {
const transitions = amr.model.transitions as PetriNetTransition[];
const results: any[] = [];
for (let i = 0; i < transitions.length; i++) {
const transition = transitions[i];
let result: any = {};

// Scan both inut and outut
transition.input.forEach((stateId: string) => {
result = Object.assign(result, getCatlabStratasDataPoint(amr as any, stateId));
});
transition.output.forEach((stateId: string) => {
result = Object.assign(result, getCatlabStratasDataPoint(amr as any, stateId));
});

// Build the @base/id for a transition
let c = 0;
let key = transition.id;
let span: any = amr.semantics?.span;
while (c < MAX_DEPTH) {
c++;
// eslint-disable-next-line
key = span[0].map.find((d: any) => d[0] === key)[1];
span = span[0].system.semantics.span;
if (!span) {
result['@base'] = key;
result.id = transition.id;
break;
}
}
results.push(result);
}
return results;
};

export function newAMR(modelName: string) {
const amr: Model = {
id: '',
Expand Down
11 changes: 4 additions & 7 deletions packages/client/hmi-client/src/utils/pivot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ const pivotAxes = (data: any[], rowDimensions: string[], colDimensions: string[]
}
}
});
return { colAxis, rowAxis };
return { colAxis, rowAxis, termsMap: cardinality };
};

// Creates a M x 1 matrix where
// M = cardinality(rowDimensions[0]) * cardinality(rowDimensions[1]) * ... * cardinality(rowDimensions[m])
export const createStateMatrix = (data: any[]) => {
export const createMatrix1D = (data: any[]) => {
const rows: any[] = [];

// Construct 1D matrix for state data
Expand All @@ -103,11 +103,7 @@ export const createStateMatrix = (data: any[]) => {
// Creates a M x N matrix where
// M = cardinality(rowDimensions[0]) * cardinality(rowDimensions[1]) * ... * cardinality(rowDimensions[m])
// N = cardinality(colDimensions[0]) * cardinality(colDimensions[1]) * ... * cardinality(colDimensions[n])
export const createTransitionMatrix = (
data: any[],
rowDimensions: string[],
colDimensions: string[]
) => {
export const createMatrix2D = (data: any[], rowDimensions: string[], colDimensions: string[]) => {
const axes = pivotAxes(data, colDimensions, rowDimensions);
const rows: any[] = [];

Expand Down Expand Up @@ -162,6 +158,7 @@ export const createTransitionMatrix = (

return {
matrix: rows,
termsMap: axes.termsMap,
colDimensions,
rowDimensions
};
Expand Down
4 changes: 2 additions & 2 deletions packages/client/hmi-client/tests/unit/utils/pivot.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { createTransitionMatrix } from '@/utils/pivot';
import { createMatrix2D } from '@/utils/pivot';
import { describe, expect, it } from 'vitest';

const data: any[] = [
Expand All @@ -10,7 +10,7 @@ const data: any[] = [

describe('pivot table tests', () => {
it('square pivot table', () => {
const r = createTransitionMatrix(data, ['fruit'], ['fruit']);
const r = createMatrix2D(data, ['fruit'], ['fruit']);

expect(r.matrix.length).to.eq(3);
expect(r.matrix[0].length).to.eq(3);
Expand Down