Child pages
  • Using model & module dependencies FAQ
Skip to end of metadata
Go to start of metadata

//    Entry point
// accessing model of the node
model model = node.model;
// "down-"casting it to the interface from MPS open API

// Be careful with package names: j.m.smodel.SModel is an implementation class, which normally should not be used

// The 'open API' resides in org.jetbrains.mps.openapi.model, e.g. org.jetbrains.mps.openapi.model.SModel.

SModel sModel = model;
// non-open API interface for MPS model
SModelInternal internalSModel = (SModelInternal) sModel;
// accesing module owning the model
SModule sModule = sModel.getModule();

//    Repository or modules
// accessing repository of modules
SRepository sRepository = sModel.getRepository();
// accessing modules from the repository
Iterable<SModule> modules = sRepository.getModules();
SModule module = sRepository.getModule(sModule.getModuleId());
SModule closuresRuntimeModule = sRepository.getModule(module reference/closures.runtime/.getModuleId());

// Languages are represented as a special module type
if (module instanceof Language) {
   Language language = (Language) module;
}

// Getting language by "direct" reference:
// Note: no repository is mentioned here. In the MPS we actually have single repository now,
// so you can use this expression to get module from the single repository
Language mpsBaseLanguage = (Language) module/jetbrains.mps.baseLanguage/;

//    Module dependencies

// accessing module dependencies
sequence<SDependency> dependencies = module.getDeclaredDependencies()

// adding used language to a module
AbstractModule internalModule = (AbstractModule) sModule;
internalModule.getModuleDescriptor().getUsedLanguages().add(module reference/jetbrains.mps.baseLanguage/);

// modify module dependencies
sequence<Dependency> internalDependencies = internalModule.getModuleDescriptor().getDependencies();
sequence<SModuleReference> usedModuleReferences = internalDependencies.select({~it => it.getModuleRef(); });
internalModule.getModuleDescriptor().getDependencies().add(new Dependency(module reference/closures.runtime/, SDependencyScope.DEFAULTfalse));

// working with DevKits:
internalModule.getModuleDescriptor().getUsedDevkits().add(module reference/jetbrains.mps.devkit.general-purpose/);

//    Models
// accessing models from the module
Iterable<SModel> models = sModule.getModels();
sModule.getModel(sModel.getModelId());

// working with imported models:
internalSModel.importedModels();
internalSModel.addModelImport(sModel.getReference(), false);
internalSModel.deleteModelImport(sModel.getReference());

// working with imported languages:
internalSModel.importedLanguages();
internalSModel.addLanguage(mpsBaseLanguage.getModuleReference());
internalSModel.addLanguage(module reference/jetbrains.mps.baseLanguage/);

// working with imported DevKits
internalSModel.importedDevkits();
internalSModel.addDevKit(module reference/jetbrains.mps.devkit.general-purpose/);
internalSModel.deleteDevKit(module reference/jetbrains.mps.devkit.general-purpose/);

  • No labels

3 Comments

  1. Anonymous

    Hello,

    I am new with MPS and use the version 3.2.

    I try to "down-"cast a model to sModel in the constraints aspect, whitout succes.

    org.jetbrains.mps.openapi.module_at_java_stub and jetbrains.mps.smodel_at_java_stub are imported  in the constraints aspect as models dependencies.

    The Code:

    --------------------------------------

    property

    Unknown macro: {languageName}

      get:<default>
      set:<default>
      is valid:(propertyValue, node)->boolean {
        model model = node.model;     SModel smodel = model;
         
        return false;
      }

    ---------------------------

    And the error:

    Error: type model is not subtype of jetbrains.mps.smodel.SModel

    What i forgot ?

    Thanks,

    Alex

  2. Anonymous

    I have the same problem.

    Regards,

    msch95

    1. This is not the right place to ask questions, however, it's an indication the page goes with certain assumptions not obvious to everyone, and is worth updating.

      Don't use j.m.smodel.SModel, it's implementation class, 'open API' in the text refers to classes in org.jetbrains.mps.openapi.model.SModel.