Skip to content

XML configurations and properties

The code for this tutorial is available on GitHub

https://github.com/ADAMMA-CDHI-ETH-Zurich/CLAIDTutorial02

In the previous tutorial, we created a new Module called "MyModule", which we manually added to CLAID at Runtime. This, however, is not the intended way to instantiate Modules with CLAID. The beauty of CLAID is, that you can put together (complex) applications by simply combining different Modules. This has be flexible and parameterizable. Hence, CLAID allows you to instantiate and combine different Modules simply by providing XML configurations. In this tutorial, we will change our previous code to load our created Module ("MyModule") from an XML configuration instead. You can continue working on the previous code, or create a new project for the code discussed here (see instructions from the previous tutorial).

Changing the previous tutorial Code to use XML configurations

All we have to do in order to add support for XML configurations to our previous application is to change the code in the main function.

Code changes C++
#include "CLAID.hpp"

int main()
{




    // Loading Modules from an XML file.
    CLAID_RUNTIME->loadFromXML("TutorialConfig.xml");

    // Starting CLAID Runtime, which automatically will initialize all Modules.
    CLAID_RUNTIME->start();
    return 0;
}
#include "CLAID.hpp"

int main()
{
    // Manually creating an instance of MyModule. 
    // Later, instantiating and configuring Modules will be done using XML configurations.
    HelloWorld::MyModule* myModule = new HelloWorld::MyModule;

    // Adding a Module to the Runtime.
    CLAID_RUNTIME->addModule(myModule);

    // Starting CLAID Runtime, which automatically will initialize all Modules.
    CLAID_RUNTIME->start();
    return 0;
}
Code changes Java
import JavaCLAID.CLAID;

public class Main 
{
    static
    {
        System.loadLibrary("JavaCLAID");
    }

    public static void main(String[] args)
    {
        CLAID.loadFromXML("TutorialConfig.xml");
        CLAID.start();
    }
}
import JavaCLAID.CLAID;

public class Main 
{
    static
    {
        System.loadLibrary("JavaCLAID");
    }

    public static void main(String[] args)
    {
        CLAID.addModule(new MyModule());
        CLAID.start();
    }
}

You see, that all we needed to do is to replace the code which manually added the Module by a corresponding call to the CLAID_RUNTIME, which will load an XML file instead. Now, all that's left to do in order to run the example again, is to create the XML configuration. Create a file called "TutorialConfig.xml" in the following location:

  • If you build a CLAID C++ application: Create a file "TutorialConfig.xml" in the main folder of the project directory (i.e., in the same directory that also the CMakeLists.txt is located).
  • If you build a CLAID Java application: Create a file "TutorialConfig.xml" in the Java application folder (e.g. "MyApplication"), next to the Main.java
XML configuration
<root>
    <Module class = "HelloWorld::MyModule">
        <!-- Properties can be added here. -->
    </Module>
</root>
<root>
    <JavaModule class = "HelloWorld.MyModule">
        <!-- Properties can be added here. -->
    </JavaModule>
</root>

Multiple Modules can be added to the configuration by using the <Module> or <JavaModule> tag. This tag requires the attribute "class", which specifies what Module to load exactly. The provided identifier is used to instantiate the Module at startup, using the ClassFactory. This identifier consists of the namespace(s) or package name of the class and it's name. In the next tutorials, we will cover more complex examples, where multiple Modules are added to the configuration, and explain how properties can be added to Modules.

Running the code

To run the example, simply recompile it and start the application, just as done in the previous part of this tutorial:

Recompile and run the example

Open a terminal in the project directory (e.g., right click in the directory and choose open with). Afterwards, execute the following commands:

make -j
./MyApplication

Open a terminal in the project directory. Afterwards, execute the following commands:

make -j
./MyApplication

Open the windows commandline (or PowerShell) in the project directory. You can also use shift + Right click in the directory, to open the commandline directly in this directory.

Afterwards, enter the following commands:

cmake --build .
\Debug\MyApplication.exe

How to specify the XML file by commandline arguments (only C++, for now)

The code discussed above will load an XML file from a hardcoded path. Perhaps you want to specify the path to the XML file by providing a commandline argument instead, i.e.

./MyApplication path "TutorialConfig.xml"

Of course, we got you covered and provide a simple way to add arbitrary commandline arguments to your program. For this, you need to make small alternations to the main function of your program. See the code below.

Adapted code to support loading from XML file specified by commandline argument.

Necessary changes were made regarding the main() function. Do not forget to include the ArgumentParser, as seen in the Code!

#include "CLAID.hpp"
// Include the CLAID ArgumentParser
#include "Utilities/ArgumentParser.hpp"
// Change main to inlcude argc and argv parameters.
int main(int argc, char** argv) 
{
    ArgumentParser parser(argc, argv);

    std::string configPath = "";
    // Register argument to the parser.
    // This automatically parses argv.
    // A default value can be specified by the last parameter of the function.
    parser.add_argument<std::string>("path", configPath, "");

    // If "path" was not specified in argv, configPath is empty.
    // In that case, we print an error message.
    if(configPath == "")
    {
        printf("Error, no path to XML configuration specified.\n"
        "Please use ./MyApplication path path/to/xml/file.\n"
        "E.g.: ./MyApplication path TutorialConfig.xml\n");
        exit(0);
    }

    // Loading Modules from an XML file specified by argument.    
    CLAID_RUNTIME->loadFromXML(configPath);

    // Starting CLAID Runtime, which automatically will initialize all Modules.
    CLAID_RUNTIME->start();
    return 0;
}
#include "CLAID.hpp"
//  
//  
//  
int main()
{


















    // Loading Modules from an XML file.
    CLAID_RUNTIME->loadFromXML("TutorialConfig.xml");

    // Starting CLAID Runtime, which automatically will initialize all Modules.
    CLAID_RUNTIME->start();
    return 0;
}

Now, after having recompiled the application, you can start it again and specify the configuration path as follows:

Recompile and run the example

Open a terminal in the project directory (e.g., right click in the directory and choose open with). Afterwards, execute the following commands:

make -j
./MyApplication path TutorialConfig.xml

Open a terminal in the project directory. Afterwards, execute the following commands:

make -j
./MyApplication path TutorialConfig.xml

Open the windows commandline (or PowerShell) in the project directory. You can also use shift + Right click in the directory, to open the commandline directly in this directory.

Afterwards, enter the following commands:

cmake --build .
\Debug\MyApplication.exe path TutorialConfig.xml