-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.js
More file actions
314 lines (271 loc) · 8.87 KB
/
Copy pathmodel.js
File metadata and controls
314 lines (271 loc) · 8.87 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
import {
concat as _concat,
intersection as _intersection,
difference as _difference,
isUndefined as _isUndefined
} from 'lodash-es'
import { ref, triggerRef } from 'vue'
import { pauseTracking, enableTracking } from '@vue/reactivity'
import { defineStore } from 'pinia'
import { VALUE_STRING_TYPE, VALUE_DOUBLE_RANGE_TYPE, VALUE_FILE_PATH_TYPE } from './porisNode'
import { xmlModelLoader } from '@/api/modelLoader'
import { parseToPorisModel } from './xmlParser'
import { debugLog } from '@/utils/debug'
export const useModelStore = defineStore('model', () => {
const xmlModel = ref(null)
const values = ref([])
const modes = ref([])
const subsystems = ref([])
const rootSubsystem = ref()
const isLoading = ref(false)
const currentModes = ref([])
const systemValues = ref({})
function loadModel(modelName) {
isLoading.value = true
let doc = xmlModelLoader(`/models/${modelName}.xml`)
doc.then((model) => {
xmlModel.value = model
let JSONmodel = parseToPorisModel(this, model)
values.value = JSONmodel.values
modes.value = JSONmodel.modes
subsystems.value = JSONmodel.subsystems
rootSubsystem.value = JSONmodel.rootSubsystem
//console.log('LIMPIANDO valores por defecto')
currentModes.value = []
systemValues.value = {}
//console.log('Initial setValidMode')
setValidMode(JSONmodel.rootSubsystem)
}).finally(() => {
isLoading.value = false
})
}
function loadModelURL(path) {
isLoading.value = true
let doc = xmlModelLoader(`${path}`)
doc.then((model) => {
xmlModel.value = model
let JSONmodel = parseToPorisModel(this, model)
values.value = JSONmodel.values
modes.value = JSONmodel.modes
subsystems.value = JSONmodel.subsystems
rootSubsystem.value = JSONmodel.rootSubsystem
//console.log('LIMPIANDO valores por defecto')
currentModes.value = []
systemValues.value = {}
//console.log('Initial setValidMode')
setValidMode(JSONmodel.rootSubsystem)
}).finally(() => {
isLoading.value = false
})
}
function getValue(id) {
return values.value.find((element) => element.id == id)
}
function getMode(id) {
return modes.value.find((element) => element.id == id)
}
function getSubsystem(id) {
return subsystems.value.find((element) => element.id == id)
}
function setValidModeFrom(subsystem, mode, validModes) {
subsystem.validModes = validModes
if (validModes.length > 0) {
let prevMode = subsystem.currentMode
let candidate = null
if (validModes.length > 0) {
if (mode != null && validModes.includes(mode)) {
// console.log(`--> setValidMode_ candidate mode ${mode.name}`)
candidate = mode
} else {
if (validModes.includes(subsystem.defaultMode)) {
candidate = subsystem.defaultMode
// console.log(`--> setValidMode_ candidate valid default ${subsystem.defaultMode.name}`)
} else {
candidate = validModes[0]
// console.log(`--> setValidMode_ candidate first mode ${subsystem.validModes[0].name}`)
}
}
} else {
// console.log(`--> setValidMode_ candidate default (root)`)
candidate = subsystem.defaultMode
}
if (prevMode != candidate) {
//console.log(`*** setValidMode_() subsystem:`, subsystem.name, candidate.name)
subsystem.currentMode = candidate
subsystem.candidateMode = candidate
currentModes.value[`${subsystem.id}`] = candidate
//console.log(`setValidMode Value set`, subsystem.currentMode)
if (subsystem.hasValues) {
getSystemValue(subsystem, subsystem.currentMode)
}
if (subsystem.hasSubsystems) {
subsystem.subsystemsNodes.forEach((ss) => {
setValidSubMode(ss, ss.currentMode, subsystem.currentMode)
})
}
}
return candidate
} else {
subsystem.currentMode = null
subsystem.candidateMode = null
// console.log(`--> setValidMode_ null (no valid mode available)`)
return null
}
}
/**
* Internal function that does the actual mode setting without tracking
*/
function setValidModeInternal(subsystem, mode) {
debugLog(`setValidMode_ ${subsystem.name} ${mode?.name}, NOT using supermode`)
let validModes = getValidObjModes(subsystem)
debugLog(` setValidMode validModes len ${validModes.length}`, validModes)
return setValidModeFrom(subsystem, mode, validModes)
}
/**
* Resets the whole tree of valid modes
* Pauses Vue reactivity tracking during the recursive update to avoid
* intermediate re-renders, then triggers a single update at the end.
* @param {*} subsystem
* @param {*} mode
*/
function setValidMode(subsystem, mode) {
pauseTracking()
let result
try {
result = setValidModeInternal(subsystem, mode)
} finally {
enableTracking()
}
// Trigger a single update for the reactive refs after all changes are done
triggerRef(currentModes)
triggerRef(systemValues)
return result
}
function setValidSubMode(subsystem, mode, supermode) {
// console.log(`setValidMode_ ${subsystem.name} ${mode?.name}, using supermode`, supermode.name)
let validModes = _intersection(subsystem.modesNodes, supermode.modesNodes)
/*
validModes.forEach((m) => {
debugLog(`setValidMode_ valid:`, m)
})
*/
debugLog(` setValidMode validModes len ${validModes.length}`, validModes)
return setValidModeFrom(subsystem, mode, validModes)
}
function getCurrentMode(modeOptions) {
const current = currentModes.value.reduce((accumulator, param) => {
if (modeOptions.includes(param.id)) {
return param
}
return accumulator
}, null)
if (current) {
return current
} else {
return modeOptions[0]
}
}
function getValidObjModes(obj) {
// if (obj.id === 534) {
// console.log(`getValidObjModes() obj.id: ${obj.id}`, obj)
//console.log(`getValidObjModes() getObjModes`, getObjModes(obj))
// console.log(`getValidObjModes() validModes`, validModes.value)
// console.log(`getValidObjModes() _intersection`, _intersection(obj.modesNodes, validModes.value))
// }
/*
debugLog(`obj: ${obj.name}`)
debugLog(`objModes: ${obj.modesNodes}`)
obj.modesNodes.forEach((m) => {
debugLog(m.name)
})
*/
if (obj.parent != null) {
debugLog(`parent: ${obj.parent.name}`)
debugLog(`parent mode: ${obj.parent.currentMode.name}`)
debugLog(`parent.mode.submodes:`, obj.parent.currentMode.modesNodes)
return _intersection(obj.modesNodes, obj.parent.currentMode.modesNodes)
} else {
return obj.modesNodes
}
}
function hasValidObjModes(obj) {
//console.log(`hasValidObjModes() id: ${obj.id}, ${getValidObjModes(obj).length > 0}`)
return getValidObjModes(obj).length > 0
}
function setSystemValue(system, value) {
// console.log(`setSystemValue() system: ${system.id}, value:`, value)
systemValues.value[`${system.id}`] = value
}
function getSystemValue(system, mode) {
let value = systemValues.value[`${system.id}`]
// console.log(
// `getSystemValue() system: ${system.id}, _isUndefined(value): ${_isUndefined(value)}, mode / value`,
// mode,
// value
// )
if (!mode.hasValues) {
//console.log(`getSystemValue() no value!!`)
return null
}
let valueOptions = mode.valuesNodes
//console.log(`value`,value)
if (
value != null &&
!_isUndefined(value) &&
valueOptions.find((val) => {
val.id === value.id
}) === null
) {
value = undefined
}
//console.log(`getModelValue() valueOptions[0].type: ${valueOptions[0].type}`)
if (valueOptions[0].type == VALUE_STRING_TYPE) {
if (_isUndefined(value)) {
value = valueOptions[0].defaultString
}
} else if (valueOptions[0].type == VALUE_DOUBLE_RANGE_TYPE) {
if (
_isUndefined(value) ||
valueOptions[0].rangemin > value ||
valueOptions[0].rangemax < value
) {
value = valueOptions[0].defaultFloat
}
} else if (valueOptions[0].type == VALUE_FILE_PATH_TYPE) {
//console.log('FILE')
if (_isUndefined(value)) {
value = valueOptions[0].defaultString
}
} else {
if (_isUndefined(value) || !valueOptions.includes(value)) {
value = valueOptions[0]
}
}
setSystemValue(system, value)
return value
}
/*
const doubleCount = computed(() => count.value * 2)
function increment() {
count.value++
}
*/
return {
xmlModel,
rootSubsystem,
isLoading,
currentModes,
systemValues,
loadModel,
loadModelURL,
getValue,
getMode,
getSubsystem,
getCurrentMode,
setValidMode,
getValidObjModes,
hasValidObjModes,
getSystemValue,
setSystemValue
}
})