#945 Rendering from diagram data

This commit is contained in:
Knut Sveidqvist
2019-09-25 21:29:32 +02:00
parent fad76ad534
commit 13baa43081
4 changed files with 64 additions and 49 deletions

View File

@@ -70,31 +70,11 @@
"stateDiagram"\s+ { console.log('Got state diagram', yytext,'#');return 'SD'; } "stateDiagram"\s+ { console.log('Got state diagram', yytext,'#');return 'SD'; }
"hide empty description" { console.log('HIDE_EMPTY', yytext,'#');return 'HIDE_EMPTY'; } "hide empty description" { console.log('HIDE_EMPTY', yytext,'#');return 'HIDE_EMPTY'; }
// "participant" { this.begin('ID'); return 'participant'; }
// <ID>[^\->:\n,;]+?(?=((?!\n)\s)+"as"(?!\n)\s|[#\n;]|$) { yytext = yytext.trim(); this.begin('ALIAS'); return 'ACTOR'; }
// <ALIAS>"as" { this.popState(); this.popState(); this.begin('LINE'); return 'AS'; }
// <ALIAS>(?:) { this.popState(); this.popState(); return 'NL'; }
// "<<fork>>" { this.begin('LINE'); return 'else'; }
// "<<join>>" { this.begin('LINE'); return 'par'; }
// "and" { this.begin('LINE'); return 'and'; }
// <LINE>[^#\n;]* { this.popState(); return 'restOfLine'; }
// "end" return 'end';
<INITIAL,struct>"[*]" { console.log('EDGE_STATE=',yytext); return 'EDGE_STATE';} <INITIAL,struct>"[*]" { console.log('EDGE_STATE=',yytext); return 'EDGE_STATE';}
<INITIAL,struct>[^:\n\s\-\{]+ { console.log('=>ID=',yytext); return 'ID';} <INITIAL,struct>[^:\n\s\-\{]+ { console.log('=>ID=',yytext); return 'ID';}
<INITIAL,struct>\s*":"[^\+\->:\n,;]+ { yytext = yytext.trim(); console.log('Descr = ', yytext); return 'DESCR'; } <INITIAL,struct>\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'; }
<INITIAL,struct>"-->" return '-->'; <INITIAL,struct>"-->" return '-->';
<struct>"--" return 'CONCURRENT'; <struct>"--" return 'CONCURRENT';
// "--" return '--';
// ":"[^#\n;]+ return 'TXT';
<<EOF>> return 'NL'; <<EOF>> return 'NL';
. return 'INVALID'; . return 'INVALID';
@@ -125,7 +105,7 @@ line
statement statement
: idStatement DESCR : idStatement DESCR
| idStatement '-->' idStatement | idStatement '-->' idStatement {yy.addRelation($1, $3);}
| idStatement '-->' idStatement DESCR | idStatement '-->' idStatement DESCR
| HIDE_EMPTY | HIDE_EMPTY
| scale WIDTH | scale WIDTH
@@ -141,8 +121,8 @@ statement
; ;
idStatement idStatement
: ID : ID {$$=$1;}
| EDGE_STATE | EDGE_STATE {$$=$1;}
; ;
notePosition notePosition

View File

@@ -3,6 +3,9 @@ import { logger } from '../../logger';
let relations = []; let relations = [];
let states = {}; let states = {};
let startCnt = 0;
let endCnt = 0;
/** /**
* Function called by parser when a node definition has been found. * Function called by parser when a node definition has been found.
* @param id * @param id
@@ -10,11 +13,12 @@ let states = {};
* @param type * @param type
* @param style * @param style
*/ */
export const addState = function(id) { export const addState = function(id, type) {
if (typeof states[id] === 'undefined') { if (typeof states[id] === 'undefined') {
states[id] = { states[id] = {
id: id, id: id,
descriptions: [] descriptions: [],
type
}; };
} }
}; };
@@ -27,19 +31,35 @@ export const clear = function() {
export const getState = function(id) { export const getState = function(id) {
return states[id]; return states[id];
}; };
export const getstates = function() { export const getStates = function() {
return states; return states;
}; };
export const getRelations = function() { export const getRelations = function() {
// const relations1 = [{ id1: 'start1', id2: 'state1' }, { id1: 'state1', id2: 'exit1' }];
// return relations;
return relations; return relations;
}; };
export const addRelation = function(relation) { export const addRelation = function(_id1, _id2) {
logger.debug('Adding relation: ' + JSON.stringify(relation)); let id1 = _id1;
addState(relation.id1); let id2 = _id2;
addState(relation.id2); let type1 = 'default';
relations.push(relation); 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) { export const addMember = function(className, member) {
@@ -83,7 +103,7 @@ export default {
addState, addState,
clear, clear,
getState, getState,
getstates, getStates,
getRelations, getRelations,
addRelation, addRelation,
addMember, addMember,

View File

@@ -8,6 +8,36 @@ describe('state diagram, ', function() {
parser.yy = stateDb; 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() { it('simple', function() {
const str = `stateDiagram\n const str = `stateDiagram\n
State1 : this is another string State1 : this is another string

View File

@@ -406,21 +406,7 @@ export const draw = function(text, id) {
return {}; return {};
}); });
// const states = stateDb.getStates(); const states = stateDb.getStates();
const states = {
start1: {
id: 'start1',
type: 'start'
},
state1: {
id: 'state1',
type: 'default'
},
exit: {
id: 'exit1',
type: 'end'
}
};
const keys = Object.keys(states); const keys = Object.keys(states);
total = keys.length; total = keys.length;
for (let i = 0; i < keys.length; i++) { for (let i = 0; i < keys.length; i++) {
@@ -433,8 +419,7 @@ export const draw = function(text, id) {
logger.info('Org height: ' + node.height); logger.info('Org height: ' + node.height);
} }
// const relations = stateDb.getRelations(); const relations = stateDb.getRelations();
const relations = [{ id1: 'start1', id2: 'state1' }, { id1: 'state1', id2: 'exit1' }];
relations.forEach(function(relation) { relations.forEach(function(relation) {
logger.info( logger.info(
'tjoho' + getGraphId(relation.id1) + getGraphId(relation.id2) + JSON.stringify(relation) 'tjoho' + getGraphId(relation.id1) + getGraphId(relation.id2) + JSON.stringify(relation)