Skip to content

Commit

Permalink
merge: Fix class properties with primitive types (#8)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wuestengecko committed Oct 17, 2024
2 parents 533909e + 65ca5ab commit 8435777
Show file tree
Hide file tree
Showing 3 changed files with 446 additions and 444 deletions.
77 changes: 40 additions & 37 deletions capella_ros_tools/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ def __init__(
self.messages = data_model.MessagePkgDef("root", [], [])
self._promise_ids: dict[str, None] = {}
self._promise_id_refs: dict[str, None] = {}
self._needed_associations: dict[str, str] = {}
self._license_header = None
if license_header_path is not None:
self._license_header = license_header_path.read_text("utf-8")
Expand Down Expand Up @@ -88,15 +89,11 @@ def _convert_package(
classes = []
enums = []
packages = []
associations = []

for msg_def in pkg_def.messages:
if msg_def.fields:
cls_yml, cls_associations = self._convert_class(
pkg_def.name, msg_def
)
cls_yml = self._convert_class(pkg_def.name, msg_def)
classes.append(cls_yml)
associations.extend(cls_associations)
for enum_def in msg_def.enums:
enums.append(self._convert_enum(msg_def.name, enum_def))

Expand All @@ -115,8 +112,6 @@ def _convert_package(
sync["enumerations"] = enums
if packages:
sync["packages"] = packages
if associations:
sync["owned_associations"] = associations

yml = {}
if sync:
Expand All @@ -126,11 +121,10 @@ def _convert_package(

def _convert_class(
self, pkg_name: str, msg_def: data_model.MessageDef
) -> tuple[dict[str, t.Any], list[dict[str, t.Any]]]:
) -> dict[str, t.Any]:
promise_id = f"{pkg_name}.{msg_def.name}"
self._promise_ids[promise_id] = None
props = []
associations = []
for field_def in msg_def.fields:
prop_promise_id = f"{promise_id}.{field_def.name}"
promise_ref = (
Expand All @@ -155,33 +149,7 @@ def _convert_class(
},
}
props.append(prop_yml)

associations.append(
{
"find": {
"navigable_members": [decl.Promise(prop_promise_id)],
},
"sync": {
"members": [
{
"find": {
"type": decl.Promise(promise_id),
},
"set": {
"_type": "Property",
"kind": "ASSOCIATION",
"min_card": decl.NewObject(
"LiteralNumericValue", value="1"
),
"max_card": decl.NewObject(
"LiteralNumericValue", value="1"
),
},
}
],
},
}
)
self._needed_associations[prop_promise_id] = promise_id

yml = {
"promise_id": promise_id,
Expand All @@ -195,7 +163,7 @@ def _convert_class(
"properties": props,
},
}
return yml, associations
return yml

def _convert_enum(
self, pkg_name: str, enum_def: data_model.EnumDef
Expand Down Expand Up @@ -239,13 +207,48 @@ def to_yaml(
) -> str:
"""Import ROS messages into a Capella data package."""
logger.info("Generating decl YAML")

instructions = [
{"parent": decl.UUIDReference(helpers.UUIDString(root_uuid))}
| self._convert_package(self.messages),
]
needed_types = [
p for p in self._promise_id_refs if p not in self._promise_ids
]

associations = []
for prop_promise_id, promise_id in self._needed_associations.items():
if prop_promise_id in needed_types:
continue
associations.append(
{
"find": {
"navigable_members": [decl.Promise(prop_promise_id)],
},
"sync": {
"members": [
{
"find": {
"type": decl.Promise(promise_id),
},
"set": {
"_type": "Property",
"kind": "ASSOCIATION",
"min_card": decl.NewObject(
"LiteralNumericValue", value="1"
),
"max_card": decl.NewObject(
"LiteralNumericValue", value="1"
),
},
}
],
},
}
)
if associations:
instructions[0]["sync"]["owned_associations"] = associations

if not needed_types:
return decl.dump(instructions)

Expand Down
Loading

0 comments on commit 8435777

Please sign in to comment.