This repository has been archived by the owner on Jul 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
fsm_base_parser.ts
225 lines (211 loc) · 7.29 KB
/
fsm_base_parser.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
// Copyright 2022
// Carlos Alberto Ruiz Naranjo [[email protected]]
// Ismael Perez Rojo [[email protected] ]
//
// This file is part of colibri2
//
// Colibri is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Colibri is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with colibri2. If not, see <https://www.gnu.org/licenses/>.
export class Parser_fsm_base {
comment_symbol = "";
check_empty_states_transitions(states: any) {
let check = true;
for (let i = 0; i < states.length; ++i) {
if (states[i].transitions.length !== 0) {
check = false;
}
}
return check;
}
check_stm(stm: any) {
const check = false;
const states = stm.states;
for (let i = 0; i < states.length; ++i) {
const transitions = states[i].transitions;
if (transitions.length > 0) {
return true;
}
}
return check;
}
json_to_svg(stm_json: any) {
const stmcat = this.get_smcat(stm_json);
const smcat = require("state-machine-cat");
let svg;
try {
svg = smcat.render(stmcat, { outputType: "svg" });
}
// eslint-disable-next-line no-empty
catch (e) { }
return svg;
}
get_smcat(stm_json: any) {
let sm_states = '';
let sm_transitions = '';
const states = stm_json.states;
let state_names = [];
for (let i = 0; i < states.length; ++i) {
if (states[i].transitions.length === 0) {
state_names.push(states[i].name);
}
}
const emptys: any = [];
for (let i = 0; i < state_names.length; ++i) {
let empty = true;
for (let j = 0; j < states.length; ++j) {
for (let m = 0; m < states[j].transitions.length; ++m) {
if (states[j].transitions[m].destination === state_names[i]) {
empty = false;
}
}
}
if (empty === true) {
emptys.push(state_names[i]);
}
}
const gosth: any = [];
state_names = [];
for (let i = 0; i < states.length; ++i) {
state_names.push(states[i].name);
}
for (let j = 0; j < states.length; ++j) {
for (let m = 0; m < states[j].transitions.length; ++m) {
if (state_names.includes(states[j].transitions[m].destination) === false) {
const element = { 'name': states[j].transitions[m].destination, 'transitions': [] };
stm_json.states.push(element);
gosth.push(states[j].transitions[m].destination);
}
}
}
const num_states = stm_json.states.length;
stm_json.states.forEach(function (i_state: any, i: any) {
const transitions = i_state.transitions;
const state_name = i_state.name;
if (emptys.includes(state_name) === true || gosth.includes(state_name) === true) {
sm_states += `${state_name} [color="red"]`;
}
else {
sm_states += `${state_name}`;
}
if (i !== num_states - 1) {
sm_states += ',';
}
else {
sm_states += ';\n';
}
if (gosth.includes(state_name) !== true) {
transitions.forEach(function (i_transition: any) {
if (gosth.includes(i_transition.destination) === true) {
sm_transitions +=
`${state_name} => ${i_transition.destination} [color="red"] : ${i_transition.condition};\n`;
}
else {
sm_transitions += `${state_name} => ${i_transition.destination} : ${i_transition.condition};\n`;
}
});
}
});
const str_stm = stm_json.state_variable_name + "{\n" + sm_states + sm_transitions + "\n};";
return str_stm;
}
only_unique(value: any, index: any, self: any) {
return self.indexOf(value) === index;
}
get_comment(comment: any) {
if (comment === undefined) {
return '';
}
const txt_comment = comment.slice(2);
if (this.comment_symbol === '') {
return txt_comment + '\n';
}
else if (txt_comment[0] === this.comment_symbol) {
return txt_comment.slice(1).trim() + '\n';
}
return '';
}
set_symbol(symbol: any) {
if (symbol === undefined) {
this.comment_symbol = '';
}
else {
this.comment_symbol = symbol;
}
}
get_item_from_childs(p: any, type: string) {
if (p === undefined) {
return undefined;
}
let item = undefined;
const cursor = p.walk();
let break_p = false;
cursor.gotoFirstChild();
do {
if (cursor.nodeType === type) {
item = cursor.currentNode();
break_p = true;
}
}
while (cursor.gotoNextSibling() === true && break_p === false);
return item;
}
search_multiple_in_tree(element: any, matching_title: string) {
const arr_match: any[] = [];
function recursive_searchTree(element: any, matching_title: string): any {
const type = element.type;
if (type === matching_title) {
arr_match.push(element);
} else if (element !== undefined) {
let result = undefined;
for (let i = 0; result === undefined && i < element.childCount; i++) {
result = recursive_searchTree(element.child(i), matching_title);
}
return result;
}
return undefined;
}
recursive_searchTree(element, matching_title);
return arr_match;
}
get_item_multiple_from_childs(p: any, type: any) {
if (p === undefined) {
return [];
}
const items: any = [];
const cursor: any = p.walk();
cursor.gotoFirstChild();
do {
if (cursor.nodeType === type) {
const item = cursor.currentNode();
items.push(item);
}
}
while (cursor.gotoNextSibling() === true);
return items;
}
get_item_from_childs_last(p: any, type: any) {
if (p === undefined) {
return undefined;
}
let item = undefined;
const cursor = p.walk();
cursor.gotoFirstChild();
do {
if (cursor.nodeType === type) {
item = cursor.currentNode();
}
}
while (cursor.gotoNextSibling() === true);
return item;
}
}