Getting Tagged Values

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

Getting Tagged Values

Postby melpers@strattechnologies.com » Mon Mar 06, 2023 7:28 pm

Hi,

I've been trying to pull the name of an Enumeration Literal via the openAPI in a 2021 R2 model.
This literal is one of the three values used in the enumerated type for the direction of a flow property (in, out, inout).
I've retrieved the "ID" of this literal, but cannot get the name.
I've read the following paragraph in the Properties section of the Users Guide (https://docs.nomagic.com/display/MD190/Properties):

"You must set some com.nomagic.magicdraw.properties.PropertyResourceProvider to your property instance in order to display a normal name, not the ID of the property in the UI of a modeling tool. PropertyResourceProvider has just one method getString(java.lang.String , Property), where a key is an ID of your property."

This would seem to describe my problem exactly, but the grammar is not good enough (or I'm not enough of a programmer) to figure it out from this description.
Can you provide a code example that works please?

In case you try to suggest another approach, we do have a solution using a Finder and TagsHelper, but we need to understand how to get the name of a property from the ID.

Thanks!
melpers@strattechnologies.com
Forum Newbie
Forum Newbie
 
Posts: 12
Posts Rating:1
Joined: Fri Apr 23, 2021 7:12 am

Re: Getting Tagged Values

Postby yu3333 » Wed Mar 29, 2023 7:40 am

To work with flow property, you need SysMLUtilities http://jdocs.nomagic.com/182/sysml/com/nomagic/magicdraw/sysml/util/SysMLUtilities.html and StereotypesHelper.

Down below is a working example, in forms of Macro in Groovy, which creates a block and add a flow property (also, set and get its direction).

Code: Select all
import com.nomagic.uml2.ext.magicdraw.activities.mdbasicactivities.impl.*;
import com.nomagic.uml2.ext.magicdraw.activities.mdfundamentalactivities.*;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.*;
import com.nomagic.uml2.ext.jmi.helpers.*;
import com.nomagic.uml2.impl.*;
import com.nomagic.uml2.ext.magicdraw.mdprofiles.*;
import com.nomagic.uml2.ext.magicdraw.compositestructures.mdports.*;
import com.nomagic.uml2.ext.magicdraw.compositestructures.mdinternalstructures.*;
import com.nomagic.magicdraw.uml.*;
import com.nomagic.magicdraw.uml.symbols.*;
import com.nomagic.magicdraw.core.*;
import com.nomagic.magicdraw.actions.*;
import com.nomagic.magicdraw.openapi.uml.*;
import com.nomagic.magicdraw.sysml.util.*;
import com.nomagic.magicdraw.properties.*;

Project project = Application.getInstance().getProject();

if (!SessionManager.getInstance().isSessionCreated(project)){
  SessionManager.getInstance().createSession(project,"MySession");
}           

// new block
Class newBlock = project.getElementsFactory().createClassInstance();
Stereotype blockStereotype = StereotypesHelper.getStereotype(project, "Block");
StereotypesHelper.addStereotype(newBlock, blockStereotype);
ModelElementsManager.getInstance().addElement(newBlock, project.getModel());


// add flow property to block
Property flowProperty = project.getElementsFactory().createPropertyInstance();
Stereotype flowPropertyStereotype =
          StereotypesHelper.getStereotype(project, "FlowProperty");
StereotypesHelper.addStereotype(flowProperty, flowPropertyStereotype);
StereotypesHelper.setStereotypePropertyValue(flowProperty,
                                              flowPropertyStereotype,
                                              "direction",
                                              "out");
ModelElementsManager.getInstance().addElement(flowProperty, newBlock);
String direction = SysMLUtilities.getFlowDirection(flowProperty);
Application.getInstance().getGUILog().log("flow property direction:  " + direction);

SessionManager.getInstance().closeSession();
yu3333
Forum Newbie
Forum Newbie
 
Posts: 7
Posts Rating:0
Joined: Wed Mar 29, 2023 1:09 am

Re: Getting Tagged Values

Postby drewbinskyn@gmail.com » Tue Apr 25, 2023 8:40 pm

The PropertyResourceProvider interface can be used to get the name of the Enumeration Literal if you have the ID of the Enumeration Literal.
drewbinskyn@gmail.com
Forum Newbie
Forum Newbie
 
Posts: 3
Posts Rating:0
Joined: Tue Apr 25, 2023 8:32 pm

Re: Getting Tagged Values

Postby donnajane1205@gmail.com » Thu Apr 27, 2023 1:48 am

Once you have this mapping, you can retrieve the property name by looking up the ID in the mapping.
tunnel rush
donnajane1205@gmail.com
Forum Newbie
Forum Newbie
 
Posts: 1
Posts Rating:0
Joined: Thu Apr 27, 2023 1:46 am

Re: Getting Tagged Values

Postby aaronramsdale » Mon Jun 26, 2023 2:03 am

You can use the PropertyResourceProvider class to retrieve the name of an Enumeration Literal in MagicDraw using the open API. Here is an example: fnf
Code: Select all
import com.nomagic.magicdraw.properties.PropertyResourceProvider;

// Assuming you have the ID of the Enumeration Literal
String literalId = "your_literal_id";

// Get the PropertyResourceProvider instance
PropertyResourceProvider resourceProvider = PropertyResourceProvider.getInstance();

// Get the name of the Enumeration Literal using the ID
String literalName = resourceProvider.getString(literalId, null);

// Print the name of the Enumeration Literal
System.out.println("Name of the Enumeration Literal: " + literalName);
aaronramsdale
Forum Newbie
Forum Newbie
 
Posts: 3
Posts Rating:0
Joined: Mon Aug 01, 2022 1:51 am

Re: Getting Tagged Values

Postby bekeanloinse@gmail.com » Thu Aug 17, 2023 10:54 pm

yu3333 wrote:To work with flow property, you need SysMLUtilities http://jdocs.nomagic.com/182/sysml/com/nomagic/magicdraw/sysml/util/SysMLUtilities.html and StereotypesHelper.

Down below is a working fnf example, in forms of Macro in Groovy, which creates a block and add a flow property (also, set and get its direction).

Code: Select all
import com.nomagic.uml2.ext.magicdraw.activities.mdbasicactivities.impl.*;
import com.nomagic.uml2.ext.magicdraw.activities.mdfundamentalactivities.*;
import com.nomagic.uml2.ext.magicdraw.classes.mdkernel.*;
import com.nomagic.uml2.ext.jmi.helpers.*;
import com.nomagic.uml2.impl.*;
import com.nomagic.uml2.ext.magicdraw.mdprofiles.*;
import com.nomagic.uml2.ext.magicdraw.compositestructures.mdports.*;
import com.nomagic.uml2.ext.magicdraw.compositestructures.mdinternalstructures.*;
import com.nomagic.magicdraw.uml.*;
import com.nomagic.magicdraw.uml.symbols.*;
import com.nomagic.magicdraw.core.*;
import com.nomagic.magicdraw.actions.*;
import com.nomagic.magicdraw.openapi.uml.*;
import com.nomagic.magicdraw.sysml.util.*;
import com.nomagic.magicdraw.properties.*;

Project project = Application.getInstance().getProject();

if (!SessionManager.getInstance().isSessionCreated(project)){
  SessionManager.getInstance().createSession(project,"MySession");
}           

// new block
Class newBlock = project.getElementsFactory().createClassInstance();
Stereotype blockStereotype = StereotypesHelper.getStereotype(project, "Block");
StereotypesHelper.addStereotype(newBlock, blockStereotype);
ModelElementsManager.getInstance().addElement(newBlock, project.getModel());


// add flow property to block
Property flowProperty = project.getElementsFactory().createPropertyInstance();
Stereotype flowPropertyStereotype =
          StereotypesHelper.getStereotype(project, "FlowProperty");
StereotypesHelper.addStereotype(flowProperty, flowPropertyStereotype);
StereotypesHelper.setStereotypePropertyValue(flowProperty,
                                              flowPropertyStereotype,
                                              "direction",
                                              "out");
ModelElementsManager.getInstance().addElement(flowProperty, newBlock);
String direction = SysMLUtilities.getFlowDirection(flowProperty);
Application.getInstance().getGUILog().log("flow property direction:  " + direction);

SessionManager.getInstance().closeSession();

I get it. Many thanks!
bekeanloinse@gmail.com
Forum Newbie
Forum Newbie
 
Posts: 1
Posts Rating:0
Joined: Tue Apr 18, 2023 9:11 pm


Return to Programmatic Extendibility

Who is online

Users browsing this forum: No registered users and 0 guests