Skip to content

Commit

Permalink
#3358 Adding db calls from node statements
Browse files Browse the repository at this point in the history
  • Loading branch information
knsv committed Aug 28, 2023
1 parent b9531d5 commit 5fc99f1
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 47 deletions.
9 changes: 2 additions & 7 deletions cypress/platform/knsv2.html
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,8 @@
</head>
<body>
<pre id="diagram" class="mermaid">
stateDiagram-v2
[*] --> Still
Still --> [*]
Still --> Moving
Moving --> Still
Moving --> Crash
Crash --> [*] </pre
block-beta
id</pre
>
<pre id="diagram" class="mermaid2">
flowchart RL
Expand Down
100 changes: 69 additions & 31 deletions packages/mermaid/src/diagrams/block/blockDB.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// import type { BlockDB } from './blockTypes.js';
import type { DiagramDB } from '../../diagram-api/types.js';
import { BlockConfig } from './blockTypes.js';
import { BlockConfig, BlockType, Block, Link } from './blockTypes.js';

import * as configApi from '../../config.js';
// import common from '../common/common.js';
Expand All @@ -13,47 +13,78 @@ import {
// getDiagramTitle,
clear as commonClear,
} from '../../commonDb.js';
import { log } from '../../logger.js';

// export type TBlockColumnsDefaultValue = 'H'; // Do we support something else, like 'auto' | 0?

// TODO: Convert to generic TreeNode type? Convert to class?
export interface Block {
ID: string;
label?: string;
parent?: Block;
children?: Block[];
columns?: number; // | TBlockColumnsDefaultValue;
}
// Initialize the node database for simple lookups
let nodeDatabase: Record<string, Node> = {};
const blockDatabase: Record<string, Block> = {};

export interface Link {
source: Block;
target: Block;
}
// Function to get a node by its ID
export const getNodeById = (id: string): Node | undefined => {
return nodeDatabase[id];
};

// TODO: Convert to generic TreeNode type? Convert to class?

let rootBlocks: Block[] = [];
let rootBlock = { ID: 'root', children: [] as Block[], columns: -1 };
let blocks: Block[] = [];
const links: Link[] = [];
let rootBlock = { ID: 'root', children: [], columns: -1 } as Block;
let currentBlock: Block | undefined;
// let rootBlock = { ID: 'root', children: [], columns: -1 } as Block;
let currentBlock = rootBlock;

const clear = (): void => {
rootBlocks = [];
blocks = [];
log.info('Clear called');
// rootBlocks = [];
blocks = [] as Block[];
commonClear();
rootBlock = { ID: 'root', children: [], columns: -1 };
currentBlock = rootBlock;
nodeDatabase = {};
blockDatabase[rootBlock.ID] = rootBlock;
};

type IAddBlock = (block: Block) => Block;
const addBlock: IAddBlock = (block: Block, parent?: Block): Block => {
if (parent) {
parent.children ??= [];
parent.children.push(block);
} else {
rootBlocks.push(block);
// type IAddBlock = (block: Block) => Block;
// const addBlock: IAddBlock = (block: Block, parent?: Block): Block => {
// log.info('addBlock', block, parent);
// if (parent) {
// parent.children ??= [];
// parent.children.push(block);
// } else {
// rootBlock.children.push(block);
// }
// blocks.push(block);
// return block;
// };

type ITypeStr2Type = (typeStr: string) => BlockType;
export function typeStr2Type(typeStr: string) {
// TODO: add all types
switch (typeStr) {
case '[]':
return 'square';
case '()':
return 'round';
default:
return 'square';
}
blocks.push(block);
return block;
}

type IAddBlock = (id: string, label: string, type: BlockType) => Block;
// Function to add a node to the database
export const addBlock = (id: string, _label?: string, type?: BlockType) => {
log.info('addNode called:', id, _label, type);
const label = _label || id;
const node: Block = {
ID: id,
label,
type: type || 'square',
};
blockDatabase[node.ID] = node;
currentBlock.children ??= [];
currentBlock.children.push(node);
return node;
};

type IAddLink = (link: Link) => Link;
Expand Down Expand Up @@ -84,16 +115,21 @@ const getBlock = (id: string, blocks: Block[]): Block | undefined => {

type IGetColumns = (blockID: string) => number;
const getColumns = (blockID: string): number => {
const blocks = [rootBlock];
const block = getBlock(blockID, blocks);
const block = blockDatabase[blockID];
if (!block) {
return -1;
}
return block.columns || -1;
if (block.columns) {
return block.columns;
}
if (!block.children) {
return -1;
}
return block.children.length;
};

type IGetBlocks = () => Block[];
const getBlocks: IGetBlocks = () => blocks;
const getBlocks: IGetBlocks = () => rootBlock.children || [];

type IGetLinks = () => Link[];
const getLinks: IGetLinks = () => links;
Expand All @@ -111,12 +147,14 @@ export interface BlockDB extends DiagramDB {
getLinks: IGetLinks;
setColumns: ISetColumns;
getColumns: IGetColumns;
typeStr2Type: ITypeStr2Type;
}

const db: BlockDB = {
getConfig: () => configApi.getConfig().block,
addBlock: addBlock,
addLink: addLink,
typeStr2Type: typeStr2Type,
getLogger, // TODO: remove
getBlocks,
getLinks,
Expand Down
33 changes: 33 additions & 0 deletions packages/mermaid/src/diagrams/block/blockTypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,36 @@ import type { BaseDiagramConfig } from '../../config.type.js';
export interface BlockConfig extends BaseDiagramConfig {
padding?: number;
}

export type BlockType =
| 'round'
| 'square'
| 'diamond'
| 'hexagon'
| 'odd'
| 'lean_right'
| 'lean_left'
| 'trapezoid'
| 'inv_trapezoid'
| 'odd_right'
| 'circle'
| 'ellipse'
| 'stadium'
| 'subroutine'
| 'cylinder'
| 'group'
| 'doublecircle';

export interface Block {
ID: string;
label?: string;
parent?: Block;
type?: BlockType;
children?: Block[];
columns?: number; // | TBlockColumnsDefaultValue;
}

export interface Link {
source: Block;
target: Block;
}
12 changes: 6 additions & 6 deletions packages/mermaid/src/diagrams/block/parser/block.jison
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ accDescr\s*"{"\s* { this.pushState("acc_descr_mul

// Start of nodes with shapes and description
"-)" { yy.getLogger().info('Lex: -)'); this.pushState('NODE');return 'NODE_D START'; }
"(-" { yy.getLogger().info('Lex: (-'); this.pushState('NODE');return 'NODE_DSTART'; }
"(-" { yy.getLogger().info('Lex: (-'); this.pushState('NODE');return 'NODE_DSTART'; }
"))" { yy.getLogger().info('Lex: ))'); this.pushState('NODE');return 'NODE_DSTART'; }
")" { yy.getLogger().info('Lex: )'); this.pushState('NODE');return 'NODE_DSTART'; }
"((" { yy.getLogger().info('Lex: )'); this.pushState('NODE');return 'NODE_DSTART'; }
Expand Down Expand Up @@ -177,8 +177,8 @@ statement
;

nodeStatement
: nodeStatement link node { yy.getLogger().info('Rule: nodeStatement (nodeStatement link node) ');}
| node { yy.getLogger().info('Rule: nodeStatement (node) ', $1);}
: nodeStatement link node { yy.getLogger().info('Rule: nodeStatement (nodeStatement link node) '); yy.addBlock($1.id);}
| node { yy.getLogger().info('Rule: nodeStatement (node) ', $1); yy.addBlock($1.id, $1.label, yy.typeStr2Type($1)); }
;

columnsStatement
Expand All @@ -192,16 +192,16 @@ blockStatement

node
: NODE_ID
{ yy.getLogger().info("Rule: node (NODE_ID seperator): ", $1); }
{ yy.getLogger().info("Rule: node (NODE_ID seperator): ", $1); $$ = { id: $1 }; }
|NODE_ID nodeShapeNLabel
{ yy.getLogger().info("Rule: node (NODE_ID nodeShapeNLabel seperator): ", $1, $2); }
{ yy.getLogger().info("Rule: node (NODE_ID nodeShapeNLabel seperator): ", $1, $2); $$ = { id: $1, label: $2.label, typeStr: $2.typeStr };}
// |nodeShapeNLabel seperator
// { yy.getLogger().info("Rule: node (nodeShapeNLabel seperator): ", $1, $2, $3); }
;

nodeShapeNLabel
: NODE_DSTART STR NODE_DEND
{ yy.getLogger().info("Rule: nodeShapeNLabel: ", $1, $2, $3); $$ = { type: $1 + $3, descr: $2 }; }
{ yy.getLogger().info("Rule: nodeShapeNLabel: ", $1, $2, $3); $$ = { typeStr: $1 + $3, label: $2 }; }
;

%%
19 changes: 17 additions & 2 deletions packages/mermaid/src/diagrams/block/parser/block.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import block from './block.jison';
import db from '../blockDB.js';
import { cleanupComments } from '../../../diagram-api/comments.js';
import { prepareTextForParsing } from '../blockUtils.js';
import * as fs from 'fs';
import * as path from 'path';
import { setConfig } from '../../../config.js';

describe('Block diagram', function () {
describe('when parsing an block diagram graph it should handle > ', function () {
Expand All @@ -20,6 +19,22 @@ describe('Block diagram', function () {
`;

block.parse(str);
const blocks = db.getBlocks();
expect(blocks.length).toBe(1);
expect(blocks[0].ID).toBe('id');
expect(blocks[0].label).toBe('id');
});
it('a node with a square shape and a label', async () => {
const str = `block-beta
id["A label"]
`;

block.parse(str);
const blocks = db.getBlocks();
expect(blocks.length).toBe(1);
expect(blocks[0].ID).toBe('id');
expect(blocks[0].label).toBe('A label');
expect(blocks[0].type).toBe('square');
});
it('a diagram with multiple nodes', async () => {
const str = `block-beta
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import flowDb from '../flowDb.js';
import flow from './flow.jison';
import { setConfig } from '../../../config.js';
import { cleanupComments } from '../../../diagram-api/comments.js';
import { setConfig } from '../../../config.js';

setConfig({
securityLevel: 'strict',
Expand Down

0 comments on commit 5fc99f1

Please sign in to comment.