#945 Support for forks and joins

This commit is contained in:
Knut Sveidqvist 2019-10-06 14:11:17 +02:00
parent 4f1186a610
commit dce09586cd
4 changed files with 41 additions and 6 deletions

View File

@ -161,6 +161,24 @@ describe('State diagram', () => {
`,
{ logLevel: 0 }
);
});
it('should render forks and joins', () => {
imgSnapshotTest(
`
stateDiagram
state fork_state <<fork>>
[*] --> fork_state
fork_state --> State2
fork_state --> State3
state join_state <<join>>
State2 --> join_state
State3 --> join_state
join_state --> State4
State4 --> [*]
`,
{ logLevel: 0 }
);
cy.get('svg');
});
});

View File

@ -43,8 +43,10 @@
<SCALE>\s+"width" {this.popState();}
<INITIAL,struct>"state"\s+ { this.pushState('STATE'); }
<STATE>.*"<<fork>>" {this.popState();console.log('Fork: ',yytext);return 'FORK';}
<STATE>.*"<<join>>" {this.popState();console.log('Join: ',yytext);return 'JOIN';}
<STATE>.*"<<fork>>" {this.popState();yytext=yytext.slice(0,-8).trim(); console.warn('Fork Fork: ',yytext);return 'FORK';}
<STATE>.*"<<join>>" {this.popState();yytext=yytext.slice(0,-8).trim();console.warn('Fork Join: ',yytext);return 'JOIN';}
<STATE>.*"[[fork]]" {this.popState();yytext=yytext.slice(0,-8).trim();console.warn('Fork Fork: ',yytext);return 'FORK';}
<STATE>.*"[[join]]" {this.popState();yytext=yytext.slice(0,-8).trim();console.warn('Fork Join: ',yytext);return 'JOIN';}
<STATE>["] this.begin("STATE_STRING");
<STATE>"as"\s* {this.popState();this.pushState('STATE_ID');return "AS";}
<STATE_ID>[^\n\{]* {this.popState();console.log('STATE_ID', yytext);return "ID";}
@ -135,8 +137,12 @@ statement
//console.warn('Adding document for state with id ', $3, $4); yy.addDocument($3);
$$={ stmt: 'state', id: $3, type: 'default', description: $1, doc: $5 }
}
| FORK
| JOIN
| FORK {
$$={ stmt: 'state', id: $1, type: 'fork' }
}
| JOIN {
$$={ stmt: 'state', id: $1, type: 'join' }
}
| CONCURRENT
| note notePosition ID NOTE_TEXT
{

View File

@ -168,6 +168,16 @@ const drawEndState = g => {
.attr('cx', conf.padding + 7)
.attr('cy', conf.padding + 7);
};
const drawForkJoinState = g => {
return g
.append('rect')
.style('stroke', 'black')
.style('fill', 'black')
.attr('width', 70)
.attr('height', 7)
.attr('x', conf.padding)
.attr('y', conf.padding);
};
export const drawText = function(elem, textData, width) {
// Remove and ignore br:s
@ -209,7 +219,7 @@ const _drawLongText = (_text, x, y, g) => {
const textBounds = span.node().getBBox();
textHeight += textBounds.height;
span.attr('x', x + conf.noteMargin);
span.attr('y', y + textHeight + 1.25* conf.noteMargin);
span.attr('y', y + textHeight + 1.25 * conf.noteMargin);
// textWidth = Math.max(textBounds.width, textWidth);
}
}
@ -264,6 +274,7 @@ export const drawState = function(elem, stateDef, graph, doc) {
if (stateDef.type === 'start') drawStartState(g);
if (stateDef.type === 'end') drawEndState(g);
if (stateDef.type === 'fork' || stateDef.type === 'join') drawForkJoinState(g);
if (stateDef.type === 'note') drawNote(stateDef.note.text, g);
if (stateDef.type === 'default' && stateDef.descriptions.length === 0)
drawSimpleState(g, stateDef);

View File

@ -61,7 +61,7 @@ const insertMarkers = function(elem) {
export const draw = function(text, id) {
parser.yy.clear();
parser.parse(text);
logger.info('Rendering diagram ' + text);
logger.warn('Rendering diagram ' + text);
// /// / Fetch the default direction, use TD if none was found
const diagram = d3.select(`[id='${id}']`);