Skip to content

Commit 9e55762

Browse files
authored
Added process namespace support (#12)
* Added namespace support to ProcessRegistry * Just make the Process class augment the specification, better error messages * Fully use the ProcessRegistry from js-commons * Fixed issues reported from @christophfriedrich in the PR review
1 parent 49ab43c commit 9e55762

File tree

10 files changed

+101
-159
lines changed

10 files changed

+101
-159
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
"assets/subtype-schemas.json"
2626
],
2727
"dependencies": {
28-
"@openeo/js-commons": "^1.3.0",
28+
"@openeo/js-commons": "^1.4.0",
2929
"ajv": "^6.12.0"
3030
},
3131
"devDependencies": {

src/error.js

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
11
const Utils = require('./utils');
22

33
const MESSAGES = {
4-
"MultipleResultNodes": "Multiple result nodes specified for process graph.",
5-
"StartNodeMissing": "No start nodes found for process graph.",
6-
"ResultNodeMissing": "No result node found for process graph.",
7-
"MultipleResultNodesCallback": "Multiple result nodes specified for the callback in the process '{process_id}' (node: '{node_id}').",
8-
"StartNodeMissingCallback": "No start nodes found for the callback in the process '{process_id}' (node: '{node_id}')'.",
9-
"ResultNodeMissingCallback": "No result node found for the callback in the process '{process_id}' (node: '{node_id}').",
10-
"ReferencedNodeMissing": "Referenced node '{node_id}' doesn't exist.",
11-
"NodeIdInvalid": "Invalid node id specified in process graph.",
12-
"NodeInvalid": "Process graph node '{node_id}' is not a valid object.",
13-
"ProcessIdMissing": "Process graph node '{node_id}' doesn't contain a process id.",
14-
"ProcessGraphParameterMissing": "Invalid parameter '{argument}' requested in the process '{process_id}' (node: '{node_id}').",
15-
"ProcessUnsupported": "Process '{process}' is not supported.",
16-
"ProcessArgumentUnsupported": "Process '{process}' does not support the following arguments: {arguments}",
17-
"ProcessArgumentRequired": "Process '{process}' requires argument '{argument}'.",
18-
"ProcessArgumentInvalid": "The argument '{argument}' in process '{process}' is invalid: {reason}",
4+
"MultipleResultNodes": "Multiple result nodes specified for the process.",
5+
"StartNodeMissing": "No start nodes found for the process.",
6+
"ResultNodeMissing": "No result node found for the process.",
7+
"ReferencedNodeMissing": "Referenced process node '{node_id}' doesn't exist.",
8+
"NodeIdInvalid": "Invalid node id specified in the process.",
9+
"NodeInvalid": "Process node '{node_id}' is not a valid object.",
10+
"ProcessIdMissing": "Process node '{node_id}' doesn't contain a process id.",
11+
"ProcessGraphParameterMissing": "Invalid parameter '{argument}' referenced in process node '{node_id}' (process: {process_id}, namespace: {namespace}).",
12+
"ProcessUnsupported": "Process '{process}' (namespace: {namespace}) is not supported.",
13+
"ProcessArgumentUnsupported": "Process '{process}' (namespace: {namespace}) does not support the following arguments: {arguments}",
14+
"ProcessArgumentRequired": "Process '{process}' (namespace: {namespace}) requires argument '{argument}'.",
15+
"ProcessArgumentInvalid": "The argument '{argument}' in process '{process}' (namespace: {namespace}) is invalid: {reason}",
1916
"ProcessGraphMissing": "No process graph specified",
2017
"ProcessMissing": "No process specified"
2118
};

src/node.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class ProcessGraphNode {
2323
this.processGraph = parent;
2424
this.source = node;
2525
this.process_id = node.process_id;
26+
this.namespace = node.namespace || null;
2627
this.arguments = Utils.isObject(node.arguments) ? Utils.deepClone(node.arguments) : {};
2728
this.description = node.description || null;
2829
this.isResultNode = node.result || false;
@@ -36,6 +37,7 @@ class ProcessGraphNode {
3637
let args = Utils.mapObjectValues(this.arguments, arg => Utils.isObject(arg) && typeof arg.toJSON === 'function' ? arg.toJSON() : arg);
3738
return Object.assign({}, this.source, {
3839
process_id: this.process_id,
40+
namespace: this.namespace,
3941
description: this.description,
4042
arguments: args,
4143
result: this.isResultNode
@@ -113,7 +115,8 @@ class ProcessGraphNode {
113115
throw new ProcessGraphError('ProcessGraphParameterMissing', {
114116
argument: name,
115117
node_id: this.id,
116-
process_id: this.process_id
118+
process_id: this.process_id,
119+
namespace: this.namespace || 'n/a'
117120
});
118121
}
119122
}

src/process.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@ const Utils = require('./utils');
1111
class BaseProcess {
1212

1313
constructor(spec) {
14-
this.spec = spec; // Keep original specification data
15-
1614
// Make properties easily accessible
1715
Object.assign(this, spec);
1816

@@ -25,7 +23,7 @@ class BaseProcess {
2523
}
2624

2725
toJSON() {
28-
return this.spec;
26+
return Utils.omitFromObject(this, ["validate", "validateArgument", "execute", "test"]);
2927
}
3028

3129
async validate(node) {
@@ -34,6 +32,7 @@ class BaseProcess {
3432
if (unsupportedArgs.length > 0) {
3533
throw new ProcessGraphError('ProcessArgumentUnsupported', {
3634
process: this.id,
35+
namespace: this.namespace || 'n/a',
3736
arguments: unsupportedArgs
3837
});
3938
}
@@ -46,6 +45,7 @@ class BaseProcess {
4645
if (!param.optional) {
4746
throw new ProcessGraphError('ProcessArgumentRequired', {
4847
process: this.id,
48+
namespace: this.namespace || 'n/a',
4949
argument: param.name
5050
});
5151
}
@@ -73,6 +73,7 @@ class BaseProcess {
7373
if (!JsonSchemaValidator.isSchemaCompatible(param.schema, callbackParam.schema)) {
7474
throw new ProcessGraphError('ProcessArgumentInvalid', {
7575
process: this.id,
76+
namespace: this.namespace || 'n/a',
7677
argument: path,
7778
reason: "Schema for parameter '" + arg.from_parameter + "' not compatible with reference"
7879
});
@@ -86,7 +87,8 @@ class BaseProcess {
8687
throw new ProcessGraphError('ProcessGraphParameterMissing', {
8788
argument: arg.from_parameter,
8889
node_id: node.id,
89-
process_id: node.process_id
90+
process_id: node.process_id,
91+
namespace: node.namespace || 'n/a'
9092
});
9193
}
9294

@@ -98,6 +100,7 @@ class BaseProcess {
98100
if (!JsonSchemaValidator.isSchemaCompatible(param.schema, parameter.schema)) {
99101
throw new ProcessGraphError('ProcessArgumentInvalid', {
100102
process: this.id,
103+
namespace: this.namespace || 'n/a',
101104
argument: path,
102105
reason: "Schema for parameter '" + arg.from_parameter + "' not compatible"
103106
});
@@ -111,6 +114,7 @@ class BaseProcess {
111114
if (!JsonSchemaValidator.isSchemaCompatible(param.schema, process.returns.schema)) {
112115
throw new ProcessGraphError('ProcessArgumentInvalid', {
113116
process: this.id,
117+
namespace: this.namespace || 'n/a',
114118
argument: path,
115119
reason: "Schema for result '" + arg.from_node + "' not compatible"
116120
});
@@ -155,6 +159,7 @@ class BaseProcess {
155159
if (errors.length > 0) {
156160
throw new ProcessGraphError('ProcessArgumentInvalid', {
157161
process: this.id,
162+
namespace: this.namespace || 'n/a',
158163
argument: path,
159164
reason: errors
160165
});
@@ -164,13 +169,13 @@ class BaseProcess {
164169

165170
/* istanbul ignore next */
166171
async execute(/*node*/) {
167-
throw "execute not implemented yet";
172+
throw new Error(`execute not implemented yet for process '${this.id}' (namespace: ${this.namespace || 'n/a'})`);
168173
}
169174

170175
/* istanbul ignore next */
171176
test() {
172177
// Run the tests from the examples
173-
throw "test not implemented yet";
178+
throw new Error(`test not implemented yet for process '${this.id}' (namespace: ${this.namespace || 'n/a'})`);
174179
}
175180

176181
}

src/processgraph.js

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const BaseProcess = require('./process');
12
const ErrorList = require('./errorlist');
23
const JsonSchemaValidator = require('./jsonschema');
34
const ProcessGraphError = require('./error');
@@ -74,6 +75,10 @@ class ProcessGraph {
7475
return this.copyProcessGraphInstanceProperties(pg);
7576
}
7677

78+
createProcessInstance(process) {
79+
return new BaseProcess(process);
80+
}
81+
7782
copyProcessGraphInstanceProperties(pg) {
7883
pg.allowEmptyGraph = this.allowEmptyGraph;
7984
pg.fillProcessParameters = this.fillProcessParameters;
@@ -472,22 +477,28 @@ class ProcessGraph {
472477
/**
473478
* Gets the process for the given process ID or node.
474479
*
475-
* @param {ProcessGraphNode|string} id
480+
* @param {ProcessGraphNode|string} process
481+
* @param {?string} [namespace=null]
476482
* @returns {object|null}
477483
* @throws {ProcessGraphError} - ProcessUnsupported
478484
*/
479-
getProcess(id) {
485+
getProcess(process, namespace = null) {
480486
if (this.processRegistry === null) {
481487
return null;
482488
}
483-
if (id instanceof ProcessGraphNode) {
484-
id = id.process_id;
489+
let id;
490+
if (process instanceof ProcessGraphNode) {
491+
id = process.process_id;
492+
namespace = process.namespace;
493+
}
494+
else {
495+
id = process;
485496
}
486-
var process = this.processRegistry.get(id);
487-
if (process === null) {
488-
throw new ProcessGraphError('ProcessUnsupported', {process: id});
497+
let spec = this.processRegistry.get(id, namespace);
498+
if (spec === null) {
499+
throw new ProcessGraphError('ProcessUnsupported', {process: id, namespace: namespace || 'n/a'});
489500
}
490-
return process;
501+
return this.createProcessInstance(spec);
491502
}
492503

493504
getParentProcessId() {

src/registry.js

Lines changed: 7 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,16 @@
1-
const Process = require('./process');
2-
const Utils = require('./utils');
1+
const CommonProcessRegistry = require('@openeo/js-commons/src/processRegistry');
32

43
/**
54
* Central registry for processes.
65
*
6+
* Implementation has been moved to @openeo/js-commons.
7+
* This wrapper here is only available for backward compatibility.
8+
*
9+
* @todo Remove in 2.0.0.
10+
* @augments CommonProcessRegistry
711
* @class
812
*/
9-
class ProcessRegistry {
10-
11-
constructor(processes = []) {
12-
// Keys added to this object must be lowercase!
13-
this.processes = {};
14-
this.addAll(processes);
15-
}
16-
17-
addAll(processes) {
18-
for(var i in processes) {
19-
this.add(processes[i]);
20-
}
21-
}
22-
23-
add(process) {
24-
if (!Utils.isObject(process)) {
25-
throw new Error("Invalid process; not an object.");
26-
}
27-
28-
let isImpl = process instanceof Process;
29-
if (!isImpl && typeof process.toJSON === 'function') {
30-
var json = process.toJSON();
31-
if (Utils.isObject(json)) {
32-
process = json;
33-
}
34-
}
35-
if (typeof process.id !== 'string') {
36-
throw new Error("Invalid process; no id specified.");
37-
}
38-
this.processes[process.id.toLowerCase()] = isImpl ? process : new Process(process);
39-
}
40-
41-
count() {
42-
return Utils.size(this.processes);
43-
}
44-
45-
all() {
46-
return Object.values(this.processes);
47-
}
48-
49-
get(id) {
50-
if (typeof id === 'string') {
51-
var pid = id.toLowerCase();
52-
if (typeof this.processes[pid] !== 'undefined') {
53-
return this.processes[pid];
54-
}
55-
}
56-
return null;
57-
}
58-
59-
toJSON() {
60-
return Object.values(this.processes).map(impl => impl.toJSON());
61-
}
13+
class ProcessRegistry extends CommonProcessRegistry {
6214

6315
}
6416

tests/errorlist.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ describe('ErrorList test', () => {
2828
expect(el.getAll()).toStrictEqual([e1]);
2929
});
3030
var e2 = new ProcessGraphError("MultipleResultNodes");
31-
var msg2 = "Multiple result nodes specified for process graph.";
31+
var msg2 = "Multiple result nodes specified for the process.";
3232
test('Adding second error - custom impl.', () => {
3333
el.add(e2);
3434
expect(el.first()).toBe(e1);

tests/node.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ var subProcess = {
1010
process_graph: {
1111
absolute: {
1212
process_id: "absolute",
13+
namespace: null,
1314
arguments: {
1415
x: from_x
1516
},
@@ -25,6 +26,7 @@ var contextResult = [fooDefault, undefined].concat(contextAdditionals);
2526

2627
var nodeObj = {
2728
process_id: "apply",
29+
namespace: null,
2830
arguments: {
2931
process: subProcess,
3032
context: contextObj,

0 commit comments

Comments
 (0)