Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add artifact name of a node in runtime system model [Merge 2nd] #48

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions ros2model/api/model_generator/message_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from ament_index_python import get_package_share_directory

Template_Folder_ROS = Path(get_package_share_directory("ros2model") + "/templates")
Template_ROS = Path(Template_Folder_ROS / "message.ros2.j2")
Template_ROS = Path(Template_Folder_ROS / "message.ros.j2")
except ImportError:
Template_ROS = None

Expand All @@ -35,7 +35,7 @@ def __init__(self, template_path=None) -> None:
if template_path != None:
self.template_path = Path(template_path).resolve()
elif Template_ROS != None and Template_ROS.is_file():
self.template_path = Template
self.template_path = Template_ROS
elif Template.is_file():
self.template_path = Template
else:
Expand Down
27 changes: 23 additions & 4 deletions ros2model/api/runtime_parser/rossystem_runtime_parser.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,47 @@
from typing import List, Optional

from pydantic import Field
from ros2model.api.runtime_parser.rosmodel_runtime_parser import (
save_to_file,
get_node_graph_names,
parse,
)
import ros2model.core.metamodels.metamodel_ros as ROSModel
from pathlib import Path
from ros2model.core.utils import find_process_by_node_name


class RuntimeGraphName(ROSModel.GraphName):
artifact_name: Optional[str] = None
pkg_name: Optional[str] = None


class RuntimeNode(ROSModel.Node):
name: RuntimeGraphName


class RuntimeRossystem(ROSModel.Rossystem):
pass
nodes: List[RuntimeNode] = Field(default_factory=list)

def __init__(self, *, system: ROSModel.Rossystem, **data):
super().__init__(name=system.name, **data)

def get_nodes(self):
nodes = get_node_graph_names()
for n in nodes:
print("node name: ", n.namespace, n.name, n.full_name)
pkg, artifact = find_process_by_node_name(n.name, n.namespace)

parsed_node = parse(
ROSModel.GraphName(
name=n.name, namespace=n.namespace, full_name=n.full_name
name=n.name,
namespace=n.namespace,
full_name=n.full_name,
)
)
self.nodes.append(parsed_node)
runtime_node = RuntimeNode(**parsed_node.model_dump())
runtime_node.name.artifact_name = artifact
runtime_node.name.pkg_name = pkg
self.nodes.append(runtime_node)


def name_system_file(grapg_name: ROSModel.GraphName):
Expand Down
14 changes: 13 additions & 1 deletion ros2model/verb/runtime_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from ros2model.api.model_generator.system_generator import SystemGenerator
import ros2model.api.runtime_parser.rossystem_runtime_parser as RuntimeParser
import ros2model.core.metamodels.metamodel_ros as ROSModel
from ament_index_python import get_package_share_directory


class RuntimeVerb(VerbExtension):
Expand Down Expand Up @@ -36,13 +37,24 @@ def add_arguments(self, parser, cli_name):
help="Consider hidden topics, services or actions.",
)

def get_template(self):
template_folder_ros = Path(
get_package_share_directory("ros2model") + "/templates"
)
return (
Path(template_folder_ros / "runtime_rossystem.rossystem.j2")
.absolute()
.as_posix()
)

def main(self, *, args):
output_file = Path(args.output_file)
system_name = output_file.name
if output_file.is_absolute() != True:
output_file = output_file.resolve()

generator = SystemGenerator()
print(self.get_template())
generator = SystemGenerator(template_path=self.get_template())

system = RuntimeParser.RuntimeRossystem(
system=ROSModel.Rossystem(name=system_name)
Expand Down
7 changes: 6 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@
("share/ament_index/resource_index/packages", ["resource/" + package_name]),
(
"share/" + package_name + "/templates",
["templates/component.ros2.j2", "templates/rossystem.rossystem.j2"],
[
"templates/component.ros2.j2",
"templates/rossystem.rossystem.j2",
"templates/runtime_rossystem.rossystem.j2",
"templates/message.ros.j2",
],
),
],
install_requires=[
Expand Down
4 changes: 2 additions & 2 deletions templates/rossystem.rossystem.j2
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
nodes:
{% for node in model.nodes %}
"{{ node.name.full_name }}":
from: "TODO.{{ node.name.full_name }}"
{% if node.publisher | length > 0 or node.subscriber | length > 0 or node.actionserver | length > 0 or node.actionclient | length > 0 or node.serviceserver | length > 0 or node.serviceclient | length > 0%}
from: "{{ node.name.full_name }}"
{% if node.publisher | length > 0 or node.subscriber | length > 0 or node.actionserver | length > 0 or node.actionclient | length > 0 or node.serviceserver | length > 0 or node.serviceclient | length > 0 %}
interfaces:
{% for interface in node.publisher %}
- "{{ interface.name }}": pub-> "TODO::{{ interface.name }}"
Expand Down
40 changes: 40 additions & 0 deletions templates/runtime_rossystem.rossystem.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{{ model.name }}:
nodes:
{% for node in model.nodes %}
"{{ node.name.full_name }}":
from: "{{ node.name.pkg_name }}.{{ node.name.full_name }}"
{% if node.publisher | length > 0 or node.subscriber | length > 0 or node.actionserver | length > 0 or node.actionclient | length > 0 or node.serviceserver | length > 0 or node.serviceclient | length > 0 %}
interfaces:
{% for interface in node.publisher %}
- "{{ interface.name }}": pub-> "TODO::{{ interface.name }}"
{% endfor %}
{% for interface in node.subscriber %}
- "{{ interface.name }}": sub-> "TODO::{{ interface.name }}"
{% endfor %}
{% for interface in node.actionserver %}
- "{{ interface.name }}": as-> "TODO::{{ interface.name }}"
{% endfor %}
{% for interface in node.actionclient %}
- "{{ interface.name }}": ac-> "TODO::{{ interface.name }}"
{% endfor %}
{% for interface in node.serviceserver %}
- "{{ interface.name }}": ss-> "{{ node.name.artifact_name }}::{{ interface.name }}"
{% endfor %}
{% for interface in node.serviceclient %}
- "{{ interface.name }}": sc-> "TODO::{{ interface.name }}"
{% endfor %}
{% endif %}
{% if node.parameter | length > 0 %}
parameters:
{% for parameter in node.parameter %}
- {{ parameter.name }}: "TODO::{{ parameter.name }}"
{% if parameter.type == "String" %}
value: "{{ parameter.value }}"
{% elif parameter.type == "Boolean" %}
value: {{ parameter.value | lower }}
{% else %}
value: {{ parameter.value }}
{% endif %}
{% endfor %}
{% endif %}
{% endfor %}
2 changes: 1 addition & 1 deletion test/outputs/test_system.rossystem
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
test_system:
nodes:
"/map_server":
from: "TODO./map_server"
from: "/map_server"
interfaces:
- "map_metadata": pub-> "TODO::map_metadata"
- "map": pub-> "TODO::map"
Expand Down
4 changes: 0 additions & 4 deletions test/unittest/test_generate_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,6 @@
],
)

from devtools import pprint

# pprint(test_model)

test_dir = "test"
output_folder = Path(__file__).parent.parent / "outputs"

Expand Down
5 changes: 3 additions & 2 deletions test/unittest/test_generate_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
test_system:
nodes:
"/map_server":
from: "TODO./map_server"
from: "/map_server"
interfaces:
- "map_metadata": pub-> "TODO::map_metadata"
- "map": pub-> "TODO::map"
Expand All @@ -58,7 +58,7 @@
"""


class test_message_generator(unittest.TestCase):
class test_system_generator(unittest.TestCase):
def setUp(self) -> None:
self.generator = SystemGenerator()
self.test_dir = Path(test_dir)
Expand All @@ -72,6 +72,7 @@ def test_generate_pkg(self):
)
with open(Path(self.output_dir / f"{test_model.name}.rossystem"), "r") as file:
data = file.read()

self.assertEqual(expect_result.strip(), data.strip())


Expand Down
Loading