How to copy languages (and dependencies) from one Model to another Model programmatically?

Do I have to cast SModel to SModelInternal? What model should I import to have access to SModelInternal? Ctrl R does not give anything ...

 

Kind Regards,

Arjan Kok.

 

 

0
4 comments
Avatar
Permanently deleted user

Hi Arjan,

I am working on something similar and came across CloneUtil (jetbrains.mps.generator.impl.CloneUtil). I asked a question in the slack channel if it is okay to use this utility to clone models which are not transient models (since CloneUtil seems to be used only for that purpose by MPS internally) awaiting an answer.
Anyway, programmatically, it works to use this utility like so:

// create an empty, new editable model
// smoduleContext is that module you want to add the new model to
EditableSModel editableSModel = SModuleOperations.createModelWithAdjustments("someModelName", smoduleContext.getModelRoots().iterator().next());

CloneUtil cloneUtil = new CloneUtil(this.modelOrig, modelClone);
// will copy all imported languages, model, devkits
cloneUtil.cloneModelWithAllImports();

If you want to do it manually, try something like this:

SModelBase smodelBaseOrig = (SModelBase) modelOrig; 
SModelBase smodelBaseCopy = (SModelBase) editableSModel;

if (smodelBaseOrig != null && smodelBaseCopy != null) {
foreach smodelReferenceImport in smodelBaseOrig.getModelImports() {
smodelBaseCopy.addModelImport(smodelReferenceImport); 
}

}

foreach language in smodelBaseOrig.getLanguages {
smodelBaseCopy.addLanguage(language);
}

foreach smoduleRefDevkit in smodelBaseOrig.importedDevkits() {
smodelBaseCopy.addDevKit(smoduleRefDevkit);
}
}

Best,

Robert

3

Hi, Robert!

Actually, CloneUtil class used in few more places in MPS except the Generator. ;-)

I think, it's ok to use this class if you'd like to copy content of the model completely.

1
Avatar
Permanently deleted user

You may find j.m.smodel.ModelImports(openapi.SModel) class handy, as it hides the knowledge about particular SModelInternal/SModelBase implementations and is intended to deal with model dependencies.

1
Avatar
Permanently deleted user

Do not know what went wrong, but Ctrl +R works fine, and returns the model I need to import. The SModelInternal interface contains indeed the necessary methods to add languages and imports to the output model.

0

Please sign in to leave a comment.