Arcane  4.1.12.0
User documentation
Loading...
Searching...
No Matches
Example No. 2

Example 2 is almost identical to Example 1, apart from the definition of the table name (and file) and the subdirectory name.

As a singleton, our service does not have access to the dataset, so we must go through one of the modules to transfer the information.

Example 2 shows how to do this simply.

.axl File – <options> Section

To start, let's look at the options of the axl file:

SimpleTableOutputExample2.axl

<options>
<simple name="tableName" type="string" default="">
<description>
Name for table file (example: Results ).
</description>
</simple>
<simple name="tableDir" type="string" default="">
<description>
Directory for table file (example: example2 ).
</description>
</simple>
</options>

If you take a look at the service's .axl (here: Service 'SimpleCsvOutput'), you will notice that it is identical. Indeed, in the case of the singleton, we use our main module to retrieve the information our service needs.

Another thing to note: the default value default="". Setting an empty default value allows us to determine whether the user specified a value in the .arc or not. Later, in the module, we could say that if there are no values for both options, then the user simply does not want CSV output. (this is the method used in QAMA).

.arc File – Module Option Section

Here is the corresponding .arc:

SimpleTableOutputExample2.arc

<simple-table-output-example2>
<!-- Le nom du répertoire à créer/utiliser. -->
<tableDir>example2</tableDir>
<!-- Le nom du fichier à créer/écraser. -->
<tableName>Results_Example2</tableName>
<!-- Au final, on aura un fichier ayant comme chemin : ./example2/Results_Example2.csv -->
</simple-table-output-example2>

As explained above, the module manages the two options, so we are in the "module option" section.

Initial Entry Point

Let's look at the start-init entry point:

SimpleTableOutputExample2Module.cc

void SimpleTableOutputExample2Module::
initModule()
{
// On utilise des valeurs (pseudo-)aléatoires.
srand(1234);
// Initialisation du service.
// On récupère un pointeur vers le singleton créé par Arcane.
ISimpleTableOutput* table = ServiceBuilder<ISimpleTableOutput>(subDomain()).getSingleton();
// On initialise le tableau grâce à un des initialisateurs.
// Le nom du tableau sera par défaut "Results_Example2_default" ou le nom choisi dans
// le .arc et le nom du fichier sortant sera "Results_Example2_default.X" (ou Y.X avec
// Y le nom choisi dans le .arc).
// X étant selon le format choisi (.csv par exemple).
if(options()->getTableName() != "")
table->init(options()->getTableName());
else
table->init("Results_Example2_default");
// On print le tableau dans son état actuel (vide, avec un titre).
table->print();
}

This is the module that manages the service's two options, including the default values. If the user does not specify a value for the tableName option in the .arc, we define a default name (knowing that the service also does this if table->init() is called without parameters).

Warning
A call to init like this: table->init() is different from a call to init like this: table->init("")! One will take a default value, the other will have an empty name, and the output file will simply have no name (just the extension).

Loop Entry Point

This entry point is identical to that of Example 1.

Exit Entry Point

Finally, let's look at the exit entry point:

SimpleTableOutputExample2Module.cc

void SimpleTableOutputExample2Module::
endModule()
{
// On récupère un pointeur vers le singleton créé par Arcane.
ISimpleTableOutput* table = ServiceBuilder<ISimpleTableOutput>(subDomain()).getSingleton();
// On print le tableau dans son état actuel.
table->print();
// On enregistre le résultat dans le dossier par défaut "example2_default" ou le dossier
// choisi par l'utilisateur dans le .arc.
// Si l'utilisateur n'a mis ni tableName, ni tableDir dans le .arc,
// il n'y aura aucune sortie.
if(options()->getTableName() != "" || options()->getTableDir() != "") {
if(options()->getTableDir() != "")
table->setOutputDirectory(options()->getTableDir());
else
table->setOutputDirectory("example2_default");
table->writeFile();
}
}

The line

if(options()->getTableName() != "" || options()->getTableDir() != "")

allows us to know if the user entered at least one of the options. If so, we check the default value and write the file. If not, we do not write a file.