[systeminfo] Prevent possible crash when failure initialization
[platform/core/api/webapi-plugins.git] / src / ml / js / ml_pipeline.js
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  *    Licensed under the Apache License, Version 2.0 (the "License");
5  *    you may not use this file except in compliance with the License.
6  *    You may obtain a copy of the License at
7  *
8  *        http://www.apache.org/licenses/LICENSE-2.0
9  *
10  *    Unless required by applicable law or agreed to in writing, software
11  *    distributed under the License is distributed on an "AS IS" BASIS,
12  *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *    See the License for the specific language governing permissions and
14  *    limitations under the License.
15  */
16
17 var kPipelineStateChangeListenerNamePrefix = 'MLPipelineStateChangeListener';
18
19 //PipelineManager::createPipeline() begin
20 var ValidPipelineDisposeExceptions = ['NotFoundError', 'NotSupportedError', 'AbortError'];
21
22 var nextPipelineId = 1;
23 function NextPipelineId() {
24     return nextPipelineId++;
25 }
26
27 var ValidPipelineManagerCreatePipelineExceptions = [
28     'InvalidValuesError',
29     'TypeMismatchError',
30     'NotSupportedError',
31     'SecurityError',
32     'AbortError'
33 ];
34
35 var CreatePipeline = function() {
36     privUtils_.log('Entered PipelineManager.createPipeline()');
37     var args = validator_.validateArgs(arguments, [
38         {
39             name: 'definition',
40             type: validator_.Types.STRING
41         },
42         {
43             name: 'listener',
44             type: validator_.Types.FUNCTION,
45             optional: true,
46             nullable: true
47         }
48     ]);
49
50     if (!args.has.definition) {
51         throw new WebAPIException(
52             WebAPIException.INVALID_VALUES_ERR,
53             'Invalid parameter: pipeline definition is mandatory'
54         );
55     }
56
57     var pipeline = new Pipeline(NextPipelineId());
58     var nativeArgs = {
59         id: pipeline._id,
60         definition: args.definition
61     };
62
63     if (args.listener) {
64         nativeArgs.listenerName = kPipelineStateChangeListenerNamePrefix + pipeline._id;
65         var stateChangeListener = function(stateObject) {
66             args.listener(stateObject.state);
67         };
68         native_.addListener(nativeArgs.listenerName, stateChangeListener);
69     }
70
71     var result = native_.callSync('MLPipelineManagerCreatePipeline', nativeArgs);
72
73     if (native_.isFailure(result)) {
74         if (nativeArgs.listenerName) {
75             native_.removeListener(nativeArgs.listenerName);
76         }
77         throw native_.getErrorObjectAndValidate(
78             result,
79             ValidPipelineManagerCreatePipelineExceptions,
80             AbortError
81         );
82     }
83
84     return pipeline;
85 };
86 //PipelineManager::createPipeline() end
87
88 //Pipeline::state begin
89 var ValidPipelineStateExceptions = ['NotSupportedError', 'AbortError'];
90 var Pipeline = function(id) {
91     Object.defineProperties(this, {
92         state: {
93             enumerable: true,
94             get: function() {
95                 var result = native_.callSync('MLPipelineGetState', {
96                     id: id
97                 });
98                 if (native_.isFailure(result)) {
99                     throw native_.getErrorObjectAndValidate(
100                         result,
101                         ValidPipelineStateExceptions,
102                         AbortError
103                     );
104                 }
105
106                 return result.result;
107             }
108         },
109         _id: {
110             value: id
111         }
112     });
113 };
114 //Pipeline::state end
115
116 //Pipeline::start() begin
117 var ValidPipelineStartStopExceptions = [
118     'NotFoundError',
119     'NotSupportedError',
120     'AbortError'
121 ];
122 Pipeline.prototype.start = function() {
123     var nativeArgs = {
124         id: this._id
125     };
126
127     var result = native_.callSync('MLPipelineStart', nativeArgs);
128     if (native_.isFailure(result)) {
129         throw native_.getErrorObjectAndValidate(
130             result,
131             ValidPipelineStartStopExceptions,
132             AbortError
133         );
134     }
135 };
136 //Pipeline::start() end
137
138 //Pipeline::stop() begin
139 Pipeline.prototype.stop = function() {
140     var nativeArgs = {
141         id: this._id
142     };
143
144     var result = native_.callSync('MLPipelineStop', nativeArgs);
145     if (native_.isFailure(result)) {
146         throw native_.getErrorObjectAndValidate(
147             result,
148             ValidPipelineStartStopExceptions,
149             AbortError
150         );
151     }
152 };
153 //Pipeline::stop() end
154
155 //Pipeline::dispose() begin
156 Pipeline.prototype.dispose = function() {
157     var result = native_.callSync('MLPipelineDispose', { id: this._id });
158
159     if (native_.isFailure(result)) {
160         throw native_.getErrorObjectAndValidate(
161             result,
162             ValidPipelineDisposeExceptions,
163             AbortError
164         );
165     }
166 };
167 //Pipeline::dispose() end
168
169 //Pipeline::getNodeInfo() begin
170 var NodeInfo = function(name, pipeline_id) {
171     Object.defineProperties(this, {
172         name: { enumerable: true, writable: false, value: name },
173         _pipeline_id: { value: pipeline_id }
174     });
175 };
176
177 var ValidPipelineGetNodeInfoExceptions = [
178     'InvalidValuesError',
179     'NotFoundError',
180     'NotSupportedError',
181     'AbortError'
182 ];
183
184 Pipeline.prototype.getNodeInfo = function() {
185     var args = validator_.validateArgs(arguments, [
186         {
187             name: 'name',
188             type: validator_.Types.STRING
189         }
190     ]);
191
192     var nativeArgs = {
193         id: this._id,
194         name: args.name
195     };
196
197     var result = native_.callSync('MLPipelineGetNodeInfo', nativeArgs);
198     if (native_.isFailure(result)) {
199         throw native_.getErrorObjectAndValidate(
200             result,
201             ValidPipelineGetNodeInfoExceptions,
202             AbortError
203         );
204     }
205
206     return new NodeInfo(args.name, this._id);
207 };
208 //Pipeline::getNodeInfo() end
209
210 //Pipeline::getSource() begin
211
212 //Pipeline::getSource() end
213
214 //Pipeline::getSwitch() begin
215 function Switch(name, type, pipeline_id) {
216     Object.defineProperties(this, {
217         name: {
218             enumerable: true,
219             value: name
220         },
221         type: {
222             enumerable: true,
223             value: type
224         },
225         _pipeline_id: {
226             value: pipeline_id
227         }
228     });
229 }
230
231 var ValidPipelineGetSwitchExceptions = [
232     'InvalidStateError',
233     'InvalidValuesError',
234     'NotFoundError',
235     'NotSupportedError',
236     'AbortError'
237 ];
238 Pipeline.prototype.getSwitch = function() {
239     var args = validator_.validateArgs(arguments, [
240         {
241             name: 'name',
242             type: validator_.Types.STRING
243         }
244     ]);
245
246     var nativeArgs = {
247         name: args.name,
248         id: this._id
249     };
250     var result = native_.callSync('MLPipelineGetSwitch', nativeArgs);
251     if (native_.isFailure(result)) {
252         throw native_.getErrorObjectAndValidate(
253             result,
254             ValidPipelineGetSwitchExceptions,
255             AbortError
256         );
257     }
258
259     return new Switch(nativeArgs.name, result.type, this._id);
260 };
261 //Pipeline::getSwitch() end
262
263 //Pipeline::getValve() begin
264 function Valve(name, pipeline_id) {
265     Object.defineProperties(this, {
266         name: {
267             enumerable: true,
268             value: name
269         },
270         _pipeline_id: {
271             value: pipeline_id
272         }
273     });
274 }
275
276 var ValidPipelineGetValveExceptions = [
277     'InvalidValuesError',
278     'NotFoundError',
279     'NotSupportedError',
280     'AbortError'
281 ];
282 Pipeline.prototype.getValve = function() {
283     var args = validator_.validateArgs(arguments, [
284         {
285             name: 'name',
286             type: validator_.Types.STRING
287         }
288     ]);
289
290     var nativeArgs = {
291         name: args.name,
292         id: this._id
293     };
294
295     var result = native_.callSync('MLPipelineGetValve', nativeArgs);
296     if (native_.isFailure(result)) {
297         throw native_.getErrorObjectAndValidate(
298             result,
299             ValidPipelineGetValveExceptions,
300             AbortError
301         );
302     }
303
304     return new Valve(nativeArgs.name, this._id);
305 };
306 //Pipeline::getValve() end
307
308 //Pipeline::registerSinkCallback() begin
309
310 //Pipeline::registerSinkCallback() end
311
312 //Pipeline::unregisterSinkCallback() begin
313
314 //Pipeline::unregisterSinkCallback() end
315
316 //Pipeline::registerCustomFilter() begin
317
318 //Pipeline::registerCustomFilter() end
319
320 //Pipeline::unregisterCustomFilter() begin
321
322 //Pipeline::unregisterCustomFilter() end
323
324 var PropertyType = {
325     BOOLEAN: 'BOOLEAN',
326     DOUBLE: 'DOUBLE',
327     ENUM: 'ENUM',
328     INT32: 'INT32',
329     INT64: 'INT64',
330     UINT32: 'UINT32',
331     UINT64: 'UINT64',
332     STRING: 'STRING'
333 };
334 //NodeInfo::getProperty() begin
335 var ValidNodeInfoGetPropertyExceptions = [
336     'InvalidValuesError',
337     'NotFoundError',
338     'NotSupportedError',
339     'TypeMismatchError',
340     'AbortError'
341 ];
342 NodeInfo.prototype.getProperty = function() {
343     var args = validator_.validateArgs(arguments, [
344         {
345             name: 'name',
346             type: validator_.Types.STRING
347         },
348         {
349             name: 'propertyType',
350             type: types_.ENUM,
351             values: Object.keys(PropertyType)
352         }
353     ]);
354
355     var nativeArgs = {
356         id: this._pipeline_id,
357         nodeName: this.name,
358         name: args.name,
359         type: args.propertyType
360     };
361
362     var result = native_.callSync('MLPipelineNodeInfoGetProperty', nativeArgs);
363     if (native_.isFailure(result)) {
364         throw native_.getErrorObjectAndValidate(
365             result,
366             ValidNodeInfoGetPropertyExceptions,
367             AbortError
368         );
369     }
370
371     return result.property;
372 };
373 //NodeInfo::getProperty() end
374
375 //NodeInfo::setProperty() begin
376 var ValidNodeInfoSetPropertyExceptions = [
377     'InvalidValuesError',
378     'NotFoundError',
379     'NotSupportedError',
380     'TypeMismatchError',
381     'AbortError'
382 ];
383 NodeInfo.prototype.setProperty = function() {
384     var args = validator_.validateArgs(arguments, [
385         {
386             name: 'name',
387             type: validator_.Types.STRING
388         },
389         {
390             name: 'propertyType',
391             type: types_.ENUM,
392             values: Object.keys(PropertyType)
393         },
394         {
395             name: 'property',
396             type: types_.SIMPLE_TYPE
397         }
398     ]);
399
400     var nativeArgs = {
401         id: this._pipeline_id,
402         nodeName: this.name,
403         name: args.name,
404         type: args.propertyType,
405         property: args.property
406     };
407
408     var result = native_.callSync('MLPipelineNodeInfoSetProperty', nativeArgs);
409     if (native_.isFailure(result)) {
410         throw native_.getErrorObjectAndValidate(
411             result,
412             ValidNodeInfoSetPropertyExceptions,
413             AbortError
414         );
415     }
416 };
417 //NodeInfo::setProperty() end
418
419 //Source::inputTensorsInfo begin
420
421 //Source::inputTensorsInfo end
422
423 //Source::inputData() begin
424
425 //Source::inputData() end
426
427 //Switch::getPadList() begin
428 var ValidSwitchGetPadListExceptions = [
429     'InvalidStateError',
430     'NotFoundError',
431     'NotSupportedError',
432     'AbortError'
433 ];
434 Switch.prototype.getPadList = function() {
435     var nativeArgs = {
436         name: this.name,
437         id: this._pipeline_id
438     };
439
440     var result = native_.callSync('MLPipelineSwitchGetPadList', nativeArgs);
441
442     if (native_.isFailure(result)) {
443         throw native_.getErrorObjectAndValidate(
444             result,
445             ValidSwitchGetPadListExceptions,
446             AbortError
447         );
448     }
449
450     return result.result;
451 };
452 //Switch::getPadList() end
453
454 //Switch::select() begin
455 var ValidSwitchSelectExceptions = [
456     'InvalidValuesError',
457     'NotFoundError',
458     'NotSupportedError',
459     'AbortError'
460 ];
461 Switch.prototype.select = function() {
462     var args = validator_.validateArgs(arguments, [
463         {
464             name: 'padName',
465             type: validator_.Types.STRING
466         }
467     ]);
468
469     if (!args.has.padName) {
470         throw new WebAPIException(
471             WebAPIException.INVALID_VALUES_ERR,
472             'Invalid parameter: pad name is mandatory'
473         );
474     }
475
476     var nativeArgs = {
477         id: this._pipeline_id,
478         name: this.name,
479         padName: args.padName
480     };
481
482     var result = native_.callSync('MLPipelineSwitchSelect', nativeArgs);
483     if (native_.isFailure(result)) {
484         throw native_.getErrorObjectAndValidate(
485             result,
486             ValidSwitchSelectExceptions,
487             AbortError
488         );
489     }
490 };
491 //Switch::select() end
492
493 //Valve::setOpen() begin
494
495 //Valve::setOpen() end
496 var MachineLearningPipeline = function() {};
497
498 MachineLearningPipeline.prototype.createPipeline = CreatePipeline;
499
500 // ML Pipeline API