Why does Finder sometimes return a HashSet others ArrayList

MagicDraw OpenAPI, scripting related questions and discussions

Moderator: Moderators

Why does Finder sometimes return a HashSet others ArrayList

Postby coolmani@hotmail.com » Tue Oct 31, 2017 9:02 am

Hi
I'm getting up to speed on macros so I'm probably doing something wrong. In the attached code, I'm trying to get the documentation from the "Function". I'm trying to limit the Finder to the SV4 under System Viewpoints. The second Finder (line 27) for SV-4 doesn't always seems to return an array, 5 out of 6 times returns a HashSet and the rest of the code doesn't seem to work (I'm showing this in the log window in line 32). Any idea why this is happening?
Code: Select all
var CollectionsAndFiles = new JavaImporter(
    java.util,
    java.io,
    java.lang,
    com.nomagic.magicdraw.automaton,
    com.nomagic.magicdraw.core,
    com.nomagic.magicdraw.uml.Finder,
    com.nomagic.magicdraw.uml.Refactoring,
   com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Class,
   com.nomagic.magicdraw.uml.ConvertElementInfo,
   com.nomagic.magicdraw.openapi.uml.SessionManager,
   com.nomagic.uml2.ext.magicdraw.mdprofiles.Stereotype,
   com.nomagic.uml2.ext.magicdraw.mdprofiles.Profile,
   com.nomagic.uml2.ext.jmi.helpers.StereotypesHelper);

with(CollectionsAndFiles)
{
    try
    {
        SessionManager.getInstance().createSession('Automaton_Macro_Script_Execute');
       
        // Get all classes for the model
        var project = Application.getInstance().getProject();
      // get the upper folder to limit search
      var sv4Var = Finder.byQualifiedName().find(project, "Systems Viewpoint::SV-4");
        // Find in projects all elements of type "activity"
        var allClasses = Finder.byTypeRecursively().find(sv4Var, activity, true);
        // Allow all functions to access the log without accessing application.
        var GUILog = Application.getInstance().getGUILog();
        // all the elements of type "Activity" are stored in an array. Get the length of the array and traverse through the array to only get elements of type "Function"
        var aLength = allClasses.length;
        GUILog.log("allClasses Type-->"+allClasses.getClass());
      for (var i = 0; i < aLength; i++) {
         var typeVal = allClasses[i].humanType;                              // get the human readable type from the array elements
         if (typeVal != null) {
            if (typeVal == "Function") {                                 // only get the types "Function"
                if(allClasses[i].hasOwnedComment()) {                        // check to see if the element has documentation
                  var originalDoc =  allClasses[i].ownedComment[0].body;
                  var modDoc =  originalDoc.replace(/<a\b[^>]*>/g,"").replace(/<\/a>/g, "");      // remove from documentation hyperlinks, which in HTML are shown as <a href ..>  ... </a>
                  // Update the documentation with the text that removes hyperlinks         
                  allClasses[i].ownedComment[0].setBody(modDoc);
                  // print a record of the element that was changed
                  GUILog.log("Updated-->"+allClasses[i].qualifiedName);
                  }
            }
            }
          }
       }
    finally
    {
        SessionManager.getInstance().closeSession();
    }
}

I'm also attaching the sample project I'm using.
You do not have the required permissions to view the files attached to this post.
coolmani@hotmail.com
Forum Newbie
Forum Newbie
 
Posts: 5
Posts Rating:0
Joined: Thu Sep 07, 2017 10:08 am
Full name: Chasing My Tail

Re: Why does Finder sometimes return a HashSet others ArrayList

Postby paudzi » Fri Nov 03, 2017 1:29 am

Hello,

I have analyzed the code and find out that the problem is in the line:
Code: Select all
var allClasses = Finder.byTypeRecursively().find(sv4Var, activity, true);

The definition of the find method is:
Code: Select all
<T extends Element> Collection<T> find(Element root, @CheckForNull Class[] types, boolean includeRoots)

so it should be called:
Code: Select all
Collection<Element> allClasses = Finder.byTypeRecursively().find(sv4Var, new java.lang.Class[]{Activity.class}, true);

It's a java code because I am not familiar with the script you are using, but I think it shouldn't be a problem to convert to script syntax.

With best regards,
No Magic Customer Support
paudzi
Customer Support
Customer Support
 
Posts: 71
Posts Rating:4
Joined: Thu Oct 20, 2011 5:50 am

Re: Why does Finder sometimes return a HashSet others ArrayList

Postby coolmani@hotmail.com » Tue Dec 19, 2017 12:38 pm

paudzi wrote:Hello,

I have analyzed the code and find out that the problem is in the line:
Code: Select all
var allClasses = Finder.byTypeRecursively().find(sv4Var, activity, true);

The definition of the find method is:
Code: Select all
<T extends Element> Collection<T> find(Element root, @CheckForNull Class[] types, boolean includeRoots)

so it should be called:
Code: Select all
Collection<Element> allClasses = Finder.byTypeRecursively().find(sv4Var, new java.lang.Class[]{Activity.class}, true);

It's a java code because I am not familiar with the script you are using, but I think it shouldn't be a problem to convert to script syntax.

With best regards,
No Magic Customer Support


Thank you for taking time to look at this. What would the Javascript code be for the solution you provided? I don't know how to translate what you gave me.
This is what I tried with no success:
Code: Select all
var searchType = Java.type("com.nomagic.uml2.ext.magicdraw.activities.mdfundamentalactivities.Activity").class;
var allClasses = Finder.byTypeRecursively().find(sv4Var, [searchType], true);

Thank you
coolmani@hotmail.com
Forum Newbie
Forum Newbie
 
Posts: 5
Posts Rating:0
Joined: Thu Sep 07, 2017 10:08 am
Full name: Chasing My Tail

Finder.byQualifiedName().find(currentProject, "Spigot")

Postby jjosburn@gmail.com » Sat Jul 24, 2021 10:11 pm

what value should the string be to find something in a bdd?
jjosburn@gmail.com
Forum Newbie
Forum Newbie
 
Posts: 3
Posts Rating:0
Joined: Thu Jul 01, 2021 12:32 pm

Re: Why does Finder sometimes return a HashSet others ArrayList

Postby safganof@gmail.com » Mon Aug 02, 2021 3:03 am

coolmani@hotmail.com wrote:Hi
I'm getting up to speed on macros so I'm probably doing something wrong. In the attached code, I'm trying to get the documentation from the "Function". I'm trying to limit the Finder to the SV4 under System Viewpoints. The second Finder (line 27) for SV-4 doesn't always seems to return an array, 5 out of 6 times returns a HashSet and the rest of the code doesn't seem to work (I'm showing this in the log window in line 32). Any idea why this is happening?
Code: Select all
var CollectionsAndFiles = new JavaImporter(
    java.util,
    java.io,
    java.lang,
    com.nomagic.magicdraw.automaton,
    com.nomagic.magicdraw.core,
    com.nomagic.magicdraw.uml.Finder,
    com.nomagic.magicdraw.uml.Refactoring,
   com.nomagic.uml2.ext.magicdraw.classes.mdkernel.Class,
   com.nomagic.magicdraw.uml.ConvertElementInfo,
   com.nomagic.magicdraw.openapi.uml.SessionManager,
   com.nomagic.uml2.ext.magicdraw.mdprofiles.Stereotype,
   com.nomagic.uml2.ext.magicdraw.mdprofiles.Profile,
   com.nomagic.uml2.ext.jmi.helpers.StereotypesHelper);

with(CollectionsAndFiles)
{
    try
    {
        SessionManager.getInstance().createSession('Automaton_Macro_Script_Execute');
       
        // Get all classes for the model
        var project = Application.getInstance().getProject();
      // get the upper folder to limit search
      var sv4Var = Finder.byQualifiedName().find(project, "Systems Viewpoint::SV-4");
        // Find in projects all elements of type "activity"
        var allClasses = Finder.byTypeRecursively().find(sv4Var, activity, true);
        // Allow all functions to access the log without accessing application.
        var GUILog = Application.getInstance().getGUILog();
        // all the elements of type "Activity" are stored in an array. Get the length of the array and traverse through the array to only get elements of type "Function"
        var aLength = allClasses.length;
        GUILog.log("allClasses Type-->"+allClasses.getClass());
      for (var i = 0; i < aLength; i++) {
         var typeVal = allClasses[i].humanType;                              // get the human readable type from the array elements
         if (typeVal != null) {
            if (typeVal == "Function") {                                 // only get the types "Function"
                if(allClasses[i].hasOwnedComment()) {                        // check to see if the element has documentation
                  var originalDoc =  allClasses[i].ownedComment[0].body;
                  var modDoc =  originalDoc.replace(/<a\b[^>]*>/g,"").replace(/<\/a>/g, "");      // remove from documentation hyperlinks, which in HTML are shown as <a href ..>  ... </a>
                  // Update the documentation with the text that removes hyperlinks         
                  allClasses[i].ownedComment[0].setBody(modDoc);
                  // print a record of the element that was changed
                  GUILog.log("Updated-->"+allClasses[i].qualifiedName);
                  }
            }
            }
          }
       }
    finally
    {
        SessionManager.getInstance().closeSession();
    }
}

I'm also attaching the sample project I'm usingMy Balance Now.


A lot of people are saying that once you get to the size where speed is Yes a linear search of a List will beat a HashSet for a small number of items.
safganof@gmail.com
Forum Newbie
Forum Newbie
 
Posts: 1
Posts Rating:0
Joined: Mon Aug 02, 2021 3:01 am

Re: Why does Finder sometimes return a HashSet others ArrayList

Postby jamalcosta27@gmail.com » Sat Sep 25, 2021 3:37 am

Thank you for posting,FirstCallOnline the post you published have some great information which is quite beneficial for me.
jamalcosta27@gmail.com
Forum Newbie
Forum Newbie
 
Posts: 1
Posts Rating:0
Joined: Sat Sep 25, 2021 3:34 am


Return to Programmatic Extendibility

Who is online

Users browsing this forum: No registered users and 2 guests