Tuesday May 07, 2013

RunStopSimulator: Complete Infrastructure for Simulator Software

Let's imagine we're creating simulation software. In other words, we have some kind of XML file (such as a SubSim Simulation File) and we want to process that file, i.e., use it to simulate something. What are the requirements for software that has the above (admittedly vague) description?

Well, first of all, we somehow need to open our file into the simulator or, at least, access it from the simulator in some way. Then, we need to be able to start the simulation and, once it has started, we shouldn't be able to start it again, until it has stopped. While it is running, we should be able to stop the simulation. We should show the current progress of the simulation to the user. We should also be able to run simulations on multiple files at the same time. And, finally, we should create an infrastructure that lets contributors easily plug in new simulations, since our XML files can be used as the basis of multiple different kinds of simulations, i.e., different algorithms or processors or filters need to be applied to our data and we don't know up front what all those simulations are going to be, hence we need a flexible architecture allowing for simple integration of external contributions.

OK. So, all the above is available out of the box and for free right here:

https://java.net/projects/nb-api-samples/sources/api-samples/show/versions/7.3/misc/RunStopSimulator

In other words, the above is an infrastructure for your own simulation software, which looks exactly like the below. Right now, only files with "Simulation" as the root element of the XML file are supported but all you need to do to change that is to go to line 38 of the SimDataObject class and change the root element to anything else.

As you can see, the green start button is enabled when a file conforming to the predefined root element has been opened, which can be done via File | Open File or via the Favorites window from the Window menu.

Once the simulation is running, the green start button is automatically disabled, the red stop button is enabled, and the progress bar starts up:

Multiple files can be run and stopped together:

When the red stop button has been clicked for one file, the green start button becomes enabled, but only for the file where the simulation has stopped. For the other files, which are still being handled, the green start button remains disabled.

And the architecture is clear, easy to understand, and modular:

New simulations are added to the simulator by implementing SimulationProcessor and registering it in the NetBeans service registry:

@ServiceProvider(service = SimulationProcessor.class)
public class SimpleSimulationProcessor implements SimulationProcessor {
    @Override
    public void process(FileObject fo) {
        OutputWriter writer;
        InputOutput io = IOProvider.getDefault().getIO(fo.getNameExt(), false);
        io.select();
        writer = io.getOut();
        try {
            List asLines = fo.asLines();
            for (String string : asLines) {
                writer.println(string);
            }
        } catch (IOException ex) {
        }
    }
}

The simple processor, above, does nothing more than print the lines of the XML file into the Output window. But, since you can work with the file in any way you like, i.e., from the FileObject you have access to java.io.File, you can parse it any way you like, or filter it however you need, all within a module dedicated to the specific task for which the filter/parser/algorithm exists.

Feature requests for the simulator infrastructure are more than welcome in the comments to this blog. If someone uses it as the basis of their own simulator software, that would be good to know about!

About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« May 2013 »
SunMonTueWedThuFriSat
   
 
       
Today