1
- import { Application , Template } from "@exabyte-io/ade.js" ;
2
- import { HashedInputArrayMixin } from "@mat3ra/code/dist/js/entity" ;
3
- import { removeTimestampableKeysFromConfig } from "@mat3ra/code/dist/js/utils" ;
4
- import { mix } from "mixwith" ;
1
+ import { Template } from "@exabyte-io/ade.js" ;
2
+ import ApplicationRegistry from "@exabyte-io/ade.js/dist/js/ApplicationRegistry" ;
3
+ import {
4
+ calculateHashFromObject ,
5
+ removeCommentsFromSourceCode ,
6
+ removeEmptyLinesFromString ,
7
+ removeTimestampableKeysFromConfig ,
8
+ } from "@mat3ra/code/dist/js/utils" ;
5
9
import _ from "underscore" ;
6
10
7
11
import { BaseUnit } from "./base" ;
8
12
9
- export class ExecutionUnit extends mix ( BaseUnit ) . with ( HashedInputArrayMixin ) {
10
- static Application = Application ;
11
-
12
- static Template = Template ;
13
-
13
+ export class ExecutionUnit extends BaseUnit {
14
14
// keys to be omitted during toJSON
15
15
static omitKeys = [
16
16
"job" ,
@@ -22,18 +22,76 @@ export class ExecutionUnit extends mix(BaseUnit).with(HashedInputArrayMixin) {
22
22
"hasRelaxation" ,
23
23
] ;
24
24
25
+ /**
26
+ * @override this method to provide entities from other sources
27
+ */
25
28
_initApplication ( config ) {
26
- this . _application = this . constructor . Application . create ( config . application ) ;
27
- this . _executable = this . _application . getExecutableByConfig ( config . executable ) ;
28
- this . _flavor = this . _executable . getFlavorByConfig ( config . flavor ) ;
29
+ this . _application = ApplicationRegistry . createApplication ( config . application ) ;
30
+ this . _executable = ApplicationRegistry . getExecutableByConfig (
31
+ this . _application . name ,
32
+ config . executable ,
33
+ ) ;
34
+ this . _flavor = ApplicationRegistry . getFlavorByConfig ( this . _executable , config . flavor ) ;
29
35
this . _templates = this . _flavor ? this . _flavor . inputAsTemplates : [ ] ;
30
36
}
31
37
38
+ /**
39
+ * @override this method to provide default executable from other source
40
+ */
41
+ _getDefaultExecutable ( ) {
42
+ return ApplicationRegistry . getExecutableByName ( this . application . name ) ;
43
+ }
44
+
45
+ /**
46
+ * @override this method to provide default flavor from other source
47
+ */
48
+ _getDefaultFlavor ( ) {
49
+ return ApplicationRegistry . getFlavorByName ( this . executable . name ) ;
50
+ }
51
+
52
+ /**
53
+ * @override this method to provide custom templates
54
+ */
55
+ _getTemplatesFromInput ( ) {
56
+ return this . getInput ( ) . map ( ( i ) => new Template ( i ) ) ;
57
+ }
58
+
59
+ /**
60
+ * @override this method to provide custom input from other sources
61
+ */
62
+ _getInput ( ) {
63
+ return (
64
+ this . input ||
65
+ ApplicationRegistry . getInputAsRenderedTemplates (
66
+ this . flavor ,
67
+ this . getCombinedContext ( ) ,
68
+ ) ||
69
+ [ ]
70
+ ) ;
71
+ }
72
+
73
+ /**
74
+ * @override this method to provide custom input as templates
75
+ */
76
+ _getInputAsTemplates ( ) {
77
+ return ApplicationRegistry . getInputAsTemplates ( this . flavor ) ;
78
+ }
79
+
32
80
_initRuntimeItems ( keys , config ) {
33
81
this . _initApplication ( config ) ;
34
82
super . _initRuntimeItems ( keys ) ;
35
83
}
36
84
85
+ /*
86
+ * @summary expects an array with elements containing field [{content: "..."}]
87
+ */
88
+ get hashFromArrayInputContent ( ) {
89
+ const objectForHashing = this . _getInput ( ) . map ( ( i ) => {
90
+ return removeEmptyLinesFromString ( removeCommentsFromSourceCode ( i . content ) ) ;
91
+ } ) ;
92
+ return calculateHashFromObject ( objectForHashing ) ;
93
+ }
94
+
37
95
get name ( ) {
38
96
return this . prop ( "name" , this . flavor . name ) ;
39
97
}
@@ -54,29 +112,25 @@ export class ExecutionUnit extends mix(BaseUnit).with(HashedInputArrayMixin) {
54
112
return this . _templates ;
55
113
}
56
114
57
- get templatesFromInput ( ) {
58
- return this . input . map ( ( i ) => new this . constructor . Template ( i ) ) ;
59
- }
60
-
61
115
setApplication ( application , omitSettingExecutable = false ) {
62
116
this . _application = application ;
63
117
this . setProp ( "application" , application . toJSON ( ) ) ;
64
118
if ( ! omitSettingExecutable ) {
65
- this . setExecutable ( this . application . defaultExecutable ) ;
119
+ this . setExecutable ( this . _getDefaultExecutable ( ) ) ;
66
120
}
67
121
}
68
122
69
123
setExecutable ( executable ) {
70
124
this . _executable = executable ;
71
125
this . setProp ( "executable" , executable . toJSON ( ) ) ;
72
- this . setFlavor ( this . executable . defaultFlavor ) ;
126
+ this . setFlavor ( this . _getDefaultFlavor ( ) ) ;
73
127
}
74
128
75
129
setFlavor ( flavor ) {
76
130
this . _flavor = flavor ;
77
131
this . setRuntimeItemsToDefaultValues ( ) ;
78
132
this . setProp ( "flavor" , flavor . toJSON ( ) ) ;
79
- this . setTemplates ( this . flavor . inputAsTemplates ) ;
133
+ this . setTemplates ( this . _getInputAsTemplates ( ) ) ;
80
134
}
81
135
82
136
setTemplates ( templates ) {
@@ -126,11 +180,7 @@ export class ExecutionUnit extends mix(BaseUnit).with(HashedInputArrayMixin) {
126
180
}
127
181
128
182
get input ( ) {
129
- return (
130
- this . prop ( "input" ) ||
131
- this . flavor . getInputAsRenderedTemplates ( this . getCombinedContext ( ) ) ||
132
- [ ]
133
- ) ;
183
+ return this . prop ( "input" ) ;
134
184
}
135
185
136
186
get renderingContext ( ) {
@@ -167,7 +217,7 @@ export class ExecutionUnit extends mix(BaseUnit).with(HashedInputArrayMixin) {
167
217
const newRenderingContext = { } ;
168
218
const renderingContext = { ...this . context , ...context } ;
169
219
this . updateContext ( renderingContext ) ; // update in-memory context to properly render templates from input below
170
- ( fromTemplates ? this . templates : this . templatesFromInput ) . forEach ( ( t ) => {
220
+ ( fromTemplates ? this . templates : this . _getTemplatesFromInput ( ) ) . forEach ( ( t ) => {
171
221
newInput . push ( t . getRenderedJSON ( renderingContext ) ) ;
172
222
Object . assign (
173
223
newRenderingContext ,
@@ -204,7 +254,7 @@ export class ExecutionUnit extends mix(BaseUnit).with(HashedInputArrayMixin) {
204
254
...super . toJSON ( ) ,
205
255
executable : this . executable . toJSON ( ) ,
206
256
flavor : this . flavor . toJSON ( ) ,
207
- input : this . input ,
257
+ input : this . _getInput ( ) ,
208
258
// keys below are not propagated to the parent class on initialization of a new unit unless explicitly given
209
259
name : this . name ,
210
260
// TODO: figure out the problem with storing context below
0 commit comments