This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
/
engine.js
315 lines (283 loc) · 12.2 KB
/
engine.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
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
315
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
'use strict';
const Logger = require('@accordproject/ergo-compiler').Logger;
const Util = require('@accordproject/ergo-compiler').Util;
/**
* <p>
* Engine class. Execution of template logic against a request object, returning a response to the caller.
* </p>
* @class
* @public
* @memberof module:ergo-engine
*/
class Engine {
/**
* Create the Engine.
*/
constructor() {
this.scripts = {};
}
/**
* Engine kind
* @return {string} which kind of engine
*/
kind() {
return 'empty';
}
/**
/**
* Compile a script for a JavaScript machine
* @param {string} script - the script
*/
compileVMScript(script) {
throw new Error('[compileVMScript] Cannot execute Engine: instantiate either VMEngine or EvalEngine');
}
/**
* Execute a call in a JavaScript machine
* @param {number} utcOffset - UTC Offset for this execution
* @param {object} context - global variables to set in the VM
* @param {object} script - the initial script to load
* @param {object} call - the execution call
*/
runVMScriptCall(utcOffset,context,script,call) {
throw new Error('[runVMScriptCall] Cannot execute Engine: instantiate either VMEngine or EvalEngine');
}
/**
* Clear the JavaScript logic cache
* @private
*/
clearCacheJsScript() {
this.scripts = {};
}
/**
* Compile and cache JavaScript logic
* @param {ScriptManager} scriptManager - the script manager
* @param {string} contractId - the contract identifier
* @return {VMScript} the cached script
* @private
*/
cacheJsScript(scriptManager, contractId) {
if (!this.scripts[contractId]) {
const allJsScripts = scriptManager.getCompiledJavaScript();
const script = this.compileVMScript(allJsScripts);
this.scripts[contractId] = script;
}
return this.scripts[contractId];
}
/**
* Trigger a clause, passing in the request object
* @param {LogicManager} logic - the logic
* @param {string} contractId - the contract identifier
* @param {object} contract - the contract data
* @param {object} request - the request, a JS object that can be deserialized
* using the Composer serializer.
* @param {object} state - the contract state, a JS object that can be deserialized
* using the Composer serializer.
* @param {string} currentTime - the definition of 'now'
* @param {object} options to the text generation
* @return {object} the result for the clause
*/
trigger(logic, contractId, contract, request, state, currentTime, options) {
// Set the current time and UTC Offset
const now = Util.setCurrentTime(currentTime);
const utcOffset = now.utcOffset();
const validOptions = options ? options : {
options: {
'$class': 'org.accordproject.ergo.options.Options',
'wrapVariables': false,
'template': false,
}
};
const validContract = logic.validateContract(contract); // ensure the contract is valid
const validRequest = logic.validateInput(request); // ensure the request is valid
const validState = logic.validateInput(state); // ensure the state is valid
Logger.debug('Engine processing request ' + request.$class + ' with state ' + state.$class);
const script = this.cacheJsScript(logic.getScriptManager(), contractId);
const callScript = logic.getDispatchCall();
const context = {
data: validContract.serialized,
state: validState,
request: validRequest
};
// execute the logic
const result = this.runVMScriptCall(utcOffset,now,validOptions,context,script,callScript);
const validResponse = logic.validateOutput(result.__response); // ensure the response is valid
const validNewState = logic.validateOutput(result.__state); // ensure the new state is valid
const validEmit = logic.validateOutputArray(result.__emit); // ensure all the emits are valid
const answer = {
'clause': contractId,
'request': request, // Keep the original request
'response': validResponse,
'state': validNewState,
'emit': validEmit,
};
return answer;
}
/**
* Invoke a clause, passing in the parameters for that clause
* @param {LogicManager} logic - the logic
* @param {string} contractId - the contract identifier
* @param {string} clauseName - the clause name
* @param {object} contract - the contract data
* @param {object} params - the clause parameters
* @param {object} state - the contract state, a JS object that can be deserialized
* using the Composer serializer.
* @param {string} currentTime - the definition of 'now'
* @param {object} options to the text generation
* @return {object} the result for the clause
*/
invoke(logic, contractId, clauseName, contract, params, state, currentTime, options) {
// Set the current time and UTC Offset
const now = Util.setCurrentTime(currentTime);
const utcOffset = now.utcOffset();
const validOptions = options ? options : {
options: {
'$class': 'org.accordproject.ergo.options.Options',
'wrapVariables': false,
'template': false,
}
};
const validContract = logic.validateContract(contract, options); // ensure the contract is valid
const validParams = logic.validateInputRecord(params); // ensure the parameters are valid
const validState = logic.validateInput(state); // ensure the state is valid
Logger.debug('Engine processing clause ' + clauseName + ' with state ' + state.$class);
const script = this.cacheJsScript(logic.getScriptManager(), contractId);
const callScript = logic.getInvokeCall(clauseName);
const context = {
data: validContract.serialized,
state: validState,
params: validParams
};
// execute the logic
const result = this.runVMScriptCall(utcOffset,now,validOptions,context,script,callScript);
const validResponse = logic.validateOutput(result.__response); // ensure the response is valid
const validNewState = logic.validateOutput(result.__state); // ensure the new state is valid
const validEmit = logic.validateOutputArray(result.__emit); // ensure all the emits are valid
const answer = {
'clause': contractId,
'params': params, // Keep the original params
'response': validResponse,
'state': validNewState,
'emit': validEmit,
};
return answer;
}
/**
* Initialize a clause
* @param {LogicManager} logic - the logic
* @param {string} contractId - the contract identifier
* @param {object} contract - the contract data
* @param {object} params - the clause parameters
* @param {string} currentTime - the definition of 'now'
* @param {object} options to the text generation
* @return {object} the result for the clause initialization
*/
init(logic, contractId, contract, params, currentTime, options) {
const defaultState = {
'$class':'org.accordproject.cicero.contract.AccordContractState',
'stateId':'org.accordproject.cicero.contract.AccordContractState#1'
};
return this.invoke(logic, contractId, 'init', contract, params, defaultState, currentTime, options);
}
/**
* Calculate formula
* @param {TemplateLogic} logic - the logic
* @param {string} contractId - the contract identifier
* @param {string} name - the formula name
* @param {object} contract - the contract data
* @param {string} currentTime - the definition of 'now'
* @param {object} options to the text generation
* @return {object} the result for draft
*/
calculate(logic, contractId, name, contract, currentTime, options) {
options = options || {};
const defaultState = {
'$class':'org.accordproject.cicero.contract.AccordContractState',
'stateId':'org.accordproject.cicero.contract.AccordContractState#1'
};
return this.invoke(logic, contractId, name, contract, {}, defaultState, currentTime, Object.assign(options, {convertResourcesToId: true}));
}
/**
* Compile then initialize a clause
*
* @param {LogicManager} logic - the logic
* @param {object} contract - the contract data
* @param {object} params - the clause parameters
* @param {string} currentTime - the definition of 'now'
* @param {object} options to the text generation
* @return {Promise} a promise that resolves to a result for the clause initialization
*/
compileAndInit(logic, contract, params, currentTime, options) {
return logic.compileLogic(false).then(() => {
const contractId = logic.getContractName();
return this.init(logic, contractId, contract, params, currentTime, options);
});
}
/**
* Compile then calculate formula
*
* @param {TemplateLogic} logic - the logic
* @param {string} name - the formula name
* @param {object} contract - the contract data
* @param {string} currentTime - the definition of 'now'
* @param {object} options to the text generation
* @return {Promise} a promise that resolves to a result for the clause initialization
*/
compileAndCalculate(logic, name, contract, currentTime, options) {
return logic.compileLogic(false).then(() => {
const contractId = logic.getContractName();
return this.calculate(logic, contractId, name, contract, currentTime, options);
});
}
/**
* Compile then invoke a clause
*
* @param {LogicManager} logic - the logic
* @param {string} clauseName - the clause name
* @param {object} contract contract data in JSON
* @param {object} params - the clause parameters
* @param {object} state - the contract state, a JS object that can be deserialized
* using the Composer serializer.
* @param {string} currentTime - the definition of 'now'
* @param {object} options to the text generation
* @return {Promise} a promise that resolves to a result for the clause initialization
*/
compileAndInvoke(logic, clauseName, contract, params, state, currentTime, options) {
return logic.compileLogic(false).then(() => {
const contractId = logic.getContractName();
return this.invoke(logic, contractId, clauseName, contract, params, state, currentTime, options);
});
}
/**
* Compile then trigger a clause, passing in the request object
*
* @param {LogicManager} logic - the logic
* @param {object} contract - the contract data
* @param {object} request - the request, a JS object that can be deserialized
* using the Composer serializer.
* @param {object} state - the contract state, a JS object that can be deserialized
* using the Composer serializer.
* @param {string} currentTime - the definition of 'now'
* @param {object} options to the text generation
* @return {Promise} a promise that resolves to a result for the clause
*/
compileAndTrigger(logic, contract, request, state, currentTime, options) {
return logic.compileLogic(false).then(() => {
const contractId = logic.getContractName();
return this.trigger(logic, contractId, contract, request, state, currentTime, options);
});
}
}
module.exports = Engine;