-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.js
163 lines (152 loc) · 4.23 KB
/
data.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
function Data() {
const states = [
{
name: 'q0',
x: 100, y: 100,
start: {
rot: 180,
id: 'start'
},
},
{
name: 'q1',
x: 300, y: 100,
},
{
name: 'q2',
x: 300, y: 200,
},
{
name: 'r',
x: 100, y: 200,
type: 'reject'
},
{
name: 't',
x: 100, y: 300,
type: 'accept',
}
];
const transitions = [
{
from: 'q0', to: 'q0',
symbol: '{', mode: 'R',
rot: 90
},
{
from: 'q0', to: 'q0',
symbol: 'a', mode: 'R'
},
{
from: 'q0', to: 'q0',
symbol: 'b', mode: 'R'
},
{
from: 'q0', to: 'q1',
symbol: '}', mode: 'L',
curve: 15, pos: 'bot'
},
{
from: 'q1', to: 'r',
symbol: '{', mode: 'R'
},
{
from: 'q1', to: 'q2',
symbol: 'a', mode: 'L'
},
{
from: 'q1', to: 'q2',
symbol: 'b', mode: 'L'
},
{
from: 'q2', to: 't',
symbol: 'a', mode: 'R'
},
{
from: 'q2', to: 'r',
symbol: 'b', mode: 'R'
},
{
from: 'q2', to: 'r',
symbol: '{', mode: 'R'
},
]
const getDefaults = () => ({
states, transitions
});
const getFromFile = async (file) => {
const data = await file.text();
const json = JSON.parse(data);
const isValid = validateData(json);
if (!isValid) {
return null;
}
return json;
};
//helper fnuction to validate data
const validateData = (json) => {
// data has states and transitions
if (!json.hasOwnProperty('states') || !json.hasOwnProperty('transitions')) {
window.alert('No states/transitions')
return false;
}
// validate states array
if (!Array.isArray(json.states)) {
window.alert('States not stored in array')
return false;
}
// validate each state object in the 'states' array
for (const state of json.states) {
// the state is an object and has the 'name' property
if (!state || typeof state !== 'object' || !state.hasOwnProperty('name') || typeof state.name !== 'string' || state.name.trim() === '') {
window.alert('States with no name found')
return false;
}
// the state has 'x' and 'y' properties with numeric values
if (!state.hasOwnProperty('x') || !state.hasOwnProperty('y') || typeof state.x !== 'number' || typeof state.y !== 'number') {
window.alert('States with invalid x/y coordinates found')
return false;
}
// the optional 'type' property, if present, has a valid value
if (state.hasOwnProperty('type') && typeof state.type !== 'string') {
window.alert('States with non-string type found')
return false;
}
// the 'type' property, if present, has a valid value ('accept' or 'reject')
if (state.type && state.type !== 'accept' && state.type !== 'reject') {
window.alert('States with invalid type found')
return false;
}
}
// validate transitions array
if (!Array.isArray(json.transitions)) {
window.alert('Transitions not stored in array')
return false;
}
// transition objects
for (const transition of json.transitions) {
// transition is an object and has the 'from' and 'to' properties
if (!transition || typeof transition !== 'object' || !transition.hasOwnProperty('from') || !transition.hasOwnProperty('to') ||
typeof transition.from !== 'string' || typeof transition.to !== 'string' || transition.from.trim() === '' || transition.to.trim() === '') {
window.alert('Transitions with no from/to states found')
return false;
}
// the 'symbol' property is a non-empty string
if (typeof transition.symbol !== 'string' || transition.symbol.trim() === '') {
window.alert('Transitions with no input symbol found')
return false;
}
// the 'mode' property has a valid value ('R' or 'L')
if (transition.mode !== 'R' && transition.mode !== 'L') {
window.alert('Transitions with invalid mode found')
return false;
}
}
return true;
};
return {
getDefaults,
getFromFile,
validateData,
};
}