Add parameter to activity parameter node

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

Add parameter to activity parameter node

Postby sandor.zsolt@netlock.hu » Thu Jan 27, 2022 11:40 pm

Hello,

What is the correct way of adding parameter to activity parameter node? I always get model inconsistency error.
sandor.zsolt@netlock.hu
Forum Newbie
Forum Newbie
 
Posts: 17
Posts Rating:4
Joined: Wed Jun 10, 2020 8:45 am

Re: Add parameter to activity parameter node

Postby sandor.zsolt@netlock.hu » Fri Jan 28, 2022 3:23 am

Found the solution, you need to set the owner before setting the parameter.

Code: Select all
def activity = elementsFactory.createActivityInstance();
activity.setOwningPackage(target);   

def parameterNode = elementsFactory.createActivityParameterNodeInstance();
parameterNode.setName("myParameter");
parameterNode.setActivity(activity);

def parameter = elementsFactory.createParameterInstance();
parameter.setOwner(activity)
parameterNode.setParameter(parameter)
sandor.zsolt@netlock.hu
Forum Newbie
Forum Newbie
 
Posts: 17
Posts Rating:4
Joined: Wed Jun 10, 2020 8:45 am

Re: Add parameter to activity parameter node

Postby marc.arias@lmco.com » Mon Nov 28, 2022 1:28 pm

Could this be done with a createPropertyInstance( ) call ?
I am trying to setOwner( ) but it is not working.

results = factory.createPackageInstance()
results.setOwner(project.mainModel)
results.setName("Results")
resultItem = factory.createPropertyInstance()
resultItem.setOwner(results)

The error message is below :

MagicDraw cannot execute the Jython macro, please make sure that the C:\Users\cameo_dataModel_beta.py is correct. null java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: can not find container for:com.nomagic.uml2.ext.magicdraw.classes.mdkernel.impl.PackageImpl com.nomagic.uml2.ext.magicdraw.classes.mdkernel.impl.PackageImpl@89eb3e64 candidates are:[associationEnd, _templateParameterSubstitutionOfOwnedActual, owningTemplateParameter, _templateParameterOfOwnedDefault, UMLClass, datatype, _artifactOfOwnedAttribute, owningAssociation, _structuredClassifierOfOwnedAttribute, owningSignal, interface, _associationOfNavigableOwnedEnd] in C:\Users\e410874\git\ma-sandbox\cameo_dataModel_beta.py at line number 303
marc.arias@lmco.com
Forum Newbie
Forum Newbie
 
Posts: 8
Posts Rating:2
Joined: Mon Nov 28, 2022 9:16 am

Re: Add parameter to activity parameter node

Postby marc.arias@lmco.com » Tue Nov 29, 2022 12:05 pm

Answering my own question :
I found a solution to the setOwner() for a property instance ()
I basically create a block using a function and used that block as the setOwner for the value Property. The steps are below
I am also using python for this and using the MagicDraw Jython wrapper as the macro language type in cameo. Note that currently MagicDraw only supports a limited amount of libraries and packages and Python 2.7 or older is currently only supported.



Code: Select all
#inital set up of project and packages
project = Application.getInstance().getProject() // setting instance of a project
results = factory.createPackageInstance()  // creating a package
results.setOwner(project.mainModel)  // setting owner of the package to main model 
results.setName("Results") // Naming the package to Results

#function to create a block
def createBlock(name_of_block, block_package):
    block = factory.createClassInstance()
    StereotypesHelper.addStereotypeByString(block, "Block")
    block.setOwner(block_package)
    block.setName(name_of_block)
    return block
   
# calling function to create the owning block
owningBlock = createBlock("owningBlock", results)

resultItem = factory.createPropertyInstance()
resultItem.setOwner(owningBlock)  // resultItem is propertyInstance - it represent the value property in your generic table
   
marc.arias@lmco.com
Forum Newbie
Forum Newbie
 
Posts: 8
Posts Rating:2
Joined: Mon Nov 28, 2022 9:16 am


Return to Programmatic Extendibility

Who is online

Users browsing this forum: No registered users and 1 guest