Your first Federate object

Now you have all the class and attribute handles, you can create a new simulation object. If you don’t, please go to the Class and Attribute handles  post.

First of all, I will create a Java class to hold my simulation objects. It’s not a rule, but I like this way.

public class CoreObject {
  private ObjectInstanceHandle instance;
  private String serialNumber;
  private String ownerNode;
  
  public CoreObject( ObjectInstanceHandle instance ) {
    this.instance = instance;
  }
  
  // getters and setters
  
}

If you remember my “CoreClass” in previous post, you’ll see a list to hold all of this objects.

private List<CoreObject> objects;

Lets change the “CoreClass” and add some methods…

public List<CoreObject> getObjects() {
  return objects;
}

public ObjectInstanceHandle createNew( String ownerNode, String serialNumber ) throws RTIexception {
  ObjectInstanceHandle coreObjectHandle = rtiamb.registerObjectInstance( classHandle );
  CoreObject co = new CoreObject( coreObjectHandle );
  co.setOwnerNode( ownerNode );
  co.setSerialNumber( serialNumber );
  objects.add( co );
  return coreObjectHandle;
}

When you call “registerObjectInstance( classHandle )” you need to pass the class handle. A new “object” of this kind will be created inside the RTI. You must store the object handle for future reference ( see CoreObject.instance ).

You can give an internal name to this object. Portico will show it in its logger and will be easy to track and debug your Federation. This name must be unique.

ObjectInstanceHandle handle = rtiamb.registerObjectInstance( classHandle, "My First Object" );

Now, you can create some simulations by using CoreClass as your “object manager”.

CoreClass myCoreClass = new CoreClass( rtiamb );
myCoreClass.takeHandles();
myCoreClass.publish();
myCoreClass.createNew( "The Owner", "ABC123" );

Piece of cake! You can use this same class in other Federate, just to use the subscribe method, but you cannot invoke the createNew() method because this Federate becomes the owner of this class. This will raise an RTIException.

 

 

Add Comment