-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel-validator.js
193 lines (160 loc) · 7.05 KB
/
model-validator.js
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
const path = require('path');
const fs = require('fs');
const codegen = require('./code-utils')
const {FSMHelpers} = require('./code-helpers')
const { Variable, Dataset } = require('./dataset')
/**
*
* FSM Model Validator
*/
class ModelValidator extends FSMHelpers {
/**
* @constructor
*
* @param {type.UMLStateMachine} baseModel
* @param {string} basePath generated files and directories to be placed
*/
constructor(baseModel) {
super(baseModel)
this.errors = [];
}
checkStateTypes() {
var states = this.extractStates(this.baseModel, true, true, true);
const pseudo_states = ['initial', 'fork', 'join', 'choice', 'shallowHistory'];
states.forEach(state => {
if (state instanceof type.UMLPseudostate && !pseudo_states.includes(state.kind)) {
this.errors.push({ 'element': state, 'msg': `PseudoState \'${state.kind}\' isn\'t supported.` });
}
if (state.doActivities && state.doActivities.length > 0) {
}
});
}
checkStateActivities() {
var states = this.extractStates(this.baseModel, true, true, true);
states.forEach(state => {
if (state.doActivities && state.doActivities.length > 0) {
this.errors.push({ 'element': state, 'msg': `Do Activities aren\'t supported.` });
}
if (state.exitActivities && state.exitActivities.length > 0) {
state.exitActivities.forEach(activity => {
if (! (activity instanceof type.UMLActivity || activity instanceof type.UMLOpaqueBehavior)) {
this.errors.push({ 'element': activity, 'msg': `Only Activities are supported as exit activity.` });
}
});
}
if (state.entryActivities && state.entryActivities.length > 0) {
state.entryActivities.forEach(activity => {
if (!(activity instanceof type.UMLActivity || activity instanceof type.UMLOpaqueBehavior)) {
this.errors.push({ 'element': activity, 'msg': `Only Activities are supported as entry activity.` });
}
});
}
});
}
checkUniqueStateNames(context) {
var states = this.extractStates(context, true, true, false),
names = [];
states.forEach(state => {
var name = this.getStateName(state);
if (names.includes(name)) {
var parentName = this.getStateName(FSMHelpers.getParent(state));
this.errors.push({ 'element': state, 'msg': `State name isn\'t unique in \'${parentName}\'.` });
}
else {
names.push(name);
}
if (FSMHelpers.isCompositeState(state)) {
names.push(...this.checkUniqueStateNames(state));
}
});
return names;
}
checkUnusedStates() {
var states = this.extractStates(this.baseModel, true, true, true);
states.forEach(state => {
var used = false;
this.baseModel.regions[0].transitions.forEach(t => {
if (FSMHelpers.isInitial(state) == false && t.target._id == state._id && t.source._id !== t.target._id) {
used = true;
} else if (FSMHelpers.isInitial(state) == true && t.source._id == state._id && t.source._id !== t.target._id) {
used = true;
}
});
if (FSMHelpers.isCompositeState(state)) {
}
else if (used ==false) {
this.errors.push({ 'element': state, 'msg': `State isn't involved in any transition at all.` });
}
});
}
checkTransitions() {
var ts = [];
this.baseModel.regions[0].transitions.forEach( t => {
var duplicate = false;
ts.forEach(_t => {
if (t.source._id == _t.source._id
&& t.target._id == _t.target._id
&& t.triggers.length == _t.triggers.length
&& t.guard == _t.guard) {
if (t.triggers.length === 0 || t.triggers[0].name === _t.triggers[0].name) {
duplicate = true;
}
return;
}
});
if (duplicate && !FSMHelpers.isFork(t.source)) {
this.errors.push({ 'element': t, 'msg': `Transition from \'${this.getStateName(t.source)}\' isn\'t unique.` });
} else {
ts.push(t);
}
if (FSMHelpers.isInitial(t.source) && (t.guard || t.triggers.length > 0)) {
this.errors.push({ 'element': t, 'msg': 'Initial states can\'t have transitions with triggers or guards.' });
}
else if (t.triggers.length >= 2) {
this.errors.push({ 'element': t, 'msg': 'Only 1 trigger allowed per transition.' });
}
if (FSMHelpers.isFork(t.source) && (t.guard || t.triggers.length > 0)) {
this.errors.push({ 'element': t, 'msg': 'Forks can\'t have outgoing transitions with triggers or guards.' });
}
if (FSMHelpers.isFork(t.source) && !FSMHelpers.isSubState(t.target)) {
this.errors.push({ 'element': t, 'msg': 'Forks outgoing transitions should be going to a substate.' });
}
if (FSMHelpers.isJoin(t.target) && !FSMHelpers.isSubState(t.source)) {
this.errors.push({ 'element': t, 'msg': 'Joins incoming transitions should be from a substate.' });
}
if (FSMHelpers.isCompositeState(t.source) && t.source._id == t.target._id && (t.kind== undefined || t.kind == 'external') && this.getInitialState(t.source) === null) {
this.errors.push({ 'element': t, 'msg': 'Composite state has no initial sub state. Transition kind \'external\' isn\'t allowed.' });
}
t.effects.forEach(effect => {
if (! (effect instanceof type.UMLActivity || effect instanceof type.UMLOpaqueBehavior)) {
this.errors.push({ 'element': effect, 'msg': `Only Activities are supported as effects.` });
}
});
});
}
validate() {
this.errors = [];
if (this.getInitialState() === null) {
this.errors.push({ 'element': this.baseModel, 'msg': 'StateMachine doesn\'t have an initial state.' });
}
this.checkStateTypes();
this.checkUniqueStateNames(this.baseModel);
this.checkStateActivities();
this.checkUnusedStates();
this.checkTransitions();
return this.errors;
}
}
/**
* Easy use wrapper arround the ModelValidator
* @param {UMLStateMachine} baseModel
* @param {string} basePath
* @param {Dataset} dataset
* @param {StructuredTextGeneratorOptions} options
* @returns
*/
function validate(baseModel, basePath, dataset, options) {
var validator = new ModelValidator(baseModel);
return validator.validate();
}
exports.validate = validate