Download MainFOM.zip
Introduction
Simulation is the process that tries to imitate the real world inside a virtual environment. It is a cheap process that can be used to test different scenarios without requiring experiment setup or special equipment.
There are many types of simulation; I will talk today about “High-level architecture (HLA)” which is a generic simulation that decomposes the virtual environment into separate components; each component is responsible about a certain task(s) and has an interface to communicate with other components.
HLA borrowed its concept from Object Oriented Programming (OOP); it consists of an application (called Federation) which consists of several objects (called Federates). Like in OOP, each federate perform some tasks and interacts with other federates. However, in distributed simulation each federate might be running on different machine and communicate with each other through a central hub called RTI as shown in the following figure.
Figure 1 Federates communicate through RTI
A little bit of history
HLA appeared in mid-90 in military world; but now it is a standardized simulation that regulated and published by IEEE. There are three versions of this standard:
HLA components
RTI<o:p>
RTI is a key component in HLA standards, it creates the federation and provides infrastructure that can be used be federates to communicate with each other. RTI must be fully compliant with the standard by implementing all methods in the java and/or C++ interface (http://standards.ieee.org/downloads/1516/1516.1-2010/IEEE1516-2010_Java_API.zip) that is provided by HLA community.
Unfortunately, there is no open source fully-compliant RTI. However, you can get a free version of two commercial RTIs (Pitch RTI, MAK RTI) but it is limited to 2 federates per federation.
Federate
The second key component of HLA simulation is a federate. This is a piece of code that references RTI library to send/receive messages with other federates. Because all RTI implement the same interface you can write a code referencing an RTI library and will work with other RTIs by simply change the referenced library.
A federate does not have to be a brand new code; an existing software can be used as federate by writing a small piece of code that transfer data between the existing software and RTI. For example, one can write a plug-in for AutoCAD or Excel etc. to utilize their features in the simulation as will be shown in the Example section.
FOM
In order to send/receive messages, federates have to agree on what will be sent (e.g. messages names, data type, etc.). This is done through an XML file called Federation Object Model (FOM) which can be generated by a free software called “simGe” (http://www.ceng.metu.edu.tr/~otopcu/simge/). HLA community developed a sample FOM for sushi restaurant simulation (http://standards.ieee.org/downloads/1516/1516.2-2010/RestaurantFOMmodule.xml).
An Example: Traffic simulation
To clarify HLA simulation concept, let’s imagine we are trying to develop a traffic system simulation; the system will be used to investigate the effect of the following parameters:
- Weather
- Road width
- Maximum speed
- Driver expertise
Once all objectives are defined, we can start defining federates. There are no hard rules to define federates, but intuitively we can say we will have the following federates:
- Road federate: to outline the roads, schedule road maintenance, etc.
- Weather federate: to provide weather updates (e.g. precipitation).
- Traffic controller federate: staff like traffic light.
- Driver federate: to simulate car movements.
- Visualizer: to visually display simulation to the end user.
Different teams might be responsible for different federates. Each team can use a different programming language (this is one of the powerful aspects of HLA simulation) to develop their federate. In addition, not all federate have to be written from scratch; for example, for visualizer federate we can tweak existing software (e.g. Unity, or WPF) by developing a plug-in to visualize updates from RTI.
Before developing their federate, all teams have to agree on the federation FOM. FOM is an XML file with two important sections “Object class” & “Interaction class”; These two sections specify what will be published be federates. For instance, we might have an object class “HourlyWeather” with attributes as following:
<objectClass>
<name>HourlyWeather</name>
<sharing>PublishSubscribe</sharing>
<attribute>
<name>WindSpeed</name>
<dataType>HLAfloat64Time</dataType>
<updateType>Static</updateType>
<ownership>NoTransfer</ownership>
<sharing>PublishSubscribe</sharing>
<transportation>HLAreliable</transportation>
<order>TimeStamp</order>
</attribute>
<attribute>
<name>Temperature</name>
<dataType>HLAfloat64Time</dataType>
<updateType>Static</updateType>
<ownership>NoTransfer</ownership>
<sharing>PublishSubscribe</sharing>
<transportation>HLAreliable</transportation>
<order>TimeStamp</order>
</attribute>
</objectClass>
Also we can have an interaction class “RoadClosure” with an attribute “RoadID”
<interactionClass>
<name>RoadClosure</name>
<sharing>PublishSubscribe</sharing>
<transportation>HLAreliable</transportation>
<order>Receive</order>
<parameter>
<name>RoadID</name>
<dataType>HLAunicodeString</dataType>
</parameter>
</interactionClass>
Note: The complete FOM file can be downloaded from the link in the top.
Please note that the FOM file doesn’t tell which federate will publish/ subscribe object and interaction classes; it just specify what will be published/ subscribed and each federate after joining the federation should inform RTI about what will be published /subscribed. So, in our example after joining the federation the weather federate should inform RIT that it will publish “HourlyWeather” while the driver federate will subscribe to it. Hence; whenever the weather federate sends “HourlyWeather” updates, it will be forward to the driver federate by the RTI. I should mention here that each object / interaction class might have more than one publisher / subscriber.
Note: The federation might have multiple FOMs and RTI will automatically merge them into one file based on certain rules, but this is outside the article scope.
HLA Listener
Objectives
HLA simulation has a very steep learning curve, it requires a lot of effort to wrap your head around all of its concept. Therefore, I developed a small app called “HLA Listener” for debugging and learning HLA concepts.
HLA Listener is a federate with a GUI, which upon completion will implement all methods in the standard. It can be used to digest all HLA concepts before delving into the code. I can safely say with even my experience in writing federates code, I learned a lot from this tool because it enables me to answer any if-question with few clicks.
Interface
HLA Listener is a maven open source project, hosted in github (https://github.com/EMostafaAli/HlaListener).
In order to use this software, first install any fully compliant RTI (I recommend using Pitch RTI), then run HLA Listener and select the library jar file (Figure 2). After this you will see the main window as shown in Figure 3. It is highly recommended to run two versions of this tool to see how the communication happens through the RTI.
Figure 2 First step select HLA library
Figure 3 HLA Listener main window
Code
Note: All samples here are extracted from GitHub repository.
Load jar file in the runtime
Because all fully-compliant RTI has to implement the exact same interface, I build my app based on this interface and at runtime the user has to provide the actual implementation that will be added to the class path as following:
private static int AddtoBuildPath(File f) {
logger.entry();
try {
URI u = f.toURI();
URLClassLoader urlClassLoader = (URLClassLoader) ClassLoader.getSystemClassLoader();
Class<URLClassLoader> urlClass = URLClassLoader.class;
Method method = urlClass.getDeclaredMethod("addURL", new Class[]{URL.class});
method.setAccessible(true);
method.invoke(urlClassLoader, new Object[]{u.toURL()});
} catch (NoSuchMethodException | SecurityException | IllegalArgumentException |
InvocationTargetException | MalformedURLException | IllegalAccessException ex) {
logger.log(Level.FATAL, "Error adding the jar file to the class path", ex);
return 1;
}
logger.exit();
return 0;
}
After that, we have to use “getRtiFactory method to get a pointer to actual RTI implementation (known as RTI ambassador).
URL[] urls = new URL[]{file.toURI().toURL()};
URLClassLoader child = new URLClassLoader(urls, this.getClass().getClassLoader());
Class RtiFactoryFactory = Class.forName("hla.rti1516e.RtiFactoryFactory", true, child);
Method getRtiFactory = RtiFactoryFactory.getMethod("getRtiFactory");
rtiFactory = (RtiFactory) getRtiFactory.invoke(null);
rtiAmb = rtiFactory.getRtiAmbassador();
Send to RTI
Once we have a pointer to RTI ambassador we can send request by invoking the corresponding method; the following code connect to RTI, create a federation, and then join the federation
rtiAmb.connect(fedAmb, CallbackModel_choiceBox.getValue());
rtiAmb.createFederationExecution(FederationExecutionName.getText(), foms.toArray(new URL[foms.size()]), LogicalTimeImplementation.getValue());
rtiAmb.joinFederationExecution(FederateType.getText(), FederationExecutionName.getText());
Receive from RTI
The above code shows how to send requests to RTI; in order to receive callbacks from RTI, we have to extend the “NullFederateAmbassador” abstract class and override its methods. The following code writes a message to the console in case of lost connection.
public class ListenerFederateAmb extends NullFederateAmbassador {
@Override
public void connectionLost(String faultDescription) throws FederateInternalError {
System.out.println("Unfortunately, connection to RTI Lost. Try to reconnect");
}
}
Progress so far
So far, I implemented the following chapters in the standard:
- Chapter 4: Connect to RTI, Create federation, Join federation, etc.
- Chapter 5: publish/subscribe attributes and interaction
- Chapter 8: Time management.
- Scattered items in the remaining chapters.
What is next?
This is an initiative step to leverage awareness about HLA simulation, which hopefully will lead to develop a free RTI. If you are new to HLA simulation download the code, build it, and teach yourself. If you are familiar with HLA, feel free to give me a hand with implementing the standards.
Conclusion
I tried as much as I can to keep this article concise; therefore, I didn’t discuss much about HLA concepts (e.g. Time management), nor went through the code thoroughly. But if required, I can write a series of articles that explains HLA concepts. I can also provide a step by step tutorial using HLA Listener but I am not sure if this match codeproject theme.
History
- May 24, 2015: Initial version
- May 25, 2015: Expand the article, add sample codes, and resize picture