How to add/create a Node to a Model from OpenAPI

Hi
I am adding a Node in my model using OpenAPI,I have used the following code
Add Node



       
    MPSModuleRepository.getInstance().getModelAccess().runWriteAction(new Runnable() { 
      public void run() { 
        for (SModule sModule : MPSModuleRepository.getInstance().getModules()) { 
          if (sModule.getModuleName().equalsIgnoreCase(" my module name")) { 
           
 smodule = sModule; 
 modelaccess = smodule.getRepository().getModelAccess(); 
 info "modelaccess" + modelaccess.canWrite(); 
 break; 
              } 
           
          } 
        } 
        \\it has only one model
        for (SModel Model : smodule.getModels()) { 
          if (Model instanceof EditableSModel) { 
            smodel = Model as EditableSModel; 
          } 
          SNode snode = new SNode("ACBLKBSD"); 
          smodel.addRootNode(snode); 
             
        } 
           
      } 
    }); 
    
 

but I am getting illegal model access exception and cannot be able to add the node in the model .So can you please suggest a way to add a node and its propeties to a model programatically. Thanks in advance
0
8 comments
Does using "ModelAccess.executeCommand" instead help?
0
Avatar
Permanently deleted user
Hi Vaclav we have tried ModelAccess.executeCommand but we are getting java.lang.UnsupportedOperationException
Can you please suggest any other way for adding the node
 MPSModuleRepository.getInstance().getModelAccess().executecommand(new Runnable() { 
      public void run() { 
        for (SModule sModule : MPSModuleRepository.getInstance().getModules()) { 
          if (sModule.getModuleName().equalsIgnoreCase(" my module name")) { 
           
 smodule = sModule; 
 modelaccess = smodule.getRepository().getModelAccess(); 
 info "modelaccess" + modelaccess.canWrite(); 
 break; 
              } 
           
          } 
        } 
        \\it has only one model
        for (SModel Model : smodule.getModels()) { 
          if (Model instanceof EditableSModel) { 
            smodel = Model as EditableSModel; 
          } 
          SNode snode = new SNode("ACBLKBSD"); 
          smodel.addRootNode(snode); 
             
        } 
           
      } 
    }); 
0
Avatar
Permanently deleted user
Hi Vaclav,
we tried it with modelacces.execute command still we are getting java.lang.UnsupportedOperationException ,are we missing anything ?
Thanks in advance
0
Could you possibly share a stack trace? Also, where are you calling the code from? Is it an intention, action or somewhere else?

Vaclav
0
Avatar
Permanently deleted user
Hi Vaclav,
Thanks for your quick reply ,I am sharing you entire class (I have added in the project component because i want my node to be added as soon as model is created in project .Can you please help me in this aspect

public class ProjectComponentclass implements ProjectComponent { 
  private final Project myproject; 
  private SModule smodule; 
  private SModel smodel; 
  private SModel smod; 
  private ModelAccess modelaccess; 
   
  private ModelRoot modelroot; 
   
  public ProjectComponent(Project project) { 
    myproject = project; 
  } 
  @Override 
  public void projectOpened() { 
       
    info "project opened"; 
       
    MPSModuleRepository.getInstance().getModelAccess().runWriteAction(new Runnable() { 
      public void run() { 
        info "in first run method"; 
        for (SModule sModule : MPSModuleRepository.getInstance().getModules()) { 
          if (sModule.getModuleName().equalsIgnoreCase(" my module name")) { 
           
 smodule = sModule; 
 modelaccess = smodule.getRepository().getModelAccess(); 
 info "modelaccess" + modelaccess.canWrite(); 
 break; 
              } 
           
          } 
        } 
        for (SModel Model : smodule.getModels()) { 
          if (Model instanceof EditableSModel) { 
            smodel = Model as EditableSModel; 
          } 
        } 
      } 
    }); 
    runcommand("in model", new Runnable() { 
      public void run() { 
        info "in run method"; 
        SNode snode = new SNode("my node"); 
           
        smodel.addRootNode(snode); 
      } 
    }); 
       
  } 
  private void runcommand(string name, final Runnable runnable) { 
    if (SwingUtilities.isEventDispatchThread()) { 
      modelaccess.executeCommand(runnable); 
    } else { 
      try { 
        SwingUtilities.invokeAndWait(new Runnable() { 
          public void run() { 
            modelaccess.executeCommand(runnable); 
          } 
        }); 
      } catch (InterruptedException e) { 
        <no statements> 
      } catch (Exception e) { 
        <no statements> 
      } 
    } 
  } 
  public void projectClosed() { 
    <no statements> 
  } 
  public void initComponent() { 
    <no statements> 
  } 
  public void disposeComponent() { 
    <no statements> 
  } 
  @NonNls 
  @NotNull() 
  public String getComponentName() { 
    return "Data Model Project"; 
  } 
}

I am getting the following Exception
java.lang.UnsupportedOperationException
java.lang.UnsupportedOperationException
 at jetbrains.mps.smodel.MPSModuleRepository$GlobalModelAccess.executeCommand(MPSModuleRepository.java:365)
 at com.ca.gen.adysm.src.ProjectComponent.runcommand(Unknown Source)
 at com.ca.gen.adysm.src.ProjectComponent.projectOpened(Unknown Source)
 at com.intellij.openapi.project.impl.ProjectImpl.projectOpened(ProjectImpl.java:412)
 at com.intellij.openapi.project.impl.ProjectImpl.access$200(ProjectImpl.java:74)
 at com.intellij.openapi.project.impl.ProjectImpl$MyProjectManagerListener.projectOpened(ProjectImpl.java:447)
 at com.intellij.openapi.project.impl.ProjectManagerImpl$2.projectOpened(ProjectManagerImpl.java:159)
 at com.intellij.openapi.project.impl.ProjectManagerImpl.fireProjectOpened(ProjectManagerImpl.java:1057)
 at com.intellij.openapi.project.impl.ProjectManagerImpl.openProject(ProjectManagerImpl.java:422)
 at com.intellij.openapi.project.impl.ProjectManagerImpl.loadAndOpenProject(ProjectManagerImpl.java:513)
 at com.intellij.ide.impl.ProjectUtil.openProject(ProjectUtil.java:181)
 at jetbrains.mps.workbench.recent.RecentMPSProjectsManager.doOpenProject(RecentMPSProjectsManager.java:53)
 at com.intellij.ide.ReopenProjectAction.actionPerformed(ReopenProjectAction.java:61)
 at com.intellij.openapi.wm.impl.welcomeScreen.RecentProjectPanel$1.onClick(RecentProjectPanel.java:77)
 at com.intellij.ui.ClickListener$1.mouseReleased(ClickListener.java:73)
 at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:290)
 at java.awt.Component.processMouseEvent(Component.java:6516)
 at javax.swing.JComponent.processMouseEvent(JComponent.java:3320)
 at java.awt.Component.processEvent(Component.java:6281)
 at java.awt.Container.processEvent(Container.java:2229)
 at java.awt.Component.dispatchEventImpl(Component.java:4872)
 at java.awt.Container.dispatchEventImpl(Container.java:2287)
 at java.awt.Component.dispatchEvent(Component.java:4698)
 at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
 at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
 at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
 at java.awt.Container.dispatchEventImpl(Container.java:2273)
 at java.awt.Window.dispatchEventImpl(Window.java:2719)
 at java.awt.Component.dispatchEvent(Component.java:4698)
 at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735)
 at java.awt.EventQueue.access$200(EventQueue.java:103)
 at java.awt.EventQueue$3.run(EventQueue.java:694)
 at java.awt.EventQueue$3.run(EventQueue.java:692)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
 at java.awt.EventQueue$4.run(EventQueue.java:708)
 at java.awt.EventQueue$4.run(EventQueue.java:706)
 at java.security.AccessController.doPrivileged(Native Method)
 at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
 at java.awt.EventQueue.dispatchEvent(EventQueue.java:705)
 at com.intellij.ide.IdeEventQueue.defaultDispatchEvent(IdeEventQueue.java:697)
 at com.intellij.ide.IdeEventQueue._dispatchEvent(IdeEventQueue.java:520)
 at com.intellij.ide.IdeEventQueue.dispatchEvent(IdeEventQueue.java:335)
 at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
 at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
 at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
 at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
0
Avatar
Permanently deleted user
Hi Vaclav,
This is the exception trace i have got ,can you please help me in adding a node from OpenAPI
0
Avatar
Permanently deleted user
Hi, mps_beginner!

Instead of MPSModuleRepository, please use ProjectRepository(myProject). You can either instantiate it or use ProjectHelper.getProjectReposiory() or ProjectHelper.getModelAccess() right away.
0
Avatar
Permanently deleted user
Thank you Daniil for your reply
0

Please sign in to leave a comment.