From 13baa43081d9c664dc57b1eae18e8eb34d95ec82 Mon Sep 17 00:00:00 2001 From: Knut Sveidqvist Date: Wed, 25 Sep 2019 21:29:32 +0200 Subject: [PATCH] #945 Rendering from diagram data --- src/diagrams/state/parser/stateDiagram.jison | 26 ++------------ src/diagrams/state/stateDb.js | 38 +++++++++++++++----- src/diagrams/state/stateDiagram.spec.js | 30 ++++++++++++++++ src/diagrams/state/stateRenderer.js | 19 ++-------- 4 files changed, 64 insertions(+), 49 deletions(-) diff --git a/src/diagrams/state/parser/stateDiagram.jison b/src/diagrams/state/parser/stateDiagram.jison index 0e73cee4a..d48c2ed66 100644 --- a/src/diagrams/state/parser/stateDiagram.jison +++ b/src/diagrams/state/parser/stateDiagram.jison @@ -70,31 +70,11 @@ "stateDiagram"\s+ { console.log('Got state diagram', yytext,'#');return 'SD'; } "hide empty description" { console.log('HIDE_EMPTY', yytext,'#');return 'HIDE_EMPTY'; } -// "participant" { this.begin('ID'); return 'participant'; } -// [^\->:\n,;]+?(?=((?!\n)\s)+"as"(?!\n)\s|[#\n;]|$) { yytext = yytext.trim(); this.begin('ALIAS'); return 'ACTOR'; } -// "as" { this.popState(); this.popState(); this.begin('LINE'); return 'AS'; } -// (?:) { this.popState(); this.popState(); return 'NL'; } -// "<>" { this.begin('LINE'); return 'else'; } -// "<>" { this.begin('LINE'); return 'par'; } -// "and" { this.begin('LINE'); return 'and'; } -// [^#\n;]* { this.popState(); return 'restOfLine'; } -// "end" return 'end'; "[*]" { console.log('EDGE_STATE=',yytext); return 'EDGE_STATE';} [^:\n\s\-\{]+ { console.log('=>ID=',yytext); return 'ID';} \s*":"[^\+\->:\n,;]+ { yytext = yytext.trim(); console.log('Descr = ', yytext); return 'DESCR'; } -// "over" return 'over'; -// "note" return 'note'; -// "activate" { this.begin('ID'); return 'activate'; } -// "deactivate" { this.begin('ID'); return 'deactivate'; } -// "title" return 'title'; -// "stateDiagram" return 'SD'; -// "," return ','; -// ";" return 'NL'; -// [^\+\->:\n,;]+ { yytext = yytext.trim(); return 'ACTOR'; } "-->" return '-->'; "--" return 'CONCURRENT'; -// "--" return '--'; -// ":"[^#\n;]+ return 'TXT'; <> return 'NL'; . return 'INVALID'; @@ -125,7 +105,7 @@ line statement : idStatement DESCR - | idStatement '-->' idStatement + | idStatement '-->' idStatement {yy.addRelation($1, $3);} | idStatement '-->' idStatement DESCR | HIDE_EMPTY | scale WIDTH @@ -141,8 +121,8 @@ statement ; idStatement - : ID - | EDGE_STATE + : ID {$$=$1;} + | EDGE_STATE {$$=$1;} ; notePosition diff --git a/src/diagrams/state/stateDb.js b/src/diagrams/state/stateDb.js index 8eb1a30e1..08928bfbb 100644 --- a/src/diagrams/state/stateDb.js +++ b/src/diagrams/state/stateDb.js @@ -3,6 +3,9 @@ import { logger } from '../../logger'; let relations = []; let states = {}; +let startCnt = 0; +let endCnt = 0; + /** * Function called by parser when a node definition has been found. * @param id @@ -10,11 +13,12 @@ let states = {}; * @param type * @param style */ -export const addState = function(id) { +export const addState = function(id, type) { if (typeof states[id] === 'undefined') { states[id] = { id: id, - descriptions: [] + descriptions: [], + type }; } }; @@ -27,19 +31,35 @@ export const clear = function() { export const getState = function(id) { return states[id]; }; -export const getstates = function() { +export const getStates = function() { return states; }; export const getRelations = function() { + // const relations1 = [{ id1: 'start1', id2: 'state1' }, { id1: 'state1', id2: 'exit1' }]; + // return relations; return relations; }; -export const addRelation = function(relation) { - logger.debug('Adding relation: ' + JSON.stringify(relation)); - addState(relation.id1); - addState(relation.id2); - relations.push(relation); +export const addRelation = function(_id1, _id2) { + let id1 = _id1; + let id2 = _id2; + let type1 = 'default'; + let type2 = 'default'; + if (_id1 === '[*]') { + startCnt++; + id1 = 'start' + startCnt; + type1 = 'start'; + } + if (_id2 === '[*]') { + endCnt++; + id2 = 'end' + startCnt; + type2 = 'end'; + } + console.log(id1, id2); + addState(id1, type1); + addState(id2, type2); + relations.push({ id1, id2 }); }; export const addMember = function(className, member) { @@ -83,7 +103,7 @@ export default { addState, clear, getState, - getstates, + getStates, getRelations, addRelation, addMember, diff --git a/src/diagrams/state/stateDiagram.spec.js b/src/diagrams/state/stateDiagram.spec.js index 58399bfdf..45555d4b4 100644 --- a/src/diagrams/state/stateDiagram.spec.js +++ b/src/diagrams/state/stateDiagram.spec.js @@ -8,6 +8,36 @@ describe('state diagram, ', function() { parser.yy = stateDb; }); + fit('super simple', function() { + const str = ` + stateDiagram + [*] --> State1 + State1 --> [*] + `; + + parser.parse(str); + expect(stateDb.getRelations()).toEqual([ + { id1: 'start1', id2: 'State1' }, + { id1: 'State1', id2: 'end1' } + ]); + expect(stateDb.getStates()).toEqual({ + State1: { + id: 'State1', + type: 'default', + descriptions: [] + }, + end1: { + id: 'end1', + type: 'end', + descriptions: [] + }, + start1: { + id: 'start1', + type: 'start', + descriptions: [] + } + }); + }); it('simple', function() { const str = `stateDiagram\n State1 : this is another string diff --git a/src/diagrams/state/stateRenderer.js b/src/diagrams/state/stateRenderer.js index c40e902f8..e6c1d2426 100644 --- a/src/diagrams/state/stateRenderer.js +++ b/src/diagrams/state/stateRenderer.js @@ -406,21 +406,7 @@ export const draw = function(text, id) { return {}; }); - // const states = stateDb.getStates(); - const states = { - start1: { - id: 'start1', - type: 'start' - }, - state1: { - id: 'state1', - type: 'default' - }, - exit: { - id: 'exit1', - type: 'end' - } - }; + const states = stateDb.getStates(); const keys = Object.keys(states); total = keys.length; for (let i = 0; i < keys.length; i++) { @@ -433,8 +419,7 @@ export const draw = function(text, id) { logger.info('Org height: ' + node.height); } - // const relations = stateDb.getRelations(); - const relations = [{ id1: 'start1', id2: 'state1' }, { id1: 'state1', id2: 'exit1' }]; + const relations = stateDb.getRelations(); relations.forEach(function(relation) { logger.info( 'tjoho' + getGraphId(relation.id1) + getGraphId(relation.id2) + JSON.stringify(relation)