Getting First-Level Children of a Package

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

Getting First-Level Children of a Package

Postby philaustin378@gmail.com » Tue Mar 22, 2022 4:56 pm

I am looking for a way to find the FIRST-LEVEL Children of a Package.

Here is a sample of the data structure I am working with:

myPackage = "Structure::PackageA" // Qualified Name

PackageA
|
+-- InstanceSpecification_1 <-- First Level Child
| InstanceSpecification_1a
| InstanceSpecification_1b
| InstanceSpecification_1c
|
+-- InstanceSpecification_2 <-- First Level Child
| InstanceSpecification_2a
| InstanceSpecification_2b
|
+-- InstanceSpecification_3 <-- First Level Child
InstanceSpecification_3a
InstanceSpecification_3b
InstanceSpecification_3c


What I want to fetch are the 3 First-Level Children InstanceSpecifications in PackageA.
From the sample structure above I want
InstanceSpecification_1
InstanceSpecification_2
InstanceSpecification_3

I have tried Finder.byType, Finder.byScope, Finder.byQualifiedName etc

From what I can tell, NONE of these Finders will give me the First-Level children of a package.

Here is an example:

Code: Select all
instanceCollection = Finder.byType().find(myPackage, ClassTypes.getClassType('InstanceSpecification'))


This Finder.byType will return *ALL* instance specifications as follows:
InstanceSpecification_1
InstanceSpecification_1a
InstanceSpecification_1b
InstanceSpecification_1c
InstanceSpecification_2
InstanceSpecification_2a
InstanceSpecification_2b
InstanceSpecification_2
InstanceSpecification_3a
InstanceSpecification_3b
InstanceSpecification_3c

Can someone please show me the best way to get Only the First-Level children of a package ?

Thank You !!
philaustin378@gmail.com
Forum Newbie
Forum Newbie
 
Posts: 5
Posts Rating:0
Joined: Thu Jan 27, 2022 10:52 am

Re: Getting First-Level Children of a Package

Postby Quentin.Cespedes@Samares-Engineering.com » Thu Mar 31, 2022 5:43 am

Hello,

Try this:

Code: Select all
packageA = Finder.byQualifiedName().find(myProject, "Structure::PackageA");
List result = packageA.getOwnedElement() //get all element under your package
         .stream() //java lambda usage
         .filter(InstanceSpecification.class::isInstance) //allow to filter the result with only the instanceSpecification
        .filter(StereotypeHelper.hasStereotype(....) //allow to filter the result with a stereotype condition
        .filter(...) // etc.
        .collect(Collectors.toList()) // get the list of your result


Q.
User avatar
Quentin.Cespedes@Samares-Engineering.com
Forum Newbie
Forum Newbie
 
Posts: 11
Posts Rating:1
Joined: Mon Jun 24, 2019 7:58 am
Location: France
Full name: CESPEDES Quentin

Re: Getting First-Level Children of a Package

Postby prasad.bhat@yahoo.com » Wed May 24, 2023 5:29 pm

Finder APIs didn't work for me.

This was far more easier to fetch the element list -

var modelRoot = AutomatonMacroAPI.getModelData();

Application.getInstance().getGUILog().log(modelRoot.Member.get(1).Name);
Application.getInstance().getGUILog().log(modelRoot.Member.get(2).Name);
prasad.bhat@yahoo.com
Forum Newbie
Forum Newbie
 
Posts: 4
Posts Rating:0
Joined: Wed Jan 08, 2020 11:58 pm


Return to Programmatic Extendibility

Who is online

Users browsing this forum: No registered users and 0 guests