-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate.js
More file actions
217 lines (203 loc) · 7.72 KB
/
Copy pathcreate.js
File metadata and controls
217 lines (203 loc) · 7.72 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
import { ApplicationRegistry } from "@mat3ra/ade";
import {
default_methods as MethodConfigs,
default_models as ModelConfigs,
MethodConversionHandler,
MethodFactory,
ModelFactory,
} from "@mat3ra/mode";
import { ApplicationMethodStandata } from "@mat3ra/standata";
import _ from "lodash";
import { UnitFactory } from "../units";
import { builders } from "../units/builders";
import { applyConfig } from "../utils";
import { dynamicSubworkflowsByApp, getSurfaceEnergySubworkflowUnits } from "./dynamic";
import { Subworkflow } from "./subworkflow";
// NOTE: DFTModel => DFTModelConfig, configs should have the same name as the model/method class + "Config" at the end
function _getConfigFromModelOrMethodName(name, kind) {
const configs = kind === "Model" ? ModelConfigs : MethodConfigs;
if (!configs[`${name}Config`]) {
// eslint-disable-next-line no-param-reassign
name = `Unknown${kind}`;
}
return configs[`${name}Config`];
}
/**
* @summary Create model from subworkflow data
* @param config {Object} model config
* @param modelFactoryCls {any} model factory to use
* @returns {DFTModel|Model}
*/
function createModel({ config, modelFactoryCls }) {
const { name, config: modelConfig = {} } = config;
const defaultConfig = _getConfigFromModelOrMethodName(name, "Model");
return modelFactoryCls.create({ ...defaultConfig, ...modelConfig });
}
/**
* @summary Create method from subworkflow data
* @param config {Object} method configuration
* @param methodFactoryCls {any}
* @param applicationConfig {Object} application configuration
* @returns {{method, setSearchText}}
*/
function createMethod({ config, methodFactoryCls, applicationConfig = {} }) {
const { name, setSearchText = null, config: methodConfig = {} } = config;
const defaultConfig = _getConfigFromModelOrMethodName(name, "Method");
const defaultConfigForApp =
new ApplicationMethodStandata().getDefaultMethodConfigForApplication(applicationConfig);
const defaultConfigForAppSimple =
defaultConfigForApp && defaultConfigForApp.units
? MethodConversionHandler.convertToSimple(defaultConfigForApp)
: defaultConfigForApp;
const method = methodFactoryCls.create({
...defaultConfig,
...defaultConfigForAppSimple,
...methodConfig,
});
return { method, setSearchText };
}
/**
* @summary Create top-level objects used in subworkflow initialization
* @param subworkflowData {Object} subworkflow data
* @param AppRegistry
* @param modelFactoryCls {any} model factory class
* @param methodFactoryCls {any} method factory class
* @returns {{application: *, method: *, model: (DFTModel|Model), setSearchText: String|null}}
*/
function createTopLevel({ subworkflowData, modelFactoryCls, methodFactoryCls, AppRegistry }) {
const { application: appConfig, model: modelConfig, method: methodConfig } = subworkflowData;
const application = AppRegistry.createApplication(appConfig);
const model = createModel({ config: modelConfig, modelFactoryCls });
const { method, setSearchText } = createMethod({
config: methodConfig,
methodFactoryCls,
applicationConfig: appConfig,
});
return {
application,
model,
method,
setSearchText,
};
}
/**
* @summary Create workflow unit from JSON configuration
* Supports applying functions to the builder prior to building via "functions"
* Supports applying attributes to the builder after building via "attributes"
* @param config {Object} unit config
* @param application {*} application
* @param unitBuilders {Object} workflow unit builders
* @param unitFactoryCls {*} workflow unit class factory
* @returns {*|{head: boolean, preProcessors: [], postProcessors: [], name: *, flowchartId: *, type: *, results: [], monitors: []}}
*/
export function createUnit({ config, application, unitBuilders, unitFactoryCls }) {
const { type, config: unitConfig } = config;
if (type === "executionBuilder") {
const { name, execName, flavorName, flowchartId } = unitConfig;
const builder = new unitBuilders.ExecutionUnitConfigBuilder(
name,
application,
execName,
flavorName,
flowchartId,
);
// config should contain "functions" and "attributes"
const cfg = applyConfig({ obj: builder, config, callBuild: true });
return unitFactoryCls.create(cfg);
}
return unitFactoryCls.create({ type, ...unitConfig });
}
/**
* @summary Dynamically create subworkflow units
* @param dynamicSubworkflow {String} name of unit creation function
* @param units {Array} configured units to provide to dynamic unit creation
* @param unitBuilders {Object} unit configuration builders
* @param unitFactoryCls {*} unit factory class
* @param application {*} application (optional)
* @returns {*}
*/
function createDynamicUnits({
dynamicSubworkflow,
units,
unitBuilders,
unitFactoryCls,
application = null,
}) {
const { name, subfolder } = dynamicSubworkflow;
const func = subfolder && _.get(dynamicSubworkflowsByApp, `${subfolder}.${name}`, () => {});
switch (name) {
case "surfaceEnergy":
// eslint-disable-next-line no-case-declarations
const [scfUnit] = units;
return getSurfaceEnergySubworkflowUnits({ scfUnit, unitBuilders });
case "getQpointIrrep":
return func({ unitBuilders, unitFactoryCls, application });
default:
throw new Error(`dynamicSubworkflow=${name} not recognized`);
}
}
function createSubworkflow({
subworkflowData,
AppRegistry = ApplicationRegistry,
modelFactoryCls = ModelFactory,
methodFactoryCls = MethodFactory,
subworkflowCls = Subworkflow,
unitFactoryCls = UnitFactory,
unitBuilders = builders,
}) {
const { application, model, method, setSearchText } = createTopLevel({
subworkflowData,
AppRegistry,
modelFactoryCls,
methodFactoryCls,
});
let units = [];
const { name, units: unitConfigs, config = {}, dynamicSubworkflow = null } = subworkflowData;
unitConfigs.forEach((_config) => {
units.push(
createUnit({
config: _config,
application,
unitBuilders,
unitFactoryCls,
}),
);
});
if (dynamicSubworkflow) {
units = createDynamicUnits({
dynamicSubworkflow,
units,
unitBuilders,
unitFactoryCls,
application,
});
}
let subworkflow = subworkflowCls.fromArguments(application, model, method, name, units, config);
const { functions = {}, attributes = {} } = config;
subworkflow = applyConfig({ obj: subworkflow, config: { functions, attributes } });
if (setSearchText) subworkflow.model.Method.setSearchText(setSearchText);
return subworkflow;
}
/**
* @summary Convenience wrapper around createSubworkflow to create by app name and swf name
* @param appName {String} application name
* @param swfName {String} subworkflow name (snake_case.yml)
* @param workflowSubworkflowMapByApplication {Object} object containing all workflow/subworkflow map by application
* @param swArgs {Object} classes for instantiation
* @returns {*} subworkflow object
*/
function createSubworkflowByName({
appName,
swfName,
workflowSubworkflowMapByApplication,
...swArgs
}) {
const { subworkflows } = workflowSubworkflowMapByApplication;
const { [appName]: allSubworkflowData } = subworkflows;
const { [swfName]: subworkflowData } = allSubworkflowData;
return createSubworkflow({
subworkflowData,
...swArgs,
});
}
export { createSubworkflow, createSubworkflowByName };