Skip to content

Commit

Permalink
docs: Add examples
Browse files Browse the repository at this point in the history
  • Loading branch information
huyenngn committed Nov 17, 2023
1 parent 8bed9f8 commit dce73c5
Show file tree
Hide file tree
Showing 37 changed files with 1,550 additions and 710 deletions.
10 changes: 4 additions & 6 deletions capella_ros_tools/modules/parse_message.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,10 @@ def from_type_file(cls, file: Path) -> list:
for line in lines
for (t, n, v, c) in RE_ENUM.findall(line)
]
if not props:
continue
commonprefix = os.path.commonprefix([prop[0] for prop in props])
name = (
commonprefix.rpartition("_")[0]
if len(blocks) > 1
else file.stem
)
name = commonprefix.rpartition("_")[0]
props = [
EnumProp(prop[0].replace(commonprefix, ""), *prop[1:])
for prop in props
Expand Down Expand Up @@ -127,7 +125,7 @@ def from_msg_folder(cls, path_to_pkg_root: Path):
def from_pkg_folders(cls, path_to_root: Path):
"""Create package from package folder."""
out = cls({}, {})
for dir in path_to_root.rglob("**/msg/"):
for dir in path_to_root.rglob("msg/"):
out.packages[dir.parent.name] = ParseMessagesPkg.from_msg_folder(
dir
)
Expand Down
8 changes: 3 additions & 5 deletions capella_ros_tools/modules/serialize_capella.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,19 +162,18 @@ def _find_type(self, type_name: str, package: t.Any) -> t.Any:
try:
return self.basic_types.datatypes.by_name(type_name)
except KeyError:
return None
self.create_basic_types({type_name})
return self.basic_types.datatypes.by_name(type_name)

def create_attribute(
self,
class_name: str,
prop: MsgProp,
package: t.Any,
) -> bool:
) -> None:
"""Create attribute in Capella model."""
superclass = package.classes.by_name(class_name)
property_type = self._find_type(prop.type, package)
if not property_type:
return False
try:
p = superclass.owned_properties.by_name(prop.name)
superclass.owned_properties.remove(p)
Expand All @@ -192,7 +191,6 @@ def create_attribute(
attribute.max_card = capellambse.new_object(
"LiteralNumericValue", value=float(prop.max or "inf")
)
return True

def save_changes(self) -> None:
"""Save changes to Capella model."""
Expand Down
72 changes: 48 additions & 24 deletions capella_ros_tools/msg2capella.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from capella_ros_tools.modules.parse_message import ParseMessagesPkg
from capella_ros_tools.modules.serialize_capella import SerializeCapella

PATH = Path(__file__).parent


class Msg2Capella:
"""Class for importing .msg to capella model."""
Expand All @@ -19,8 +21,11 @@ def __init__(self, path_to_capella_model, layer, overlap):
self.serializer = SerializeCapella(path_to_capella_model, layer)
self.overlap = overlap

def add_objects(self, messages, packages, current_root):
def add_objects(self, current_root_struct, current_root):
"""Add objects to capella model."""
messages = current_root_struct.messages
packages = current_root_struct.packages

self.serializer.create_packages(set(packages.keys()), current_root)

func = (
Expand All @@ -43,36 +48,48 @@ def add_objects(self, messages, packages, current_root):
)
raise click.Abort()
for cls in overlap:
if self.overlap == "overwrite" or (
self.overlap == "ask"
and click.confirm(
f"{cls.name} already exists. Do you want to overwrite?"
)
):
if self.overlap in ["ask", "overwrite"]:
if self.overlap == "ask":
confirm = click.prompt(
f"{cls.name} already exists. Do you want to overwrite?",
type=click.Choice(
["y", "Y", "n", "N"],
case_sensitive=True,
),
)
match confirm:
case "n":
continue
case "N":
self.overlap = "keep"
case "Y":
self.overlap = "overwrite"

func[1]([cls], current_root)
func[0]({cls.name: messages[cls.name].as_struct}, current_root)

for pkg_name, pkg in packages.items():
new_root = current_root.packages.by_name(pkg_name)
self.add_objects(pkg.messages, pkg.packages, new_root)
self.add_objects(pkg, new_root)

def add_relations(self, messages, packages, current_root):
def add_relations(self, current_root_struct, current_root):
"""Add relations to capella model."""
messages = current_root_struct.messages
packages = current_root_struct.packages

if current_root.name == "types":
return
for class_name, cls in messages.items():
for prop in cls.props:
if not self.serializer.create_composition(
class_name, prop, current_root
):
while not self.serializer.create_attribute(
self.serializer.create_attribute(
class_name, prop, current_root
):
self.serializer.create_basic_types({prop.type})

)
for pkg_name, pkg in packages.items():
new_root = current_root.packages.by_name(pkg_name)
self.add_relations(pkg.messages, pkg.packages, new_root)
self.add_relations(pkg, new_root)


@click.command()
Expand Down Expand Up @@ -117,23 +134,30 @@ def add_relations(self, messages, packages, current_root):
)
@click.option("--debug", is_flag=True)
def msg2capella(
path_to_msgs_root, path_to_capella_model, layer, overlap, debug
path_to_msgs_root: Path,
path_to_capella_model: str,
layer: str,
overlap: str,
debug: bool,
):
"""Parse .msg files and import them to capella model."""
converter = Msg2Capella(path_to_capella_model, layer, overlap)

converter = Msg2Capella(path_to_capella_model, layer, "keep")
current_root = converter.serializer.data

ros_interfaces = ParseMessagesPkg.from_pkg_folders(
Path(__file__).joinpath("ros_interfaces")
PATH.joinpath("ros_interfaces")
)
current_root_struct = ParseMessagesPkg(
{}, {ROS_INTERFACES: ros_interfaces}
)

msg = ParseMessagesPkg.from_msg_folder(path_to_msgs_root)
msg.packages |= {ROS_INTERFACES: ros_interfaces}

current_root = converter.serializer.data

converter.add_objects(msg.messages, msg.packages, current_root)
converter.add_objects(current_root_struct, current_root)

converter.add_relations(msg.messages, msg.packages, current_root)
converter.overlap = overlap
current_root_struct = ParseMessagesPkg.from_pkg_folders(path_to_msgs_root)
converter.add_objects(current_root_struct, current_root)
converter.add_relations(current_root_struct, current_root)

if debug:
click.echo(converter.serializer.data)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ros_msgs</name>
<name>empty_model</name>
<comment></comment>
<projects>
</projects>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Copyright DB Netz AG and contributors
SPDX-License-Identifier: Apache-2.0
SPDX-License-Identifier: CC0-1.0
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<metadata:Metadata xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:metadata="http://www.polarsys.org/kitalpha/ad/metadata/1.0.0" id="_IMYo4GmiEe6H9OTeegFukw">
<viewpointReferences id="_MvN64GmiEe6H9OTeegFukw" vpId="org.polarsys.capella.core.viewpoint" version="6.0.0"/>
<metadata:Metadata xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:metadata="http://www.polarsys.org/kitalpha/ad/metadata/1.0.0" id="__ap8AIUVEe6wQ-QATp9Kpg">
<viewpointReferences id="__d6rYIUVEe6wQ-QATp9Kpg" vpId="org.polarsys.capella.core.viewpoint" version="6.0.0"/>
</metadata:Metadata>
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Copyright DB Netz AG and contributors
SPDX-License-Identifier: Apache-2.0
SPDX-License-Identifier: CC0-1.0
26 changes: 26 additions & 0 deletions docs/examples/data/empty_model/empty_model.aird
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<viewpoint:DAnalysis xmi:version="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:description="http://www.eclipse.org/sirius/description/1.1.0" xmlns:viewpoint="http://www.eclipse.org/sirius/1.1.0" xsi:schemaLocation="http://www.eclipse.org/sirius/description/1.1.0 http://www.eclipse.org/sirius/1.1.0#//description" uid="__a3XYIUVEe6wQ-QATp9Kpg" selectedViews="__fWOwIUVEe6wQ-QATp9Kpg __jcEkIUVEe6wQ-QATp9Kpg __tTyQIUVEe6wQ-QATp9Kpg __u2DUIUVEe6wQ-QATp9Kpg __vDesIUVEe6wQ-QATp9Kpg __wXtUIUVEe6wQ-QATp9Kpg __y-VYIUVEe6wQ-QATp9Kpg" version="15.0.0.202201261500">
<semanticResources>empty_model.afm</semanticResources>
<semanticResources>empty_model.capella</semanticResources>
<ownedViews xmi:type="viewpoint:DView" uid="__fWOwIUVEe6wQ-QATp9Kpg">
<viewpoint xmi:type="description:Viewpoint" href="platform:/plugin/org.polarsys.kitalpha.ad.integration.sirius/description/ad.odesign#//@ownedViewpoints[name='ad']"/>
</ownedViews>
<ownedViews xmi:type="viewpoint:DView" uid="__jcEkIUVEe6wQ-QATp9Kpg">
<viewpoint xmi:type="description:Viewpoint" href="platform:/plugin/org.polarsys.capella.core.sirius.analysis/description/common.odesign#//@ownedViewpoints[name='Common']"/>
</ownedViews>
<ownedViews xmi:type="viewpoint:DView" uid="__tTyQIUVEe6wQ-QATp9Kpg">
<viewpoint xmi:type="description:Viewpoint" href="platform:/plugin/org.polarsys.capella.core.sirius.analysis/description/logical.odesign#//@ownedViewpoints[name='Logical%20Architecture']"/>
</ownedViews>
<ownedViews xmi:type="viewpoint:DView" uid="__u2DUIUVEe6wQ-QATp9Kpg">
<viewpoint xmi:type="description:Viewpoint" href="platform:/plugin/org.polarsys.capella.core.sirius.analysis/description/EPBS.odesign#//@ownedViewpoints[name='EPBS%20architecture']"/>
</ownedViews>
<ownedViews xmi:type="viewpoint:DView" uid="__vDesIUVEe6wQ-QATp9Kpg">
<viewpoint xmi:type="description:Viewpoint" href="platform:/plugin/org.polarsys.capella.core.sirius.analysis/description/oa.odesign#//@ownedViewpoints[name='Operational%20Analysis']"/>
</ownedViews>
<ownedViews xmi:type="viewpoint:DView" uid="__wXtUIUVEe6wQ-QATp9Kpg">
<viewpoint xmi:type="description:Viewpoint" href="platform:/plugin/org.polarsys.capella.core.sirius.analysis/description/context.odesign#//@ownedViewpoints[name='System%20Analysis']"/>
</ownedViews>
<ownedViews xmi:type="viewpoint:DView" uid="__y-VYIUVEe6wQ-QATp9Kpg">
<viewpoint xmi:type="description:Viewpoint" href="platform:/plugin/org.polarsys.capella.core.sirius.analysis/description/physical.odesign#//@ownedViewpoints[name='Physical%20Architecture']"/>
</ownedViews>
</viewpoint:DAnalysis>
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
Copyright DB Netz AG and contributors
SPDX-License-Identifier: Apache-2.0
SPDX-License-Identifier: CC0-1.0
Loading

0 comments on commit dce73c5

Please sign in to comment.