CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   STAR-CCM+ (https://www.cfd-online.com/Forums/star-ccm/)
-   -   Collections in Java (https://www.cfd-online.com/Forums/star-ccm/109295-collections-java.html)

ryancoe November 14, 2012 09:53

Collections in Java
 
I'd like to use a Collection (or something else) to do the following:
  1. Create Collection containing all of the reports in a simulation
  2. Check if the Collection contains all the necessary reports
  3. If it is missing a report, add it

My sticking point is that to use the Collection.contains(Object o) method, the Object you input must be of the same class as those in the Collection you're dealing with.

Thanks,

Ryan

abdul099 November 14, 2012 16:45

It should also work when the object you input is inherited from the objects in the collection, e.g. a MaximumReport is inherited from Report. So it should work when you put all reports in a collection containing report type objects.

ryancoe November 15, 2012 08:05

Ah ok, thanks.

How would I create a Report object in my macro without creating a report in the simulation?

abdul099 November 15, 2012 18:39

Hm, it seems I didn't get the point when I first read your post.

Creating reports just in your macro isn't possible since the report constructor needs a ClientServerObject (which you don't have).
You also can't compare with a number of pre-created reports (let's assume you've saved them from another simulation by serialization). The reason is, the old report will not point to the same object as the current one.
That's like having a picture of your friend's Corvette. When you see one on the street, it looks the same - but it isn't your friend's one. It's not the same object.

When you just want to to check if a number of needed reports is in the simulation, I would do a simple name comparison. This can be done by getting all reports in your simulation, creation of a collection (or array) of strings containing the report names. Then you just need to iterate through the reports collection and check if the report name matches the first field of your string collection. Then go to the second string in the collection, iterate over the reports collection...

That's a lot of work for your machine, and for sure it's not the most elegant way. Maybe there's a better way, but for today it's too late. I need to check my little helper macro collection first :D

You might shortcut it a little by using some try-catch statements. Just try to get a specific report in the try-block. When it fails, create it in the catch-block. With this you'll avoid to iterate through a collection for every object of a collection your're iterating through. (Can also be put in a single loop, so you don't have to write ton's of try-catch statements). This would be a nice solution when you write your own class, containing all necessary data like report name, report type, boundaries to put in... etc. Then you just need to loop over a collection with this own-class objects, get the name, try to get the corresponding report in CCM+ by name comparison, and if it fails, you have all information available to create the report in CCM+.

ryancoe November 15, 2012 21:03

Abdul,

Thanks much for the detailed reply!

You've confirmed my suspicions about needing to compare an string array containing the report names. While this may take a minute, that shouldn't be much of an issue since it only needs to happen once (at the beginning of a simulation which will take itself take hours to iterate).

Your try-catch idea, with a self-defined class containing the reports' particulars, is a great idea. I'll give that a try and let you know how it goes.

Thanks again,

Ryan


All times are GMT -4. The time now is 00:45.