How to get attributes of JaamSim objects in Java source code?

722 views
Skip to first unread message

Martin B.

unread,
Mar 26, 2018, 11:51:30 AM3/26/18
to Jaamsim Users Discussion Group


Hi Harry,
we are trying to adapt the source code of the Assign object to realize some custom logic implementation in Java. We are intending to create an object which is located in front of our branch following specific rules to guide the entities to different servers (see Picture). Attached you will find our source code. For example with this object we want to choose the next server of the entity based on the WorkingState Attribute of the servers.

Now we don´t want to fill out the AttributeAssignmentList with the attributes of our servers everytime we create a new model.
We want to do it automatically in our sourcecode, which is also much easier when the models get more complicated.

In our latest version we are trying to do it with a for loop and the ExpressionParser but it doesnt work so far. The idea is that we only enter the number of servers behind the Keyword "Number of Servers" in the input of the function and then the AssignmentString is generated and parsed, so that we get the information about the server statusses.

    private ArrayList<ExpParser.Assignment> getAssignmentString2(int numberOfServers) {
        ArrayList<ExpParser.Assignment> servStatus = new ArrayList<>();
        for(int i = 1; i <= numberOfServers; i++) {
            ExpParser.Assignment assignment = new ExpParser.Assignment("this.Server" + i + "=[Server" + i + "].Working");
            servStatus.add(assignment);
        }
        return servStatus;

Another approach, which we are currently testing, is to filter for example all servers from the entityList ((p) basicsim/ (c) ObjectSelector) and work with this list.

I would be great if it somehow possible to simply access the list of all servers in the model, so that the number can be read out and for each server in the list the attributes can used.

Do you have some advice for us?


Thank you very much!

Martin

MyAssign.java
Test_MyAssign_v01_20180322_gpp_ms.cfg

Harry King

unread,
Mar 26, 2018, 1:34:10 PM3/26/18
to Jaamsim Users Discussion Group
Martin,

I think you can do everything you need using an expression. Create an attribute name ServerList on Branch1 containing '{ [Server1], [Server2] }' and enter the following expression to its Choice keyword:

'n = size(this.ServerList); list = filter(|x|(!this.ServerList(x).Working), range(n)); size(list) > 0 ? list(1) : 1'

This expression works as follows:
  • local variable 'n' is the number of servers
  • local variable 'list' is a list containing the indices of the servers that are not working
  • If any of the servers are not working then the index of the first one is returned. If none of the servers are idle then index of the first server (1) is returned.
Is this the basic functionality that you want?

If more flexibility is needed, I have considered allowing the NextComponent keyword to accept an expression that returns an object. This would allow any object to act like a Branch object. My only concern with this idea is that it is perhaps a bit too flexible.

Harry

Martin B.

unread,
Mar 28, 2018, 5:20:12 AM3/28/18
to Jaamsim Users Discussion Group
Hi Harry,
thank you for your fast answer. That´s not the basic functionality we want. We basically want to build a custom object which is located in front of our branch following specific rules to guide the entities to different servers. All in all we want to code as little as possible in the JaamSim user environment to keep it clear and easy for the user. Actually we barely want to use the Input Editor. For that we want to access the list of all servers of the model, so that the number can be read out and for each server in the list the attributes can be used. We want to do that in the source code of our MyAssign object. MyAssign extends LinkedComponent and is a individual object besides Assign.

Our code:
https://pastebin.com/AYdvcu1Y

Kind regards
Martin

Harry King

unread,
Mar 29, 2018, 12:41:02 PM3/29/18
to Jaamsim Users Discussion Group
Martin,

The following Java code will produce an ArrayList containing all the Servers in a model:

    ArrayList<Server> serverList = new ArrayList<>();
    for (Server s : Entity.getClonesOfIterator(Server.class)) {
        serverList.add(s);
    }

If you want save serverList by making it a property of some object, then place this code in the object's earlyInit() method. This method is called at the start of each simulation run, after the 'Play' button is clicked.

Harry

Martin B.

unread,
Apr 5, 2018, 10:15:57 AM4/5/18
to Jaamsim Users Discussion Group
Harry,
i tried to place your code in our MyAssign object but there is no earlyInit() method in it (Code of our MyAssign object: https://pastebin.com/AYdvcu1Y ). Actually i dont know which method you refer to.

Finally we want to replace the AttributeDefinitionList and the AttributeAssignmentList with the serverList in the Input Editor (see graphic below). Therefor we need the list of all servers with the associated attributes like {Server1 0 Server2 0} for example. The reason is that we want to create an environment which is easy to establish and which allows us to test hundreds of different configurations in a really short time (or maybe automatized with an excel sheet). Is there a way to get such a list and and replace our expressions in the Input Editor? This would help us a lot.

Thank your very much and greetings from germany!
Martin

Harry King

unread,
Apr 7, 2018, 10:34:16 AM4/7/18
to Jaamsim Users Discussion Group
Martin,

The earlyInit() method is defined in Entity and is overridden as required in its sub-classes. Look in Server for an example.

You can set input values in code using the method InputAgent.applyArgs(Entity ent, String keyword, String... args), where ent is the entity whose keyword is to be set, keyword is the keyword string, and args is an array of strings containing the input text. Look in ContextMenu for examples of its use.

Harry
Reply all
Reply to author
Forward
Message has been deleted
0 new messages