Using external simulator to synchronize Brahms engine and create Brahms agents in external simulation which can communicate with other agents in external simulation as well

43 views
Skip to first unread message

Mingxin Zhang

unread,
Jul 16, 2015, 5:22:55 PM7/16/15
to brahms...@googlegroups.com
Dear friends,

This is originally a question sent to Ruben in Ejenta, and we decide to post it here in order to attract more Brahms experts to contribute on it together, as if you are also interested in the synchronization and communication between Brahms engine and external simulators.

My plan is to use an external dominating simulator to synchronize the time advance of Brahms. With the codes shown below, I can create new BVM and VM  controller in my JAVA application where the external simulator resides.


            // Create new BVM

            bvm brahmsVirtualMachine = new bvm();

            // Generate list of bvm arguments

            List<String> bvmArguments = new ArrayList<String>();

            bvmArguments.add("-cf");

            bvmArguments.add("./vm.cfg");

            // Initialize bvm

            int errorCode = brahmsVirtualMachine.initialize(bvmArguments.toArray(new String[0]));

            // Create time sync hook

            MyTimeSyncHook myTimeSyncHook = new MyTimeSyncHook();

            // Get VMController for the BVM

            VMController vmController = brahmsVirtualMachine.VMCONTROLLER;

            // Load Brahms Model

            vmController.loadModel("gov.nasa.arc.brahms.atm.AtmModel");

            // Register time sync hook

            vmController.registerTimeSyncHook(myTimeSyncHook);

            // Start the vm controller

            vmController.start();


But when I tried to generate new Brahms agents in my application using the codes as follows,


        // Create array of agent groups

        IGroup[] groups = new IGroup[1];

        groups[0] = getModel().getGroup("gov.nasa.arc.brahms.atm.Student");

        // Create agent that inherits from the defined groups, named Agent0

        IAgent agent = AgentFactory.createAgent("Agent0", groups, getContext());

        // Set belief for agent

        agent.setBeliefAttributeBoolean(agent, "male", true, getContext());


I got the following exception:

 

java.lang.NullPointerException

       at gov.nasa.arc.brahms.vm.api.jagt.AbstractExternalAgent$Context.getOwner(AbstractExternalAgent.java:2245)

       at gov.nasa.arc.brahms.vm.japi.common.JAgent.createAgent(JAgent.java:150)

       at gov.nasa.arc.brahms.vm.api.common.AgentFactory.createAgent(AgentFactory.java:40)

 

I have added the definition of the Brahms agent in my Brahms model as an external agent, and it works well if the Brahms engine is  dominating and the Composer did all the initializations.

 

Then I think this exception is caused as I might have missed some necessary initialization steps before using the AgentFactory to  create new Brahms agents in the external simulation, but it’s really difficult to ‘guess’ the right sequential initialization steps by just reading the JAVA Docs of Brahms. Thus, I'd like to kindly ask help to offer me any project example that can show the right way to generate Brahms agents in external JAVA application, and I also want to enable  these externally generated Brahms agents can communicate with outside simulation agents through IAgent interface.

Please fell free to comment and discuss in this post, and thank you in advance if you can contribute on it.

Kind regards,
Mingxin    

Ruben van der Dussen

unread,
Jul 21, 2015, 11:53:51 AM7/21/15
to brahms...@googlegroups.com
Dear Mingxin,

The issue you are experiencing is caused by the fact that by default you cannot access the simulation context from the outside simulation directly. What you need to do is to set a static reference to the simulation using an external agent. You then can use this static reference to the simulation context to create agents from the outside simulation:

Create a static variable for storing the context reference:

package bvm;

import gov.nasa.arc.brahms.vm.api.common.IContext;

public class Bvm {
    public static IContext context;
}

Set the static variable by adding the following line to the onStart method of an external agent that is started with the simulation:

@Override
public void onStart() throws ExternalException {
    Bvm.context = getContext();
}

Now you can use the reference to the simulation context to create and access new agents from the outside simulation:

try {
    IGroup[] groups = new IGroup[1];
    groups[0] = Bvm.model.getGroup("gov.nasa.arc.brahms.atm.Student");
    // Create agent that inherits from the defined groups, named Mingxin
    IAgent agent = AgentFactory.createAgent("Mingxin", groups, Bvm.context);
    // Set belief for agent
    agent.setBeliefAttributeBoolean(agent, "male", true, Bvm.context);
} catch (ExternalException e) {
    e.printStackTrace();
}

Let me know if this works for you.

Ruben
Message has been deleted

Mingxin Zhang

unread,
Jul 21, 2015, 5:26:43 PM7/21/15
to Brahms Forum

Hi Ruben,

 

Thanks for this solution! With it, I found the problem in my project and sort of solved it. What I did is: 1) start the external simulation; 2) start the Brahms engine; 3) use Brahms engine to create a Brahms agent 4) use external simulation to create Brahms agents and it works. So the ’Icontext’ can only be accessed if there is  at least an existing Brahms agent created by Brahms engine.

 

Thanks for your help!

 

Best regards,

Mingxin

Reply all
Reply to author
Forward
0 new messages