Skip to content

SOF-7644: move context providers from ade/code #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,21 @@
{
"extends": ["@exabyte-io/eslint-config"]
"extends": [
"@exabyte-io/eslint-config"
],
"ignorePatterns": [
"dist/",
"src/workflows/workflows.js"
],
"settings": {
"import/resolver": {
"node": {
"extensions": [
".js",
".jsx",
".ts",
".tsx"
]
}
}
}
}

984 changes: 920 additions & 64 deletions package-lock.json

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,23 @@
"@babel/preset-react": "7.16.7",
"@babel/register": "^7.16.0",
"@babel/runtime-corejs3": "7.16.8",
"@exabyte-io/periodic-table.js": "2022.6.8-0",
"crypto-js": "^4.2.0",
"js-yaml": "^4.1.0",
"lodash": "^4.17.21",
"mixwith": "^0.1.1",
"underscore": "^1.13.3",
"underscore.string": "^3.3.4"
},
"devDependencies": {
"@exabyte-io/ade.js": "2025.5.5-0",
"@exabyte-io/ade.js": "git+https://github.com/Exabyte-io/ade.js.git#bbb62dc12f4ae7c4ba32edc60e459b8bbbcd232d",
"@exabyte-io/application-flavors.js": "2025.5.10-0",
"@exabyte-io/eslint-config": "^2022.11.17-0",
"@exabyte-io/eslint-config": "2025.5.13-0",
"@exabyte-io/ide.js": "2024.3.26-0",
"@exabyte-io/mode.js": "2024.4.28-0",
"@mat3ra/code": "2025.4.27-0",
"@mat3ra/code": "git+https://github.com/Exabyte-io/code.git#3fbf30e5318c9f1ab7a3a5b967fa57d2b70cad3e",
"@mat3ra/esse": "2025.4.26-0",
"@mat3ra/made": "2025.4.30-0",
"@mat3ra/made": "git+https://github.com/Exabyte-io/made.git#84807b83b15dfd632fae9495be301aa7df503b11",
"chai": "^4.3.4",
"eslint": "7.32.0",
"eslint-config-airbnb": "19.0.2",
Expand Down
10 changes: 10 additions & 0 deletions src/context/context.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { BoundaryConditionsFormDataProvider } from "./providers/BoundaryConditionsFormDataProvider";
import QENEBContextProvider from "./providers/by_application/espresso/QENEBContextProvider";
import QEPWXContextProvider from "./providers/by_application/espresso/QEPWXContextProvider";
import NWChemTotalEnergyContextProvider from "./providers/by_application/nwchem/NWChemTotalEnergyContextProvider";
import VASPContextProvider from "./providers/by_application/vasp/VASPContextProvider";
import VASPNEBContextProvider from "./providers/by_application/vasp/VASPNEBContextProvider";
import { CollinearMagnetizationContextProvider } from "./providers/CollinearMagnetizationContextProvider";
import { HubbardContextProviderLegacy } from "./providers/HubbardContextProviderLegacy";
import { HubbardJContextProvider } from "./providers/HubbardJContextProvider";
Expand Down Expand Up @@ -34,4 +39,9 @@ export default {
IonDynamicsContextProvider,
CollinearMagnetizationContextProvider,
NonCollinearMagnetizationContextProvider,
VASPContextProvider,
VASPNEBContextProvider,
QEPWXContextProvider,
QENEBContextProvider,
NWChemTotalEnergyContextProvider,
};
19 changes: 19 additions & 0 deletions src/context/mixins/ApplicationContextMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { globalSettings } from "../providers/settings";

export function applicationContextMixin(item) {
const properties = {
_application: undefined,

initApplicationContextMixin() {
this._application =
(this.config.context && this.config.context.application) ||
globalSettings.Application.createDefault();
},

get application() {
return this._application;
},
};

Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
}
36 changes: 36 additions & 0 deletions src/context/mixins/JobContextMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const defaultJob = {
workflow: {
subworkflows: [],
units: [],
},
status: "pre-submission",
compute: {
queue: "D",
nodes: 1,
ppn: 1,
timeLimit: "3600",
},
_project: {
_id: "",
},
};

export function jobContextMixin(item) {
const properties = {
isEdited: false,

_job: defaultJob,

get job() {
return this._job;
},

initJobContextMixin() {
const { config } = this;
this._job = (config.context && config.context.job) || defaultJob;
this.isEdited = false; // we always get the `defaultData` (recalculated from scratch, not persistent)
},
};

Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
}
41 changes: 41 additions & 0 deletions src/context/mixins/MaterialContextMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { globalSettings } from "../providers/settings";

export function materialContextMixin(item) {
const properties = {
_material: undefined,

updateMaterialHash() {
if (this.isEditedIsSetToFalseOnMaterialUpdate) this.isEdited = false;
this.extraData = { materialHash: this.material.hash };
},

// Workaround: Material.createDefault() used to initiate workflow reducer and hence here too
// does not have an id. Here we catch when such material is used and avoid resetting isEdited
get isMaterialCreatedDefault() {
return !this.material.id;
},

get isMaterialUpdated() {
return Boolean(this.extraData && this.extraData.materialHash !== this.material.hash);
},

get material() {
if (!this._material) {
throw new Error("Material is not set");
}
return this._material;
},

initMaterialContextMixin() {
this._material = this.config.context && this.config.context.material;

if (!this._material) {
this._material = globalSettings.Material.createDefault();
}

this.updateMaterialHash();
},
};

Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
}
18 changes: 18 additions & 0 deletions src/context/mixins/MaterialsContextMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { globalSettings } from "../providers/settings";

export function materialsContextMixin(item) {
const properties = {
get materials() {
return this._materials;
},
initMaterialsContextMixin() {
const materials = this.config.context?.materials;
this._materials =
materials && materials.length
? materials
: [globalSettings.Material.createDefault()];
},
};

Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
}
24 changes: 24 additions & 0 deletions src/context/mixins/MaterialsSetContextMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { compareEntitiesInOrderedSetForSorting } from "@mat3ra/code/dist/js/entity/set/ordered/utils";

export function materialsSetContextMixin(item) {
const properties = {
_materialsSet: undefined,

get materialsSet() {
return this._materialsSet;
},

initMaterialsSetContextMixin() {
this._materialsSet = this.config.context?.materialsSet;
},

sortMaterialsByIndexInSet(materials = []) {
// DO NOT SORT IN PLACE AS IT CHANGES THE ORDER IN `this.materials` AND HAS SIDE EFFECTS (MaterialViewer).
return materials.concat().sort((a, b) => {
return compareEntitiesInOrderedSetForSorting(a, b, this.materialsSet._id, false);
});
},
};

Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
}
48 changes: 48 additions & 0 deletions src/context/mixins/MethodDataContextMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import CryptoJS from "crypto-js";

export function methodDataContextMixin(item) {
const properties = {
_methodData: undefined,

isEdited: false,

methodDataHash: undefined,

extraData: undefined,

initMethodDataContextMixin() {
this._methodData = (this.config.context && this.config.context.methodData) || {};
this.isEdited = Boolean(this.config.isEdited);
},

/* @summary Replace the logic in constructor with this in order to enable passing `methodDataHash` between
* subsequent initializations of the derived class. Not used at present and kept for the record.
*/
_initMethodDataHash() {
this.methodDataHash = CryptoJS.MD5(JSON.stringify(this.methodData)).toString();
this.extraData = { methodDataHash: this.methodDataHash };
if (!this._methodData) {
this._methodData = {};
this.isEdited = false;
// Commented out to reduce effect on performance. Uncomment for debugging purposes.
// TODO: remove on next refactoring or convert to log
// console.warn("MethodDataContextMixin: methodData is undefined or null");
} else if (this.isMethodDataUpdated) {
this.isEdited = false;
} else {
// eslint-disable-next-line no-undef
this.isEdited = config.isEdited;
}
},

get methodData() {
return this._methodData;
},

get isMethodDataUpdated() {
return Boolean(this.extraData && this.extraData.methodDataHash !== this.methodDataHash);
},
};

Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
}
25 changes: 25 additions & 0 deletions src/context/mixins/WorkflowContextMixin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
const defaultWorkflow = {
subworkflows: [],
units: [],
hasRelaxation: false,
};

export function workflowContextMixin(item) {
const properties = {
isEdited: false,

_workflow: defaultWorkflow,

get workflow() {
return this._workflow;
},

initWorkflowContextMixin() {
const { config } = this; // as WorkflowConfig;
this._workflow = (config.context && config.context.workflow) || defaultWorkflow;
this.isEdited = false; // we always get the `defaultData` (recalculated from scratch, not persistent)
},
};

Object.defineProperties(item, Object.getOwnPropertyDescriptors(properties));
}
25 changes: 25 additions & 0 deletions src/context/providers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ const {
IonDynamicsContextProvider,
CollinearMagnetizationContextProvider,
NonCollinearMagnetizationContextProvider,
VASPContextProvider,
VASPNEBContextProvider,
QEPWXContextProvider,
QENEBContextProvider,
NWChemTotalEnergyContextProvider,
} = context;

const CONTEXT_DOMAINS = {
Expand Down Expand Up @@ -112,4 +117,24 @@ export const wodeProviders = {
providerCls: NonCollinearMagnetizationContextProvider,
config: _makeImportant({ name: "nonCollinearMagnetization" }),
},
QEPWXInputDataManager: {
providerCls: QEPWXContextProvider,
config: { name: "input" },
},
QENEBInputDataManager: {
providerCls: QENEBContextProvider,
config: { name: "input" },
},
VASPInputDataManager: {
providerCls: VASPContextProvider,
config: { name: "input" },
},
VASPNEBInputDataManager: {
providerCls: VASPNEBContextProvider,
config: { name: "input" },
},
NWChemInputDataManager: {
providerCls: NWChemTotalEnergyContextProvider,
config: { name: "input" },
},
};
16 changes: 10 additions & 6 deletions src/context/providers/BoundaryConditionsFormDataProvider.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import { JSONSchemaFormDataProvider, MaterialContextMixin } from "@mat3ra/code/dist/js/context";
import JSONSchemaFormDataProvider from "@exabyte-io/ade.js/dist/js/context/JSONSchemaFormDataProvider";
import { deepClone } from "@mat3ra/code/dist/js/utils";
import { Made } from "@mat3ra/made";
import { mix } from "mixwith";

export class BoundaryConditionsFormDataProvider extends mix(JSONSchemaFormDataProvider).with(
MaterialContextMixin,
) {
static Material = Made.Material;
import { materialContextMixin } from "../mixins/MaterialContextMixin";

export class BoundaryConditionsFormDataProvider extends JSONSchemaFormDataProvider {
constructor(config) {
super(config);
this.initMaterialContextMixin();
}

get boundaryConditions() {
return this.material.metadata.boundaryConditions || {};
Expand Down Expand Up @@ -74,3 +76,5 @@ export class BoundaryConditionsFormDataProvider extends mix(JSONSchemaFormDataPr
};
}
}

materialContextMixin(BoundaryConditionsFormDataProvider.prototype);
15 changes: 8 additions & 7 deletions src/context/providers/CollinearMagnetizationContextProvider.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { JSONSchemaFormDataProvider, MaterialContextMixin } from "@mat3ra/code/dist/js/context";
import { Made } from "@mat3ra/made";
import JSONSchemaFormDataProvider from "@exabyte-io/ade.js/dist/js/context/JSONSchemaFormDataProvider";
import lodash from "lodash";
import { mix } from "mixwith";

export class CollinearMagnetizationContextProvider extends mix(JSONSchemaFormDataProvider).with(
MaterialContextMixin,
) {
static Material = Made.Material;
import { materialContextMixin } from "../mixins/MaterialContextMixin";

export class CollinearMagnetizationContextProvider extends JSONSchemaFormDataProvider {
constructor(config) {
super(config);

this.initMaterialContextMixin();

this.firstElement =
this.uniqueElementsWithLabels?.length > 0 ? this.uniqueElementsWithLabels[0] : "";
this.isTotalMagnetization = lodash.get(this.data, "isTotalMagnetization", false);
Expand Down Expand Up @@ -114,3 +113,5 @@ export class CollinearMagnetizationContextProvider extends mix(JSONSchemaFormDat
};
}
}

materialContextMixin(CollinearMagnetizationContextProvider.prototype);
Loading