Skip to content

Commit

Permalink
Merge pull request #1 from convince-project/dev
Browse files Browse the repository at this point in the history
Update with HL-SCXML and topics
  • Loading branch information
SofiaFaraci authored Nov 6, 2024
2 parents eda4824 + 9834a2f commit 8457ae5
Show file tree
Hide file tree
Showing 25 changed files with 1,155 additions and 222 deletions.
1 change: 1 addition & 0 deletions code/CMakeLists.txt → CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ target_sources( ${PROJECT_NAME} PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/src/Data.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/strManipulation.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Replacer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/Translator.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ExtractFromXML.cpp
${CMAKE_CURRENT_SOURCE_DIR}/src/ExtractFromElement.cpp
)
Expand Down
34 changes: 21 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Please access the documentation via [convince-project.github.io/model2code](https://convince-project.github.io/model2code/)
MODEL2CODE is a tool that generates CPP code from a SCXML model.
MODEL2CODE is a tool that generates a ROS package with C++ code from a ROS-flavoured SCXML model.

## Installation

Expand All @@ -12,37 +12,45 @@ MODEL2CODE has been tested on Ubuntu 22.04 with Python 3.10.12.

MODEL2CODE requires the following dependencies:

* [TinyXML2](https://github.com/leethomason/tinyxml2/) (Tested with v10.0.0)
* [TinyXML2](https://github.com/leethomason/tinyxml2/) (Tested with v10.0.0) for XML file handling.

To install see [TinyXML2 Installation](https://github.com/leethomason/tinyxml2/tree/master?tab=readme-ov-file#building-tinyxml-2---using-vcpkg)

## Compiling
To compile the code run the following commands
To compile the code run the following commands:
```
git clone https://github.com/convince-project/model2code.git
cd model2code/code
cd model2code
mkdir build
cd build
cmake ../code
make install
```

## Execution
To run the `model2code` tool you need three different files:
- An SCXML file with your state machine
- An XML file with the definition of your model
- An XML file with the definition of your interfaces
- A directory containing the templates of the files to generate
To run the `model2code` tool, you will need the following:
- An SCXML file with the state machine of the model;
- An XML file defining the model of your project;
- An XML file defining the interfaces of your project;
- A directory containing templates for the files to be generated.

To run the `model2code` tool use the following command
```
model2code --input_filename "input_filename.scxml" --model_filename "model_filename.scxml" --interface_filename "interface_filename.scxml" --output_path "path/to/output/directory" --template_path "path/to/template_skill/directory"
model2code --input_filename "input_model.scxml" --model_filename "project_model_definition.xml" --interface_filename "interface_definition.xml" --output_path "path/to/output/directory" --template_path "path/to/template_skill/directory"
```
replace `input_filename.scxml`, `model_filename.scxml`, `interface_filename.scxml`, `path/to/output/directory` and `path/to/template_skill/directory` with your needs.
By default the `path/to/output/directory` is set to the location of `input_filename.scxml` and `path/to/template_skill/directory` is set to the 'templates/skills/template_skill' directory of this repository.
replace `input_model.scxml`, `project_model_definition.scxml`, `interface_definition.scxml`, `path/to/output/directory` and `path/to/template_skill/directory` with your needs.

By default the `path/to/output/directory` is set to the location of `input_model.scxml`, and `path/to/template_skill/directory` is set to the 'templates/skills/template_skill' directory of this repository.

Example XML files with the required structure for defining the project's model and interfaces are available in the `template/specifications` folder.

## Run examples
To run an example of MODEL2CODE go to the main directory and run the following commands:
Example 1:
```
model2code --input_filename "tutorials/skills/first_tutorial_skill/src/FirstTutorialSkill.scxml" --model_filename "templates/specifications/full-model.xml" --interface_filename "templates/specifications/interfaces.xml" --output_path "tutorials/skills/first_tutorial_skill"
```
model2code --input_filename "templates/skills/first_template_skill/src/FirstTemplateSkillSM.scxml" --model_filename "templates/specifications/full-model.xml" --interface_filename "templates/specifications/interfaces.xml"
Example 2:
```
model2code --input_filename "tutorials/skills/second_tutorial_skill/src/SecondTutorialSkill.scxml" --model_filename "templates/specifications/full-model.xml" --interface_filename "templates/specifications/interfaces.xml"
```
42 changes: 19 additions & 23 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
@@ -1,38 +1,34 @@
CONVINCE model2code Documentation
==================================
The tool is developed as part of the CONVINCE project and is mainly used to generate skill-level code starting from an SCXML model.
The model2code tool is developed as part of the CONVINCE project and is used to generate skill-level code from the high-level SCXML model of a skill.

The inputs of the program are:
Program inputs:

- an SCXML model that describes the behavior of the skill,
- an XML file that describes the full model of the program, which includes the behavior tree, the skills and the components used in the system,
- an XML file that describes the interfaces used between behavior tree and skills, and between skills and components,
- a directory containing the templates of the files to generate.
- An SCXML model that describes the behavior of the skill,
- An XML file that outlines the full model of the program, including the behavior tree, skills, and components used in the system.
- An XML file that specifies the interfaces between the behavior tree and skills, as well as between skills and components.
- A directory containing templates for the files to be generated.

The output of the program is composed of:
Program outputs:

- a C++ file that contains the code of the skill,
- a header file that contains the declaration of the skill,
- a main C++ file that contains the main function of the skill,
- a package.xml file that contains the information of the ROS package,
- a CMakeLists.txt file that includes the generated files.
- A C++ file containing skill code.
- A header file declaring the skill.
- A main C++ file that includes the main function of the skill.
- A package.xml file with information about the ROS package.
- A CMakeLists.txt file that incorporates the generated files.

The output above is only if the data model is ECMAscript, otherwise, you will need to add the parameter ``--datamodel_mode`` to generate the C++ data model files.
In this case, you will get 2 additional files:

- a C++ file that contains the code of the data model,
- a header file that contains the declaration of the data model.
Required parameters:

- ``--input_filename``: The path to the SCXML file that describes the behavior of the skill.
- ``--model_filename``: The path to the XML file that describes the full model of the program.
- ``--interface_filename``: The path to the XML file that describes the interfaces used.

The parameters required to run the program are:
By default, the program generates the code in the same directory as the SCXML file specified by the ``--input_filename`` parameter. However, you can select a different output directory by using the ``--output_path`` parameter.

- ``--input_filename`` : the path to the SCXML file that describes the behavior of the skill,
- ``--model_filename`` : the path to the XML file that describes the full model of the program,
- ``--interface_filename`` : the path to the XML file that describes the interfaces used.
Additionally, the program uses files from the ``templates`` directory by default to generate the code, but you can specify a different directory with the ``--templates_path`` parameter.

By default the program will generate the code in the directory where the SCXML file passed by the parameter ``--input_filename`` is located, but you can specify a different directory by using the parameter ``--output_path``.

The skills generated are based on a behavior tree structure and will have a ROS2 tick service in case they are a condition a ROS2 tick and halt services in case they are an action.
The generated skills are based on a behavior tree structure. Skills defined as conditions will have a ROS2 tick service, while skills defined as actions will have both tick and halt services.

Contents
----------
Expand Down
9 changes: 9 additions & 0 deletions code/include/Data.h → include/Data.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ struct eventDataStr{

struct fileDataStr{
std::string inputFileName;
std::string inputFileNameGeneration;
std::string interfaceFileName;
std::string modelFileName;
std::string outputPath;
Expand All @@ -80,8 +81,11 @@ struct fileDataStr{
std::string outputDatamodelFileNameCPP;
std::string outputFileNameH;
std::string outputFileNameCPP;
std::string outputFileTranslatedSM;
bool debug_mode;
bool datamodel_mode;
bool translate_mode;
bool generate_mode;
};

struct templateFileDataStr{
Expand All @@ -103,6 +107,11 @@ struct savedCodeStr{
std::string interfaceCMake;
std::string packageCMake;
std::string interfaceXML;
std::string topicSubscriptionC;
std::string topicCallbackC;
std::string topicSubscriptionH;
std::string topicCallbackH;
std::string topicInterfaceH;
};

struct cppCodeStr
Expand Down
File renamed without changes.
File renamed without changes.
7 changes: 5 additions & 2 deletions code/include/Replacer.h → include/Replacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
* @param fileData file data structure passed by reference from which the event data is extracted
* @param eventData event data structure passed by reference where the event data is stored
*/
void getEventData(fileDataStr fileData, eventDataStr& eventData);
bool getEventData(fileDataStr fileData, eventDataStr& eventData);

/**
* @brief Get the Event Data from the vector of event elements
Expand All @@ -27,7 +27,7 @@ void getEventData(fileDataStr fileData, eventDataStr& eventData);
* @param elementsTransition vector of transition event elements
* @param elementsSend vector of send event elements
*/
void getEventsVecData(fileDataStr fileData, const std::vector<tinyxml2::XMLElement*> elementsTransition, const std::vector<tinyxml2::XMLElement*> elementsSend);
bool getEventsVecData(fileDataStr fileData, const std::vector<tinyxml2::XMLElement*> elementsTransition, const std::vector<tinyxml2::XMLElement*> elementsSend);

/**
* @brief function to keep or delete the sections of the code related to the Command Tick event
Expand Down Expand Up @@ -95,6 +95,9 @@ void replaceEventCode(std::map <std::string, std::string>& codeMap);
*/
bool readTemplates(templateFileDataStr& templateFileData, std::map <std::string, std::string>& codeMap);


bool createDirectory(const std::string& path);

/**
* @brief main function to get the code from template files and replace the placeholders with the data from the input file
*
Expand Down
44 changes: 44 additions & 0 deletions include/Translator.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-FileCopyrightText: 2024 Humanoid Sensing and Perception, Istituto Italiano di Tecnologia
// SPDX-License-Identifier: APACHE-2.0

/**
* @file Translator.h
* @brief This file contains the functions to translate the High-Level CONVINCE XML to the compilable SCXML used for code generation.
* @version 0.1
* @date 2024-09-18
*
*/
#include "Replacer.h"
#include <fstream>

/**
* @brief Deletes a specific attribute of a given XML element
*
* @param element element from which to delete the attribute
* @param attribute attribute name to be deleted
* @return bool
*/
bool deleteElementAttribute(tinyxml2::XMLElement* element, const std::string& attribute);

/**
* @brief Deletes a specific attribute from all elements in a vector
*
* @param elements vector of elements from which to delete the attribute
* @param attribute attribute name to be deleted
* @return bool Returns true if at least one attribute was successfully deleted, false otherwise
*/
bool deleteElementAttributeFromVector(std::vector<tinyxml2::XMLElement*>& elements, const std::string& attribute);

/**
* @brief
*
* @param
*/
bool readHLXMLFile(tinyxml2::XMLDocument& doc, const std::string fileName);

/**
* @brief
*
* @param
*/
bool Translator(fileDataStr& fileData);
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 8457ae5

Please sign in to comment.